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(doc): optimization of text selections (#461)

Ran Luo7c4f41f59bf1
diff --git a/packages/base-docs/src/commands/commands/inline-format.command.ts b/packages/base-docs/src/commands/commands/inline-format.command.ts
index 5480d8a81f..6df96e0ea5 100644
--- a/packages/base-docs/src/commands/commands/inline-format.command.ts
+++ b/packages/base-docs/src/commands/commands/inline-format.command.ts
@@ -1,4 +1,4 @@
-import { ITextRangeWithStyle } from '@univerjs/base-render';
+import { TextRange } from '@univerjs/base-render';
import {
BooleanNumber,
CommandType,
@@ -207,7 +207,7 @@ function isTextDecoration(value: unknown | ITextDecoration): value is ITextDecor
function getReverseFormatValueInSelection(
textRuns: ITextRun[],
key: keyof IStyleBase,
- selections: ITextRangeWithStyle[]
+ selections: TextRange[]
): BooleanNumber | ITextDecoration {
let ti = 0;
let si = 0;
@@ -218,9 +218,9 @@ function getReverseFormatValueInSelection(
// TODO: @jocs handle sid in textRun
const { st, ed, ts } = textRuns[ti];
- if (endOffset <= st) {
+ if (endOffset! <= st) {
si++;
- } else if (ed <= startOffset) {
+ } else if (ed <= startOffset!) {
ti++;
} else {
if (ts?.[key] == null) {
diff --git a/packages/base-docs/src/commands/operations/text-selection.operation.ts b/packages/base-docs/src/commands/operations/text-selection.operation.ts
index bc247f1675..515356cc54 100644
--- a/packages/base-docs/src/commands/operations/text-selection.operation.ts
+++ b/packages/base-docs/src/commands/operations/text-selection.operation.ts
@@ -1,6 +1,6 @@
// This file provide operations to change selection of sheets.
-import { ITextRangeWithStyle } from '@univerjs/base-render';
+import { TextRange } from '@univerjs/base-render';
import { CommandType, IOperation } from '@univerjs/core';
import { TextSelectionManagerService } from '../../services/text-selection-manager.service';
@@ -8,7 +8,7 @@ import { TextSelectionManagerService } from '../../services/text-selection-manag
export interface ISetTextSelectionsOperationParams {
unitId: string;
pluginName: string;
- ranges: ITextRangeWithStyle[];
+ ranges: TextRange[];
}
export const SetTextSelectionsOperation: IOperation<ISetTextSelectionsOperationParams> = {
@@ -17,7 +17,7 @@ export const SetTextSelectionsOperation: IOperation<ISetTextSelectionsOperationP
handler: (accessor, params) => {
const textSelectionManagerService = accessor.get(TextSelectionManagerService);
- textSelectionManagerService.replaceWithNoRefresh(params!.ranges);
+ textSelectionManagerService.replaceTextRangesWithNoRefresh(params!.ranges);
return true;
},
diff --git a/packages/base-docs/src/controllers/delete-left-input.controller.ts b/packages/base-docs/src/controllers/delete-left-input.controller.ts
index 3c33f872aa..34b883aa4a 100644
--- a/packages/base-docs/src/controllers/delete-left-input.controller.ts
+++ b/packages/base-docs/src/controllers/delete-left-input.controller.ts
@@ -163,7 +163,7 @@ export class DeleteLeftInputController extends Disposable {
}
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: cursor,
endOffset: cursor,
diff --git a/packages/base-docs/src/controllers/ime-input.controller.ts b/packages/base-docs/src/controllers/ime-input.controller.ts
index cb6a2943c5..2a92fd3928 100644
--- a/packages/base-docs/src/controllers/ime-input.controller.ts
+++ b/packages/base-docs/src/controllers/ime-input.controller.ts
@@ -113,7 +113,7 @@ export class IMEInputController extends Disposable {
const len = content.length;
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: startOffset + len,
endOffset: endOffset + len,
diff --git a/packages/base-docs/src/controllers/line-break-input.controller.ts b/packages/base-docs/src/controllers/line-break-input.controller.ts
index 42a205e17c..3c05c88aa4 100644
--- a/packages/base-docs/src/controllers/line-break-input.controller.ts
+++ b/packages/base-docs/src/controllers/line-break-input.controller.ts
@@ -103,7 +103,7 @@ export class LineBreakInputController extends Disposable {
skeleton?.calculate();
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: startOffset + 1,
endOffset: startOffset + 1,
diff --git a/packages/base-docs/src/controllers/move-cursor.controller.ts b/packages/base-docs/src/controllers/move-cursor.controller.ts
index e5d2e4b032..daebebe163 100644
--- a/packages/base-docs/src/controllers/move-cursor.controller.ts
+++ b/packages/base-docs/src/controllers/move-cursor.controller.ts
@@ -107,7 +107,7 @@ export class MoveCursorController extends Disposable {
max = Math.max(max, range.endOffset!);
}
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: direction === Direction.LEFT || direction === Direction.UP ? max : min,
endOffset: direction === Direction.LEFT || direction === Direction.UP ? min : max,
@@ -134,7 +134,7 @@ export class MoveCursorController extends Disposable {
focusOffset = Math.min(dataStreamLength - 2, Math.max(0, focusOffset));
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: anchorOffset,
endOffset: focusOffset,
@@ -158,7 +158,7 @@ export class MoveCursorController extends Disposable {
).cursorList[0];
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: anchorOffset,
endOffset: newActiveRange.endOffset,
@@ -206,7 +206,7 @@ export class MoveCursorController extends Disposable {
}
}
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: cursor,
endOffset: cursor,
@@ -230,7 +230,7 @@ export class MoveCursorController extends Disposable {
).cursorList[0];
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
...newActiveRange,
style,
diff --git a/packages/base-docs/src/controllers/normal-input.controller.ts b/packages/base-docs/src/controllers/normal-input.controller.ts
index 301e6741d4..a488a2bc49 100644
--- a/packages/base-docs/src/controllers/normal-input.controller.ts
+++ b/packages/base-docs/src/controllers/normal-input.controller.ts
@@ -80,7 +80,7 @@ export class NormalInputController extends Disposable {
const len = content.length;
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: startOffset + len,
endOffset: startOffset + len,
diff --git a/packages/base-docs/src/controllers/text-selection.controller.ts b/packages/base-docs/src/controllers/text-selection.controller.ts
index 8303f714a5..e091b554f9 100644
--- a/packages/base-docs/src/controllers/text-selection.controller.ts
+++ b/packages/base-docs/src/controllers/text-selection.controller.ts
@@ -1,14 +1,10 @@
import {
CURSOR_TYPE,
- cursorConvertToTextRange,
Documents,
- getOneTextSelectionRange,
IMouseEvent,
IPointerEvent,
IRenderManagerService,
- ITextRangeWithStyle,
ITextSelectionRenderManager,
- NodePositionConvertToCursor,
} from '@univerjs/base-render';
import {
Disposable,
@@ -130,7 +126,7 @@ export class TextSelectionController extends Disposable {
this._textSelectionManagerService.textSelectionInfo$.subscribe((param) => {
const unitId = this._textSelectionManagerService.getCurrentSelection()?.unitId;
// Remove all textRanges.
- this._textSelectionRenderManager.reset();
+ this._textSelectionRenderManager.removeAllTextRanges();
if (param == null || unitId == null) {
return;
@@ -146,19 +142,11 @@ export class TextSelectionController extends Disposable {
const { scene, document } = currentRender;
- for (const selectionWithStyle of param) {
- const textSelection = cursorConvertToTextRange(
- scene,
- selectionWithStyle,
- docSkeleton,
- document.getOffsetConfig()
- );
- this._textSelectionRenderManager.add(textSelection);
- }
-
- this._textSelectionRenderManager.sync();
-
- this._textSelectionRenderManager.scroll();
+ this._textSelectionRenderManager.addTextRanges(param, {
+ scene,
+ skeleton: docSkeleton,
+ documentOffsetConfig: document.getOffsetConfig(),
+ });
});
}
@@ -170,33 +158,12 @@ export class TextSelectionController extends Disposable {
return;
}
- const { skeleton: docSkeleton, unitId } = docsObject;
-
- const document = this._getDocObjectById(unitId)?.document;
-
- if (document == null) {
- return;
- }
-
- const convert = new NodePositionConvertToCursor(document.getOffsetConfig(), docSkeleton);
+ const { unitId } = docsObject;
this._commandService.executeCommand(SetTextSelectionsOperation.id, {
unitId,
pluginName: NORMAL_TEXT_SELECTION_PLUGIN_NAME,
- ranges: textRanges
- .map((textRange) => {
- let { focusNodePosition } = textRange;
- const { anchorNodePosition } = textRange;
-
- if (focusNodePosition == null) {
- focusNodePosition = anchorNodePosition;
- }
-
- const rangeList = convert.getRangePointData(anchorNodePosition, focusNodePosition).cursorList;
-
- return getOneTextSelectionRange(rangeList);
- })
- .filter((x) => x !== null) as ITextRangeWithStyle[],
+ ranges: textRanges,
});
});
}
diff --git a/packages/base-docs/src/services/text-selection-manager.service.ts b/packages/base-docs/src/services/text-selection-manager.service.ts
index 66e80a3de1..8bee373227 100644
--- a/packages/base-docs/src/services/text-selection-manager.service.ts
+++ b/packages/base-docs/src/services/text-selection-manager.service.ts
@@ -1,4 +1,4 @@
-import { ITextRangeWithStyle } from '@univerjs/base-render';
+import { ITextRangeWithStyle, TextRange } from '@univerjs/base-render';
import { ISelectionCell, Nullable } from '@univerjs/core';
import { IDisposable } from '@wendellhu/redi';
import { BehaviorSubject } from 'rxjs';
@@ -11,11 +11,10 @@ export interface ITextSelectionManagerSearchParam {
}
export interface ITextSelectionManagerInsertParam extends ITextSelectionManagerSearchParam {
- textRanges: ITextRangeWithStyle[];
+ textRanges: TextRange[];
}
-//{ [pluginName: string]: { [unitId: string]: ITextRangeWithStyle[] } }
-export type ITextSelectionInfo = Map<string, Map<string, ITextRangeWithStyle[]>>;
+export type ITextSelectionInfo = Map<string, Map<string, TextRange[]>>;
/**
* This service is for selection.
@@ -33,31 +32,6 @@ export class TextSelectionManagerService implements IDisposable {
return this._currentSelection;
}
- reset() {
- if (this._currentSelection == null) {
- return;
- }
-
- this._currentSelection = {
- pluginName: NORMAL_TEXT_SELECTION_PLUGIN_NAME,
- unitId: this._currentSelection?.unitId,
- };
-
- this._textSelectionInfo.clear();
-
- this.refresh(this._currentSelection);
- }
-
- resetPlugin() {
- if (this._currentSelection == null) {
- return;
- }
-
- this._currentSelection.pluginName = NORMAL_TEXT_SELECTION_PLUGIN_NAME;
-
- this.refresh(this._currentSelection);
- }
-
dispose(): void {
this._textSelectionInfo$.complete();
}
@@ -67,13 +41,13 @@ export class TextSelectionManagerService implements IDisposable {
return;
}
- this.refresh(this._currentSelection);
+ this._refresh(this._currentSelection);
}
setCurrentSelection(param: ITextSelectionManagerSearchParam) {
this._currentSelection = param;
- this.refresh(param);
+ this._refresh(param);
}
setCurrentSelectionNotRefresh(param: ITextSelectionManagerSearchParam) {
@@ -84,49 +58,45 @@ export class TextSelectionManagerService implements IDisposable {
return this._textSelectionInfo;
}
- getTextRangesByParam(param: Nullable<ITextSelectionManagerSearchParam>): Readonly<Nullable<ITextRangeWithStyle[]>> {
+ getTextRangesByParam(param: Nullable<ITextSelectionManagerSearchParam>): Readonly<Nullable<TextRange[]>> {
return this._getTextRanges(param);
}
- getSelections(): Readonly<Nullable<ITextRangeWithStyle[]>> {
+ getSelections(): Readonly<Nullable<TextRange[]>> {
return this._getTextRanges(this._currentSelection);
}
- getFirst(): Readonly<Nullable<ITextRangeWithStyle>> {
+ getFirst(): Readonly<Nullable<TextRange>> {
return this._getFirstByParam(this._currentSelection);
}
- getLast(): Readonly<Nullable<ITextRangeWithStyle & { primary: ISelectionCell }>> {
+ getLast(): Readonly<Nullable<TextRange & { primary: ISelectionCell }>> {
// The last selection position must have a primary.
return this._getLastByParam(this._currentSelection) as Readonly<
- Nullable<ITextRangeWithStyle & { primary: ISelectionCell }>
+ Nullable<TextRange & { primary: ISelectionCell }>
>;
}
+ // Only used in tests.
add(textRanges: ITextRangeWithStyle[]) {
if (this._currentSelection == null) {
return;
}
this._addByParam({
...this._currentSelection,
- textRanges,
+ textRanges: textRanges as TextRange[],
});
}
- replace(textRanges: ITextRangeWithStyle[]) {
+ replaceTextRanges(textRanges: ITextRangeWithStyle[]) {
if (this._currentSelection == null) {
return;
}
- this._replaceByParam({
- ...this._currentSelection,
- textRanges,
- });
-
- this.refresh(this._currentSelection);
+ this._textSelectionInfo$.next(textRanges);
}
- replaceWithNoRefresh(textRanges: ITextRangeWithStyle[]) {
+ replaceTextRangesWithNoRefresh(textRanges: TextRange[]) {
if (this._currentSelection == null) {
return;
}
@@ -137,7 +107,27 @@ export class TextSelectionManagerService implements IDisposable {
});
}
- clear(): void {
+ private _addByParam(insertParam: ITextSelectionManagerInsertParam): void {
+ const { pluginName, unitId, textRanges } = insertParam;
+
+ if (!this._textSelectionInfo.has(pluginName)) {
+ this._textSelectionInfo.set(pluginName, new Map());
+ }
+
+ const unitTextRange = this._textSelectionInfo.get(pluginName)!;
+
+ if (!unitTextRange.has(unitId)) {
+ unitTextRange.set(unitId, [...textRanges]);
+ } else {
+ const OldTextRanges = unitTextRange.get(unitId)!;
+ OldTextRanges.push(...textRanges);
+ }
+
+ this._refresh({ pluginName, unitId });
+ }
+
+ // It is will being opened when it needs to be used
+ private clear(): void {
if (this._currentSelection == null) {
return;
}
@@ -145,7 +135,8 @@ export class TextSelectionManagerService implements IDisposable {
this._clearByParam(this._currentSelection);
}
- remove(index: number): void {
+ // It is will being opened when it needs to be used
+ private remove(index: number): void {
if (this._currentSelection == null) {
return;
}
@@ -153,6 +144,33 @@ export class TextSelectionManagerService implements IDisposable {
this._removeByParam(index, this._currentSelection);
}
+ // It is will being opened when it needs to be used
+ private reset() {
+ if (this._currentSelection == null) {
+ return;
+ }
+
+ this._currentSelection = {
+ pluginName: NORMAL_TEXT_SELECTION_PLUGIN_NAME,
+ unitId: this._currentSelection?.unitId,
+ };
+
+ this._textSelectionInfo.clear();
+
+ this._refresh(this._currentSelection);
+ }
+
+ // It is will being opened when it needs to be used
+ private resetPlugin() {
+ if (this._currentSelection == null) {
+ return;
+ }
+
+ this._currentSelection.pluginName = NORMAL_TEXT_SELECTION_PLUGIN_NAME;
+
+ this._refresh(this._currentSelection);
+ }
+
private _getTextRanges(param: Nullable<ITextSelectionManagerSearchParam>) {
if (param == null) {
return;
@@ -163,45 +181,38 @@ export class TextSelectionManagerService implements IDisposable {
return this._textSelectionInfo.get(pluginName)?.get(unitId);
}
- private refresh(param: ITextSelectionManagerSearchParam): void {
- this._textSelectionInfo$.next(this._getTextRanges(param));
+ private _refresh(param: ITextSelectionManagerSearchParam): void {
+ const allTextRanges = this._getTextRanges(param) ?? [];
+
+ this._textSelectionInfo$.next(
+ allTextRanges.map((textRange) => {
+ const startOffset = textRange.startOffset!;
+ const endOffset = textRange.endOffset!;
+ const collapsed = textRange.collapsed!;
+ const style = textRange.style!;
+
+ return {
+ startOffset,
+ endOffset,
+ collapsed,
+ style,
+ };
+ })
+ );
}
- private _getFirstByParam(
- param: Nullable<ITextSelectionManagerSearchParam>
- ): Readonly<Nullable<ITextRangeWithStyle>> {
+ private _getFirstByParam(param: Nullable<ITextSelectionManagerSearchParam>): Readonly<Nullable<TextRange>> {
const textRange = this._getTextRanges(param);
return textRange?.[0];
}
- private _getLastByParam(
- param: Nullable<ITextSelectionManagerSearchParam>
- ): Readonly<Nullable<ITextRangeWithStyle>> {
+ private _getLastByParam(param: Nullable<ITextSelectionManagerSearchParam>): Readonly<Nullable<TextRange>> {
const textRange = this._getTextRanges(param);
return textRange?.[textRange.length - 1];
}
- private _addByParam(insertParam: ITextSelectionManagerInsertParam): void {
- const { pluginName, unitId, textRanges } = insertParam;
-
- if (!this._textSelectionInfo.has(pluginName)) {
- this._textSelectionInfo.set(pluginName, new Map());
- }
-
- const unitTextRange = this._textSelectionInfo.get(pluginName)!;
-
- if (!unitTextRange.has(unitId)) {
- unitTextRange.set(unitId, [...textRanges]);
- } else {
- const OldTextRanges = unitTextRange.get(unitId)!;
- OldTextRanges.push(...textRanges);
- }
-
- this.refresh({ pluginName, unitId });
- }
-
private _replaceByParam(insertParam: ITextSelectionManagerInsertParam) {
const { pluginName, unitId, textRanges } = insertParam;
@@ -217,8 +228,6 @@ export class TextSelectionManagerService implements IDisposable {
const OldTextRanges = unitTextRange.get(unitId)!;
OldTextRanges.splice(0, OldTextRanges.length, ...textRanges);
}
-
- // this.refresh({ pluginName, unitId, sheetId });
}
private _clearByParam(param: ITextSelectionManagerSearchParam): void {
@@ -226,7 +235,7 @@ export class TextSelectionManagerService implements IDisposable {
textRange?.splice(0);
- this.refresh(param);
+ this._refresh(param);
}
private _removeByParam(index: number, param: ITextSelectionManagerSearchParam): void {
@@ -234,6 +243,6 @@ export class TextSelectionManagerService implements IDisposable {
textRange?.splice(index, 1);
- this.refresh(param);
+ this._refresh(param);
}
}
diff --git a/packages/base-render/src/components/docs/common/range.ts b/packages/base-render/src/components/docs/common/range.ts
index 2d5d6a4bf7..d75a6b0eca 100644
--- a/packages/base-render/src/components/docs/common/range.ts
+++ b/packages/base-render/src/components/docs/common/range.ts
@@ -71,7 +71,7 @@ export class TextRange {
private _docSkeleton: DocumentSkeleton,
public anchorNodePosition?: Nullable<INodePosition>,
public focusNodePosition?: Nullable<INodePosition>,
- private _style: ITextSelectionStyle = NORMAL_TEXT_SELECTION_PLUGIN_STYLE
+ public style: ITextSelectionStyle = NORMAL_TEXT_SELECTION_PLUGIN_STYLE
) {}
// The start position of the range
@@ -140,13 +140,13 @@ export class TextRange {
activeStatic() {
this._anchorShape?.setProps({
- stroke: this._style?.strokeActive || getColor(COLORS.black, 1),
+ stroke: this.style?.strokeActive || getColor(COLORS.black, 1),
});
}
deactivateStatic() {
this._anchorShape?.setProps({
- stroke: this._style?.stroke || getColor(COLORS.black, 0),
+ stroke: this.style?.stroke || getColor(COLORS.black, 0),
});
}
@@ -258,7 +258,7 @@ export class TextRange {
const OPACITY = 0.2;
const polygon = new RegularPolygon(TEXT_RANGE_KEY_PREFIX + Tools.generateRandomId(ID_LENGTH), {
pointsGroup,
- fill: this._style?.fill || getColor(COLORS.black, OPACITY),
+ fill: this.style?.fill || getColor(COLORS.black, OPACITY),
left,
top,
evented: false,
@@ -302,8 +302,8 @@ export class TextRange {
left: left + docsLeft,
top: top + docsTop,
height,
- strokeWidth: this._style?.strokeWidth || 1,
- stroke: this._style?.stroke || getColor(COLORS.black, 0),
+ strokeWidth: this.style?.strokeWidth || 1,
+ stroke: this.style?.stroke || getColor(COLORS.black, 0),
evented: false,
});
diff --git a/packages/base-render/src/components/docs/text-selection-render-manager.ts b/packages/base-render/src/components/docs/text-selection-render-manager.ts
index 5453d302ba..64006b7d5f 100644
--- a/packages/base-render/src/components/docs/text-selection-render-manager.ts
+++ b/packages/base-render/src/components/docs/text-selection-render-manager.ts
@@ -21,7 +21,7 @@ import { Engine } from '../../engine';
import { Scene } from '../../scene';
import { ScrollTimer } from '../../scroll-timer';
import { IScrollObserverParam, Viewport } from '../../viewport';
-import { TextRange } from './common/range';
+import { cursorConvertToTextRange, TextRange } from './common/range';
import { DocumentSkeleton } from './doc-skeleton';
import { IDocumentOffsetConfig } from './document';
@@ -43,6 +43,12 @@ export function getCanvasOffsetByEngine(engine: Nullable<Engine>) {
};
}
+interface IAddTextRangesConfig {
+ scene: Scene;
+ skeleton: DocumentSkeleton;
+ documentOffsetConfig: IDocumentOffsetConfig;
+}
+
export interface ITextSelectionRenderManager {
readonly onInputBefore$: Observable<Nullable<IEditorInputConfig>>;
readonly onKeydown$: Observable<Nullable<IEditorInputConfig>>;
@@ -69,11 +75,11 @@ export interface ITextSelectionRenderManager {
getAllTextRanges(): TextRange[];
- add(textSelection: Nullable<TextRange>): void;
+ removeAllTextRanges(): void;
- sync(): void;
+ addTextRanges(ranges: ITextRangeWithStyle[], config?: IAddTextRangesConfig): void;
- scroll(): void;
+ sync(): void;
activate(x: number, y: number): void;
@@ -87,8 +93,6 @@ export interface ITextSelectionRenderManager {
dispose(): void;
- reset(): void;
-
getActiveRange(): Nullable<
ITextRangeWithStyle & {
startNodePosition: Nullable<INodePosition>;
@@ -276,25 +280,35 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
return this._rangeList;
}
- add(textRange: Nullable<TextRange>) {
- if (textRange == null) {
- return;
+ addTextRanges(ranges: ITextRangeWithStyle[], config?: IAddTextRangesConfig) {
+ if (config != null) {
+ const { scene, skeleton, documentOffsetConfig } = config;
+
+ this.changeRuntime(skeleton, scene, null, documentOffsetConfig);
}
- this._addTextRange(textRange);
+ for (const range of ranges) {
+ const textSelection = cursorConvertToTextRange(
+ this._scene!,
+ range,
+ this._docSkeleton!,
+ this._documentOffsetConfig
+ );
- // FIXME: @jocs, why I need to refresh textRange? it already refreshed when create.
- textRange.refresh();
- }
+ this._add(textSelection);
+ }
+
+ this._textSelection$.next(this.getAllTextRanges());
- sync() {
this._syncDomToSelection();
- }
- scroll() {
this._scrollToSelection();
}
+ sync() {
+ this._syncDomToSelection();
+ }
+
activate(x: number, y: number) {
this._container.style.left = `${x}px`;
this._container.style.top = `${y}px`;
@@ -323,7 +337,12 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._cursor.style.display = 'none';
}
- changeRuntime(docSkeleton: DocumentSkeleton, scene: Scene, viewport?: Nullable<Viewport>) {
+ changeRuntime(
+ docSkeleton: DocumentSkeleton,
+ scene: Scene,
+ viewport?: Nullable<Viewport>,
+ documentOffsetConfig?: IDocumentOffsetConfig
+ ) {
// this._docSkeleton?.onRecalculateChangeObservable.remove(this._skeletonObserver);
this._docSkeleton = docSkeleton;
@@ -332,7 +351,9 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._activeViewport = viewport || scene.getViewports()[0];
- // this._attachSelectionEvent(this._documents);
+ if (documentOffsetConfig) {
+ this._documentOffsetConfig = documentOffsetConfig;
+ }
}
// Handle pointer down.
@@ -431,11 +452,22 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
}
- reset() {
+ removeAllTextRanges() {
this._removeAllTextRanges();
this.deactivate();
}
+ private _add(textRange: Nullable<TextRange>) {
+ if (textRange == null) {
+ return;
+ }
+
+ this._addTextRange(textRange);
+
+ // FIXME: @JOCS, why I need to refresh textRange? it already refreshed when create.
+ textRange.refresh();
+ }
+
private getActiveRangeInstance() {
return this._rangeList.find((range) => range.isActive());
}
@@ -896,10 +928,6 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
});
}
- private _handlePaste(e: ClipboardEvent) {
- // TODO: emit the paste event to subscribers
- }
-
private _getTransformCoordForDocumentOffset(evtOffsetX: number, evtOffsetY: number) {
const { documentTransform } = this._documentOffsetConfig;
if (this._activeViewport == null || documentTransform == null) {
diff --git a/packages/ui-plugin-docs/src/controllers/clipboard.controller.ts b/packages/ui-plugin-docs/src/controllers/clipboard.controller.ts
index de5b7855cf..3d2e769681 100644
--- a/packages/ui-plugin-docs/src/controllers/clipboard.controller.ts
+++ b/packages/ui-plugin-docs/src/controllers/clipboard.controller.ts
@@ -114,7 +114,7 @@ export class DocClipboardController extends Disposable {
}
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: cursor,
endOffset: cursor,
@@ -259,7 +259,7 @@ export class DocClipboardController extends Disposable {
}
// move selection
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: cursor,
endOffset: cursor,
diff --git a/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts b/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
index 44f6288a1f..b34aabc4bd 100644
--- a/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/editor/start-edit.controller.ts
@@ -161,7 +161,7 @@ export class StartEditController extends Disposable {
this._textSelectionRenderManager.changeRuntime(documentSkeleton, scene);
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: 0,
endOffset: 0,
@@ -454,7 +454,7 @@ export class StartEditController extends Disposable {
skeleton.calculate();
}
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: 0,
endOffset: 0,
@@ -469,7 +469,7 @@ export class StartEditController extends Disposable {
y: Infinity,
});
- this._textSelectionManagerService.replace([
+ this._textSelectionManagerService.replaceTextRanges([
{
startOffset: cursor,
endOffset: cursor,
diff --git a/packages/ui-plugin-sheets/src/controllers/selection.controller.ts b/packages/ui-plugin-sheets/src/controllers/selection.controller.ts
index 8e419452a5..c4f3757e05 100644
--- a/packages/ui-plugin-sheets/src/controllers/selection.controller.ts
+++ b/packages/ui-plugin-sheets/src/controllers/selection.controller.ts
@@ -101,10 +101,13 @@ export class SelectionController extends Disposable {
this._selectionRenderService.resetStyle();
const param = this._selectionManagerService.getSelections();
const current = this._selectionManagerService.getCurrent();
+
if (param == null || current?.pluginName !== NORMAL_SELECTION_PLUGIN_NAME) {
return;
}
+
this._selectionRenderService.reset();
+
for (const selectionWithStyle of param) {
if (selectionWithStyle == null) {
continue;