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 dev
DR-Univera1ccbaa5a416
diff --git a/packages/core/src/Command/CommonParameter.ts b/packages/core/src/Command/CommonParameter.tsindex 686fcd4371..906b614975 100644--- a/packages/core/src/Command/CommonParameter.ts+++ b/packages/core/src/Command/CommonParameter.ts@@ -1,33 +1,12 @@-import {- ITextRun,- IParagraph,- ISectionBreak,- ICustomBlock,- ITable,- ICustomRange,-} from '../Interfaces';-import { Nullable } from '../Shared';--type CommonParameterAttribute = Nullable<- ITextRun | IParagraph | ISectionBreak | ICustomBlock | ITable | ICustomRange->;-export class CommonParameter {cursor: number;- attributes: CommonParameterAttribute[] = [];-reset() {this.cursor = 0;- this.attributes = [];return this;}- resetAttribute() {- this.attributes = [];- }-- addAttribute(attr: CommonParameterAttribute) {- this.attributes.push(attr);+ moveCursor(pos: number) {+ this.cursor += pos;}}diff --git a/packages/core/src/Const/DOC_ACTION_NAMES.ts b/packages/core/src/Const/DOC_ACTION_NAMES.tsindex 3afc7498a9..b17b9f3d06 100644--- a/packages/core/src/Const/DOC_ACTION_NAMES.ts+++ b/packages/core/src/Const/DOC_ACTION_NAMES.ts@@ -1,25 +1,6 @@export enum DOC_ACTION_NAMES {- INSERT_TEXT_ACTION_NAME = 'InsertTextAction',- DELETE_TEXT_ACTION_NAME = 'DeleteTextAction',- CLEAR_TEXT_STYLE_ACTION_NAME = 'ClearTextStyleAction',- DELETE_PARAGRAPH_ACTION_NAME = 'DeleteParagraphAction',- INSERT_PARAGRAPH_ACTION_NAME = 'InsertParagraphAction',- MERGE_PARAGRAPH_ACTION_NAME = 'MergeParagraphAction',- MOVE_PARAGRAPH_ACTION_NAME = 'MoveParagraphAction',- SET_TEXT_STYLE_ACTION_NAME = 'SetTextStyleAction',- SPLIT_PARAGRAPH_ACTION_NAME = 'SplitParagraphAction',- UPDATE_DOCUMENT_STYLE_ACTION_NAME = 'UpdateDocumentAction',- UPDATE_PARAGRAPH_STYLE_ACTION_NAME = 'UpdateParagraphAction',- UPDATE_SECTION_ACTION_NAME = 'UpdateSectionAction',-- DELETE_BULLET_ACTION_NAME = 'DeleteBulletAction',- DELETE_PARAGRAPH_BULLET_ACTION_NAME = 'DeleteParagraphBulletAction',- DELETE_SECTION_ACTION_NAME = 'DeleteSectionAction',- INSERT_BULLET_ACTION_NAME = 'InsertBulletAction',- INSERT_PARAGRAPH_BULLET_ACTION_NAME = 'InsertParagraphBulletAction',- INSERT_SECTION_ACTION_NAME = 'InsertSectionAction',- UPDATE_BULLET_ACTION_NAME = 'UpdateBulletAction',- UPDATE_PARAGRAPH_BULLET_ACTION_NAME = 'UpdateParagraphBulletAction',-- TEXT_INDEX_ADJUST_ACTION_NAME = 'TextIndexAdjustAction',+ INSERT_ACTION_NAME = 'InsertAction',+ DELETE_ACTION_NAME = 'DeleteAction',+ RETAIN_ACTION_NAME = 'RetainAction',+ UPDATE_DOCUMENT_ACTION_NAME = 'UpdateDocumentAction',}diff --git a/packages/core/src/Docs/Action/DeleteAction.ts b/packages/core/src/Docs/Action/DeleteAction.tsindex f82a854595..ba44a51ab9 100644--- a/packages/core/src/Docs/Action/DeleteAction.ts+++ b/packages/core/src/Docs/Action/DeleteAction.ts@@ -1,27 +1,42 @@import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';-import { ActionObservers, CommandManager, CommandUnit } from '../../Command';-import { IInsertTextActionData } from './InsertAction';+import {+ ActionObservers,+ CommandManager,+ CommandUnit,+ CommonParameter,+} from '../../Command';+import { IInsertActionData } from './InsertAction';+import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';+import { IDocumentBody } from '../../Interfaces';-export interface IDeleteTextActionData extends IDocActionData {+export interface IDeleteActionData extends IDocActionData {len: number;+ line: number;segmentId?: string;}-export class DeleteTextAction extends DocActionBase<- IDeleteTextActionData,- IInsertTextActionData+export class DeleteAction extends DocActionBase<+ IDeleteActionData,+ IInsertActionData> {- static Name = 'DeleteTextAction';+ static Name = 'DeleteAction';constructor(- actionData: IDeleteTextActionData,+ actionData: IDeleteActionData,commandUnit: CommandUnit,- observers: ActionObservers+ observers: ActionObservers,+ commonParameter: CommonParameter) {super(actionData, commandUnit, observers);this._doActionData = { ...actionData };- this.do();- const { len, segmentId } = actionData;+ const { len, line, segmentId } = actionData;++ this._oldActionData = {+ ...actionData,+ actionName: DOC_ACTION_NAMES.INSERT_ACTION_NAME,+ body: this.do(commonParameter),+ };+// this._oldActionData = {// actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,// text,@@ -32,14 +47,15 @@ export class DeleteTextAction extends DocActionBase<// };}- redo(): void {- this.do();+ redo(commonParameter: CommonParameter): void {+ this.do(commonParameter);}- do(): void {+ do(commonParameter: CommonParameter): IDocumentBody {const actionData = this.getDoActionData();const document = this.getDocument();const { len, segmentId } = actionData;+ commonParameter.moveCursor(len);// DeleteTextApply(document, {// cursorStart,// cursorEnd,@@ -48,13 +64,16 @@ export class DeleteTextAction extends DocActionBase<// isCollapse,// segmentId,// });+ return {+ dataStream: '',+ };}- undo(): void {- // TODO ...+ undo(commonParameter: CommonParameter): void {const actionData = this.getOldActionData();const document = this.getDocument();- const { text, len, segmentId } = actionData;+ const { body, len, segmentId } = actionData;+ commonParameter.moveCursor(len);// InsertTextApply(document, text, textLength, {// cursorStart,// isStartBack,@@ -67,4 +86,4 @@ export class DeleteTextAction extends DocActionBase<}}-CommandManager.register(DeleteTextAction.Name, DeleteTextAction);+CommandManager.register(DeleteAction.Name, DeleteAction);diff --git a/packages/core/src/Docs/Action/InsertAction.ts b/packages/core/src/Docs/Action/InsertAction.tsindex c5631143fe..7e264cffe3 100644--- a/packages/core/src/Docs/Action/InsertAction.ts+++ b/packages/core/src/Docs/Action/InsertAction.ts@@ -6,22 +6,25 @@ import {CommandUnit,CommonParameter,} from '../../Command';-import { IDeleteTextActionData } from './DeleteAction';+import { IDeleteActionData } from './DeleteAction';+import { IDocumentBody } from '../../Interfaces';+import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';-export interface IInsertTextActionData extends IDocActionData {- text: 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 class InsertTextAction extends DocActionBase<- IInsertTextActionData,- IDeleteTextActionData+export class InsertAction extends DocActionBase<+ IInsertActionData,+ IDeleteActionData> {- static NAME = 'InsertTextAction';+ static NAME = 'InsertAction';constructor(- actionData: IInsertTextActionData,+ actionData: IInsertActionData,commandUnit: CommandUnit,observers: ActionObservers,commonParameter: CommonParameter@@ -29,14 +32,13 @@ export class InsertTextAction extends DocActionBase<super(actionData, commandUnit, observers);this._doActionData = { ...actionData };this.do(commonParameter);- const { segmentId, text, len } = actionData;- // this._oldActionData = {- // ...actionData,- // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,- // cursorEnd: cursorStart + textLength,- // isEndBack: isStartBack,- // isCollapse: true,- // };+ const { segmentId, line, len } = actionData;+ this._oldActionData = {+ actionName: DOC_ACTION_NAMES.DELETE_ACTION_NAME,+ len,+ line,+ segmentId,+ };}redo(commonParameter: CommonParameter): void {@@ -46,7 +48,8 @@ export class InsertTextAction extends DocActionBase<do(commonParameter: CommonParameter): void {const actionData = this.getDoActionData();const document = this.getDocument();- const { text, len, segmentId } = actionData;+ const { body, len, segmentId } = actionData;+ commonParameter.moveCursor(len);// InsertTextApply(document, text, textLength, {// cursorStart,// isStartBack,@@ -63,7 +66,8 @@ export class InsertTextAction extends DocActionBase<undo(commonParameter: CommonParameter): void {const actionData = this.getOldActionData();const document = this.getDocument();- // const { length, start, segmentId } = actionData;+ const { len, segmentId } = actionData;+ commonParameter.moveCursor(len);// DeleteTextApply(document, { ...actionData });this._observers.notifyObservers({@@ -78,4 +82,4 @@ export class InsertTextAction extends DocActionBase<}}-CommandManager.register(InsertTextAction.NAME, InsertTextAction);+CommandManager.register(InsertAction.NAME, InsertAction);diff --git a/packages/core/src/Docs/Action/RetainAction.ts b/packages/core/src/Docs/Action/RetainAction.tsnew file mode 100644index 0000000000..92d6624a27--- /dev/null+++ b/packages/core/src/Docs/Action/RetainAction.ts@@ -0,0 +1,61 @@+import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';+import {+ ActionObservers,+ CommandManager,+ CommandUnit,+ CommonParameter,+} from '../../Command';+import { CommonParameterAttribute } from '../../Interfaces/IDocumentData';++export interface IRetainActionData extends IDocActionData {+ attribute?: CommonParameterAttribute;+ len: number;+ segmentId?: string;+}++export class RetainAction extends DocActionBase<+ IRetainActionData,+ IRetainActionData+> {+ static NAME = 'RetainAction';++ constructor(+ actionData: IRetainActionData,+ commandUnit: CommandUnit,+ observers: ActionObservers,+ commonParameter: CommonParameter+ ) {+ super(actionData, commandUnit, observers);+ this._doActionData = { ...actionData };+ this._oldActionData = { ...actionData, attribute: this.do(commonParameter) };+ }++ redo(commonParameter: CommonParameter): void {+ this.do(commonParameter);+ }++ do(commonParameter: CommonParameter): CommonParameterAttribute {+ const actionData = this.getDoActionData();+ const document = this.getDocument();+ const { len, segmentId, attribute } = actionData;+ commonParameter.moveCursor(len);+ // InsertTextApply(document, actionData.text);+ return {+ ed: 0,+ st: 0,+ };+ }++ undo(commonParameter: CommonParameter): void {+ const actionData = this.getOldActionData();+ const document = this.getDocument();+ const { len, segmentId, attribute } = actionData;+ commonParameter.moveCursor(len);+ }++ validate(): boolean {+ return false;+ }+}++CommandManager.register(RetainAction.NAME, RetainAction);diff --git a/packages/core/src/Docs/Action/UpdateAttributeAction.ts b/packages/core/src/Docs/Action/UpdateAttributeAction.tsdeleted file mode 100644index 66872d6b8e..0000000000--- a/packages/core/src/Docs/Action/UpdateAttributeAction.ts+++ /dev/null@@ -1,41 +0,0 @@-import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';-import { ActionObservers, CommandUnit } from '../../Command';--export interface IUpdateParagraphActionData extends IDocActionData {- text: string;-}--export class UpdateParagraphAction extends DocActionBase<- IUpdateParagraphActionData,- IUpdateParagraphActionData-> {- constructor(- actionData: IUpdateParagraphActionData,- commandUnit: CommandUnit,- observers: ActionObservers- ) {- super(actionData, commandUnit, observers);- this._doActionData = { ...actionData };- this.do();- this._oldActionData = { ...actionData };- }-- redo(): void {- this.do();- }-- do(): void {- const actionData = this.getDoActionData();- const document = this.getDocument();- // InsertTextApply(document, actionData.text);- }-- undo(): void {- // TODO ...- // ...- }-- validate(): boolean {- return false;- }-}diff --git a/packages/core/src/Docs/Action/UpdateDocumentAction.ts b/packages/core/src/Docs/Action/UpdateDocumentAction.tsindex 3f5c63b1f0..7674f7d001 100644--- a/packages/core/src/Docs/Action/UpdateDocumentAction.ts+++ b/packages/core/src/Docs/Action/UpdateDocumentAction.ts@@ -1,5 +1,5 @@import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';-import { ActionObservers, CommandUnit } from '../../Command';+import { ActionObservers, CommandManager, CommandUnit } from '../../Command';export interface IUpdateDocumentActionData extends IDocActionData {text: string;@@ -9,6 +9,8 @@ export class UpdateDocumentAction extends DocActionBase<IUpdateDocumentActionData,IUpdateDocumentActionData> {+ static NAME = 'UpdateDocumentAction';+constructor(actionData: IUpdateDocumentActionData,commandUnit: CommandUnit,@@ -39,3 +41,5 @@ export class UpdateDocumentAction extends DocActionBase<return false;}}++CommandManager.register(UpdateDocumentAction.NAME, UpdateDocumentAction);diff --git a/packages/core/src/Docs/Action/index.ts b/packages/core/src/Docs/Action/index.tsindex a0433700f7..b811604f2c 100644--- a/packages/core/src/Docs/Action/index.ts+++ b/packages/core/src/Docs/Action/index.ts@@ -1,4 +1,4 @@export * from './InsertAction';export * from './DeleteAction';export * from './UpdateDocumentAction';-export * from './UpdateAttributeAction';+export * from './RetainAction';diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.tsdeleted file mode 100644index a01057c645..0000000000--- a/packages/core/src/Docs/Apply/Common.ts+++ /dev/null@@ -1,87 +0,0 @@-import { IDocumentData, ITextRun } from '../../Interfaces/IDocumentData';-import { deepCompare } from '../../Shared/Compare';--export function getTextIndexByCursor(index: number, isBack: boolean = false) {- return isBack ? index - 1 : index;-}--export function insertTextToContent(content: string, start: number, text: string) {- return content.slice(0, start) + text + content.slice(start);-}--export function deleteContent(content: string, start: number, end: number) {- if (start > end) {- return content;- }-- if (start === end) {- start -= 1;- }-- return content.slice(0, start) + content.slice(end);-}--// export function moveElementCharIndex(element?: IElement, moveIndex: number = 0) {-// if (element == null) {-// return;-// }--// element.st += moveIndex;-// element.ed += moveIndex;-// }--// export function moveBlockCharIndex(-// blockElement?: IBlockElement,-// moveIndex: number = 0-// ) {-// if (blockElement == null) {-// return;-// }--// blockElement.st += moveIndex;-// blockElement.ed += moveIndex;-// }--export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {- let body = model.body;-- if (segmentId) {- const { headers, footers } = model;- if (headers?.[segmentId]) {- body = headers[segmentId].body;- } else if (footers?.[segmentId]) {- body = footers[segmentId].body;- }- }-- return body;-}--export function isSameStyleTextRun(tr1: ITextRun, tr2: ITextRun) {- if (!tr1.ts || !tr2.ts) {- return false;- }- return deepCompare(tr1.ts, tr2.ts);-}--// export function mergeSameTextRun(mainTr: ITextRun, mergeTr: ITextRun) {-// const isSame = isSameStyleTextRun(mainTr, mergeTr);-// if (!isSame) {-// return;-// }--// mainTr.ct += mergeTr.ct || '';--// return mainTr;-// }--// export function getTextLength(text: string | ITextRun[]) {-// if (text instanceof Object) {-// let len = 0;-// for (let textRun of text) {-// len += textRun.ct?.length || 0;-// }-// return len;-// }-// return text.length;-// }diff --git a/packages/core/src/Docs/Apply/DeleteTextApply.ts b/packages/core/src/Docs/Apply/DeleteApply.tssimilarity index 97%rename from packages/core/src/Docs/Apply/DeleteTextApply.tsrename to packages/core/src/Docs/Apply/DeleteApply.tsindex f1812f3211..9fc74cce03 100644--- a/packages/core/src/Docs/Apply/DeleteTextApply.ts+++ b/packages/core/src/Docs/Apply/DeleteApply.ts@@ -1,8 +1,8 @@import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';import { DocumentModel } from '../Domain/DocumentModel';-import { getDocsUpdateBody } from './Common';+import { getDocsUpdateBody } from '../../Shared/Common';-export function DeleteTextApply(+export function DeleteApply(document: DocumentModel,range: ITextSelectionRangeParam) {diff --git a/packages/core/src/Docs/Apply/InsertApply.ts b/packages/core/src/Docs/Apply/InsertApply.tsnew file mode 100644index 0000000000..b494432104--- /dev/null+++ b/packages/core/src/Docs/Apply/InsertApply.ts@@ -0,0 +1,225 @@+import { IDocumentBody } from '../../Interfaces/IDocumentData';+import { sortRulesFactory } from '../../Shared/SortRules';+import { DocumentModel } from '../Domain/DocumentModel';+import { getDocsUpdateBody } from '../../Shared/Common';+import { isSameStyleTextRun } from '../../Shared/Compare';++export function InsertApply(+ document: DocumentModel,+ insertBody: IDocumentBody,+ textLength: number,+ currentIndex: number,+ segmentId?: string+) {+ const doc = document.snapshot;++ const bodyModel = document.bodyModel;++ const body = getDocsUpdateBody(doc, segmentId);++ if (textLength === 0) {+ return;+ }++ if (body == null) {+ throw new Error('no body has changed');+ }++ updateAttribute(body, insertBody, textLength, currentIndex);+}++function updateAttribute(+ body: IDocumentBody,+ insertBody: IDocumentBody,+ 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;+ }+ }++ 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+ );+ }+ }+ }++ 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 insertParagraphs = insertBody.paragraphs;+ if (insertParagraphs) {+ for (let i = 0, len = insertParagraphs.length; i < len; i++) {+ const insertTextRun = insertParagraphs[i];+ insertTextRun.startIndex += currentIndex;+ }++ paragraphs.push(...insertParagraphs);+ paragraphs.sort(sortRulesFactory('startIndex'));+ }+ }++ 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;+ }+ }++ const insertSectionBreaks = insertBody.sectionBreaks;+ if (insertSectionBreaks) {+ for (let i = 0, len = insertSectionBreaks.length; i < len; i++) {+ const sectionBreak = insertSectionBreaks[i];+ sectionBreak.startIndex += currentIndex;+ }++ sectionBreaks.push(...insertSectionBreaks);+ sectionBreaks.sort(sortRulesFactory('startIndex'));+ }+ }++ 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 insertCustomBlocks = insertBody.customBlocks;+ if (insertCustomBlocks) {+ for (let i = 0, len = insertCustomBlocks.length; i < len; i++) {+ const customBlock = insertCustomBlocks[i];+ customBlock.startIndex += currentIndex;+ }++ customBlocks.push(...insertCustomBlocks);+ customBlocks.sort(sortRulesFactory('startIndex'));+ }+ }++ 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 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;+ }++ tables.push(...insertTables);+ tables.sort(sortRulesFactory('startIndex'));+ }+ }++ 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 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/Apply/InsertTextApply.ts b/packages/core/src/Docs/Apply/InsertTextApply.tsdeleted file mode 100644index 1362c7674e..0000000000--- a/packages/core/src/Docs/Apply/InsertTextApply.ts+++ /dev/null@@ -1,159 +0,0 @@-import { IDocumentBody } from '../../Interfaces/IDocumentData';-import { ITextSelectionRangeStartParam } from '../../Interfaces/ISelectionData';-import { DocumentModel } from '../Domain/DocumentModel';-import { getDocsUpdateBody } from './Common';--export function InsertTextApply(- document: DocumentModel,- insertBody: IDocumentBody,- textLength: number,- collapseRange: ITextSelectionRangeStartParam-) {- const doc = document.snapshot;-- const { segmentId } = collapseRange;-- const body = getDocsUpdateBody(doc, segmentId);-- if (textLength === 0) {- return;- }-- if (body == null) {- throw new Error('no body has changed');- }-- // const { blockElements } = body;-- // for (let blockElement of blockElements) {- // if (blockElement == null) {- // continue;- // }-- // const { blockType } = blockElement;-- // switch (blockType) {- // case BlockType.PARAGRAPH:- // blockElement.paragraph &&- // insertText(- // text,- // textLength,- // blockElement,- // blockElement.paragraph,- // collapseRange- // );- // }- // }-}--// function insertText(-// text: string | IElement[],-// textLength: number,-// blockElement: IBlockElement,-// paragraph: IParagraph,-// collapseRange: ITextSelectionRangeStartParam-// ) {-// const { st: blockStartIndex, ed: blockEndIndex } = blockElement;--// const { cursorStart, isStartBack, segmentId } = collapseRange;--// const textStart = getTextIndexByCursor(cursorStart, isStartBack);--// if (textStart > blockEndIndex || paragraph == null) {-// return;-// }--// const { elements } = paragraph;--// let index = 0;--// for (let element of elements) {-// const { et: paragraphElementType } = element;--// if (paragraphElementType === ParagraphElementType.DRAWING) {-// index++;-// continue;-// }--// const { st: elementStartIndex, ed: elementEndIndex, tr } = element;--// if (tr == null || tr.tab === BooleanNumber.TRUE) {-// index++;-// continue;-// }-// if (textStart < elementStartIndex || textStart > elementEndIndex) {-// index++;-// continue;-// }--// console.log(-// 'paragraphApply',-// textStart,-// elementStartIndex,-// elementEndIndex,-// element,-// textStart < elementStartIndex || textStart > elementEndIndex-// );--// if (paragraphElementType === ParagraphElementType.TEXT_RUN) {-// let relativeStart = textStart - elementStartIndex + 1;--// if (relativeStart <= 0) {-// relativeStart = 0;-// }--// if (text instanceof Object) {-// const nextEle = elements[index + 1];-// const insertedElements = splitTextRun(-// text,-// textLength,-// relativeStart,-// element,-// nextEle-// );-// } else {-// const newContent = insertTextToContent(-// tr.ct || '',-// relativeStart,-// text as string-// );--// tr.ct = newContent;--// index++;-// }-// }-// }-// }--// function splitTextRun(-// insertTextRunList: IElement[],-// textLength: number,-// relativeStart: number,-// currentElement: IElement,-// nextElement: IElement-// ) {-// const trLen = insertTextRunList.length;--// const insertLastTrEle = insertTextRunList[trLen - 1];--// const {-// st: currentStartIndex,-// ed: currentEndIndex,-// tr: currentTr,-// } = currentElement;--// const currentContent = currentTr?.ct || '';--// const newElements: IElement[] = [];--// if (trLen === 1) {-// const splitLeft = currentContent.slice(0, relativeStart);--// const splitRight = currentContent.slice(relativeStart);-// } else if (trLen > 1) {-// const splitLeft = currentContent.slice(0, relativeStart);--// const splitRight = currentContent.slice(relativeStart);-// }-// }diff --git a/packages/core/src/Docs/Apply/UpdateAttribute.ts b/packages/core/src/Docs/Apply/UpdateAttribute.tsnew file mode 100644index 0000000000..e69de29bb2diff --git a/packages/core/src/Docs/Apply/UpdateDocument.ts b/packages/core/src/Docs/Apply/UpdateDocument.tsnew file mode 100644index 0000000000..e69de29bb2diff --git a/packages/core/src/Docs/Apply/index.ts b/packages/core/src/Docs/Apply/index.tsindex 78fd705807..d7f3d2ee66 100644--- a/packages/core/src/Docs/Apply/index.ts+++ b/packages/core/src/Docs/Apply/index.ts@@ -1,2 +1,4 @@-export * from './InsertTextApply';-export * from './DeleteTextApply';+export * from './InsertApply';+export * from './DeleteApply';+export * from './UpdateAttribute';+export * from './UpdateDocument';diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.tsindex 46c8f466a4..80c75c766d 100644--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts@@ -1,4 +1,5 @@import { IDocumentBody, ITextRun } from '../../Interfaces/IDocumentData';+import { Nullable, insertTextToContent } from '../../Shared';export type DocumentBodyModelOrSimple = DocumentBodyModelSimple | DocumentBodyModel;@@ -198,12 +199,80 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {return new DocumentBodyModel(body);}- insert() {}+ insert(insertBody: IDocumentBody, insertIndex = 0) {+ const dataStream = insertBody.dataStream;+ let dataStreamLen = dataStream.length;+ const insertNode = this.getParagraphByTree(this.children, insertIndex);+ if (insertNode == null) {+ return;+ }++ if (dataStream[dataStream.length - 1] === '\n') {+ const newBodyModel = new DocumentBodyModelSimple(insertBody);+ dataStreamLen -= 1; // sectionBreak can not be inserted+ } else {+ insertNode.insertText(dataStream, insertIndex);++ insertNode.endIndex += dataStreamLen;++ this.foreachTop(insertNode.parent, (currentNode) => {+ currentNode.endIndex += dataStreamLen;+ const children = currentNode.children;+ let isStartFix = false;+ for (let node of children) {+ if (node === currentNode) {+ isStartFix = true;+ }++ if (!isStartFix) {+ continue;+ }++ this.foreachDown(node, (newNode) => {+ newNode.plus(dataStreamLen);+ });+ }+ });+ }+ }update() {}delete() {}+ getParagraphByTree(+ nodes: DataStreamTreeNode[],+ insertIndex: number+ ): Nullable<DataStreamTreeNode> {+ for (let node of nodes) {+ const { startIndex, endIndex, children } = node;+ if (node.exclude(insertIndex)) {+ continue;+ }+ if (node.nodeType === DataStreamTreeNodeType.PARAGRAPH) {+ return node;+ }+ return this.getParagraphByTree(children, insertIndex);+ }+ return null;+ }++ foreachTop(node: DataStreamTreeNode, func: (node: DataStreamTreeNode) => void) {+ let parent = node;+ while (parent) {+ func(parent);+ parent = parent.parent;+ }+ }++ foreachDown(node: DataStreamTreeNode, func: (node: DataStreamTreeNode) => void) {+ func(node);+ const children = node.children;+ for (node of children) {+ this.foreachDown(node, func);+ }+ }+resetCache() {this.sectionBreakCurrentIndex = 0;this.paragraphCurrentIndex = 0;@@ -410,20 +479,6 @@ export class DataStreamTreeNode {return new DataStreamTreeNode(nodeType, bodyModel, content);}- forEach(- func: (- content: string,- startIndex: number,- endIndex: number,- type: DataStreamTreeNodeType- ) => void- ) {- for (let child of this.children) {- child.forEach(func);- }- func(this.content || '', this.startIndex, this.endIndex, this.nodeType);- }-addBlocks(blocks: number[]) {this.blocks = this.blocks.concat(blocks);}@@ -433,9 +488,22 @@ export class DataStreamTreeNode {this.endIndex = endIndex;}- include(index: number) {}+ insertText(text: string, insertIndex: number) {+ this.content = insertTextToContent(+ this.content || '',+ insertIndex - this.startIndex + 1,+ text+ );+ }- plus(len: number) {}+ exclude(index: number) {+ return index < this.startIndex || index > this.endIndex;+ }++ plus(len: number) {+ this.startIndex += len;+ this.endIndex += len;+ }minus(len: number) {}diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.tsindex b24e4cb4a5..acf08567a1 100644--- a/packages/core/src/Docs/Domain/DocumentModel.ts+++ b/packages/core/src/Docs/Domain/DocumentModel.ts@@ -1,9 +1,13 @@import { DocContext } from '../../Basics';-import { Command, IActionData } from '../../Command';-import { IDocumentData, ITextRun, ITextSelectionRange } from '../../Interfaces';+import { Command } from '../../Command';+import {+ CommonParameterAttribute,+ IDocumentBody,+ IDocumentData,+} from '../../Interfaces/IDocumentData';+import { ITextSelectionRange } from '../../Interfaces/ISelectionData';import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';-import { getDocsUpdateBody } from '../Apply/Common';-import { Nullable, Tools } from '../../Shared';+import { Tools, getTextIndexByCursor } from '../../Shared';import { DocumentBodyModel } from './DocumentBodyModel';import { DEFAULT_DOC } from '../../Const';@@ -110,112 +114,137 @@ export class DocumentModel extends DocumentModelSimple {return this._unitId;}- insertText(- text: string | ITextRun,+ insert(+ body: IDocumentBody,range: ITextSelectionRange,segmentId?: string): DocumentModel {- const { _context } = this;- const _commandManager = _context.getCommandManager();- const insertTextAction = {- actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,- ...range,- text,+ const commandManager = this._context.getCommandManager();++ const { cursorStart, cursorEnd, isEndBack, isStartBack, isCollapse } = range;++ const textStart = getTextIndexByCursor(cursorStart, isStartBack);++ const actionList = [];++ if (isCollapse) {+ actionList.push({+ actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,+ len: textStart,+ segmentId,+ });+ } else {+ actionList.push(...this._getDeleteAction(range, segmentId));+ }++ actionList.push({+ actionName: DOC_ACTION_NAMES.INSERT_ACTION_NAME,+ body,+ len: body.dataStream.length,+ line: 0,segmentId,- };+ });+const command = new Command({DocumentUnit: this,},- insertTextAction,- this._addTextIndexAdustAction(range, segmentId)+ ...actionList);- _commandManager.invoke(command);+ commandManager.invoke(command);return this;}- deleteText(range: ITextSelectionRange, segmentId?: string): DocumentModel {- const { _context } = this;- const _commandManager = _context.getCommandManager();- // const deleteTextAction = {- // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,- // ...range,- // segmentId,- // };+ delete(range: ITextSelectionRange, segmentId?: string): DocumentModel {+ const commandManager = this._context.getCommandManager();- const deleteTextActionList = this._getDeleteTextAction(range, segmentId);+ const deleteActionList = this._getDeleteAction(range, segmentId);const command = new Command({DocumentUnit: this,},- ...deleteTextActionList,- this._addTextIndexAdustAction(range, segmentId)+ ...deleteActionList);- _commandManager.invoke(command);+ commandManager.invoke(command);return this;}- updateText(newText: string, oldText: string, start: number, segmentId?: string) {- const { _context } = this;- const _commandManager = _context.getCommandManager();-- // const deleteTextAction = {- // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,- // text: oldText,- // start: start + oldText.length,- // length: oldText.length,- // segmentId,- // };- const deleteTextActionList: IActionData[] = [];-- if (oldText.length > 0) {- deleteTextActionList.push(- ...this._getDeleteTextAction(- {- cursorStart: start,- isStartBack: false,- isCollapse: false,- cursorEnd: start + oldText.length,- isEndBack: false,- },- segmentId- )- );- }+ update(+ attribute: CommonParameterAttribute,+ range: ITextSelectionRange,+ segmentId?: string+ ) {+ const commandManager = this._context.getCommandManager();++ const { cursorStart, cursorEnd, isEndBack, isStartBack } = range;+ const actionList = [];++ const textStart = getTextIndexByCursor(cursorStart, isStartBack);- // const insertTextAction = {- // actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,- // text: newText,- // start,- // length: newText.length,- // segmentId,- // };-- const insertTextAction = {- actionName: DOC_ACTION_NAMES.INSERT_TEXT_ACTION_NAME,- cursorStart: start,- isStartBack: false,- text: newText,+ const textEnd = getTextIndexByCursor(cursorEnd, isEndBack);++ actionList.push({+ actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,+ len: textStart,+ segmentId,+ });++ actionList.push({+ actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,+ len: textEnd - textStart + 1,+ attribute,segmentId,- };+ });const command = new Command({DocumentUnit: this,},- ...deleteTextActionList,- insertTextAction,- this._addTextIndexAdustAction(- {- cursorStart: start,- cursorEnd: start + Math.max(oldText.length, newText.length),- isCollapse: false,- isEndBack: false,- isStartBack: false,- },- segmentId- )+ ...actionList+ );+ commandManager.invoke(command);+ return this;+ }++ IMEInput(+ newText: string,+ oldTextLen: number,+ start: number,+ segmentId?: string+ ) {+ const _commandManager = this._context.getCommandManager();++ const actionList = [];++ actionList.push({+ actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,+ len: start,+ segmentId,+ });++ actionList.push({+ actionName: DOC_ACTION_NAMES.DELETE_ACTION_NAME,+ len: oldTextLen,+ line: 0,+ segmentId,+ });++ actionList.push({+ actionName: DOC_ACTION_NAMES.INSERT_ACTION_NAME,+ body: {+ dataStream: newText,+ },+ len: newText.length,+ line: 0,+ segmentId,+ });++ const command = new Command(+ {+ DocumentUnit: this,+ },+ ...actionList);_commandManager.invoke(command);return this;@@ -248,236 +277,29 @@ export class DocumentModel extends DocumentModelSimple {}}- private _addTextIndexAdustAction(- range: ITextSelectionRange,- segmentId?: string- ) {- const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;- return {- actionName: DOC_ACTION_NAMES.TEXT_INDEX_ADJUST_ACTION_NAME,- cursorStart,- cursorEnd,- isCollapse,- isEndBack,- isStartBack,- segmentId,- };- }-- private _getDeleteTextAction(range: ITextSelectionRange, segmentId?: string) {- const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;- let actionList = [];- if (isCollapse) {- actionList = this._getDeleteTextActionCollapse(range, segmentId);- } else {- actionList = this._getDeleteTextActionRange(range, segmentId);- }-- if (actionList.length === 0) {- return [];- }-- // actionList.push({- // actionName: DOC_ACTION_NAMES.TEXT_INDEX_ADJUST_ACTION_NAME,- // cursorStart,- // cursorEnd,- // isCollapse,- // isEndBack,- // isStartBack,- // segmentId,- // });-- return actionList;- }-- private _getDeleteTextActionCollapse(- range: ITextSelectionRange,- segmentId?: string- ) {- const snapshot = this.snapshot;-- const body = getDocsUpdateBody(snapshot, segmentId);-- if (body == null) {- return [];- }- const { cursorStart, isCollapse, isStartBack } = range;-- // const actionList = [];-- let startBlockId: Nullable<string> = null;-- let endBlockId: Nullable<string> = null;-- let preBlockId: Nullable<string> = null;-- // const { blockElements } = body;-- // for (let blockElement of blockElements) {- // if (blockElement == null) {- // continue;- // }-- // const { blockType, st, ed, blockId } = blockElement;-- // if (blockType === BlockType.PARAGRAPH) {- // if (cursorStart > ed || cursorStart < st) {- // continue;- // }-- // if (cursorStart === st && isStartBack) {- // if (blockElement.paragraph?.bullet) {- // // bullet- // actionList.push({- // actionName: DOC_ACTION_NAMES.DELETE_BULLET_ACTION_NAME,- // blockId,- // blockElement,- // segmentId,- // });- // } else {- // startBlockId = preBlockId;- // endBlockId = blockId;- // }- // } else {- // // textRun- // actionList.push({- // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,- // cursorStart,- // cursorEnd: cursorStart,- // isCollapse: true,- // isEndBack: isStartBack,- // isStartBack,- // segmentId,- // });- // }- // }-- // preBlockId = blockId;- // }-- // if (- // startBlockId != null &&- // endBlockId != null &&- // startBlockId !== endBlockId- // ) {- // actionList.push({- // actionName: DOC_ACTION_NAMES.MERGE_PARAGRAPH_ACTION_NAME,- // startBlockId,- // endBlockId,- // segmentId,- // cursorStart,- // isStartBack,- // });- // }-- return [];- }+ private _getDeleteAction(range: ITextSelectionRange, segmentId?: string) {+ const { cursorStart, cursorEnd, isEndBack, isStartBack } = range;+ const actionList = [];- private _getDeleteTextActionRange(- range: ITextSelectionRange,- segmentId?: string- ) {- const snapshot = this.snapshot;+ const textStart = getTextIndexByCursor(cursorStart, isStartBack);- const body = getDocsUpdateBody(snapshot, segmentId);+ const textEnd = getTextIndexByCursor(cursorEnd, isEndBack);- if (body == null) {- return [];+ if (textStart > 0) {+ actionList.push({+ actionName: DOC_ACTION_NAMES.RETAIN_ACTION_NAME,+ len: textStart,+ segmentId,+ });}- const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;-- const actionList = [];+ actionList.push({+ actionName: DOC_ACTION_NAMES.DELETE_ACTION_NAME,+ len: textEnd - textStart + 1,+ line: 0,+ segmentId,+ });- let startBlockId: Nullable<string> = null;-- let endBlockId: Nullable<string> = null;-- return [];-- // const { blockElements } = body;-- // for (let blockElement of blockElements) {- // if (blockElement == null) {- // continue;- // }-- // const { blockType, st, ed, blockId } = blockElement;-- // if (blockType === BlockType.PARAGRAPH) {- // if (cursorStart > ed || cursorEnd < st) {- // continue;- // }-- // let start = st;-- // let end = ed;-- // let isStartBackCurrent = isStartBack;-- // let isEndBackCurrent = isEndBack;-- // if (st <= cursorStart && ed >= cursorStart) {- // startBlockId = blockId;- // start = cursorStart;- // } else {- // isStartBackCurrent = true;- // }-- // if (st <= cursorEnd && ed >= cursorEnd) {- // endBlockId = blockId;- // end = cursorEnd;- // } else {- // isEndBackCurrent = false;- // }-- // if (- // (cursorStart < st || (cursorStart === st && isStartBack)) &&- // (cursorEnd > ed || (cursorEnd === ed && !isEndBack)) &&- // !isCollapse- // ) {- // // Paragraph- // actionList.push({- // actionName: DOC_ACTION_NAMES.DELETE_PARAGRAPH_ACTION_NAME,- // cursorStart: st,- // cursorEnd: ed,- // isCollapse: false,- // isEndBack: false,- // isStartBack: true,- // blockId,- // blockElement,- // segmentId,- // });- // } else {- // // textRun- // actionList.push({- // actionName: DOC_ACTION_NAMES.DELETE_TEXT_ACTION_NAME,- // cursorStart: start,- // cursorEnd: end,- // isCollapse,- // isEndBack: isEndBackCurrent,- // isStartBack: isStartBackCurrent,- // segmentId,- // });- // }- // }- // }-- // if (- // startBlockId != null &&- // endBlockId != null &&- // startBlockId !== endBlockId- // ) {- // actionList.push({- // actionName: DOC_ACTION_NAMES.MERGE_PARAGRAPH_ACTION_NAME,- // startBlockId,- // endBlockId,- // segmentId,- // cursorStart,- // isStartBack,- // });- // }-- // return actionList;+ return actionList;}}diff --git a/packages/core/src/Interfaces/IDocumentData.ts b/packages/core/src/Interfaces/IDocumentData.tsindex 8c11855448..0c197612d8 100644--- a/packages/core/src/Interfaces/IDocumentData.ts+++ b/packages/core/src/Interfaces/IDocumentData.ts@@ -72,6 +72,14 @@ export interface ISizeData {height?: number;}+export type CommonParameterAttribute =+ | ITextRun+ | IParagraph+ | ISectionBreak+ | ICustomBlock+ | ITable+ | ICustomRange;+/*** Properties of document body* 三种更新类型,范围叠加、范围互斥、占位符diff --git a/packages/core/src/Shared/Common.ts b/packages/core/src/Shared/Common.tsindex 3780fc1d22..b4c000567a 100644--- a/packages/core/src/Shared/Common.ts+++ b/packages/core/src/Shared/Common.ts@@ -490,3 +490,38 @@ export function getBorderStyleType(type: string) {}return str;}++export function getTextIndexByCursor(index: number, isBack: boolean = false) {+ return isBack ? index - 1 : index;+}++export function insertTextToContent(content: string, start: number, text: string) {+ return content.slice(0, start) + text + content.slice(start);+}++export function deleteContent(content: string, start: number, end: number) {+ if (start > end) {+ return content;+ }++ if (start === end) {+ start -= 1;+ }++ return content.slice(0, start) + content.slice(end);+}++export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {+ let body = model.body;++ if (segmentId) {+ const { headers, footers } = model;+ if (headers?.[segmentId]) {+ body = headers[segmentId].body;+ } else if (footers?.[segmentId]) {+ body = footers[segmentId].body;+ }+ }++ return body;+}diff --git a/packages/core/src/Shared/Compare.ts b/packages/core/src/Shared/Compare.tsindex 34fe9a3f36..4dbd2ef422 100644--- a/packages/core/src/Shared/Compare.ts+++ b/packages/core/src/Shared/Compare.ts@@ -1,3 +1,5 @@+import { ITextRun } from '../Interfaces/IDocumentData';+type AnyObject = {[key: number | string]: AnyObject | AnyObject[] | Array<[number | string]> | any;};@@ -21,3 +23,14 @@ export function deepCompare(arg1: AnyObject, arg2: AnyObject): boolean {}return false;}++export function isSameStyleTextRun(tr1: ITextRun, tr2: ITextRun) {+ let ts1 = tr1.ts || {};+ let ts2 = tr2.ts || {};++ if (tr1.sId !== tr2.sId) {+ return false;+ }++ return deepCompare(ts1, ts2);+}diff --git a/packages/core/src/Shared/SortRules.ts b/packages/core/src/Shared/SortRules.tsindex 431fd023ad..1547a761a3 100644--- a/packages/core/src/Shared/SortRules.ts+++ b/packages/core/src/Shared/SortRules.ts@@ -17,3 +17,21 @@ export function sortRulesByDesc(oa: any, ob: any) {}return 1;}++/**+ *+ * @param key compare key+ * @param ruler 1:asc , 0:desc+ * @returns sort function+ */+export function sortRulesFactory(key = 'index', ruler = 1) {+ return (oa: any, ob: any) => {+ if (oa[key] > ob[key]) {+ return ruler;+ }+ if (oa[key] === ob[key]) {+ return 0;+ }+ return -ruler;+ };+}