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(doc): editor text Index error fix

DR-Univer56ddb2ffe646
diff --git a/packages/base-docs/src/Controller/InputController.ts b/packages/base-docs/src/Controller/InputController.ts
index 127d526688..00327f1a47 100644
--- a/packages/base-docs/src/Controller/InputController.ts
+++ b/packages/base-docs/src/Controller/InputController.ts
@@ -149,6 +149,8 @@ export class InputController {
const span = document.findNodeByCharIndex(cursor + content.length);
+ this._adjustSelection(document, selectionRemain, span);
+
console.log(
'Compositionupdate',
content,
@@ -162,8 +164,6 @@ export class InputController {
skeleton
);
- this._adjustSelection(document, selectionRemain, span);
-
this._previousIMEContent = content;
});
diff --git a/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts b/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
index dc161ea112..32bfc4a898 100644
--- a/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
+++ b/packages/core/src/Docs/Apply/TextIndexAdjustApply.ts
@@ -69,7 +69,7 @@ function paragraphIndexAdjust(
const { elements } = paragraph;
- const preBlockEnd = preBlockElement ? preBlockElement.ed : 0;
+ const preBlockEnd = preBlockElement ? preBlockElement.ed : -1;
if (cursorEnd < blockStart) {
const moveIndex = preBlockEnd - blockStart + 1;
diff --git a/packages/core/src/Docs/Domain/DocumentModel.ts b/packages/core/src/Docs/Domain/DocumentModel.ts
index f1c2c314ce..0b197b3ab4 100644
--- a/packages/core/src/Docs/Domain/DocumentModel.ts
+++ b/packages/core/src/Docs/Domain/DocumentModel.ts
@@ -44,7 +44,8 @@ export class DocumentModel {
{
DocumentUnit: this,
},
- insertTextAction
+ insertTextAction,
+ this._addTextIndexAdustAction(range, segmentId)
);
_commandManager.invoke(command);
return this;
@@ -60,11 +61,13 @@ export class DocumentModel {
// };
const deleteTextActionList = this._getDeleteTextAction(range, segmentId);
+
const command = new Command(
{
DocumentUnit: this,
},
- ...deleteTextActionList
+ ...deleteTextActionList,
+ this._addTextIndexAdustAction(range, segmentId)
);
_commandManager.invoke(command);
return this;
@@ -119,12 +122,38 @@ export class DocumentModel {
DocumentUnit: this,
},
...deleteTextActionList,
- insertTextAction
+ insertTextAction,
+ this._addTextIndexAdustAction(
+ {
+ cursorStart: start,
+ cursorEnd: start + Math.max(oldText.length, newText.length),
+ isCollapse: false,
+ isEndBack: false,
+ isStartBack: false,
+ },
+ segmentId
+ )
);
_commandManager.invoke(command);
return this;
}
+ private _addTextIndexAdustAction(
+ range: ITextSelectionRange,
+ segmentId?: string
+ ) {
+ const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;
+ return {
+ actionName: DOC_ACTION_NAMES.TEXT_INDEX_ADJUST_ACTION_NAME,
+ cursorStart,
+ cursorEnd,
+ isCollapse,
+ isEndBack,
+ isStartBack,
+ segmentId,
+ };
+ }
+
private _getDeleteTextAction(range: ITextSelectionRange, segmentId?: string) {
const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack } = range;
let actionList = [];
@@ -138,15 +167,15 @@ export class DocumentModel {
return [];
}
- actionList.push({
- actionName: DOC_ACTION_NAMES.TEXT_INDEX_ADJUST_ACTION_NAME,
- cursorStart,
- cursorEnd,
- isCollapse,
- isEndBack,
- isStartBack,
- segmentId,
- });
+ // actionList.push({
+ // actionName: DOC_ACTION_NAMES.TEXT_INDEX_ADJUST_ACTION_NAME,
+ // cursorStart,
+ // cursorEnd,
+ // isCollapse,
+ // isEndBack,
+ // isStartBack,
+ // segmentId,
+ // });
return actionList;
}