diff --git a/docs/en/guide/command-get.md b/docs/en/guide/command-get.md index 225c635..f758a34 100644 --- a/docs/en/guide/command-get.md +++ b/docs/en/guide/command-get.md @@ -231,3 +231,17 @@ Usage: ```javascript const container = await instance.command.getContainer() ``` + +## getTitleValue + +Feature: Get title value + +Usage: + +```javascript +const { + value: string | null + elementList: IElement[] + zone: EditorZone +}[] = await instance.command.getTitleValue(payload: IGetTitleValueOption) +``` diff --git a/docs/en/guide/schema.md b/docs/en/guide/schema.md index c56bb06..3ab9de7 100644 --- a/docs/en/guide/schema.md +++ b/docs/en/guide/schema.md @@ -164,6 +164,7 @@ interface IElement { }; // title level?: TitleLevel; + title?: ITitle; // list listType?: ListType; listStyle?: ListStyle; diff --git a/docs/guide/command-get.md b/docs/guide/command-get.md index 189eaec..da99397 100644 --- a/docs/guide/command-get.md +++ b/docs/guide/command-get.md @@ -209,7 +209,8 @@ const groupIds = await instance.command.getGroupIds() const { value: string | null innerText: string | null -} = await instance.command.getControlValue(payload: IGetControlValueOption) + zone: EditorZone +}[] = await instance.command.getControlValue(payload: IGetControlValueOption) ``` ## getControlList @@ -231,3 +232,17 @@ const controlList = await instance.command.getControlList() ```javascript const container = await instance.command.getContainer() ``` + +## getTitleValue + +功能:获取标题值 + +用法: + +```javascript +const { + value: string | null + elementList: IElement[] + zone: EditorZone +}[] = await instance.command.getTitleValue(payload: IGetTitleValueOption) +``` diff --git a/docs/guide/schema.md b/docs/guide/schema.md index a2de5e5..b4ec204 100644 --- a/docs/guide/schema.md +++ b/docs/guide/schema.md @@ -164,6 +164,7 @@ interface IElement { }; // 标题 level?: TitleLevel; + title?: ITitle; // 列表 listType?: ListType; listStyle?: ListStyle; diff --git a/src/editor/core/command/Command.ts b/src/editor/core/command/Command.ts index 59e81ac..1e91db0 100644 --- a/src/editor/core/command/Command.ts +++ b/src/editor/core/command/Command.ts @@ -110,6 +110,7 @@ export class Command { public getControlValue: CommandAdapt['getControlValue'] public getControlList: CommandAdapt['getControlList'] public getContainer: CommandAdapt['getContainer'] + public getTitleValue: CommandAdapt['getTitleValue'] constructor(adapt: CommandAdapt) { // 全局命令 @@ -223,6 +224,7 @@ export class Command { this.getLocale = adapt.getLocale.bind(adapt) this.getGroupIds = adapt.getGroupIds.bind(adapt) this.getContainer = adapt.getContainer.bind(adapt) + this.getTitleValue = adapt.getTitleValue.bind(adapt) // 控件 this.executeSetControlValue = adapt.setControlValue.bind(adapt) this.executeSetControlExtension = adapt.setControlExtension.bind(adapt) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index db434a3..5ac1d20 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -1,6 +1,9 @@ import { NBSP, WRAP, ZERO } from '../../dataset/constant/Common' import { EDITOR_ELEMENT_STYLE_ATTR } from '../../dataset/constant/Element' -import { titleSizeMapping } from '../../dataset/constant/Title' +import { + titleOrderNumberMapping, + titleSizeMapping +} from '../../dataset/constant/Title' import { defaultWatermarkOption } from '../../dataset/constant/Watermark' import { ImageDisplay } from '../../dataset/enum/Common' import { ControlComponent } from '../../dataset/enum/Control' @@ -52,6 +55,10 @@ import { IColgroup } from '../../interface/table/Colgroup' import { ITd } from '../../interface/table/Td' import { ITr } from '../../interface/table/Tr' import { ITextDecoration } from '../../interface/Text' +import { + IGetTitleValueOption, + IGetTitleValueResult +} from '../../interface/Title' import { IWatermark } from '../../interface/Watermark' import { deepClone, downloadFile, getUUID, isObjectEqual } from '../../utils' import { @@ -2398,4 +2405,70 @@ export class CommandAdapt { public getContainer(): HTMLDivElement { return this.draw.getContainer() } + + public getTitleValue( + payload: IGetTitleValueOption + ): IGetTitleValueResult | null { + const { conceptId } = payload + const result: IGetTitleValueResult = [] + const getValue = (elementList: IElement[], zone: EditorZone) => { + let i = 0 + while (i < elementList.length) { + const element = elementList[i] + i++ + if (element.type === ElementType.TABLE) { + const trList = element.trList! + for (let r = 0; r < trList.length; r++) { + const tr = trList[r] + for (let d = 0; d < tr.tdList.length; d++) { + const td = tr.tdList[d] + getValue(td.value, zone) + } + } + } + if (element?.title?.conceptId !== conceptId) continue + // 先查找到标题,后循环至同级或上级标题处停止 + const valueList: IElement[] = [] + let j = i + while (j < elementList.length) { + const nextElement = elementList[j] + j++ + if (element.titleId === nextElement.titleId) continue + if ( + nextElement.level && + titleOrderNumberMapping[nextElement.level] <= + titleOrderNumberMapping[element.level!] + ) { + break + } + valueList.push(nextElement) + } + result.push({ + ...element.title!, + value: getTextFromElementList(valueList), + elementList: zipElementList(valueList), + zone + }) + i = j + } + } + const data = [ + { + zone: EditorZone.HEADER, + elementList: this.draw.getHeaderElementList() + }, + { + zone: EditorZone.MAIN, + elementList: this.draw.getOriginalMainElementList() + }, + { + zone: EditorZone.FOOTER, + elementList: this.draw.getFooterElementList() + } + ] + for (const { zone, elementList } of data) { + getValue(elementList, zone) + } + return result + } } diff --git a/src/editor/dataset/constant/Element.ts b/src/editor/dataset/constant/Element.ts index 2d65579..0c51600 100644 --- a/src/editor/dataset/constant/Element.ts +++ b/src/editor/dataset/constant/Element.ts @@ -64,6 +64,7 @@ export const EDITOR_ELEMENT_ZIP_ATTR: Array = [ 'dateFormat', 'block', 'level', + 'title', 'listType', 'listStyle', 'listWrap', @@ -88,7 +89,11 @@ export const TABLE_CONTEXT_ATTR: Array = [ 'tableId' ] -export const TITLE_CONTEXT_ATTR: Array = ['level', 'titleId'] +export const TITLE_CONTEXT_ATTR: Array = [ + 'level', + 'titleId', + 'title' +] export const LIST_CONTEXT_ATTR: Array = [ 'listId', diff --git a/src/editor/interface/Element.ts b/src/editor/interface/Element.ts index 97a7f92..049fb5c 100644 --- a/src/editor/interface/Element.ts +++ b/src/editor/interface/Element.ts @@ -10,6 +10,7 @@ import { ICheckbox } from './Checkbox' import { IControl } from './Control' import { IRadio } from './Radio' import { ITextDecoration } from './Text' +import { ITitle } from './Title' import { IColgroup } from './table/Colgroup' import { ITr } from './table/Tr' @@ -45,6 +46,7 @@ export interface ITitleElement { valueList?: IElement[] level?: TitleLevel titleId?: string + title?: ITitle } export interface IListElement { diff --git a/src/editor/interface/Title.ts b/src/editor/interface/Title.ts index 48f13e6..0f4641a 100644 --- a/src/editor/interface/Title.ts +++ b/src/editor/interface/Title.ts @@ -1,3 +1,6 @@ +import { EditorZone } from '../dataset/enum/Editor' +import { IElement } from './Element' + export interface ITitleSizeOption { defaultFirstSize?: number defaultSecondSize?: number @@ -8,3 +11,17 @@ export interface ITitleSizeOption { } export type ITitleOption = ITitleSizeOption & {} + +export interface ITitle { + conceptId?: string +} + +export interface IGetTitleValueOption { + conceptId: string +} + +export type IGetTitleValueResult = (ITitle & { + value: string | null + elementList: IElement[] + zone: EditorZone +})[] diff --git a/src/editor/utils/element.ts b/src/editor/utils/element.ts index 4bf0ca3..f5d0675 100644 --- a/src/editor/utils/element.ts +++ b/src/editor/utils/element.ts @@ -103,6 +103,7 @@ export function formatElementList( const titleOptions = editorOptions.title for (let v = 0; v < valueList.length; v++) { const value = valueList[v] + value.title = el.title if (el.level) { value.titleId = titleId value.level = el.level @@ -523,6 +524,7 @@ export function zipElementList(payload: IElement[]): IElement[] { const level = element.level const titleElement: IElement = { type: ElementType.TITLE, + title: element.title, value: '', level } @@ -534,6 +536,7 @@ export function zipElementList(payload: IElement[]): IElement[] { break } delete titleE.level + delete titleE.title valueList.push(titleE) e++ }