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.

fix(sheet): doc apply fix

liuyang.0809409829a367e5
diff --git a/package.json b/package.json
index 6f587ad003..78cec76875 100644
--- a/package.json
+++ b/package.json
@@ -49,7 +49,7 @@
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.6.0",
"eslint-import-resolver-typescript": "^3.5.3",
- "eslint-plugin-import-newlines": "^1.3.1",
+ "eslint-plugin-import-newlines": "^1.3.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^2.0.0",
"execa": "^4.1.0",
diff --git a/packages/core/src/Docs/Action/DeleteTextAction.ts b/packages/core/src/Docs/Action/DeleteTextAction.ts
index b207f49046..fd23336ce9 100644
--- a/packages/core/src/Docs/Action/DeleteTextAction.ts
+++ b/packages/core/src/Docs/Action/DeleteTextAction.ts
@@ -65,8 +65,8 @@ export class DeleteTextAction extends DocActionBase<
// TODO ...
const actionData = this.getOldActionData();
const document = this.getDocument();
- // const { text, start, length } = actionData;
- InsertTextApply(document, { ...actionData });
+ const { text, cursorStart, isStartBack, segmentId } = actionData;
+ InsertTextApply(document, text, { cursorStart, isStartBack, segmentId });
}
validate(): boolean {
diff --git a/packages/core/src/Docs/Action/InsertTextAction.ts b/packages/core/src/Docs/Action/InsertTextAction.ts
index 60ee96c5e3..ce4c128aa0 100644
--- a/packages/core/src/Docs/Action/InsertTextAction.ts
+++ b/packages/core/src/Docs/Action/InsertTextAction.ts
@@ -43,8 +43,8 @@ export class InsertTextAction extends DocActionBase<
do(): void {
const actionData = this.getDoActionData();
const document = this.getDocument();
- // const { text, start, length, segmentId } = actionData;
- InsertTextApply(document, { ...actionData });
+ const { text, cursorStart, isStartBack, segmentId } = actionData;
+ InsertTextApply(document, text, { cursorStart, isStartBack, segmentId });
this._observers.notifyObservers({
type: ActionType.REDO,
diff --git a/packages/core/src/Docs/Apply/Common.ts b/packages/core/src/Docs/Apply/Common.ts
index 4bb1dcac35..e75323012c 100644
--- a/packages/core/src/Docs/Apply/Common.ts
+++ b/packages/core/src/Docs/Apply/Common.ts
@@ -4,12 +4,8 @@ import {
IElement,
} from '../../Interfaces/IDocumentData';
-export function getTextStartByAnchor(start: number) {
- let textStart = start - 1;
-
- textStart = textStart < 0 ? 0 : textStart;
-
- return textStart;
+export function getTextIndexByCursor(index: number, isBack: boolean = false) {
+ return isBack ? index - 1 : index;
}
export function insertTextToContent(content: string, start: number, text: string) {
diff --git a/packages/core/src/Docs/Apply/DeleteTextApply.ts b/packages/core/src/Docs/Apply/DeleteTextApply.ts
index 9d7ea23cdc..760811d7bc 100644
--- a/packages/core/src/Docs/Apply/DeleteTextApply.ts
+++ b/packages/core/src/Docs/Apply/DeleteTextApply.ts
@@ -7,7 +7,7 @@ import {
} from '../../Interfaces/IDocumentData';
import { ITextSelectionRangeParam } from '../../Interfaces/ISelectionData';
import { DocumentModel } from '../Domain/DocumentModel';
-import { deleteContent, getDocsUpdateBody } from './Common';
+import { deleteContent, getDocsUpdateBody, getTextIndexByCursor } from './Common';
export function DeleteTextApply(
document: DocumentModel,
@@ -48,6 +48,10 @@ function deleteText(
) {
const { cursorStart, cursorEnd, isStartBack, isEndBack, isCollapse } = range;
+ const textStart = getTextIndexByCursor(cursorStart, isStartBack);
+
+ const textEnd = getTextIndexByCursor(cursorEnd, isEndBack);
+
const { st, ed } = blockElement;
if (cursorStart > ed || cursorEnd < st) {
@@ -75,7 +79,7 @@ function deleteText(
if (paragraphElementType === ParagraphElementType.TEXT_RUN) {
let relative = textStart - st + 1;
- if (start === 0) {
+ if (textStart === 0) {
relative = 0;
}
diff --git a/packages/core/src/Docs/Apply/InsertTextApply.ts b/packages/core/src/Docs/Apply/InsertTextApply.ts
index ed3b625089..0c921b13e6 100644
--- a/packages/core/src/Docs/Apply/InsertTextApply.ts
+++ b/packages/core/src/Docs/Apply/InsertTextApply.ts
@@ -5,21 +5,22 @@ import {
IParagraph,
ParagraphElementType,
} from '../../Interfaces/IDocumentData';
+import { ITextSelectionRangeStartParam } from '../../Interfaces/ISelectionData';
import { DocumentModel } from '../Domain/DocumentModel';
import {
getDocsUpdateBody,
- getTextStartByAnchor,
insertTextToContent,
+ getTextIndexByCursor,
} from './Common';
export function InsertTextApply(
document: DocumentModel,
- config: { text: string; start: number; length: number; segmentId?: string }
+ text: string,
+ collapseRange: ITextSelectionRangeStartParam
) {
const doc = document.getSnapshot();
- const { text, start, length, segmentId } = config;
- const textStart = getTextStartByAnchor(start);
+ const { segmentId } = collapseRange;
const body = getDocsUpdateBody(doc, segmentId);
@@ -42,26 +43,29 @@ export function InsertTextApply(
switch (blockType) {
case BlockType.PARAGRAPH:
- paragraphApply(
- text,
- start,
- textStart,
- blockElement,
- blockElement.paragraph
- );
+ blockElement.paragraph &&
+ insertText(
+ text,
+ blockElement,
+ blockElement.paragraph,
+ collapseRange
+ );
}
}
}
-function paragraphApply(
+function insertText(
text: string,
- start: number,
- textStart: number,
blockElement: IBlockElement,
- paragraph?: IParagraph
+ paragraph: IParagraph,
+ collapseRange: ITextSelectionRangeStartParam
) {
const { st, ed } = blockElement;
+ const { cursorStart, isStartBack, segmentId } = collapseRange;
+
+ const textStart = getTextIndexByCursor(cursorStart, isStartBack);
+
if (textStart > ed || paragraph == null) {
return;
}
@@ -96,7 +100,7 @@ function paragraphApply(
if (paragraphElementType === ParagraphElementType.TEXT_RUN) {
let relative = textStart - st + 1;
- if (start === 0) {
+ if (textStart === 0) {
relative = 0;
}
diff --git a/packages/core/src/Interfaces/ISelectionData.ts b/packages/core/src/Interfaces/ISelectionData.ts
index 9dec31ed14..756ec07015 100644
--- a/packages/core/src/Interfaces/ISelectionData.ts
+++ b/packages/core/src/Interfaces/ISelectionData.ts
@@ -29,10 +29,13 @@ export interface ICellInfo extends IPosition {
export interface ISelection extends IPosition, IRangeData {}
-export interface ITextSelectionRange {
+export interface ITextSelectionRangeStart {
cursorStart: number;
- cursorEnd: number;
isStartBack: boolean;
+}
+
+export interface ITextSelectionRange extends ITextSelectionRangeStart {
+ cursorEnd: number;
isEndBack: boolean;
isCollapse: boolean;
}
@@ -40,3 +43,7 @@ export interface ITextSelectionRange {
export interface ITextSelectionRangeParam extends ITextSelectionRange {
segmentId?: string; //The ID of the header, footer or footnote the location is in. An empty segment ID signifies the document's body.
}
+
+export interface ITextSelectionRangeStartParam extends ITextSelectionRangeStart {
+ segmentId?: string;
+}