From 1e48893e92ee16eb3de463910d6ccebe83aefce0 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Wed, 18 Sep 2024 21:14:24 +0800 Subject: [PATCH] feat: add the pageNo property to the getCatalog api --- src/editor/core/worker/WorkerManager.ts | 6 ++++- src/editor/core/worker/works/catalog.ts | 32 ++++++++++++++++++------- src/editor/interface/Catalog.ts | 1 + 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/editor/core/worker/WorkerManager.ts b/src/editor/core/worker/WorkerManager.ts index 67d800902..6114e3d26 100644 --- a/src/editor/core/worker/WorkerManager.ts +++ b/src/editor/core/worker/WorkerManager.ts @@ -43,7 +43,11 @@ export class WorkerManager { } const elementList = this.draw.getOriginalMainElementList() - this.catalogWorker.postMessage(elementList) + const positionList = this.draw.getPosition().getOriginalMainPositionList() + this.catalogWorker.postMessage({ + elementList, + positionList + }) }) } diff --git a/src/editor/core/worker/works/catalog.ts b/src/editor/core/worker/works/catalog.ts index c31b5f3d4..ddd79333d 100644 --- a/src/editor/core/worker/works/catalog.ts +++ b/src/editor/core/worker/works/catalog.ts @@ -1,5 +1,14 @@ import { ICatalog, ICatalogItem } from '../../../interface/Catalog' -import { IElement } from '../../../interface/Element' +import { IElement, IElementPosition } from '../../../interface/Element' + +interface IGetCatalogPayload { + elementList: IElement[] + positionList: IElementPosition[] +} + +type ICatalogElement = IElement & { + pageNo: number +} enum ElementType { TEXT = 'text', @@ -54,20 +63,22 @@ function isTextLikeElement(element: IElement): boolean { return !element.type || TEXTLIKE_ELEMENT_TYPE.includes(element.type) } -function getCatalog(elementList: IElement[]): ICatalog | null { +function getCatalog(payload: IGetCatalogPayload): ICatalog | null { + const { elementList, positionList } = payload // 筛选标题 - const titleElementList: IElement[] = [] + const titleElementList: ICatalogElement[] = [] let t = 0 while (t < elementList.length) { const element = elementList[t] if (element.titleId) { const titleId = element.titleId const level = element.level - const titleElement: IElement = { + const titleElement: ICatalogElement = { type: ElementType.TITLE, value: '', level, - titleId + titleId, + pageNo: positionList[t].pageNo } const valueList: IElement[] = [] while (t < elementList.length) { @@ -90,7 +101,10 @@ function getCatalog(elementList: IElement[]): ICatalog | null { } if (!titleElementList.length) return null // 查找到比最新元素大的标题时终止 - const recursiveInsert = (title: IElement, catalogItem: ICatalogItem) => { + const recursiveInsert = ( + title: ICatalogElement, + catalogItem: ICatalogItem + ) => { const subCatalogItem = catalogItem.subCatalog[catalogItem.subCatalog.length - 1] const catalogItemLevel = titleOrderNumberMapping[subCatalogItem?.level] @@ -102,6 +116,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null { id: title.titleId!, name: title.value, level: title.level!, + pageNo: title.pageNo, subCatalog: [] }) } @@ -122,6 +137,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null { id: title.titleId!, name: title.value, level: title.level!, + pageNo: title.pageNo, subCatalog: [] }) } @@ -130,7 +146,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null { } onmessage = evt => { - const elementList = evt.data - const catalog = getCatalog(elementList) + const payload = evt.data + const catalog = getCatalog(payload) postMessage(catalog) } diff --git a/src/editor/interface/Catalog.ts b/src/editor/interface/Catalog.ts index 2a0701379..f1ac61803 100644 --- a/src/editor/interface/Catalog.ts +++ b/src/editor/interface/Catalog.ts @@ -4,6 +4,7 @@ export interface ICatalogItem { id: string name: string level: TitleLevel + pageNo: number subCatalog: ICatalogItem[] }