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-Univer70f38acf6894
diff --git a/packages/core/src/Command/ActionObservers.ts b/packages/core/src/Command/ActionObservers.tsindex e7ea15a007..0a5818644b 100644--- a/packages/core/src/Command/ActionObservers.ts+++ b/packages/core/src/Command/ActionObservers.ts@@ -1,5 +1,5 @@import { Observable } from '../Observer';-import { IActionData, ActionBase } from './index';+import { IActionData, ActionBase, CommonParameter } from './index';/*** Action type@@ -16,6 +16,7 @@ export interface IActionObserverProps {type: ActionType;data: IActionData;action: ActionBase<IActionData, IActionData>;+ commonParameter?: CommonParameter;}/**diff --git a/packages/core/src/Docs/Action/InsertAction.ts b/packages/core/src/Docs/Action/InsertAction.tsindex 7e264cffe3..0b02e0f306 100644--- a/packages/core/src/Docs/Action/InsertAction.ts+++ b/packages/core/src/Docs/Action/InsertAction.ts@@ -9,6 +9,7 @@ import {import { IDeleteActionData } from './DeleteAction';import { IDocumentBody } from '../../Interfaces';import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';+import { InsertApply } from '../Apply/InsertApply';export interface IInsertActionData extends IDocActionData {body: IDocumentBody;@@ -49,18 +50,17 @@ export class InsertAction extends DocActionBase<const actionData = this.getDoActionData();const document = this.getDocument();const { body, len, segmentId } = actionData;- commonParameter.moveCursor(len);- // InsertTextApply(document, text, textLength, {- // cursorStart,- // isStartBack,- // segmentId,- // });++ InsertApply(document, body, len, commonParameter.cursor, segmentId);this._observers.notifyObservers({type: ActionType.REDO,data: this._doActionData,action: this,+ commonParameter,});++ commonParameter.moveCursor(len);}undo(commonParameter: CommonParameter): void {diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.tsindex 9fc74cce03..c55251c611 100644--- a/packages/core/src/Docs/Apply/DeleteApply.ts+++ b/packages/core/src/Docs/Apply/DeleteApply.ts@@ -1,14 +1,16 @@-import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';import { DocumentModel } from '../Domain/DocumentModel';import { getDocsUpdateBody } from '../../Shared/Common';+import { IDocumentBody } from '../../Interfaces/IDocumentData';export function DeleteApply(document: DocumentModel,- range: ITextSelectionRangeParam+ textLength: number,+ currentIndex: number,+ segmentId?: string) {const doc = document.snapshot;- const { segmentId } = range;+ const bodyModel = document.getBodyModel(segmentId);const body = getDocsUpdateBody(doc, segmentId);@@ -16,103 +18,113 @@ export function DeleteApply(throw new Error('no body has changed');}- // const { blockElements } = body;+ updateAttributeByDelete(body, textLength, currentIndex);- // for (let blockElement of blockElements) {- // if (blockElement == null) {- // continue;- // }-- // const { blockType } = blockElement;-- // switch (blockType) {- // case BlockType.PARAGRAPH:- // if (blockElement.paragraph) {- // deleteText(blockElement, blockElement.paragraph, range);- // }- // }- // }+ bodyModel.delete();}-// function deleteText(-// blockElement: IBlockElement,-// paragraph: IParagraph,-// range: ITextSelectionRangeParam-// ) {-// const { cursorStart, cursorEnd, isStartBack, isEndBack, isCollapse } = range;--// const textStart = getTextIndexByCursor(cursorStart, isStartBack);--// const textEnd = getTextIndexByCursor(cursorEnd, isEndBack);--// const { st: blockStartIndex, ed: blockEndIndex } = blockElement;--// if (cursorStart > blockEndIndex || cursorEnd < blockStartIndex) {-// 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 (textEnd < elementStartIndex || textStart > elementEndIndex) {-// index++;-// continue;-// }--// if (paragraphElementType === ParagraphElementType.TEXT_RUN) {-// let relativeStart = textStart - elementStartIndex + 1;--// let relativeEnd = textEnd - elementStartIndex + 1;--// if (relativeStart <= 0) {-// relativeStart = 0;-// }--// if (relativeEnd >= elementEndIndex - elementStartIndex - 1) {-// relativeStart = elementEndIndex - elementStartIndex - 1;-// }+function updateAttributeByDelete(+ body: IDocumentBody,+ textLength: number,+ currentIndex: number+) {+ const {+ textRuns,+ paragraphs,+ sectionBreaks,+ customBlocks,+ tables,+ customRanges,+ dataStream,+ } = body;++ const startIndex = currentIndex;++ const endIndex = currentIndex + textLength - 1;++ 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 && startIndex <= ed) {+ textRun.ed = startIndex - 1;+ } else if (endIndex >= st && endIndex <= ed) {+ textRun.st = endIndex + 1;+ } else if (startIndex <= st && endIndex >= ed) {+ continue;+ } else if (st > endIndex) {+ textRun.st -= textLength;+ textRun.ed -= textLength;+ }+ newTextRuns.push(textRun);+ }+ body.textRuns = newTextRuns;+ }-// const newContent = deleteContent(-// tr.ct || '',-// relativeStart,-// relativeEnd-// );+ 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;+ }+ }+ }-// tr.ct = newContent;-// }+ 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;+ }+ }+ }-// if (tr.ct?.length === 0) {-// elements.splice(index, 1);-// } else {-// const preTr = elements[index - 1]?.tr;+ 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;+ }+ }+ }-// if (!preTr) {-// index++;-// continue;-// }+ 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 m = mergeSameTextRun(tr, preTr);+ 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;+ }+ }+ }-// if (m) {-// elements.splice(index, 1);-// } else {-// index++;-// }-// }-// }-// }+ return {+ textRuns,+ paragraphs,+ sectionBreaks,+ customBlocks,+ tables,+ customRanges,+ dataStream,+ };+}diff --git a/packages/core/src/Docs/Apply/InsertApply.ts b/packages/core/src/Docs/Apply/InsertApply.tsindex b494432104..e70f4814b7 100644--- a/packages/core/src/Docs/Apply/InsertApply.ts+++ b/packages/core/src/Docs/Apply/InsertApply.ts@@ -13,7 +13,7 @@ export function InsertApply() {const doc = document.snapshot;- const bodyModel = document.bodyModel;+ const bodyModel = document.getBodyModel(segmentId);const body = getDocsUpdateBody(doc, segmentId);@@ -25,10 +25,12 @@ export function InsertApply(throw new Error('no body has changed');}- updateAttribute(body, insertBody, textLength, currentIndex);+ updateAttributeByInsert(body, insertBody, textLength, currentIndex);++ bodyModel.insert(insertBody, currentIndex);}-function updateAttribute(+function updateAttributeByInsert(body: IDocumentBody,insertBody: IDocumentBody,textLength: number,diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.tsindex 80c75c766d..36c5596544 100644--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts@@ -202,20 +202,64 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {insert(insertBody: IDocumentBody, insertIndex = 0) {const dataStream = insertBody.dataStream;let dataStreamLen = dataStream.length;- const insertNode = this.getParagraphByTree(this.children, insertIndex);- if (insertNode == null) {+ const insertedNode = this.getParagraphByTree(this.children, insertIndex);+ if (insertedNode == null) {return;}if (dataStream[dataStream.length - 1] === '\n') {- const newBodyModel = new DocumentBodyModelSimple(insertBody);+ const insertBodyModel = new DocumentBodyModelSimple(insertBody);dataStreamLen -= 1; // sectionBreak can not be inserted++ const insertNodes = insertBodyModel.children;++ for (let node of insertNodes) {+ this.foreachDown(node, (newNode) => {+ newNode.plus(insertIndex);+ });+ }++ const insertedNodeSplit = insertedNode.split(insertIndex);++ if (insertedNodeSplit == null) {+ return;+ }++ const { firstNode: insertedFirstNode, lastNode: insertedLastNode } =+ insertedNodeSplit;++ insertedNode.parent.children.splice(+ insertedNode.getPositionInParent(),+ 1,+ insertedFirstNode,+ ...insertNodes,+ insertedLastNode+ );++ this.foreachTop(insertedNode.parent, (currentNode) => {+ currentNode.endIndex += dataStreamLen;+ const children = currentNode.children;+ let isStartFix = false;+ for (let node of children) {+ if (node === insertedLastNode) {+ isStartFix = true;+ }++ if (!isStartFix) {+ continue;+ }++ this.foreachDown(node, (newNode) => {+ newNode.plus(dataStreamLen);+ });+ }+ });} else {- insertNode.insertText(dataStream, insertIndex);+ insertedNode.insertText(dataStream, insertIndex);- insertNode.endIndex += dataStreamLen;+ insertedNode.endIndex += dataStreamLen;- this.foreachTop(insertNode.parent, (currentNode) => {+ this.foreachTop(insertedNode.parent, (currentNode) => {currentNode.endIndex += dataStreamLen;const children = currentNode.children;let isStartFix = false;@@ -479,6 +523,18 @@ export class DataStreamTreeNode {return new DataStreamTreeNode(nodeType, bodyModel, content);}+ getProps() {+ return {+ children: this.children,+ parent: this.parent,+ startIndex: this.startIndex,+ endIndex: this.endIndex,+ nodeType: this.nodeType,+ bodyModel: this.bodyModel,+ content: this.content,+ };+ }+addBlocks(blocks: number[]) {this.blocks = this.blocks.concat(blocks);}@@ -505,6 +561,82 @@ export class DataStreamTreeNode {this.endIndex += len;}+ split(index: number) {+ const {+ children,+ parent,+ startIndex,+ endIndex,+ nodeType,+ bodyModel,+ content = '',+ } = this.getProps();++ if (this.exclude(index)) {+ return;+ }++ const firstStartIndex = 0;+ const firstEndIndex = index - startIndex;++ const lastStartIndex = index - startIndex + 1;+ const lastEndIndex = endIndex;++ const firstNode = DataStreamTreeNode.create(+ nodeType,+ bodyModel,+ content.slice(firstStartIndex, firstEndIndex)+ );++ firstNode.parent = parent;+ firstNode.setIndexRange(firstStartIndex, firstEndIndex);++ const lastNode = DataStreamTreeNode.create(+ nodeType,+ bodyModel,+ content.slice(lastStartIndex, lastEndIndex)+ );++ lastNode.parent = parent;+ lastNode.setIndexRange(lastStartIndex, lastEndIndex);++ const firstChildNodes: DataStreamTreeNode[] = [];+ const lastChildNodes: DataStreamTreeNode[] = [];++ for (let node of children) {+ const { startIndex: childStartIndex } = node;+ if (node.exclude(index)) {+ if (index < childStartIndex) {+ firstChildNodes.push(node);+ } else {+ lastChildNodes.push(node);+ }+ } else {+ const splitData = node.split(index);+ if (splitData == null) {+ firstChildNodes.push(node);+ continue;+ }+ const { firstNode, lastNode } = splitData;+ firstChildNodes.push(firstNode);+ firstChildNodes.push(lastNode);+ }+ }++ firstNode.children = firstChildNodes;++ lastNode.children = lastChildNodes;++ return {+ firstNode,+ lastNode,+ };+ }++ getPositionInParent() {+ return this.parent.children.indexOf(this);+ }+minus(len: number) {}moveTo(newParent: DataStreamTreeNode) {}diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.tsindex acf08567a1..64a3d52158 100644--- a/packages/core/src/Docs/Domain/DocumentModel.ts+++ b/packages/core/src/Docs/Domain/DocumentModel.ts@@ -50,6 +50,22 @@ export class DocumentModelSimple {return this.snapshot.lists;}+ getBodyModel(segmentId?: string) {+ if (segmentId == null) {+ return this.bodyModel;+ }++ if (this.headerTreeMap.has(segmentId)) {+ return this.headerTreeMap.get(segmentId)!;+ }++ if (this.footerTreeMap.has(segmentId)) {+ return this.footerTreeMap.get(segmentId)!;+ }++ return this.bodyModel;+ }+getSnapshot() {return this.snapshot;}