Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(doc): doc edit develop
liuyang.0809d2e3a9b4c3d8
diff --git a/packages/base-render/src/Basics/Interfaces.ts b/packages/base-render/src/Basics/Interfaces.tsindex 6dc88bf3bd..9ce178fcea 100644--- a/packages/base-render/src/Basics/Interfaces.ts+++ b/packages/base-render/src/Basics/Interfaces.ts@@ -13,8 +13,6 @@ import {ITransformState,DocumentBodyModel,} from '@univerjs/core';-import { TextSelection } from '../Component/Docs/Common/TextSelection';-import { Documents } from '../Component/Docs/Document';import {IDocumentSkeletonDrawingAnchor,IDocumentSkeletonBullet,@@ -115,14 +113,6 @@ export interface IFontCreateConfig {pageWidth?: number;}-export interface IEditorInputConfig {- event: Event | CompositionEvent | KeyboardEvent;- content?: string;- document: Documents;- activeSelection?: TextSelection;- selectionList?: TextSelection[];-}-// export interface IPageConfig {// pageNumberStart: number;// pageSize: ISizeData;diff --git a/packages/base-render/src/Basics/Position.ts b/packages/base-render/src/Basics/Position.tsindex 04febc7624..adb79c6576 100644--- a/packages/base-render/src/Basics/Position.ts+++ b/packages/base-render/src/Basics/Position.ts@@ -1,25 +1,6 @@import { BaseObject } from '../BaseObject';-import { ScrollTimer } from '../ScrollTimer';import { IBoundRect } from './Vector2';-export function getCurrentScrollXY(scrollTimer: ScrollTimer) {- const scene = scrollTimer.getScene();- const viewport = scrollTimer.getViewportByCoord(scene);- let scrollX = 0;- let scrollY = 0;- if (!viewport) {- return {- scrollX,- scrollY,- };- }- const actualScroll = viewport.getActualScroll(viewport.scrollX, viewport.scrollY);- return {- scrollX: actualScroll.x,- scrollY: actualScroll.y,- };-}-export function getOffsetRectForDom(ele: HTMLElement) {let box = ele.getBoundingClientRect();let body = document.body;diff --git a/packages/base-render/src/Basics/ScrollXY.ts b/packages/base-render/src/Basics/ScrollXY.tsnew file mode 100644index 0000000000..e5014104e9--- /dev/null+++ b/packages/base-render/src/Basics/ScrollXY.ts@@ -0,0 +1,19 @@+import { ScrollTimer } from '../ScrollTimer';++export function getCurrentScrollXY(scrollTimer: ScrollTimer) {+ const scene = scrollTimer.getScene();+ const viewport = scrollTimer.getViewportByCoord(scene);+ let scrollX = 0;+ let scrollY = 0;+ if (!viewport) {+ return {+ scrollX,+ scrollY,+ };+ }+ const actualScroll = viewport.getActualScroll(viewport.scrollX, viewport.scrollY);+ return {+ scrollX: actualScroll.x,+ scrollY: actualScroll.y,+ };+}diff --git a/packages/base-render/src/Basics/Vector2.ts b/packages/base-render/src/Basics/Vector2.tsindex 5896586522..38151357ef 100644--- a/packages/base-render/src/Basics/Vector2.ts+++ b/packages/base-render/src/Basics/Vector2.ts@@ -1,6 +1,4 @@-import { IKeyValue } from '@univerjs/core';import { DeepImmutable, FloatArray } from './IEvents';-import { Transform } from './Transform';export interface IPoint {x: number;@@ -253,21 +251,21 @@ export class Vector2 implements IPoint {return new Vector2(x, y);}- /**- * Transforms the given vector coordinates by the given transformation Transform and stores the result in the vector "result" coordinates- * @param vector defines the vector to transform- * @param transformation defines the Transform to apply- * @param result defines the target vector- */- static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Transform>, ignoreOffset: boolean = false) {- const t = transformation as IKeyValue;- const p = vector;-- if (ignoreOffset) {- return new Vector2(t[0] * p.x + t[2] * p.y, t[1] * p.x + t[3] * p.y);- }- return new Vector2(t[0] * p.x + t[2] * p.y + t[4], t[1] * p.x + t[3] * p.y + t[5]);- }+ // /**+ // * Transforms the given vector coordinates by the given transformation Transform and stores the result in the vector "result" coordinates+ // * @param vector defines the vector to transform+ // * @param transformation defines the Transform to apply+ // * @param result defines the target vector+ // */+ // static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Transform>, ignoreOffset: boolean = false) {+ // const t = transformation as IKeyValue;+ // const p = vector;++ // if (ignoreOffset) {+ // return new Vector2(t[0] * p.x + t[2] * p.y, t[1] * p.x + t[3] * p.y);+ // }+ // return new Vector2(t[0] * p.x + t[2] * p.y + t[4], t[1] * p.x + t[3] * p.y + t[5]);+ // }/*** Determines if a given vector is included in a trianglediff --git a/packages/base-render/src/Basics/index.ts b/packages/base-render/src/Basics/index.tsindex b9c6031899..e6e1719692 100644--- a/packages/base-render/src/Basics/index.ts+++ b/packages/base-render/src/Basics/index.ts@@ -9,3 +9,4 @@ export * from './Draw';export * from './Vector2';export * from './Path2';export * from './Tools';+export * from './ScrollXY';diff --git a/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts b/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.tsindex 2b3007b19e..33e5b3934a 100644--- a/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts+++ b/packages/base-render/src/Component/Docs/Block/Paragraph/Language.Ruler.ts@@ -1,4 +1,4 @@-import { DataStreamTreeNode, IParagraphStyle, Nullable } from '@univerjs/core';+import { DataStreamTreeNode, DocumentBodyModel, IParagraphStyle, Nullable } from '@univerjs/core';import { createSkeletonLetterSpan, createSkeletonWordSpan } from '../../Common/Span';import { hasArabic, hasCJK, hasSpaceAndTab, hasTibetan } from '../../../../Basics/Tools';import { IDocumentSkeletonSpan } from '../../../../Basics/IDocumentSkeletonCached';@@ -14,18 +14,19 @@ export function composeCharForLanguage(char: string,index: number,charArray: string,+ bodyModel: DocumentBodyModel,paragraphNode: DataStreamTreeNode,sectionBreakConfig: ISectionBreakConfig,paragraphStyle: IParagraphStyle): Nullable<LanguageResult> {if (hasArabic(char)) {- return ArabicHandler(char, index, charArray, paragraphNode, sectionBreakConfig, paragraphStyle);+ return ArabicHandler(char, index, charArray, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);}if (hasTibetan(char)) {- return TibetanHandler(char, index, charArray, paragraphNode, sectionBreakConfig, paragraphStyle);+ return TibetanHandler(char, index, charArray, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);}if (!hasCJK(char)) {- return notCJKHandler(char, index, charArray, paragraphNode, sectionBreakConfig, paragraphStyle);+ return notCJKHandler(char, index, charArray, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);}}@@ -33,13 +34,14 @@ function notCJKHandler(char: string,index: number,charArray: string,+ bodyModel: DocumentBodyModel,paragraphNode: DataStreamTreeNode,sectionBreakConfig: ISectionBreakConfig,paragraphStyle: IParagraphStyle) {// Chinese, Japanese, and Korean (CJK) characters.// https://en.wikipedia.org/wiki/CJK_characters- const config = getFontCreateConfig(index, paragraphNode, sectionBreakConfig, paragraphStyle);+ const config = getFontCreateConfig(index, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);const { pageWidth = Infinity } = config;const charSke = createSkeletonLetterSpan(char, config);const spanGroup = [charSke];@@ -48,7 +50,7 @@ function notCJKHandler(for (let i = index + 1; i < charArray.length; i++) {const newChar = charArray[i];if (!hasCJK(newChar) && !hasSpaceAndTab(newChar)) {- const newConfig = getFontCreateConfig(i, paragraphNode, sectionBreakConfig, paragraphStyle);+ const newConfig = getFontCreateConfig(i, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);const newSpan = createSkeletonLetterSpan(newChar, newConfig);const newCharWidth = newSpan.width;if (allWidth + newCharWidth > pageWidth) {@@ -72,12 +74,13 @@ function ArabicHandler(char: string,index: number,charArray: string,+ bodyModel: DocumentBodyModel,paragraphNode: DataStreamTreeNode,sectionBreakConfig: ISectionBreakConfig,paragraphStyle: IParagraphStyle) {// 组合阿拉伯语的词组- const config = getFontCreateConfig(index, paragraphNode, sectionBreakConfig, paragraphStyle);+ const config = getFontCreateConfig(index, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);const span = [char];let newCharIndex = index;for (let i = index + 1; i < charArray.length; i++) {@@ -99,12 +102,13 @@ function TibetanHandler(char: string,index: number,charArray: string,+ bodyModel: DocumentBodyModel,paragraphNode: DataStreamTreeNode,sectionBreakConfig: ISectionBreakConfig,paragraphStyle: IParagraphStyle) {// 组合藏语词组- const config = getFontCreateConfig(index, paragraphNode, sectionBreakConfig, paragraphStyle);+ const config = getFontCreateConfig(index, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);const span = [char];let newCharIndex = index;for (let i = index + 1; i < charArray.length; i++) {diff --git a/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts b/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.tsindex a9bfe33856..145c56c9a0 100644--- a/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts+++ b/packages/base-render/src/Component/Docs/Block/Paragraph/Paragraph.ts@@ -11,6 +11,7 @@ import {BooleanNumber,DataStreamTreeTokenType,ICustomBlock,+ DocumentBodyModel,} from '@univerjs/core';import { dealWidthBullet } from './Bullet';import { dealWidthInlineDrawing } from './InlineDrawing';@@ -34,7 +35,9 @@ import {import { IParagraphConfig, ISectionBreakConfig } from '../../../../Basics/Interfaces';import { createSkeletonTabSpan, createSkeletonWordSpan } from '../../Common/Span';+// eslint-disable-next-line max-lines-per-functionexport function dealWidthParagraph(+ bodyModel: DocumentBodyModel,paragraphNode: DataStreamTreeNode,curPage: IDocumentSkeletonPage,sectionBreakConfig: ISectionBreakConfig,@@ -61,8 +64,6 @@ export function dealWidthParagraph(const { endIndex, startIndex, content = '', blocks = [] } = paragraphNode;- const bodyModel = paragraphNode.bodyModel;-const paragraph = bodyModel.getParagraph(endIndex) || { startIndex: 0 };const { paragraphStyle = {}, bullet } = paragraph;@@ -139,7 +140,7 @@ export function dealWidthParagraph(const charIndex = i + startIndex;const char = content[i];- const fontCreateConfig = getFontCreateConfig(i, paragraphNode, sectionBreakConfig, paragraphStyle);+ const fontCreateConfig = getFontCreateConfig(i, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle);if (char === DataStreamTreeTokenType.TAB) {const charSpaceApply = getCharSpaceApply(charSpace, defaultTabStop, gridType, snapToGrid);@@ -174,7 +175,7 @@ export function dealWidthParagraph(}const paragraphStart = i === 0;- const languageHandlerResult = composeCharForLanguage(char, i, content, paragraphNode, sectionBreakConfig, paragraphStyle); // Handling special languages such as Tibetan, Arabic+ const languageHandlerResult = composeCharForLanguage(char, i, content, bodyModel, paragraphNode, sectionBreakConfig, paragraphStyle); // Handling special languages such as Tibetan, Arabiclet newSpanGroup = [];if (languageHandlerResult) {const { charIndex: newCharIndex, spanGroup } = languageHandlerResult;diff --git a/packages/base-render/src/Component/Docs/Block/Section.ts b/packages/base-render/src/Component/Docs/Block/Section.tsindex e0bd43e668..191929f2eb 100644--- a/packages/base-render/src/Component/Docs/Block/Section.ts+++ b/packages/base-render/src/Component/Docs/Block/Section.ts@@ -1,10 +1,11 @@-import { DataStreamTreeNodeType, ContextBase, DataStreamTreeNode } from '@univerjs/core';+import { DataStreamTreeNodeType, ContextBase, DataStreamTreeNode, DocumentBodyModel } from '@univerjs/core';import { dealWidthParagraph } from './Paragraph';import { dealWithBlockError } from './BlockError';import { IDocumentSkeletonPage, ISkeletonResourceReference } from '../../../Basics/IDocumentSkeletonCached';import { ISectionBreakConfig } from '../../../Basics/Interfaces';export function dealWithSections(+ bodyModel: DocumentBodyModel,sectionNode: DataStreamTreeNode,curPage: IDocumentSkeletonPage,sectionBreakConfig: ISectionBreakConfig,@@ -26,7 +27,7 @@ export function dealWithSections(}if (node.nodeType === DataStreamTreeNodeType.PARAGRAPH) {// Paragraph 段落- skeletonPages = dealWidthParagraph(node, currentPageCache, sectionBreakConfig, skeletonResourceReference, context);+ skeletonPages = dealWidthParagraph(bodyModel, node, currentPageCache, sectionBreakConfig, skeletonResourceReference, context);} else if (node.nodeType === DataStreamTreeNodeType.TABLE) {// Table 表格}diff --git a/packages/base-render/src/Component/Docs/Common/Page.ts b/packages/base-render/src/Component/Docs/Common/Page.tsindex d1fa1d15ee..6c39e809ff 100644--- a/packages/base-render/src/Component/Docs/Common/Page.ts+++ b/packages/base-render/src/Component/Docs/Common/Page.ts@@ -7,6 +7,7 @@ import { updateBlockIndex } from './Tools';// 新增数据结构框架// 判断奇数和偶数页码+// eslint-disable-next-line max-lines-per-functionexport function createSkeletonPage(sectionBreakConfig: ISectionBreakConfig,skeletonResourceReference: ISkeletonResourceReference,@@ -169,7 +170,7 @@ function _createSkeletonHeader(};const areaPage = createSkeletonPage(headerConfig, skeletonResourceReference);- const page = dealWithSections(headerOrFooter.children[0], areaPage, headerConfig, skeletonResourceReference).pages[0];+ const page = dealWithSections(headerOrFooter, headerOrFooter.children[0], areaPage, headerConfig, skeletonResourceReference).pages[0];updateBlockIndex([page]);const column = page.sections[0].columns[0];const height = column.height || 0;diff --git a/packages/base-render/src/Component/Docs/Common/Tools.ts b/packages/base-render/src/Component/Docs/Common/Tools.tsindex eb9a2b1b62..d64d8be992 100644--- a/packages/base-render/src/Component/Docs/Common/Tools.ts+++ b/packages/base-render/src/Component/Docs/Common/Tools.ts@@ -15,6 +15,7 @@ import {IParagraphStyle,ITextStyle,ObjectMatrix,+ DocumentBodyModel,} from '@univerjs/core';import {IDocumentSkeletonColumn,@@ -259,6 +260,7 @@ export function getCharSpaceConfig(sectionBreakConfig: ISectionBreakConfig, para};}+// eslint-disable-next-line max-lines-per-functionexport function updateBlockIndex(pages: IDocumentSkeletonPage[], start: number = -1) {const firstPage = pages[0];const { st: firstPageStartIndex } = firstPage;@@ -446,6 +448,7 @@ export function columnIterator(pages: IDocumentSkeletonPage[], iteratorFunction:}}+// eslint-disable-next-line max-lines-per-functionexport function getPositionHorizon(positionH: ObjectPositionH, column: IDocumentSkeletonColumn, page: IDocumentSkeletonPage, objectWidth: number, isPageBreak: boolean = false) {const { relativeFrom, align, posOffset, percent } = positionH;@@ -647,9 +650,14 @@ export function clearFontCreateConfigCache() {fontCreateConfigCache.reset();}-export function getFontCreateConfig(index: number, paragraphNode: DataStreamTreeNode, sectionBreakConfig: ISectionBreakConfig, paragraphStyle: IParagraphStyle) {+export function getFontCreateConfig(+ index: number,+ bodyModel: DocumentBodyModel,+ paragraphNode: DataStreamTreeNode,+ sectionBreakConfig: ISectionBreakConfig,+ paragraphStyle: IParagraphStyle+) {const { startIndex } = paragraphNode;- const bodyModel = paragraphNode.bodyModel;const textRun = bodyModel.getTextRun(index + startIndex) || { ts: {}, st: 0, ed: 0 };const { ts: textStyle = {}, st, ed } = textRun;const cache = fontCreateConfigCache.getValue(st, ed);diff --git a/packages/base-render/src/Component/Docs/DocSkeleton.ts b/packages/base-render/src/Component/Docs/DocSkeleton.tsindex 000fbe7c04..f676ab9164 100644--- a/packages/base-render/src/Component/Docs/DocSkeleton.ts+++ b/packages/base-render/src/Component/Docs/DocSkeleton.ts@@ -126,6 +126,7 @@ export class DocumentSkeleton extends Skeleton {* 按照SectionBreak分割文档,进行布局计算* @returns view model: skeleton*/+ // eslint-disable-next-line max-lines-per-functionprivate _createSkeleton(bounds?: IBoundRect) {// 每一个布局const DEFAULT_PAGE_SIZE = { width: Infinity, height: Infinity };@@ -284,7 +285,7 @@ export class DocumentSkeleton extends Skeleton {}// 计算页内布局,block结构const context = this.getContext();- const blockInfo = dealWithSections(sectionNode, curSkeletonPage, sectionBreakConfig, skeletonResourceReference, this._renderedBlockIdMap, context);+ const blockInfo = dealWithSections(bodyModel, sectionNode, curSkeletonPage, sectionBreakConfig, skeletonResourceReference, this._renderedBlockIdMap, context);// todo: 当本节有多个列,且下一节为连续节类型的时候,需要按照列数分割,重新计算linesif (sectionTypeNext === SectionType.CONTINUOUS && columnProperties.length > 0) {diff --git a/packages/base-render/src/Component/Docs/Document.Editor.ts b/packages/base-render/src/Component/Docs/Document.Editor.tsindex 374a094230..432a0a464a 100644--- a/packages/base-render/src/Component/Docs/Document.Editor.ts+++ b/packages/base-render/src/Component/Docs/Document.Editor.ts@@ -2,7 +2,7 @@ import { DataStreamTreeTokenType, Nullable, Observable, Observer } from '@univerimport { IDocumentSkeletonCached, IDocumentSkeletonSpan } from '../../Basics/IDocumentSkeletonCached';import { CURSOR_TYPE } from '../../Basics/Const';import { IMouseEvent, IPointerEvent } from '../../Basics/IEvents';-import { getCurrentScrollXY, getOffsetRectForDom, transformBoundingCoord } from '../../Basics/Position';+import { getOffsetRectForDom, transformBoundingCoord } from '../../Basics/Position';import { ScrollTimer } from '../../ScrollTimer';import { Documents } from './Document';@@ -11,8 +11,15 @@ import { IScrollObserverParam, Viewport } from '../../Viewport';import { checkStyle, injectStyle } from '../../Basics/Tools';import { Vector2 } from '../../Basics/Vector2';--import { IEditorInputConfig } from '../../Basics/Interfaces';+import { getCurrentScrollXY } from '../../Basics/ScrollXY';++interface IEditorInputConfig {+ event: Event | CompositionEvent | KeyboardEvent;+ content?: string;+ document: Documents;+ activeSelection?: TextSelection;+ selectionList?: TextSelection[];+}interface INodeInfo {node: IDocumentSkeletonSpan;diff --git a/packages/base-render/src/Scene.Transformer.ts b/packages/base-render/src/Scene.Transformer.tsindex b70749c1a6..4ae61460e2 100644--- a/packages/base-render/src/Scene.Transformer.ts+++ b/packages/base-render/src/Scene.Transformer.ts@@ -2,7 +2,7 @@ import { IKeyValue, Nullable, Observable, Observer } from '@univerjs/core';import { BaseObject } from './BaseObject';import { CURSOR_TYPE } from './Basics/Const';import { IMouseEvent, IPointerEvent } from './Basics/IEvents';-import { getCurrentScrollXY } from './Basics/Position';+import { getCurrentScrollXY } from './Basics/ScrollXY';import { Group } from './Group';import { Scene } from './Scene';import { ScrollTimer } from './ScrollTimer';diff --git a/packages/base-render/src/Viewport.ts b/packages/base-render/src/Viewport.tsindex f69cc2d288..efdd156d32 100644--- a/packages/base-render/src/Viewport.ts+++ b/packages/base-render/src/Viewport.ts@@ -439,6 +439,7 @@ export class Viewport {return svCoord;}+ // eslint-disable-next-line max-lines-per-functiononMouseWheel(evt: IWheelEvent, state: EventState) {if (!this._scrollBar) {return;diff --git a/packages/core/src/Docs/Action/ActionDataInterface.ts b/packages/core/src/Docs/Action/ActionDataInterface.tsnew file mode 100644index 0000000000..d3b99e31c6--- /dev/null+++ b/packages/core/src/Docs/Action/ActionDataInterface.ts@@ -0,0 +1,28 @@+import { IDocActionData } from '../../Command/DocActionBase';+import {+ CommonParameterAttribute,+ IDocumentBody,+} from '../../Interfaces/IDocumentData';++export interface IDeleteActionData extends IDocActionData {+ len: number;+ line: number;+ segmentId?: string;+}++export interface IInsertActionData extends IDocActionData {+ body: IDocumentBody;+ len: number;+ line: number;+ segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.+}++export interface IRetainActionData extends IDocActionData {+ attribute?: CommonParameterAttribute;+ len: number;+ segmentId?: string;+}++export interface IUpdateDocumentActionData extends IDocActionData {+ text: string;+}diff --git a/packages/core/src/Docs/Action/DeleteAction.ts b/packages/core/src/Docs/Action/DeleteAction.tsindex 6053b5889d..d3c5551949 100644--- a/packages/core/src/Docs/Action/DeleteAction.ts+++ b/packages/core/src/Docs/Action/DeleteAction.ts@@ -1,4 +1,4 @@-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';+import { DocActionBase } from '../../Command/DocActionBase';import {ActionObservers,ActionType,@@ -6,17 +6,12 @@ import {CommandUnit,CommonParameter,} from '../../Command';-import { IInsertActionData } from './InsertAction';+import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';import { IDocumentBody } from '../../Interfaces';import { DeleteApply } from '../Apply/DeleteApply';import { InsertApply } from '../Apply/InsertApply';--export interface IDeleteActionData extends IDocActionData {- len: number;- line: number;- segmentId?: string;-}+import { IDeleteActionData, IInsertActionData } from './ActionDataInterface';export class DeleteAction extends DocActionBase<IDeleteActionData,diff --git a/packages/core/src/Docs/Action/InsertAction.ts b/packages/core/src/Docs/Action/InsertAction.tsindex 29faf7427e..b8f9cf2543 100644--- a/packages/core/src/Docs/Action/InsertAction.ts+++ b/packages/core/src/Docs/Action/InsertAction.ts@@ -1,4 +1,4 @@-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';+import { DocActionBase } from '../../Command/DocActionBase';import {ActionObservers,ActionType,@@ -6,18 +6,10 @@ import {CommandUnit,CommonParameter,} from '../../Command';-import { IDeleteActionData } from './DeleteAction';-import { IDocumentBody } from '../../Interfaces';import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';import { InsertApply } from '../Apply/InsertApply';import { DeleteApply } from '../Apply/DeleteApply';--export interface IInsertActionData extends IDocActionData {- body: IDocumentBody;- len: number;- line: number;- segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.-}+import { IInsertActionData, IDeleteActionData } from './ActionDataInterface';export class InsertAction extends DocActionBase<IInsertActionData,diff --git a/packages/core/src/Docs/Action/RetainAction.ts b/packages/core/src/Docs/Action/RetainAction.tsindex 92d6624a27..c87ab10e7e 100644--- a/packages/core/src/Docs/Action/RetainAction.ts+++ b/packages/core/src/Docs/Action/RetainAction.ts@@ -1,4 +1,4 @@-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';+import { DocActionBase } from '../../Command/DocActionBase';import {ActionObservers,CommandManager,@@ -6,12 +6,7 @@ import {CommonParameter,} from '../../Command';import { CommonParameterAttribute } from '../../Interfaces/IDocumentData';--export interface IRetainActionData extends IDocActionData {- attribute?: CommonParameterAttribute;- len: number;- segmentId?: string;-}+import { IRetainActionData } from './ActionDataInterface';export class RetainAction extends DocActionBase<IRetainActionData,diff --git a/packages/core/src/Docs/Action/UpdateDocumentAction.ts b/packages/core/src/Docs/Action/UpdateDocumentAction.tsindex 7674f7d001..bfd9c18e2c 100644--- a/packages/core/src/Docs/Action/UpdateDocumentAction.ts+++ b/packages/core/src/Docs/Action/UpdateDocumentAction.ts@@ -1,9 +1,6 @@-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';+import { DocActionBase } from '../../Command/DocActionBase';import { ActionObservers, CommandManager, CommandUnit } from '../../Command';--export interface IUpdateDocumentActionData extends IDocActionData {- text: string;-}+import { IUpdateDocumentActionData } from './ActionDataInterface';export class UpdateDocumentAction extends DocActionBase<IUpdateDocumentActionData,diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.tsindex f07b27f5f7..6517ad9225 100644--- a/packages/core/src/Docs/Apply/DeleteApply.ts+++ b/packages/core/src/Docs/Apply/DeleteApply.ts@@ -32,20 +32,52 @@ function updateAttributeByDelete(textLength: number,currentIndex: number): IDocumentBody {- const {- textRuns,- paragraphs,- sectionBreaks,- customBlocks,- tables,- customRanges,- dataStream,- } = body;+ const { dataStream } = body;const startIndex = currentIndex;const endIndex = currentIndex + textLength - 1;+ const removeTextRuns = processTextRuns(body, textLength, currentIndex);++ const removeParagraphs = processParagraphs(body, textLength, currentIndex);++ const removeSectionBreaks = processSectionBreaks(body, textLength, currentIndex);++ const removeCustomBlocks = processCustomBlocks(body, textLength, currentIndex);++ const removeTables = processTables(body, textLength, currentIndex);++ const removeCustomRanges = processCustomRanges(body, textLength, currentIndex);++ let removeDataStream = '';+ if (dataStream) {+ body.dataStream = deleteContent(dataStream, startIndex, endIndex);++ removeDataStream = dataStream.slice(startIndex, endIndex);+ }++ return {+ dataStream: removeDataStream,+ textRuns: removeTextRuns,+ paragraphs: removeParagraphs,+ sectionBreaks: removeSectionBreaks,+ customBlocks: removeCustomBlocks,+ tables: removeTables,+ customRanges: removeCustomRanges,+ };+}++function processTextRuns(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { textRuns } = body;++ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeTextRuns = [];if (textRuns) {const newTextRuns = [];@@ -100,6 +132,19 @@ function updateAttributeByDelete(body.textRuns = newTextRuns;}+ return removeTextRuns;+}++function processParagraphs(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { paragraphs } = body;++ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeParagraphs = [];if (paragraphs) {const newParagraphs = [];@@ -120,7 +165,19 @@ function updateAttributeByDelete(}body.paragraphs = newParagraphs;}+ return removeParagraphs;+}++function processSectionBreaks(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { sectionBreaks } = body;+ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeSectionBreaks = [];if (sectionBreaks) {const newSectionBreaks = [];@@ -141,7 +198,19 @@ function updateAttributeByDelete(}body.sectionBreaks = newSectionBreaks;}+ return removeSectionBreaks;+}++function processCustomBlocks(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { customBlocks } = body;+ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeCustomBlocks = [];if (customBlocks) {const newCustomBlocks = [];@@ -162,7 +231,19 @@ function updateAttributeByDelete(}body.customBlocks = newCustomBlocks;}+ return removeCustomBlocks;+}++function processTables(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { tables } = body;+ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeTables = [];if (tables) {const newTables = [];@@ -201,7 +282,19 @@ function updateAttributeByDelete(}body.tables = newTables;}+ return removeTables;+}+function processCustomRanges(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { customRanges } = body;++ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;const removeCustomRanges = [];if (customRanges) {const newCustomRanges = [];@@ -240,21 +333,5 @@ function updateAttributeByDelete(}body.customRanges = newCustomRanges;}-- let removeDataStream = '';- if (dataStream) {- body.dataStream = deleteContent(dataStream, startIndex, endIndex);-- removeDataStream = dataStream.slice(startIndex, endIndex);- }-- return {- dataStream: removeDataStream,- textRuns: removeTextRuns,- paragraphs: removeParagraphs,- sectionBreaks: removeSectionBreaks,- customBlocks: removeCustomBlocks,- tables: removeTables,- customRanges: removeCustomRanges,- };+ return removeCustomRanges;}diff --git a/packages/core/src/Docs/Apply/InsertApply.ts b/packages/core/src/Docs/Apply/InsertApply.tsindex e70f4814b7..51b516056a 100644--- a/packages/core/src/Docs/Apply/InsertApply.ts+++ b/packages/core/src/Docs/Apply/InsertApply.ts@@ -25,9 +25,9 @@ export function InsertApply(throw new Error('no body has changed');}- updateAttributeByInsert(body, insertBody, textLength, currentIndex);-bodyModel.insert(insertBody, currentIndex);++ updateAttributeByInsert(body, insertBody, textLength, currentIndex);}function updateAttributeByInsert(@@ -36,192 +36,260 @@ function updateAttributeByInsert(textLength: number,currentIndex: number) {- const {- textRuns,- paragraphs,- sectionBreaks,- customBlocks,- tables,- customRanges,- } = body;-- if (textRuns) {- let insertIndex = Infinity;- for (let i = 0, len = textRuns.length; i < len; i++) {- const textRun = textRuns[i];- const { st, ed } = textRun;- if (st > currentIndex) {- textRun.st += textLength;- textRun.ed += textLength;-- if (insertIndex === Infinity) {- insertIndex = -Infinity;- }- } else if (ed >= currentIndex) {- textRun.ed += textLength;- insertIndex = i;- }- }+ processTextRuns(body, insertBody, textLength, currentIndex);++ processParagraphs(body, insertBody, textLength, currentIndex);++ processSectionBreaks(body, insertBody, textLength, currentIndex);++ processCustomBlocks(body, insertBody, textLength, currentIndex);++ processTables(body, insertBody, textLength, currentIndex);++ processCustomRanges(body, insertBody, textLength, currentIndex);+}++function processTextRuns(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { textRuns } = body;++ if (textRuns == null) {+ return;+ }++ let insertIndex = Infinity;+ for (let i = 0, len = textRuns.length; i < len; i++) {+ const textRun = textRuns[i];+ const { st, ed } = textRun;+ if (st > currentIndex) {+ textRun.st += textLength;+ textRun.ed += textLength;- const insertTextRuns = insertBody.textRuns;- if (insertTextRuns) {- for (let i = 0, len = insertTextRuns.length; i < len; i++) {- const insertTextRun = insertTextRuns[i];- insertTextRun.st += currentIndex;- insertTextRun.ed += currentIndex;- }if (insertIndex === Infinity) {- textRuns.push(...insertTextRuns);- }- if (insertIndex === -Infinity) {- textRuns.unshift(...insertTextRuns);- } else {- const splitTextRun = textRuns[insertIndex];- const { st, ed } = splitTextRun;- const startSplitTextRun = {- ...splitTextRun,- st,- ed: insertTextRuns[0].st - 1,- };-- if (isSameStyleTextRun(startSplitTextRun, insertTextRuns[0])) {- startSplitTextRun.ed = insertTextRuns[0].ed;- insertTextRuns.shift();- }-- const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];- const endSplitTextRun = {- ...splitTextRun,- st: lastInsertTextRuns.ed + 1,- ed,- };-- if (isSameStyleTextRun(endSplitTextRun, lastInsertTextRuns)) {- endSplitTextRun.st = lastInsertTextRuns.st;- insertTextRuns.pop();- }-- textRuns.splice(- insertIndex,- 1,- startSplitTextRun,- ...insertTextRuns,- endSplitTextRun- );+ insertIndex = -Infinity;}+ } else if (ed >= currentIndex) {+ textRun.ed += textLength;+ insertIndex = i;}}- if (paragraphs) {- for (let i = 0, len = paragraphs.length; i < len; i++) {- const paragraph = paragraphs[i];- const { startIndex } = paragraph;- if (startIndex > currentIndex) {- paragraph.startIndex += textLength;- }+ const insertTextRuns = insertBody.textRuns;+ if (insertTextRuns) {+ for (let i = 0, len = insertTextRuns.length; i < len; i++) {+ const insertTextRun = insertTextRuns[i];+ insertTextRun.st += currentIndex;+ insertTextRun.ed += currentIndex;}+ if (insertIndex === Infinity) {+ textRuns.push(...insertTextRuns);+ }+ if (insertIndex === -Infinity) {+ textRuns.unshift(...insertTextRuns);+ } else {+ const splitTextRun = textRuns[insertIndex];+ const { st, ed } = splitTextRun;+ const startSplitTextRun = {+ ...splitTextRun,+ st,+ ed: insertTextRuns[0].st - 1,+ };++ if (isSameStyleTextRun(startSplitTextRun, insertTextRuns[0])) {+ startSplitTextRun.ed = insertTextRuns[0].ed;+ insertTextRuns.shift();+ }- const insertParagraphs = insertBody.paragraphs;- if (insertParagraphs) {- for (let i = 0, len = insertParagraphs.length; i < len; i++) {- const insertTextRun = insertParagraphs[i];- insertTextRun.startIndex += currentIndex;+ const lastInsertTextRuns = insertTextRuns[insertTextRuns.length - 1];+ const endSplitTextRun = {+ ...splitTextRun,+ st: lastInsertTextRuns.ed + 1,+ ed,+ };++ if (isSameStyleTextRun(endSplitTextRun, lastInsertTextRuns)) {+ endSplitTextRun.st = lastInsertTextRuns.st;+ insertTextRuns.pop();}- paragraphs.push(...insertParagraphs);- paragraphs.sort(sortRulesFactory('startIndex'));+ textRuns.splice(+ insertIndex,+ 1,+ startSplitTextRun,+ ...insertTextRuns,+ endSplitTextRun+ );}}+}- if (sectionBreaks) {- for (let i = 0, len = sectionBreaks.length; i < len; i++) {- const sectionBreak = sectionBreaks[i];- const { startIndex } = sectionBreak;- if (startIndex > currentIndex) {- sectionBreak.startIndex += textLength;- }+function processParagraphs(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { paragraphs } = body;++ if (paragraphs == null) {+ return;+ }+ for (let i = 0, len = paragraphs.length; i < len; i++) {+ const paragraph = paragraphs[i];+ const { startIndex } = paragraph;+ if (startIndex > currentIndex) {+ paragraph.startIndex += textLength;+ }+ }++ const insertParagraphs = insertBody.paragraphs;+ if (insertParagraphs) {+ for (let i = 0, len = insertParagraphs.length; i < len; i++) {+ const insertTextRun = insertParagraphs[i];+ insertTextRun.startIndex += currentIndex;}- const insertSectionBreaks = insertBody.sectionBreaks;- if (insertSectionBreaks) {- for (let i = 0, len = insertSectionBreaks.length; i < len; i++) {- const sectionBreak = insertSectionBreaks[i];- sectionBreak.startIndex += currentIndex;- }+ paragraphs.push(...insertParagraphs);+ paragraphs.sort(sortRulesFactory('startIndex'));+ }+}- sectionBreaks.push(...insertSectionBreaks);- sectionBreaks.sort(sortRulesFactory('startIndex'));+function processSectionBreaks(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { sectionBreaks } = body;++ if (sectionBreaks == null) {+ return;+ }++ for (let i = 0, len = sectionBreaks.length; i < len; i++) {+ const sectionBreak = sectionBreaks[i];+ const { startIndex } = sectionBreak;+ if (startIndex > currentIndex) {+ sectionBreak.startIndex += textLength;}}- if (customBlocks) {- for (let i = 0, len = customBlocks.length; i < len; i++) {- const customBlock = customBlocks[i];- const { startIndex } = customBlock;- if (startIndex > currentIndex) {- customBlock.startIndex += textLength;- }+ const insertSectionBreaks = insertBody.sectionBreaks;+ if (insertSectionBreaks) {+ for (let i = 0, len = insertSectionBreaks.length; i < len; i++) {+ const sectionBreak = insertSectionBreaks[i];+ sectionBreak.startIndex += currentIndex;}- const insertCustomBlocks = insertBody.customBlocks;- if (insertCustomBlocks) {- for (let i = 0, len = insertCustomBlocks.length; i < len; i++) {- const customBlock = insertCustomBlocks[i];- customBlock.startIndex += currentIndex;- }+ sectionBreaks.push(...insertSectionBreaks);+ sectionBreaks.sort(sortRulesFactory('startIndex'));+ }+}++function processCustomBlocks(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { customBlocks } = body;++ if (customBlocks == null) {+ return;+ }- customBlocks.push(...insertCustomBlocks);- customBlocks.sort(sortRulesFactory('startIndex'));+ for (let i = 0, len = customBlocks.length; i < len; i++) {+ const customBlock = customBlocks[i];+ const { startIndex } = customBlock;+ if (startIndex > currentIndex) {+ customBlock.startIndex += textLength;}}- if (tables) {- for (let i = 0, len = tables.length; i < len; i++) {- const table = tables[i];- const { startIndex, endIndex } = table;- if (startIndex > currentIndex) {- table.startIndex += textLength;- table.endIndex += textLength;- } else if (endIndex >= currentIndex) {- table.endIndex += textLength;- }+ const insertCustomBlocks = insertBody.customBlocks;+ if (insertCustomBlocks) {+ for (let i = 0, len = insertCustomBlocks.length; i < len; i++) {+ const customBlock = insertCustomBlocks[i];+ customBlock.startIndex += currentIndex;}- const insertTables = insertBody.tables;- if (insertTables) {- for (let i = 0, len = insertTables.length; i < len; i++) {- const table = insertTables[i];- table.startIndex += currentIndex;- table.endIndex += currentIndex;- }+ customBlocks.push(...insertCustomBlocks);+ customBlocks.sort(sortRulesFactory('startIndex'));+ }+}++function processTables(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { tables } = body;++ if (tables == null) {+ return;+ }- tables.push(...insertTables);- tables.sort(sortRulesFactory('startIndex'));+ for (let i = 0, len = tables.length; i < len; i++) {+ const table = tables[i];+ const { startIndex, endIndex } = table;+ if (startIndex > currentIndex) {+ table.startIndex += textLength;+ table.endIndex += textLength;+ } else if (endIndex >= currentIndex) {+ table.endIndex += textLength;}}- if (customRanges) {- for (let i = 0, len = customRanges.length; i < len; i++) {- const customRange = customRanges[i];- const { startIndex, endIndex } = customRange;- if (startIndex > currentIndex) {- customRange.startIndex += textLength;- customRange.endIndex += textLength;- } else if (endIndex >= currentIndex) {- customRange.endIndex += textLength;- }+ const insertTables = insertBody.tables;+ if (insertTables) {+ for (let i = 0, len = insertTables.length; i < len; i++) {+ const table = insertTables[i];+ table.startIndex += currentIndex;+ table.endIndex += currentIndex;}- const insertCustomRanges = insertBody.customRanges;- if (insertCustomRanges) {- for (let i = 0, len = insertCustomRanges.length; i < len; i++) {- const customRange = insertCustomRanges[i];- customRange.startIndex += currentIndex;- customRange.endIndex += currentIndex;- }+ tables.push(...insertTables);+ tables.sort(sortRulesFactory('startIndex'));+ }+}++function processCustomRanges(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const { customRanges } = body;++ if (customRanges == null) {+ return;+ }++ for (let i = 0, len = customRanges.length; i < len; i++) {+ const customRange = customRanges[i];+ const { startIndex, endIndex } = customRange;+ if (startIndex > currentIndex) {+ customRange.startIndex += textLength;+ customRange.endIndex += textLength;+ } else if (endIndex >= currentIndex) {+ customRange.endIndex += textLength;+ }+ }- customRanges.push(...insertCustomRanges);- customRanges.sort(sortRulesFactory('startIndex'));+ const insertCustomRanges = insertBody.customRanges;+ if (insertCustomRanges) {+ for (let i = 0, len = insertCustomRanges.length; i < len; i++) {+ const customRange = insertCustomRanges[i];+ customRange.startIndex += currentIndex;+ customRange.endIndex += currentIndex;}++ customRanges.push(...insertCustomRanges);+ customRanges.sort(sortRulesFactory('startIndex'));}}diff --git a/packages/core/src/Docs/Domain/DataStreamTreeNode.ts b/packages/core/src/Docs/Domain/DataStreamTreeNode.tsnew file mode 100644index 0000000000..21d8fd0295--- /dev/null+++ b/packages/core/src/Docs/Domain/DataStreamTreeNode.ts@@ -0,0 +1,175 @@+import {+ deleteContent,+ horizontalLineSegmentsSubtraction,+ insertTextToContent,+} from '../../Shared/Common';+import { Nullable } from '../../Shared/Types';+import { DataStreamTreeNodeType } from './Types';++export class DataStreamTreeNode {+ children: DataStreamTreeNode[] = [];++ parent: Nullable<DataStreamTreeNode>;++ startIndex: number;++ endIndex: number;++ blocks: number[] = [];++ constructor(public nodeType: DataStreamTreeNodeType, public content?: string) {}++ static create(nodeType: DataStreamTreeNodeType, content?: string) {+ return new DataStreamTreeNode(nodeType, content);+ }++ getProps() {+ return {+ children: this.children,+ parent: this.parent,+ startIndex: this.startIndex,+ endIndex: this.endIndex,+ nodeType: this.nodeType,++ content: this.content,+ };+ }++ addBlocks(blocks: number[]) {+ this.blocks = this.blocks.concat(blocks);+ }++ setIndexRange(startIndex: number, endIndex: number) {+ this.startIndex = startIndex;+ this.endIndex = endIndex;+ }++ insertText(text: string, insertIndex: number) {+ this.content = insertTextToContent(+ this.content || '',+ insertIndex - this.startIndex + 1,+ text+ );+ }++ exclude(index: number) {+ return index < this.startIndex || index > this.endIndex;+ }++ plus(len: number) {+ this.startIndex += len;+ this.endIndex += len;+ }++ split(index: number) {+ const {+ children,+ parent,+ startIndex,+ endIndex,+ nodeType,+ content = '',+ } = this.getProps();++ if (this.exclude(index)) {+ return;+ }++ const firstStartIndex = 0;+ const firstEndIndex = index - startIndex;++ const lastStartIndex = index - startIndex + 1;+ const lastEndIndex = endIndex;++ const firstNode = DataStreamTreeNode.create(+ nodeType,++ content.slice(firstStartIndex, firstEndIndex)+ );++ firstNode.parent = parent;+ firstNode.setIndexRange(firstStartIndex, firstEndIndex);++ const lastNode = DataStreamTreeNode.create(+ nodeType,++ content.slice(lastStartIndex, lastEndIndex)+ );++ lastNode.parent = parent;+ lastNode.setIndexRange(lastStartIndex, lastEndIndex);++ const firstChildNodes: DataStreamTreeNode[] = [];+ const lastChildNodes: DataStreamTreeNode[] = [];++ for (let node of children) {+ const { startIndex: childStartIndex } = node;+ if (node.exclude(index)) {+ if (index < childStartIndex) {+ firstChildNodes.push(node);+ } else {+ lastChildNodes.push(node);+ }+ } else {+ const splitData = node.split(index);+ if (splitData == null) {+ firstChildNodes.push(node);+ continue;+ }+ const { firstNode, lastNode } = splitData;+ firstChildNodes.push(firstNode);+ firstChildNodes.push(lastNode);+ }+ }++ firstNode.children = firstChildNodes;++ lastNode.children = lastChildNodes;++ return {+ firstNode,+ lastNode,+ };+ }++ getPositionInParent() {+ return this.parent?.children.indexOf(this) || -1;+ }++ remove() {+ this.children = [];+ if (this.parent == null) {+ return;+ }+ this.parent.children.splice(this.getPositionInParent(), 1);+ this.parent = null;+ }++ minus(startIndex: number, endIndex: number) {+ const segments = horizontalLineSegmentsSubtraction(+ this.startIndex,+ this.endIndex,+ startIndex,+ endIndex+ );++ if (segments.length > 2) {+ const seg1 = segments[0];+ const seg2 = segments[1];+ this.startIndex = seg1[0];+ this.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;+ } else {+ this.startIndex = segments[0][0];+ this.endIndex = segments[0][1];+ }++ this.content = deleteContent(this.content || '', startIndex, endIndex);+ }++ merge(node: DataStreamTreeNode) {+ const { endIndex, children } = node;+ this.endIndex = endIndex;+ this.children.push(...children);+ node.remove();+ }+}diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.tsindex 766908145c..77964bc1c0 100644--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts@@ -1,10 +1,8 @@import { IDocumentBody, ITextRun } from '../../Interfaces/IDocumentData';-import {- deleteContent,- horizontalLineSegmentsSubtraction,- insertTextToContent,-} from '../../Shared/Common';+import { Nullable } from '../../Shared/Types';+import { DataStreamTreeNode } from './DataStreamTreeNode';+import { DataStreamTreeNodeType, DataStreamTreeTokenType } from './Types';export type DocumentBodyModelOrSimple = DocumentBodyModelSimple | DocumentBodyModel;@@ -57,7 +55,7 @@ export class DocumentBodyModelSimple {content += DataStreamTreeTokenType.PARAGRAPH;const node = DataStreamTreeNode.create(DataStreamTreeNodeType.PARAGRAPH,- this,+content);node.setIndexRange(i - content.length + 1, i);@@ -67,8 +65,7 @@ export class DocumentBodyModelSimple {currentBlocks = [];} else if (char === DataStreamTreeTokenType.SECTION_BREAK) {const sectionTree = DataStreamTreeNode.create(- DataStreamTreeNodeType.SECTION_BREAK,- this+ DataStreamTreeNodeType.SECTION_BREAK);this.batchParent(sectionTree, nodeList);let lastNode = nodeList[nodeList.length - 1];@@ -79,18 +76,15 @@ export class DocumentBodyModelSimple {nodeList = [];} else if (char === DataStreamTreeTokenType.TABLE_START) {nodeList.push(- DataStreamTreeNode.create(DataStreamTreeNodeType.TABLE, this)+ DataStreamTreeNode.create(DataStreamTreeNodeType.TABLE));} else if (char === DataStreamTreeTokenType.TABLE_ROW_START) {nodeList.push(- DataStreamTreeNode.create(DataStreamTreeNodeType.TABLE_ROW, this)+ DataStreamTreeNode.create(DataStreamTreeNodeType.TABLE_ROW));} else if (char === DataStreamTreeTokenType.TABLE_CELL_START) {nodeList.push(- DataStreamTreeNode.create(- DataStreamTreeNodeType.TABLE_CELL,- this- )+ DataStreamTreeNode.create(DataStreamTreeNodeType.TABLE_CELL));} else if (char === DataStreamTreeTokenType.TABLE_END) {this.processPreviousNodesUntil(@@ -537,224 +531,3 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {private merge() {}}--export class DataStreamTreeNode {- children: DataStreamTreeNode[] = [];-- parent: Nullable<DataStreamTreeNode>;-- startIndex: number;-- endIndex: number;-- blocks: number[] = [];-- constructor(- public nodeType: DataStreamTreeNodeType,- public bodyModel: DocumentBodyModelOrSimple,- public content?: string- ) {}-- static create(- nodeType: DataStreamTreeNodeType,- bodyModel: DocumentBodyModelOrSimple,- content?: string- ) {- return new DataStreamTreeNode(nodeType, bodyModel, content);- }-- getProps() {- return {- children: this.children,- parent: this.parent,- startIndex: this.startIndex,- endIndex: this.endIndex,- nodeType: this.nodeType,- bodyModel: this.bodyModel,- content: this.content,- };- }-- addBlocks(blocks: number[]) {- this.blocks = this.blocks.concat(blocks);- }-- setIndexRange(startIndex: number, endIndex: number) {- this.startIndex = startIndex;- this.endIndex = endIndex;- }-- insertText(text: string, insertIndex: number) {- this.content = insertTextToContent(- this.content || '',- insertIndex - this.startIndex + 1,- text- );- }-- exclude(index: number) {- return index < this.startIndex || index > this.endIndex;- }-- plus(len: number) {- this.startIndex += len;- this.endIndex += len;- }-- split(index: number) {- const {- children,- parent,- startIndex,- endIndex,- nodeType,- bodyModel,- content = '',- } = this.getProps();-- if (this.exclude(index)) {- return;- }-- const firstStartIndex = 0;- const firstEndIndex = index - startIndex;-- const lastStartIndex = index - startIndex + 1;- const lastEndIndex = endIndex;-- const firstNode = DataStreamTreeNode.create(- nodeType,- bodyModel,- content.slice(firstStartIndex, firstEndIndex)- );-- firstNode.parent = parent;- firstNode.setIndexRange(firstStartIndex, firstEndIndex);-- const lastNode = DataStreamTreeNode.create(- nodeType,- bodyModel,- content.slice(lastStartIndex, lastEndIndex)- );-- lastNode.parent = parent;- lastNode.setIndexRange(lastStartIndex, lastEndIndex);-- const firstChildNodes: DataStreamTreeNode[] = [];- const lastChildNodes: DataStreamTreeNode[] = [];-- for (let node of children) {- const { startIndex: childStartIndex } = node;- if (node.exclude(index)) {- if (index < childStartIndex) {- firstChildNodes.push(node);- } else {- lastChildNodes.push(node);- }- } else {- const splitData = node.split(index);- if (splitData == null) {- firstChildNodes.push(node);- continue;- }- const { firstNode, lastNode } = splitData;- firstChildNodes.push(firstNode);- firstChildNodes.push(lastNode);- }- }-- firstNode.children = firstChildNodes;-- lastNode.children = lastChildNodes;-- return {- firstNode,- lastNode,- };- }-- getPositionInParent() {- return this.parent?.children.indexOf(this) || -1;- }-- remove() {- this.children = [];- if (this.parent == null) {- return;- }- this.parent.children.splice(this.getPositionInParent(), 1);- this.parent = null;- }-- minus(startIndex: number, endIndex: number) {- const segments = horizontalLineSegmentsSubtraction(- this.startIndex,- this.endIndex,- startIndex,- endIndex- );-- if (segments.length > 2) {- const seg1 = segments[0];- const seg2 = segments[1];- this.startIndex = seg1[0];- this.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;- } else {- this.startIndex = segments[0][0];- this.endIndex = segments[0][1];- }-- this.content = deleteContent(this.content || '', startIndex, endIndex);- }-- merge(node: DataStreamTreeNode) {- const { endIndex, children } = node;- this.endIndex = endIndex;- this.children.push(...children);- node.remove();- }-- moveTo(newParent: DataStreamTreeNode) {}-}--export enum DataStreamTreeNodeType {- // COLUMN_BREAK, // \v 换列- // PAGE_BREAK, // \f 换页- // DOCS_END, // \0 文档结尾- // TAB, // \t 制表符- PARAGRAPH, // \r 段落- SECTION_BREAK, // \n 章节- TABLE,- TABLE_ROW,- TABLE_CELL,- // CUSTOM_BLOCK, // \b 图片 mention等不参与文档流的场景- // TABLE_START, // \x1A 表格开始- // TABLE_ROW_START, // \x1B 表格开始- // TABLE_CELL_START, // \x1C 表格开始- // TABLE_CELL_END, //* \x1D 表格开始- // TABLE_ROW_END, // \x1E 表格开始- // TABLE_END, // \x1F 表格结束- // CUSTOM_RANGE_START, // \x1F 自定义范围开始- // CUSTOM_RANGE_END, // \x1E 自定义范围结束-}--export enum DataStreamTreeTokenType {- PARAGRAPH = '\r', // 段落- SECTION_BREAK = '\n', // 章节- TABLE_START = '\x1A', // 表格开始- TABLE_ROW_START = '\x1B', // 表格开始- TABLE_CELL_START = '\x1C', // 表格开始- TABLE_CELL_END = '\x1D', // 表格开始- TABLE_ROW_END = '\x1E', // 表格开始- TABLE_END = '\x1F', // 表格结束- CUSTOM_RANGE_START = '\x1F', // 自定义范围开始- CUSTOM_RANGE_END = '\x1E', // 自定义范围结束-- COLUMN_BREAK = '\v', // 换列- PAGE_BREAK = '\f', // 换页- DOCS_END = '\0', // 文档结尾- TAB = '\t', // 制表符- CUSTOM_BLOCK = '\b', // 图片 mention等不参与文档流的场景-- LETTER = '',-}diff --git a/packages/core/src/Docs/Domain/Types.ts b/packages/core/src/Docs/Domain/Types.tsnew file mode 100644index 0000000000..ad493d4bf0--- /dev/null+++ b/packages/core/src/Docs/Domain/Types.ts@@ -0,0 +1,41 @@+export enum DataStreamTreeNodeType {+ // COLUMN_BREAK, // \v 换列+ // PAGE_BREAK, // \f 换页+ // DOCS_END, // \0 文档结尾+ // TAB, // \t 制表符+ PARAGRAPH, // \r 段落+ SECTION_BREAK, // \n 章节+ TABLE,+ TABLE_ROW,+ TABLE_CELL,+ // CUSTOM_BLOCK, // \b 图片 mention等不参与文档流的场景+ // TABLE_START, // \x1A 表格开始+ // TABLE_ROW_START, // \x1B 表格开始+ // TABLE_CELL_START, // \x1C 表格开始+ // TABLE_CELL_END, //* \x1D 表格开始+ // TABLE_ROW_END, // \x1E 表格开始+ // TABLE_END, // \x1F 表格结束+ // CUSTOM_RANGE_START, // \x1F 自定义范围开始+ // CUSTOM_RANGE_END, // \x1E 自定义范围结束+}++export enum DataStreamTreeTokenType {+ PARAGRAPH = '\r', // 段落+ SECTION_BREAK = '\n', // 章节+ TABLE_START = '\x1A', // 表格开始+ TABLE_ROW_START = '\x1B', // 表格开始+ TABLE_CELL_START = '\x1C', // 表格开始+ TABLE_CELL_END = '\x1D', // 表格开始+ TABLE_ROW_END = '\x1E', // 表格开始+ TABLE_END = '\x1F', // 表格结束+ CUSTOM_RANGE_START = '\x1F', // 自定义范围开始+ CUSTOM_RANGE_END = '\x1E', // 自定义范围结束++ COLUMN_BREAK = '\v', // 换列+ PAGE_BREAK = '\f', // 换页+ DOCS_END = '\0', // 文档结尾+ TAB = '\t', // 制表符+ CUSTOM_BLOCK = '\b', // 图片 mention等不参与文档流的场景++ LETTER = '',+}diff --git a/packages/core/src/Docs/Domain/index.ts b/packages/core/src/Docs/Domain/index.tsindex c08bfc8db2..19cc3bcca6 100644--- a/packages/core/src/Docs/Domain/index.ts+++ b/packages/core/src/Docs/Domain/index.ts@@ -1,2 +1,4 @@export * from './DocumentModel';+export * from './Types';+export * from './DataStreamTreeNode';export * from './DocumentBodyModel';