Skip to content

Commit

Permalink
feat: add the pageNo property to the getCatalog api
Browse files Browse the repository at this point in the history
  • Loading branch information
Hufe921 committed Sep 18, 2024
1 parent 9880deb commit 1e48893
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/editor/core/worker/WorkerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})
}

Expand Down
32 changes: 24 additions & 8 deletions src/editor/core/worker/works/catalog.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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) {
Expand All @@ -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]
Expand All @@ -102,6 +116,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null {
id: title.titleId!,
name: title.value,
level: title.level!,
pageNo: title.pageNo,
subCatalog: []
})
}
Expand All @@ -122,6 +137,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null {
id: title.titleId!,
name: title.value,
level: title.level!,
pageNo: title.pageNo,
subCatalog: []
})
}
Expand All @@ -130,7 +146,7 @@ function getCatalog(elementList: IElement[]): ICatalog | null {
}

onmessage = evt => {
const elementList = <IElement[]>evt.data
const catalog = getCatalog(elementList)
const payload = <IGetCatalogPayload>evt.data
const catalog = getCatalog(payload)
postMessage(catalog)
}
1 change: 1 addition & 0 deletions src/editor/interface/Catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ICatalogItem {
id: string
name: string
level: TitleLevel
pageNo: number
subCatalog: ICatalogItem[]
}

Expand Down

0 comments on commit 1e48893

Please sign in to comment.