Skip to content

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.

feat(doc): doc edit develop

DR-Univer5871f34da36c
diff --git a/packages/core/src/Docs/Action/ActionDataInterface.ts b/packages/core/src/Docs/Action/ActionDataInterface.ts
index 2a5df054b3..eb81733c8c 100644
--- a/packages/core/src/Docs/Action/ActionDataInterface.ts
+++ b/packages/core/src/Docs/Action/ActionDataInterface.ts
@@ -1,5 +1,6 @@
import { IDocActionData } from '../../Command/DocActionBase';
-import { UpdateAttributeValueType, IDocumentBody } from '../../Interfaces/IDocumentData';
+import { IDocumentBody } from '../../Interfaces/IDocumentData';
+import { Nullable } from '../../Shared';
import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
export enum UpdateAttributeType {
@@ -24,8 +25,7 @@ export interface IInsertActionData extends IDocActionData {
}
export interface IRetainActionData extends IDocActionData {
- attributes?: UpdateAttributeValueType[];
- attributeType?: UpdateAttributeType;
+ body: Nullable<IDocumentBody>;
len: number;
coverType: UpdateDocsAttributeType;
segmentId?: string;
diff --git a/packages/core/src/Docs/Action/DeleteAction.ts b/packages/core/src/Docs/Action/DeleteAction.ts
index d3c5551949..d2f64944a4 100644
--- a/packages/core/src/Docs/Action/DeleteAction.ts
+++ b/packages/core/src/Docs/Action/DeleteAction.ts
@@ -1,11 +1,5 @@
import { DocActionBase } from '../../Command/DocActionBase';
-import {
- ActionObservers,
- ActionType,
- CommandManager,
- CommandUnit,
- CommonParameter,
-} from '../../Command';
+import { ActionObservers, ActionType, CommandManager, CommandUnit, CommonParameter } from '../../Command';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
import { IDocumentBody } from '../../Interfaces';
@@ -13,18 +7,10 @@ import { DeleteApply } from '../Apply/DeleteApply';
import { InsertApply } from '../Apply/InsertApply';
import { IDeleteActionData, IInsertActionData } from './ActionDataInterface';
-export class DeleteAction extends DocActionBase<
- IDeleteActionData,
- IInsertActionData
-> {
+export class DeleteAction extends DocActionBase<IDeleteActionData, IInsertActionData> {
static Name = 'DeleteAction';
- constructor(
- actionData: IDeleteActionData,
- commandUnit: CommandUnit,
- observers: ActionObservers,
- commonParameter: CommonParameter
- ) {
+ constructor(actionData: IDeleteActionData, commandUnit: CommandUnit, observers: ActionObservers, commonParameter: CommonParameter) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
const { len, line, segmentId } = actionData;
diff --git a/packages/core/src/Docs/Action/InsertAction.ts b/packages/core/src/Docs/Action/InsertAction.ts
index b8f9cf2543..e1b14cc28d 100644
--- a/packages/core/src/Docs/Action/InsertAction.ts
+++ b/packages/core/src/Docs/Action/InsertAction.ts
@@ -1,28 +1,14 @@
import { DocActionBase } from '../../Command/DocActionBase';
-import {
- ActionObservers,
- ActionType,
- CommandManager,
- CommandUnit,
- CommonParameter,
-} from '../../Command';
+import { ActionObservers, ActionType, CommandManager, CommandUnit, CommonParameter } from '../../Command';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
import { InsertApply } from '../Apply/InsertApply';
import { DeleteApply } from '../Apply/DeleteApply';
import { IInsertActionData, IDeleteActionData } from './ActionDataInterface';
-export class InsertAction extends DocActionBase<
- IInsertActionData,
- IDeleteActionData
-> {
+export class InsertAction extends DocActionBase<IInsertActionData, IDeleteActionData> {
static NAME = 'InsertAction';
- constructor(
- actionData: IInsertActionData,
- commandUnit: CommandUnit,
- observers: ActionObservers,
- commonParameter: CommonParameter
- ) {
+ constructor(actionData: IInsertActionData, commandUnit: CommandUnit, observers: ActionObservers, commonParameter: CommonParameter) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
this.do(commonParameter);
diff --git a/packages/core/src/Docs/Action/RetainAction.ts b/packages/core/src/Docs/Action/RetainAction.ts
index 172f0f1fe6..d6da8486e2 100644
--- a/packages/core/src/Docs/Action/RetainAction.ts
+++ b/packages/core/src/Docs/Action/RetainAction.ts
@@ -1,9 +1,10 @@
import { DocActionBase } from '../../Command/DocActionBase';
import { ActionObservers, CommandManager, CommandUnit, CommonParameter } from '../../Command';
-import { UpdateAttributeValueType } from '../../Interfaces/IDocumentData';
import { IRetainActionData } from './ActionDataInterface';
import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
import { UpdateAttributeApply } from '../Apply/UpdateAttributeApply';
+import { IDocumentBody } from '../../Interfaces';
+import { Nullable } from '../../Shared';
export class RetainAction extends DocActionBase<IRetainActionData, IRetainActionData> {
static NAME = 'RetainAction';
@@ -11,40 +12,35 @@ export class RetainAction extends DocActionBase<IRetainActionData, IRetainAction
constructor(actionData: IRetainActionData, commandUnit: CommandUnit, observers: ActionObservers, commonParameter: CommonParameter) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
- const undoData = { ...actionData, coverType: UpdateDocsAttributeType.REPLACE };
-
- const attributes = this.do(commonParameter);
- if (attributes.length > 0) {
- undoData.attributes = attributes;
- }
- this._oldActionData = undoData;
+ this._oldActionData = {
+ ...actionData,
+ coverType: UpdateDocsAttributeType.REPLACE,
+ body: this.do(commonParameter),
+ };
}
redo(commonParameter: CommonParameter): void {
this.do(commonParameter);
}
- do(commonParameter: CommonParameter): UpdateAttributeValueType[] {
+ do(commonParameter: CommonParameter): Nullable<IDocumentBody> {
const actionData = this.getDoActionData();
const document = this.getDocument();
- const { len, segmentId, attributes, coverType, attributeType } = actionData;
+ const { len, segmentId, body, coverType } = actionData;
- let results: UpdateAttributeValueType[] = [];
- if (attributes && attributeType) {
- results = UpdateAttributeApply(document, len, commonParameter.cursor, attributes, attributeType, coverType, segmentId);
- }
+ let result: Nullable<IDocumentBody> = UpdateAttributeApply(document, body, len, commonParameter.cursor, coverType, segmentId);
commonParameter.moveCursor(len);
- return results;
+ return result;
}
undo(commonParameter: CommonParameter): void {
const actionData = this.getOldActionData();
const document = this.getDocument();
- const { len, segmentId, attributes, coverType, attributeType } = actionData;
+ const { len, segmentId, body, coverType } = actionData;
- if (attributes && attributeType) UpdateAttributeApply(document, len, commonParameter.cursor, attributes, attributeType, coverType, segmentId);
+ if (body) UpdateAttributeApply(document, body, len, commonParameter.cursor, coverType, segmentId);
commonParameter.moveCursor(len);
}
diff --git a/packages/core/src/Docs/Action/UpdateDocumentAction.ts b/packages/core/src/Docs/Action/UpdateDocumentAction.ts
index bfd9c18e2c..d4e67c8e2d 100644
--- a/packages/core/src/Docs/Action/UpdateDocumentAction.ts
+++ b/packages/core/src/Docs/Action/UpdateDocumentAction.ts
@@ -2,17 +2,10 @@ import { DocActionBase } from '../../Command/DocActionBase';
import { ActionObservers, CommandManager, CommandUnit } from '../../Command';
import { IUpdateDocumentActionData } from './ActionDataInterface';
-export class UpdateDocumentAction extends DocActionBase<
- IUpdateDocumentActionData,
- IUpdateDocumentActionData
-> {
+export class UpdateDocumentAction extends DocActionBase<IUpdateDocumentActionData, IUpdateDocumentActionData> {
static NAME = 'UpdateDocumentAction';
- constructor(
- actionData: IUpdateDocumentActionData,
- commandUnit: CommandUnit,
- observers: ActionObservers
- ) {
+ constructor(actionData: IUpdateDocumentActionData, commandUnit: CommandUnit, observers: ActionObservers) {
super(actionData, commandUnit, observers);
this._doActionData = { ...actionData };
this.do();
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
new file mode 100644
index 0000000000..68f4f4573e
--- /dev/null
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -0,0 +1,442 @@
+import { IDocumentBody } from '../../Interfaces/IDocumentData';
+import { horizontalLineSegmentsSubtraction } from '../../Shared/Common';
+import { isSameStyleTextRun } from '../../Shared/Compare';
+import { sortRulesFactory } from '../../Shared/SortRules';
+
+export function insertTextRuns(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;
+
+ 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);
+ }
+ }
+}
+
+export function insertParagraphs(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;
+ }
+
+ paragraphs.push(...insertParagraphs);
+ paragraphs.sort(sortRulesFactory('startIndex'));
+ }
+}
+
+export function insertSectionBreaks(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;
+ }
+ }
+
+ 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'));
+ }
+}
+
+export function insertCustomBlocks(body: IDocumentBody, insertBody: IDocumentBody, textLength: number, currentIndex: number) {
+ const { customBlocks } = body;
+
+ if (customBlocks == null) {
+ return;
+ }
+
+ 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'));
+ }
+}
+
+export function insertTables(body: IDocumentBody, insertBody: IDocumentBody, textLength: number, currentIndex: number) {
+ const { tables } = body;
+
+ if (tables == null) {
+ return;
+ }
+
+ 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'));
+ }
+}
+
+export function insertCustomRanges(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;
+ }
+ }
+
+ 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'));
+ }
+}
+
+export function deleteTextRuns(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { textRuns } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeTextRuns = [];
+ if (textRuns) {
+ const newTextRuns = [];
+ for (let i = 0, len = textRuns.length; i < len; i++) {
+ const textRun = textRuns[i];
+ const { st, ed } = textRun;
+
+ if (startIndex <= st && endIndex >= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } else if (st <= startIndex && ed >= endIndex) {
+ const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length > 2) {
+ const seg1 = segments[0];
+ const seg2 = segments[1];
+ textRun.st = seg1[0];
+ textRun.ed = seg1[1] + seg2[1] - seg2[0] + 1;
+ } else {
+ textRun.st = segments[0][0];
+ textRun.ed = segments[0][1];
+ }
+ } else if (startIndex >= st && startIndex <= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: startIndex - currentIndex,
+ ed: ed - currentIndex,
+ });
+ textRun.ed = startIndex - 1;
+ } else if (endIndex >= st && endIndex <= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: st - currentIndex,
+ ed: endIndex - currentIndex,
+ });
+ textRun.st = endIndex + 1;
+ } else if (st > endIndex) {
+ textRun.st -= textLength;
+ textRun.ed -= textLength;
+ }
+ newTextRuns.push(textRun);
+ }
+ body.textRuns = newTextRuns;
+ }
+
+ return removeTextRuns;
+}
+
+export function deleteParagraphs(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { paragraphs } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeParagraphs = [];
+ if (paragraphs) {
+ const newParagraphs = [];
+ for (let i = 0, len = paragraphs.length; i < len; i++) {
+ const paragraph = paragraphs[i];
+ const { startIndex: index } = paragraph;
+ if (index >= startIndex && index <= endIndex) {
+ removeParagraphs.push({
+ ...paragraph,
+ startIndex: index - currentIndex,
+ });
+ continue;
+ } else if (index > endIndex) {
+ paragraph.startIndex -= textLength;
+ }
+
+ newParagraphs.push(paragraph);
+ }
+ body.paragraphs = newParagraphs;
+ }
+ return removeParagraphs;
+}
+
+export function deleteSectionBreaks(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { sectionBreaks } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeSectionBreaks = [];
+ if (sectionBreaks) {
+ const newSectionBreaks = [];
+ for (let i = 0, len = sectionBreaks.length; i < len; i++) {
+ const sectionBreak = sectionBreaks[i];
+ const { startIndex: index } = sectionBreak;
+ if (index >= startIndex && index <= endIndex) {
+ removeSectionBreaks.push({
+ ...sectionBreak,
+ startIndex: index - currentIndex,
+ });
+ continue;
+ } else if (index > endIndex) {
+ sectionBreak.startIndex -= textLength;
+ }
+
+ newSectionBreaks.push(sectionBreak);
+ }
+ body.sectionBreaks = newSectionBreaks;
+ }
+ return removeSectionBreaks;
+}
+
+export function deleteCustomBlocks(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { customBlocks } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeCustomBlocks = [];
+ if (customBlocks) {
+ const newCustomBlocks = [];
+ for (let i = 0, len = customBlocks.length; i < len; i++) {
+ const customBlock = customBlocks[i];
+ const { startIndex: index } = customBlock;
+ if (index >= startIndex && index <= endIndex) {
+ removeCustomBlocks.push({
+ ...customBlock,
+ startIndex: index - currentIndex,
+ });
+ continue;
+ } else if (index > endIndex) {
+ customBlock.startIndex -= textLength;
+ }
+
+ newCustomBlocks.push(customBlock);
+ }
+ body.customBlocks = newCustomBlocks;
+ }
+ return removeCustomBlocks;
+}
+
+export function deleteTables(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { tables } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeTables = [];
+ if (tables) {
+ const newTables = [];
+ for (let i = 0, len = tables.length; i < len; i++) {
+ const table = tables[i];
+ const { startIndex: st, endIndex: ed } = table;
+ if (startIndex <= st && endIndex >= ed) {
+ removeTables.push({
+ ...table,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } else if (st <= startIndex && ed >= endIndex) {
+ const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length > 2) {
+ const seg1 = segments[0];
+ const seg2 = segments[1];
+ table.startIndex = seg1[0];
+ table.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;
+ } else {
+ table.startIndex = segments[0][0];
+ table.endIndex = segments[0][1];
+ }
+ } else if (endIndex < st) {
+ table.startIndex -= textLength;
+ table.endIndex -= textLength;
+ }
+ newTables.push(table);
+ }
+ body.tables = newTables;
+ }
+ return removeTables;
+}
+
+export function deleteCustomRanges(body: IDocumentBody, textLength: number, currentIndex: number) {
+ const { customRanges } = body;
+
+ const startIndex = currentIndex;
+
+ const endIndex = currentIndex + textLength - 1;
+ const removeCustomRanges = [];
+ if (customRanges) {
+ const newCustomRanges = [];
+ for (let i = 0, len = customRanges.length; i < len; i++) {
+ const customRange = customRanges[i];
+ const { startIndex: st, endIndex: ed } = customRange;
+ if (startIndex <= st && endIndex >= ed) {
+ removeCustomRanges.push({
+ ...customRange,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } else if (st <= startIndex && ed >= endIndex) {
+ const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
+
+ if (segments.length > 2) {
+ const seg1 = segments[0];
+ const seg2 = segments[1];
+ customRange.startIndex = seg1[0];
+ customRange.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;
+ } else {
+ customRange.startIndex = segments[0][0];
+ customRange.endIndex = segments[0][1];
+ }
+ } else if (endIndex < st) {
+ customRange.startIndex -= textLength;
+ customRange.endIndex -= textLength;
+ }
+ newCustomRanges.push(customRange);
+ }
+ body.customRanges = newCustomRanges;
+ }
+ return removeCustomRanges;
+}
diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.ts
index 37a6229869..3e84839b26 100644
--- a/packages/core/src/Docs/Apply/DeleteApply.ts
+++ b/packages/core/src/Docs/Apply/DeleteApply.ts
@@ -1,6 +1,7 @@
import { DocumentModel } from '../Domain/DocumentModel';
-import { deleteContent, getDocsUpdateBody, horizontalLineSegmentsSubtraction } from '../../Shared/Common';
+import { deleteContent, getDocsUpdateBody } from '../../Shared/Common';
import { IDocumentBody } from '../../Interfaces/IDocumentData';
+import { deleteCustomBlocks, deleteCustomRanges, deleteParagraphs, deleteSectionBreaks, deleteTables, deleteTextRuns } from './Common';
export function DeleteApply(document: DocumentModel, textLength: number, currentIndex: number, segmentId?: string) {
const doc = document.snapshot;
@@ -25,17 +26,17 @@ function updateAttributeByDelete(body: IDocumentBody, textLength: number, curren
const endIndex = currentIndex + textLength - 1;
- const removeTextRuns = processTextRuns(body, textLength, currentIndex);
+ const removeTextRuns = deleteTextRuns(body, textLength, currentIndex);
- const removeParagraphs = processParagraphs(body, textLength, currentIndex);
+ const removeParagraphs = deleteParagraphs(body, textLength, currentIndex);
- const removeSectionBreaks = processSectionBreaks(body, textLength, currentIndex);
+ const removeSectionBreaks = deleteSectionBreaks(body, textLength, currentIndex);
- const removeCustomBlocks = processCustomBlocks(body, textLength, currentIndex);
+ const removeCustomBlocks = deleteCustomBlocks(body, textLength, currentIndex);
- const removeTables = processTables(body, textLength, currentIndex);
+ const removeTables = deleteTables(body, textLength, currentIndex);
- const removeCustomRanges = processCustomRanges(body, textLength, currentIndex);
+ const removeCustomRanges = deleteCustomRanges(body, textLength, currentIndex);
let removeDataStream = '';
if (dataStream) {
@@ -54,232 +55,3 @@ function updateAttributeByDelete(body: IDocumentBody, textLength: number, curren
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 = [];
- for (let i = 0, len = textRuns.length; i < len; i++) {
- const textRun = textRuns[i];
- const { st, ed } = textRun;
-
- if (startIndex <= st && endIndex >= ed) {
- removeTextRuns.push({
- ...textRun,
- st: st - currentIndex,
- ed: ed - currentIndex,
- });
- continue;
- } else if (st <= startIndex && ed >= endIndex) {
- const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
-
- if (segments.length > 2) {
- const seg1 = segments[0];
- const seg2 = segments[1];
- textRun.st = seg1[0];
- textRun.ed = seg1[1] + seg2[1] - seg2[0] + 1;
- } else {
- textRun.st = segments[0][0];
- textRun.ed = segments[0][1];
- }
- } else if (startIndex >= st && startIndex <= ed) {
- removeTextRuns.push({
- ...textRun,
- st: startIndex - currentIndex,
- ed: ed - currentIndex,
- });
- textRun.ed = startIndex - 1;
- } else if (endIndex >= st && endIndex <= ed) {
- removeTextRuns.push({
- ...textRun,
- st: st - currentIndex,
- ed: endIndex - currentIndex,
- });
- textRun.st = endIndex + 1;
- } else if (st > endIndex) {
- textRun.st -= textLength;
- textRun.ed -= textLength;
- }
- newTextRuns.push(textRun);
- }
- 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 = [];
- for (let i = 0, len = paragraphs.length; i < len; i++) {
- const paragraph = paragraphs[i];
- const { startIndex: index } = paragraph;
- if (index >= startIndex && index <= endIndex) {
- removeParagraphs.push({
- ...paragraph,
- startIndex: index - currentIndex,
- });
- continue;
- } else if (index > endIndex) {
- paragraph.startIndex -= textLength;
- }
-
- newParagraphs.push(paragraph);
- }
- 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 = [];
- for (let i = 0, len = sectionBreaks.length; i < len; i++) {
- const sectionBreak = sectionBreaks[i];
- const { startIndex: index } = sectionBreak;
- if (index >= startIndex && index <= endIndex) {
- removeSectionBreaks.push({
- ...sectionBreak,
- startIndex: index - currentIndex,
- });
- continue;
- } else if (index > endIndex) {
- sectionBreak.startIndex -= textLength;
- }
-
- newSectionBreaks.push(sectionBreak);
- }
- 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 = [];
- for (let i = 0, len = customBlocks.length; i < len; i++) {
- const customBlock = customBlocks[i];
- const { startIndex: index } = customBlock;
- if (index >= startIndex && index <= endIndex) {
- removeCustomBlocks.push({
- ...customBlock,
- startIndex: index - currentIndex,
- });
- continue;
- } else if (index > endIndex) {
- customBlock.startIndex -= textLength;
- }
-
- newCustomBlocks.push(customBlock);
- }
- 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 = [];
- for (let i = 0, len = tables.length; i < len; i++) {
- const table = tables[i];
- const { startIndex: st, endIndex: ed } = table;
- if (startIndex <= st && endIndex >= ed) {
- removeTables.push({
- ...table,
- st: st - currentIndex,
- ed: ed - currentIndex,
- });
- continue;
- } else if (st <= startIndex && ed >= endIndex) {
- const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
-
- if (segments.length > 2) {
- const seg1 = segments[0];
- const seg2 = segments[1];
- table.startIndex = seg1[0];
- table.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;
- } else {
- table.startIndex = segments[0][0];
- table.endIndex = segments[0][1];
- }
- } else if (endIndex < st) {
- table.startIndex -= textLength;
- table.endIndex -= textLength;
- }
- newTables.push(table);
- }
- 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 = [];
- for (let i = 0, len = customRanges.length; i < len; i++) {
- const customRange = customRanges[i];
- const { startIndex: st, endIndex: ed } = customRange;
- if (startIndex <= st && endIndex >= ed) {
- removeCustomRanges.push({
- ...customRange,
- st: st - currentIndex,
- ed: ed - currentIndex,
- });
- continue;
- } else if (st <= startIndex && ed >= endIndex) {
- const segments = horizontalLineSegmentsSubtraction(st, ed, startIndex, endIndex);
-
- if (segments.length > 2) {
- const seg1 = segments[0];
- const seg2 = segments[1];
- customRange.startIndex = seg1[0];
- customRange.endIndex = seg1[1] + seg2[1] - seg2[0] + 1;
- } else {
- customRange.startIndex = segments[0][0];
- customRange.endIndex = segments[0][1];
- }
- } else if (endIndex < st) {
- customRange.startIndex -= textLength;
- customRange.endIndex -= textLength;
- }
- newCustomRanges.push(customRange);
- }
- body.customRanges = newCustomRanges;
- }
- return removeCustomRanges;
-}
diff --git a/packages/core/src/Docs/Apply/InsertApply.ts b/packages/core/src/Docs/Apply/InsertApply.ts
index a088a40ebf..158da15e6d 100644
--- a/packages/core/src/Docs/Apply/InsertApply.ts
+++ b/packages/core/src/Docs/Apply/InsertApply.ts
@@ -1,8 +1,7 @@
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';
+import { insertCustomBlocks, insertCustomRanges, insertParagraphs, insertSectionBreaks, insertTables, insertTextRuns } from './Common';
export function InsertApply(document: DocumentModel, insertBody: IDocumentBody, textLength: number, currentIndex: number, segmentId?: string) {
const doc = document.snapshot;
@@ -25,224 +24,15 @@ export function InsertApply(document: DocumentModel, insertBody: IDocumentBody,
}
function updateAttributeByInsert(body: IDocumentBody, insertBody: IDocumentBody, textLength: number, currentIndex: number) {
- processTextRuns(body, insertBody, textLength, currentIndex);
+ insertTextRuns(body, insertBody, textLength, currentIndex);
- processParagraphs(body, insertBody, textLength, currentIndex);
+ insertParagraphs(body, insertBody, textLength, currentIndex);
- processSectionBreaks(body, insertBody, textLength, currentIndex);
+ insertSectionBreaks(body, insertBody, textLength, currentIndex);
- processCustomBlocks(body, insertBody, textLength, currentIndex);
+ insertCustomBlocks(body, insertBody, textLength, currentIndex);
- processTables(body, insertBody, textLength, currentIndex);
+ insertTables(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;
-
- 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);
- }
- }
-}
-
-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;
- }
-
- paragraphs.push(...insertParagraphs);
- paragraphs.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;
- }
- }
-
- 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'));
- }
-}
-
-function processCustomBlocks(body: IDocumentBody, insertBody: IDocumentBody, textLength: number, currentIndex: number) {
- const { customBlocks } = body;
-
- if (customBlocks == null) {
- return;
- }
-
- 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'));
- }
-}
-
-function processTables(body: IDocumentBody, insertBody: IDocumentBody, textLength: number, currentIndex: number) {
- const { tables } = body;
-
- if (tables == null) {
- return;
- }
-
- 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'));
- }
-}
-
-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;
- }
- }
-
- 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'));
- }
+ insertCustomRanges(body, insertBody, textLength, currentIndex);
}
diff --git a/packages/core/src/Docs/Apply/UpdateAttributeApply.ts b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
index 2b38c36b93..aa71ef84cc 100644
--- a/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
+++ b/packages/core/src/Docs/Apply/UpdateAttributeApply.ts
@@ -1,53 +1,53 @@
-import { UpdateAttributeValueType, IDocumentBody, ITextRun } from '../../Interfaces/IDocumentData';
+import { IDocumentBody } from '../../Interfaces/IDocumentData';
import { DocumentModel } from '../Domain/DocumentModel';
import { getDocsUpdateBody, horizontalLineSegmentsSubtraction } from '../../Shared/Common';
import { UpdateDocsAttributeType } from '../../Shared/CommandEnum';
-import { UpdateAttributeType } from '../Action/ActionDataInterface';
+import { Nullable } from '../../Shared/Types';
export function UpdateAttributeApply(
document: DocumentModel,
+ updateBody: Nullable<IDocumentBody>,
textLength: number,
currentIndex: number,
- attributes: UpdateAttributeValueType[],
- attributeType: UpdateAttributeType,
coverType: UpdateDocsAttributeType,
segmentId?: string
) {
+ if (updateBody == null) {
+ return;
+ }
+
const doc = document.snapshot;
const body = getDocsUpdateBody(doc, segmentId);
- if (textLength === 0) {
- return [];
- }
-
if (body == null) {
throw new Error('no body has changed');
}
- return updateAttribute(body, textLength, currentIndex, attributes, attributeType, coverType);
+ return updateAttribute(body, updateBody, textLength, currentIndex, coverType);
}
-function updateAttribute(
- body: IDocumentBody,
- textLength: number,
- currentIndex: number,
- attributes: UpdateAttributeValueType[],
- attributeType: UpdateAttributeType,
- coverType: UpdateDocsAttributeType
-) {
- if (attributeType === UpdateAttributeType.TEXT_RUN) {
- return processTextRuns(body, textLength, currentIndex, attributes as ITextRun[], coverType);
- }
-
- return [];
+function updateAttribute(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType): IDocumentBody {
+ const removeTextRuns = updateTextRuns(body, updateBody, textLength, currentIndex, coverType);
+
+ return {
+ dataStream: '',
+ textRuns: removeTextRuns,
+ // paragraphs: removeParagraphs,
+ // sectionBreaks: removeSectionBreaks,
+ // customBlocks: removeCustomBlocks,
+ // tables: removeTables,
+ // customRanges: removeCustomRanges,
+ };
}
-function processTextRuns(body: IDocumentBody, textLength: number, currentIndex: number, updateTextRuns: ITextRun[], coverType: UpdateDocsAttributeType) {
+function updateTextRuns(body: IDocumentBody, updateBody: IDocumentBody, textLength: number, currentIndex: number, coverType: UpdateDocsAttributeType) {
const { textRuns } = body;
- if (textRuns == null) {
- return [];
+ const { textRuns: updateTextRuns } = updateBody;
+
+ if (textRuns == null || updateTextRuns == null) {
+ return;
}
const startIndex = currentIndex;
diff --git a/packages/core/src/Interfaces/IDocumentData.ts b/packages/core/src/Interfaces/IDocumentData.ts
index 17f0f04320..f2d57a96ba 100644
--- a/packages/core/src/Interfaces/IDocumentData.ts
+++ b/packages/core/src/Interfaces/IDocumentData.ts
@@ -65,8 +65,6 @@ export interface ISizeData {
height?: number;
}
-export type UpdateAttributeValueType = ITextRun | IParagraph | ISectionBreak | ICustomBlock | ITable | ICustomRange;
-
/**
* Properties of document body
* 三种更新类型,范围叠加、范围互斥、占位符