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.

refactor: update dom cursor to canvas cursor (#1442)

Ran Luo0011d7f94c7d
diff --git a/packages/engine-render/src/basics/range.ts b/packages/engine-render/src/basics/range.ts
index 5dabfe74fe..8db26f8d8f 100644
--- a/packages/engine-render/src/basics/range.ts
+++ b/packages/engine-render/src/basics/range.ts
@@ -25,8 +25,8 @@ export interface ITextSelectionStyle {
export const NORMAL_TEXT_SELECTION_PLUGIN_STYLE: ITextSelectionStyle = {
strokeWidth: 1,
- stroke: 'rgba(0,0,0, 0)',
- strokeActive: 'rgba(0,0,0, 1)',
+ stroke: 'rgba(0, 0, 0, 0)',
+ strokeActive: 'rgba(0, 0, 0, 1)',
fill: 'rgba(0, 0, 0, 0.2)',
};
diff --git a/packages/engine-render/src/components/docs/common/text-range.ts b/packages/engine-render/src/components/docs/common/text-range.ts
index 2367330869..2c8bb2ca86 100644
--- a/packages/engine-render/src/components/docs/common/text-range.ts
+++ b/packages/engine-render/src/components/docs/common/text-range.ts
@@ -42,6 +42,11 @@ const TEXT_ANCHOR_KEY_PREFIX = '__TestSelectionAnchor__';
const ID_LENGTH = 6;
+const BLINK_ON = 500;
+const BLINK_OFF = 500;
+
+export const TEXT_RANGE_LAYER_INDEX = 1;
+
export function cursorConvertToTextRange(
scene: Scene,
range: ISuccinctTextRangeParam,
@@ -70,6 +75,8 @@ export class TextRange {
private _cursorList: ITextRange[] = [];
+ private _anchorBlinkTimer: Nullable<ReturnType<typeof setInterval>> = null;
+
constructor(
private _scene: ThinScene,
private _document: Documents,
@@ -77,7 +84,31 @@ export class TextRange {
public anchorNodePosition?: Nullable<INodePosition>,
public focusNodePosition?: Nullable<INodePosition>,
public style: ITextSelectionStyle = NORMAL_TEXT_SELECTION_PLUGIN_STYLE
- ) {}
+ ) {
+ this._anchorBlink();
+ }
+
+ private _anchorBlink() {
+ setTimeout(() => {
+ if (this._anchorShape) {
+ if (this._anchorShape.visible) {
+ this.deactivateStatic();
+ }
+ }
+ }, BLINK_ON);
+
+ this._anchorBlinkTimer = setInterval(() => {
+ if (this._anchorShape) {
+ if (this._anchorShape.visible) {
+ this.activeStatic();
+
+ setTimeout(() => {
+ this.deactivateStatic();
+ }, BLINK_ON);
+ }
+ }
+ }, BLINK_OFF + BLINK_ON);
+ }
// The start position of the range
get startOffset() {
@@ -186,6 +217,11 @@ export class TextRange {
this._rangeShape = null;
this._anchorShape?.dispose();
this._anchorShape = null;
+
+ if (this._anchorBlinkTimer) {
+ clearInterval(this._anchorBlinkTimer);
+ this._anchorBlinkTimer = null;
+ }
}
isIntersection(compareRange: TextRange) {
@@ -288,7 +324,7 @@ export class TextRange {
this._rangeShape = polygon;
- this._scene.addObject(polygon, 2);
+ this._scene.addObject(polygon, TEXT_RANGE_LAYER_INDEX);
}
private _getAnchorBounding(pointsGroup: IPoint[][]) {
@@ -324,13 +360,13 @@ export class TextRange {
top: top + docsTop,
height,
strokeWidth: this.style?.strokeWidth || 1,
- stroke: this.style?.stroke || getColor(COLORS.black, 0),
+ stroke: this.style?.strokeActive || getColor(COLORS.black, 1),
evented: false,
});
this._anchorShape = anchor;
- this._scene.addObject(anchor, 2);
+ this._scene.addObject(anchor, TEXT_RANGE_LAYER_INDEX);
}
private _setCursorList(cursorList: ITextRange[]) {
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 0878a90301..a8d6d0ea27 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
@@ -34,7 +34,6 @@ import type {
} from '../../basics/range';
import { NORMAL_TEXT_SELECTION_PLUGIN_STYLE } from '../../basics/range';
import { getCurrentScrollXY } from '../../basics/scroll-xy';
-import { checkStyle, injectStyle } from '../../basics/tools';
import { Vector2 } from '../../basics/vector2';
import type { Engine } from '../../engine';
import type { Scene } from '../../scene';
@@ -42,7 +41,7 @@ import { ScrollTimer } from '../../scroll-timer';
import type { IScrollObserverParam, Viewport } from '../../viewport';
import { cursorConvertToTextRange, TextRange } from './common/text-range';
import type { DocumentSkeleton } from './doc-skeleton';
-import type { Documents, IDocumentOffsetConfig } from './document';
+import type { Documents } from './document';
export function getCanvasOffsetByEngine(engine: Nullable<Engine>) {
const canvas = engine?.getCanvasElement();
@@ -99,12 +98,6 @@ function getParagraphInfoBySpan(node: IDocumentSkeletonSpan) {
};
}
-interface IAddTextRangesConfig {
- scene: Scene;
- skeleton: DocumentSkeleton;
- documentOffsetConfig: IDocumentOffsetConfig;
-}
-
export interface ITextSelectionInnerParam {
textRanges: TextRange[];
segmentId: string;
@@ -221,8 +214,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
private _input!: HTMLDivElement;
- private _cursor!: HTMLDivElement;
-
private _moveObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
private _upObserver: Nullable<Observer<IPointerEvent | IMouseEvent>>;
@@ -307,14 +298,14 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
isEditing,
});
- this._updateDomCursorPositionAndSize();
+ this._updateInputPosition();
this._scrollToSelection();
}
// Sync canvas selection to dom selection.
sync() {
- this._updateDomCursorPositionAndSize();
+ this._updateInputPosition();
}
activate(x: number, y: number) {
@@ -322,12 +313,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._container.style.top = `${y}px`;
this._container.style.zIndex = '1000';
- this._cursor.style.animation = 'univer_cursor_blinkStyle 1s steps(1) infinite';
- this._cursor.style.display = 'revert';
-
this.focus();
-
- // requestAnimationFrame(() => this.focus());
}
hasFocus(): boolean {
@@ -346,9 +332,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
deactivate() {
this._container.style.left = '0px';
this._container.style.top = '0px';
-
- this._cursor.style.animation = '';
- this._cursor.style.display = 'none';
}
changeRuntime(docSkeleton: DocumentSkeleton, scene: Scene, document: Documents) {
@@ -543,7 +526,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
scrollTimer.dispose();
- this._updateDomCursorPositionAndSize();
+ this._updateInputPosition();
});
}
@@ -612,19 +595,15 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
const inputParent = document.createElement('div');
const inputDom = document.createElement('div');
- const cursorDom = document.createElement('div');
inputParent.appendChild(inputDom);
container.appendChild(inputParent);
- container.appendChild(cursorDom);
this._container = container;
this._inputParent = inputParent;
this._input = inputDom;
- this._cursor = cursorDom;
this._initInput();
- this._initDOMCursor();
this._initInputEvents();
document.body.appendChild(container);
@@ -655,74 +634,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
`;
}
- private _initDOMCursor() {
- this._cursor.style.cssText = `
- visibility: visible;
- position: absolute;
- background: #000;
- left: 0;
- top: 0;
- width: 1px;
- height: 20px;
- opacity: 0;
- z-index: 0;
- pointer-events: none;
- display: none
- `;
-
- if (!checkStyle('keyframes univer_cursor_blinkStyle')) {
- const styles = [
- `
- @-webkit-keyframes univer_cursor_blinkStyle {
- 0% {
- opacity: 1;
- }
-
- 13% {
- opacity: 0;
- }
-
- 50% {
- opacity: 0;
- }
-
- 63% {
- opacity: 1;
- }
-
- 100% {
- opacity: 1;
- }
- }
- `,
- `
- @keyframes univer_cursor_blinkStyle {
- 0% {
- opacity: 1;
- }
-
- 13% {
- opacity: 0;
- }
-
- 50% {
- opacity: 0;
- }
-
- 63% {
- opacity: 1;
- }
-
- 100% {
- opacity: 1;
- }
- }
- `,
- ];
- injectStyle(styles);
- }
- }
-
private _getNodePosition(node: Nullable<INodeInfo>) {
if (node == null) {
return;
@@ -888,7 +799,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._activeViewport.scrollBy(config);
}
- private _updateDomCursorPositionAndSize() {
+ private _updateInputPosition() {
const activeRangeInstance = this._getActiveRangeInstance();
const anchor = activeRangeInstance?.getAnchor();
@@ -897,16 +808,12 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return;
}
- const { height, left, top } = anchor;
+ const { left, top } = anchor;
const absoluteCoord = this._activeViewport.getAbsoluteVector(Vector2.FromArray([left, top]));
- const { scaleY } = this._scene?.getAncestorScale() || { scaleX: 1, scaleY: 1 };
-
const { x, y } = absoluteCoord;
- this._cursor.style.height = `${height * scaleY}px`;
-
let { left: canvasLeft, top: canvasTop } = this._getCanvasOffset();
canvasLeft += x;
@@ -966,8 +873,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
const activeRangeInstance = this._getActiveRangeInstance();
activeRangeInstance?.activeStatic();
-
- this._cursor.style.display = 'none';
});
const scrollStop = viewport.onScrollStopObserver.add((param: IScrollObserverParam) => {
@@ -995,11 +900,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
}
}
- this._updateDomCursorPositionAndSize();
-
- activeRangeInstance?.deactivateStatic();
-
- this._cursor.style.display = 'revert';
+ this._updateInputPosition();
});
this._viewPortObserverMap.set(key, {