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, finished delete and insert
DR-Univerfc27da1ff919
diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.tsindex 22331158de..f07b27f5f7 100644--- a/packages/core/src/Docs/Apply/DeleteApply.ts+++ b/packages/core/src/Docs/Apply/DeleteApply.ts@@ -1,5 +1,9 @@import { DocumentModel } from '../Domain/DocumentModel';-import { deleteContent, getDocsUpdateBody } from '../../Shared/Common';+import {+ deleteContent,+ getDocsUpdateBody,+ horizontalLineSegmentsSubtraction,+} from '../../Shared/Common';import { IDocumentBody } from '../../Interfaces/IDocumentData';export function DeleteApply(@@ -56,6 +60,23 @@ function updateAttributeByDelete(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,@@ -155,6 +176,23 @@ function updateAttributeByDelete(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;@@ -177,6 +215,23 @@ function updateAttributeByDelete(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;diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.tsindex 0043ac2da2..766908145c 100644--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts@@ -1,5 +1,10 @@import { IDocumentBody, ITextRun } from '../../Interfaces/IDocumentData';-import { Nullable, insertTextToContent } from '../../Shared';+import {+ deleteContent,+ horizontalLineSegmentsSubtraction,+ insertTextToContent,+} from '../../Shared/Common';+import { Nullable } from '../../Shared/Types';export type DocumentBodyModelOrSimple = DocumentBodyModelSimple | DocumentBodyModel;@@ -228,7 +233,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {const { firstNode: insertedFirstNode, lastNode: insertedLastNode } =insertedNodeSplit;- insertedNode.parent.children.splice(+ insertedNode.parent?.children.splice(insertedNode.getPositionInParent(),1,insertedFirstNode,@@ -280,7 +285,41 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- delete(currentIndex: number, textLength: number) {}+ delete(currentIndex: number, textLength: number) {+ const nodes = this.children;+ this.deleteTree(nodes, currentIndex, textLength);+ }++ deleteTree(+ nodes: DataStreamTreeNode[],+ currentIndex: number,+ textLength: number+ ) {+ const startIndex = currentIndex;+ const endIndex = currentIndex + textLength - 1;+ let mergeNode: Nullable<DataStreamTreeNode> = null;+ for (let node of nodes) {+ const { startIndex: st, endIndex: ed, children } = node;++ this.deleteTree(children, currentIndex, textLength);++ if (startIndex <= st && endIndex >= ed) {+ node.remove();+ } else if (st <= startIndex && ed >= endIndex) {+ node.minus(startIndex, ed);+ } else if (startIndex >= st && startIndex <= ed) {+ node.minus(startIndex, ed);+ mergeNode = node;+ } else if (endIndex >= st && endIndex <= ed) {+ node.minus(st, endIndex);+ if (mergeNode) {+ mergeNode.merge(node);+ }+ } else if (st > endIndex) {+ node.plus(-textLength);+ }+ }+ }getParagraphByTree(nodes: DataStreamTreeNode[],@@ -299,8 +338,11 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {return null;}- foreachTop(node: DataStreamTreeNode, func: (node: DataStreamTreeNode) => void) {- let parent = node;+ foreachTop(+ node: Nullable<DataStreamTreeNode>,+ func: (node: DataStreamTreeNode) => void+ ) {+ let parent: Nullable<DataStreamTreeNode> = node;while (parent) {func(parent);parent = parent.parent;@@ -315,7 +357,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- resetCache() {+ override resetCache() {this.sectionBreakCurrentIndex = 0;this.paragraphCurrentIndex = 0;this.textRunCurrentIndex = 0;@@ -324,7 +366,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {this.customRangeCurrentIndex = 0;}- getSectionBreak(index: number) {+ override getSectionBreak(index: number) {if (index == null) {return;}@@ -342,7 +384,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- getParagraph(index: number) {+ override getParagraph(index: number) {const paragraphs = this.body.paragraphs;if (paragraphs == null) {return;@@ -357,7 +399,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- getTextRunRange(startIndex: number = 0, endIndex: number) {+ override getTextRunRange(startIndex: number = 0, endIndex: number) {const textRuns = this.body.textRuns;if (textRuns == null) {return [@@ -412,7 +454,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {return trRange;}- getTextRun(index: number) {+ override getTextRun(index: number) {const textRuns = this.body.textRuns;if (textRuns == null) {return;@@ -442,7 +484,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- getCustomBlock(index: number) {+ override getCustomBlock(index: number) {const customBlocks = this.body.customBlocks;if (customBlocks == null) {return;@@ -457,7 +499,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- getTable(index: number) {+ override getTable(index: number) {const tables = this.body.tables;if (tables == null) {return;@@ -472,7 +514,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {}}- getCustomRange(index: number) {+ override getCustomRange(index: number) {const customRanges = this.body.customRanges;if (customRanges == null) {return;@@ -499,7 +541,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {export class DataStreamTreeNode {children: DataStreamTreeNode[] = [];- parent: DataStreamTreeNode;+ parent: Nullable<DataStreamTreeNode>;startIndex: number;@@ -632,10 +674,45 @@ export class DataStreamTreeNode {}getPositionInParent() {- return this.parent.children.indexOf(this);+ 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(len: number) {}+ 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) {}}diff --git a/packages/core/src/Shared/Common.ts b/packages/core/src/Shared/Common.tsindex b4c000567a..7911d6bfd3 100644--- a/packages/core/src/Shared/Common.ts+++ b/packages/core/src/Shared/Common.ts@@ -525,3 +525,40 @@ export function getDocsUpdateBody(model: IDocumentData, segmentId?: string) {return body;}++export function horizontalLineSegmentsSubtraction(+ A1: number,+ A2: number,+ B1: number,+ B2: number+) {+ // 确保A1 < A2, B1 < B2+ if (A1 > A2) {+ [A1, A2] = [A2, A1];+ }+ if (B1 > B2) {+ [B1, B2] = [B2, B1];+ }++ // 检查线段是否重叠+ if (A2 < B1 || B2 < A1) {+ return [[A1, A2]]; // 无重叠,返回原线段A+ }++ const overlapStart = Math.max(A1, B1);+ const overlapEnd = Math.min(A2, B2);++ const result = [];++ // 添加重叠之前的线段+ if (overlapStart > A1) {+ result.push([A1, overlapStart]);+ }++ // 添加重叠之后的线段+ if (overlapEnd < A2) {+ result.push([overlapEnd, A2]);+ }++ return result;+}