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: should not remove the last line break in editor (#753)

Ran Luof3ca2c3ef36f
diff --git a/packages/engine-render/src/components/docs/common/range.ts b/packages/engine-render/src/components/docs/common/range.ts
index 86a50a09e5..1b262e2c3f 100644
--- a/packages/engine-render/src/components/docs/common/range.ts
+++ b/packages/engine-render/src/components/docs/common/range.ts
@@ -90,15 +90,29 @@ export class TextRange {
// The start position of the range
get startOffset() {
const { startOffset } = getOneTextSelectionRange(this._cursorList) ?? {};
+ const body = this._docSkeleton.getViewModel().getBody();
- return startOffset;
+ if (startOffset == null || body == null) {
+ return startOffset;
+ }
+ // The cursor cannot be placed after the last line break
+ const maxLength = body.dataStream.length - 2;
+
+ return Math.min(maxLength, startOffset);
}
// The end position of the range
get endOffset() {
const { endOffset } = getOneTextSelectionRange(this._cursorList) ?? {};
+ const body = this._docSkeleton.getViewModel().getBody();
+
+ if (endOffset == null || body == null) {
+ return endOffset;
+ }
+ // The cursor cannot be placed after the last line break
+ const maxLength = body.dataStream.length - 2;
- return endOffset;
+ return Math.min(endOffset, maxLength);
}
get collapsed() {
diff --git a/packages/engine-render/src/components/docs/text-selection-render-manager.ts b/packages/engine-render/src/components/docs/text-selection-render-manager.ts
index 762a4f7681..db7309c795 100644
--- a/packages/engine-render/src/components/docs/text-selection-render-manager.ts
+++ b/packages/engine-render/src/components/docs/text-selection-render-manager.ts
@@ -344,14 +344,14 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
style: this._selectionStyle,
});
- this._syncDomToSelection();
+ this._updateDomCursorPositionAndSize();
this._scrollToSelection();
}
// Sync canvas selection to dom selection.
sync() {
- this._syncDomToSelection();
+ this._updateDomCursorPositionAndSize();
}
activate(x: number, y: number) {
@@ -587,6 +587,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
const { offsetX: moveOffsetX, offsetY: moveOffsetY } = moveEvt;
scene.setCursor(CURSOR_TYPE.TEXT);
+ // eslint-disable-next-line no-magic-numbers
if (Math.sqrt((moveOffsetX - preMoveOffsetX) ** 2 + (moveOffsetY - preMoveOffsetY) ** 2) < 3) {
return;
}
@@ -614,7 +615,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
scrollTimer.dispose();
- this._syncDomToSelection();
+ this._updateDomCursorPositionAndSize();
});
}
@@ -933,7 +934,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._activeViewport.scrollBy(config);
}
- private _syncDomToSelection() {
+ private _updateDomCursorPositionAndSize() {
const activeRangeInstance = this._getActiveRangeInstance();
const anchor = activeRangeInstance?.getAnchor();
@@ -1035,7 +1036,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
}
}
- this._syncDomToSelection();
+ this._updateDomCursorPositionAndSize();
activeRangeInstance?.deactivateStatic();