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: cursor size follow anchor glyph (#2365)

Ran Luo9d8bb89a9a0e
diff --git a/packages/engine-render/src/components/docs/text-selection/convert-cursor.ts b/packages/engine-render/src/components/docs/text-selection/convert-cursor.ts
index 6fa02d2d7c..d55db12f74 100644
--- a/packages/engine-render/src/components/docs/text-selection/convert-cursor.ts
+++ b/packages/engine-render/src/components/docs/text-selection/convert-cursor.ts
@@ -210,7 +210,6 @@ export class NodePositionConvertToCursor {
getRangePointData(startOrigin: Nullable<INodePosition>, endOrigin: Nullable<INodePosition>) {
const borderBoxPointGroup: IPoint[][] = [];
const contentBoxPointGroup: IPoint[][] = [];
-
const cursorList: ITextRange[] = [];
if (startOrigin == null || endOrigin == null) {
@@ -224,25 +223,24 @@ export class NodePositionConvertToCursor {
const { start, end } = compareNodePosition(startOrigin, endOrigin);
this._selectionIterator(start, end, (start_sp, end_sp, isFirst, isLast, divide, line) => {
- const { lineHeight, marginTop, contentHeight } = line;
-
+ const { lineHeight, marginTop, asc } = line;
const { glyphGroup, st } = divide;
-
const { x: startX, y: startY } = this._liquid;
let borderBoxPosition: IPosition;
let contentBoxPosition: IPosition;
- const firstSpan = glyphGroup[start_sp];
- const lastSpan = glyphGroup[end_sp];
+ const firstGlyph = glyphGroup[start_sp];
+ const lastGlyph = glyphGroup[end_sp];
+ const preGlyph = glyphGroup[start_sp - 1];
- const firstSpanLeft = firstSpan?.left || 0;
- const firstSpanWidth = firstSpan?.width || 0;
+ const firstGlyphLeft = firstGlyph?.left || 0;
+ const firstGlyphWidth = firstGlyph?.width || 0;
- const lastSpanLeft = lastSpan?.left || 0;
- const lastSpanWidth = lastSpan?.width || 0;
+ const lastGlyphLeft = lastGlyph?.left || 0;
+ const lastGlyphWidth = lastGlyph?.width || 0;
- const isCurrentList = firstSpan?.glyphType === GlyphType.LIST;
+ const isCurrentList = firstGlyph?.glyphType === GlyphType.LIST;
const { startOffset, endOffset } = getOffsetInDivide(glyphGroup, start_sp, end_sp, st);
@@ -251,36 +249,37 @@ export class NodePositionConvertToCursor {
const isEndBack = end.glyph === end_sp && isLast ? end.isBack : false;
const collapsed = start === end;
+ const anchorGlyph = isStartBack ? (preGlyph ?? firstGlyph) : firstGlyph;
if (start_sp === 0 && end_sp === glyphGroup.length - 1) {
borderBoxPosition = {
- startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),
+ startX: startX + firstGlyphLeft + (isCurrentList ? firstGlyphWidth : 0),
startY,
- endX: startX + lastSpanLeft + lastSpanWidth,
+ endX: startX + lastGlyphLeft + lastGlyphWidth,
endY: startY + lineHeight,
};
contentBoxPosition = {
- startX: startX + firstSpanLeft + (isCurrentList ? firstSpanWidth : 0),
- startY: startY + marginTop,
- endX: startX + lastSpanLeft + lastSpanWidth,
- endY: startY + marginTop + contentHeight,
+ startX: startX + firstGlyphLeft + (isCurrentList ? firstGlyphWidth : 0),
+ startY: startY + marginTop + asc - anchorGlyph.bBox.ba,
+ endX: startX + lastGlyphLeft + lastGlyphWidth,
+ endY: startY + marginTop + asc + anchorGlyph.bBox.bd,
};
} else {
const isStartBackFin = isStartBack && !isCurrentList;
borderBoxPosition = {
- startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),
+ startX: startX + firstGlyphLeft + (isStartBackFin ? 0 : firstGlyphWidth),
startY,
- endX: startX + lastSpanLeft + (isEndBack ? 0 : lastSpanWidth),
+ endX: startX + lastGlyphLeft + (isEndBack ? 0 : lastGlyphWidth),
endY: startY + lineHeight,
};
contentBoxPosition = {
- startX: startX + firstSpanLeft + (isStartBackFin ? 0 : firstSpanWidth),
- startY: startY + marginTop,
- endX: startX + lastSpanLeft + (isEndBack ? 0 : lastSpanWidth),
- endY: startY + marginTop + contentHeight,
+ startX: startX + firstGlyphLeft + (isStartBackFin ? 0 : firstGlyphWidth),
+ startY: startY + marginTop + asc - anchorGlyph.bBox.ba,
+ endX: startX + lastGlyphLeft + (isEndBack ? 0 : lastGlyphWidth),
+ endY: startY + marginTop + asc + anchorGlyph.bBox.bd,
};
}
@@ -288,8 +287,8 @@ export class NodePositionConvertToCursor {
contentBoxPointGroup.push(this._pushToPoints(contentBoxPosition));
cursorList.push({
- startOffset: isStartBack ? startOffset : startOffset + firstSpan.count,
- endOffset: isEndBack ? endOffset : endOffset + lastSpan.count,
+ startOffset: isStartBack ? startOffset : startOffset + firstGlyph.count,
+ endOffset: isEndBack ? endOffset : endOffset + lastGlyph.count,
collapsed,
});
});
@@ -546,7 +545,7 @@ export class NodePositionConvertToCursor {
this._liquid.translateSave();
this._liquid.translateDivide(divide);
- const glyphGroup = divide.glyphGroup;
+ const { glyphGroup } = divide;
const { start_next: start_sp, end_next: end_sp } = this._getSelectionRuler(
NodePositionMap.divide,
@@ -557,7 +556,6 @@ export class NodePositionConvertToCursor {
);
let isFirst = false;
-
let isLast = false;
if (p === pageIndex && s === start_s && c === start_c && l === start_l && d === start_d) {
diff --git a/packages/engine-render/src/components/docs/text-selection/text-range.ts b/packages/engine-render/src/components/docs/text-selection/text-range.ts
index f8e055e23c..f8ff819eea 100644
--- a/packages/engine-render/src/components/docs/text-selection/text-range.ts
+++ b/packages/engine-render/src/components/docs/text-selection/text-range.ts
@@ -16,7 +16,6 @@
import type { ITextRange, Nullable } from '@univerjs/core';
import { COLORS, Tools } from '@univerjs/core';
-
import type { INodePosition } from '../../../basics/interfaces';
import type { ISuccinctTextRangeParam, ITextSelectionStyle } from '../../../basics/range';
import { NORMAL_TEXT_SELECTION_PLUGIN_STYLE, RANGE_DIRECTION } from '../../../basics/range';
@@ -37,11 +36,9 @@ import {
} from './convert-cursor';
const TEXT_RANGE_KEY_PREFIX = '__TestSelectionRange__';
-
const TEXT_ANCHOR_KEY_PREFIX = '__TestSelectionAnchor__';
const ID_LENGTH = 6;
-
const BLINK_ON = 500;
const BLINK_OFF = 500;