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: move cursor at the beginning of docment (#746)

Ran Luoa2020c38aa0d
diff --git a/packages/docs/src/controllers/move-cursor.controller.ts b/packages/docs/src/controllers/move-cursor.controller.ts
index 3377928545..5f2873d99e 100644
--- a/packages/docs/src/controllers/move-cursor.controller.ts
+++ b/packages/docs/src/controllers/move-cursor.controller.ts
@@ -137,10 +137,11 @@ export class MoveCursorController extends Disposable {
const dataStreamLength = docDataModel.getBody()!.dataStream.length ?? Infinity;
if (direction === Direction.LEFT || direction === Direction.RIGHT) {
- const preSpan = skeleton.findNodeByCharIndex(focusOffset - 1)!;
+ const preSpan = skeleton.findNodeByCharIndex(focusOffset - 1);
const curSpan = skeleton.findNodeByCharIndex(focusOffset)!;
- focusOffset = direction === Direction.RIGHT ? focusOffset + curSpan.count : focusOffset - preSpan.count;
+ focusOffset =
+ direction === Direction.RIGHT ? focusOffset + curSpan.count : focusOffset - (preSpan?.count ?? 0);
focusOffset = Math.min(dataStreamLength - 2, Math.max(0, focusOffset));
@@ -223,11 +224,11 @@ export class MoveCursorController extends Disposable {
cursor = direction === Direction.LEFT ? min : max;
} else {
- const preSpan = skeleton.findNodeByCharIndex(startOffset - 1)!;
+ const preSpan = skeleton.findNodeByCharIndex(startOffset - 1);
const curSpan = skeleton.findNodeByCharIndex(startOffset)!;
if (direction === Direction.LEFT) {
- cursor = Math.max(0, startOffset - preSpan.count);
+ cursor = Math.max(0, startOffset - (preSpan?.count ?? 0));
} else {
// -1 because the length of the string will be 1 larger than the index, and the reason for subtracting another 1 is because it ends in \n
cursor = Math.min(dataStreamLength - 2, endOffset + curSpan.count);