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-Univer56c7eb1a9ee0
diff --git a/packages/core/src/Docs/Action/DeleteAction.ts b/packages/core/src/Docs/Action/DeleteAction.ts
index ba44a51ab9..6053b5889d 100644
--- a/packages/core/src/Docs/Action/DeleteAction.ts
+++ b/packages/core/src/Docs/Action/DeleteAction.ts
@@ -1,6 +1,7 @@
import { DocActionBase, IDocActionData } from '../../Command/DocActionBase';
import {
ActionObservers,
+ ActionType,
CommandManager,
CommandUnit,
CommonParameter,
@@ -8,6 +9,8 @@ import {
import { IInsertActionData } from './InsertAction';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
import { IDocumentBody } from '../../Interfaces';
+import { DeleteApply } from '../Apply/DeleteApply';
+import { InsertApply } from '../Apply/InsertApply';
export interface IDeleteActionData extends IDocActionData {
len: number;
@@ -55,30 +58,36 @@ export class DeleteAction extends DocActionBase<
const actionData = this.getDoActionData();
const document = this.getDocument();
const { len, segmentId } = actionData;
+
+ const body = DeleteApply(document, len, commonParameter.cursor, segmentId);
+
commonParameter.moveCursor(len);
- // DeleteTextApply(document, {
- // cursorStart,
- // cursorEnd,
- // isStartBack,
- // isEndBack,
- // isCollapse,
- // segmentId,
- // });
- return {
- dataStream: '',
- };
+
+ this._observers.notifyObservers({
+ type: ActionType.REDO,
+ data: this._doActionData,
+ action: this,
+ commonParameter,
+ });
+
+ return body;
}
undo(commonParameter: CommonParameter): void {
const actionData = this.getOldActionData();
const document = this.getDocument();
const { body, len, segmentId } = actionData;
+
+ InsertApply(document, body, len, commonParameter.cursor, segmentId);
+
commonParameter.moveCursor(len);
- // InsertTextApply(document, text, textLength, {
- // cursorStart,
- // isStartBack,
- // segmentId,
- // });
+
+ this._observers.notifyObservers({
+ type: ActionType.UNDO,
+ data: this._oldActionData,
+ action: this,
+ commonParameter,
+ });
}
validate(): boolean {
diff --git a/packages/core/src/Docs/Action/InsertAction.ts b/packages/core/src/Docs/Action/InsertAction.ts
index 0b02e0f306..29faf7427e 100644
--- a/packages/core/src/Docs/Action/InsertAction.ts
+++ b/packages/core/src/Docs/Action/InsertAction.ts
@@ -10,6 +10,7 @@ import { IDeleteActionData } from './DeleteAction';
import { IDocumentBody } from '../../Interfaces';
import { DOC_ACTION_NAMES } from '../../Const/DOC_ACTION_NAMES';
import { InsertApply } from '../Apply/InsertApply';
+import { DeleteApply } from '../Apply/DeleteApply';
export interface IInsertActionData extends IDocActionData {
body: IDocumentBody;
@@ -67,6 +68,9 @@ export class InsertAction extends DocActionBase<
const actionData = this.getOldActionData();
const document = this.getDocument();
const { len, segmentId } = actionData;
+
+ DeleteApply(document, len, commonParameter.cursor, segmentId);
+
commonParameter.moveCursor(len);
// DeleteTextApply(document, { ...actionData });
@@ -74,6 +78,7 @@ export class InsertAction extends DocActionBase<
type: ActionType.UNDO,
data: this._oldActionData,
action: this,
+ commonParameter,
});
}
diff --git a/packages/core/src/Docs/Apply/DeleteApply.ts b/packages/core/src/Docs/Apply/DeleteApply.ts
index c55251c611..22331158de 100644
--- a/packages/core/src/Docs/Apply/DeleteApply.ts
+++ b/packages/core/src/Docs/Apply/DeleteApply.ts
@@ -1,5 +1,5 @@
import { DocumentModel } from '../Domain/DocumentModel';
-import { getDocsUpdateBody } from '../../Shared/Common';
+import { deleteContent, getDocsUpdateBody } from '../../Shared/Common';
import { IDocumentBody } from '../../Interfaces/IDocumentData';
export function DeleteApply(
@@ -18,16 +18,16 @@ export function DeleteApply(
throw new Error('no body has changed');
}
- updateAttributeByDelete(body, textLength, currentIndex);
+ bodyModel.delete(currentIndex, textLength);
- bodyModel.delete();
+ return updateAttributeByDelete(body, textLength, currentIndex);
}
function updateAttributeByDelete(
body: IDocumentBody,
textLength: number,
currentIndex: number
-) {
+): IDocumentBody {
const {
textRuns,
paragraphs,
@@ -42,17 +42,34 @@ function updateAttributeByDelete(
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 && startIndex <= ed) {
+
+ if (startIndex <= st && endIndex >= ed) {
+ removeTextRuns.push({
+ ...textRun,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } 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 (startIndex <= st && endIndex >= ed) {
- continue;
} else if (st > endIndex) {
textRun.st -= textLength;
textRun.ed -= textLength;
@@ -62,69 +79,127 @@ function updateAttributeByDelete(
body.textRuns = newTextRuns;
}
+ const removeParagraphs = [];
if (paragraphs) {
+ const newParagraphs = [];
for (let i = 0, len = paragraphs.length; i < len; i++) {
const paragraph = paragraphs[i];
- const { startIndex } = paragraph;
- if (startIndex > currentIndex) {
- paragraph.startIndex += textLength;
+ 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;
}
+ const removeSectionBreaks = [];
if (sectionBreaks) {
+ const newSectionBreaks = [];
for (let i = 0, len = sectionBreaks.length; i < len; i++) {
const sectionBreak = sectionBreaks[i];
- const { startIndex } = sectionBreak;
- if (startIndex > currentIndex) {
- sectionBreak.startIndex += textLength;
+ 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;
}
+ const removeCustomBlocks = [];
if (customBlocks) {
+ const newCustomBlocks = [];
for (let i = 0, len = customBlocks.length; i < len; i++) {
const customBlock = customBlocks[i];
- const { startIndex } = customBlock;
- if (startIndex > currentIndex) {
- customBlock.startIndex += textLength;
+ 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;
}
+ const removeTables = [];
if (tables) {
+ const newTables = [];
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 { startIndex: st, endIndex: ed } = table;
+ if (startIndex <= st && endIndex >= ed) {
+ removeTables.push({
+ ...table,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } else if (endIndex < st) {
+ table.startIndex -= textLength;
+ table.endIndex -= textLength;
}
+ newTables.push(table);
}
+ body.tables = newTables;
}
+ const removeCustomRanges = [];
if (customRanges) {
+ const newCustomRanges = [];
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 { startIndex: st, endIndex: ed } = customRange;
+ if (startIndex <= st && endIndex >= ed) {
+ removeCustomRanges.push({
+ ...customRange,
+ st: st - currentIndex,
+ ed: ed - currentIndex,
+ });
+ continue;
+ } else if (endIndex < st) {
+ customRange.startIndex -= textLength;
+ customRange.endIndex -= textLength;
}
+ newCustomRanges.push(customRange);
}
+ body.customRanges = newCustomRanges;
+ }
+
+ let removeDataStream = '';
+ if (dataStream) {
+ body.dataStream = deleteContent(dataStream, startIndex, endIndex);
+
+ removeDataStream = dataStream.slice(startIndex, endIndex);
}
return {
- textRuns,
- paragraphs,
- sectionBreaks,
- customBlocks,
- tables,
- customRanges,
- dataStream,
+ dataStream: removeDataStream,
+ textRuns: removeTextRuns,
+ paragraphs: removeParagraphs,
+ sectionBreaks: removeSectionBreaks,
+ customBlocks: removeCustomBlocks,
+ tables: removeTables,
+ customRanges: removeCustomRanges,
};
}
diff --git a/packages/core/src/Docs/Domain/DocumentBodyModel.ts b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
index 36c5596544..0043ac2da2 100644
--- a/packages/core/src/Docs/Domain/DocumentBodyModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentBodyModel.ts
@@ -280,9 +280,7 @@ export class DocumentBodyModel extends DocumentBodyModelSimple {
}
}
- update() {}
-
- delete() {}
+ delete(currentIndex: number, textLength: number) {}
getParagraphByTree(
nodes: DataStreamTreeNode[],