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(sheet): some editor error (#312)

Univerfb681a6535e9
diff --git a/packages/base-docs/src/Controller/normal-input.controller.ts b/packages/base-docs/src/Controller/normal-input.controller.ts
index be02a9ac87..7e63448a2d 100644
--- a/packages/base-docs/src/Controller/normal-input.controller.ts
+++ b/packages/base-docs/src/Controller/normal-input.controller.ts
@@ -43,7 +43,7 @@ export class NormalInputController extends Disposable {
}
private _initialNormalInput() {
- this._onInputSubscription = this._textSelectionRenderManager.onInput$.subscribe((config) => {
+ this._onInputSubscription = this._textSelectionRenderManager.onInput$.subscribe(async (config) => {
if (config == null) {
return;
}
@@ -66,7 +66,7 @@ export class NormalInputController extends Disposable {
const { cursorStart, cursorEnd, isCollapse, isEndBack, isStartBack, segmentId, style } = activeRange;
- this._commandService.executeCommand(InsertCommand.id, {
+ await this._commandService.executeCommand(InsertCommand.id, {
unitId: documentModel.getUnitId(),
body: {
dataStream: content,
diff --git a/packages/base-docs/src/commands/mutations/functions/delete-apply.ts b/packages/base-docs/src/commands/mutations/functions/delete-apply.ts
index 1314735305..77683c4c8f 100644
--- a/packages/base-docs/src/commands/mutations/functions/delete-apply.ts
+++ b/packages/base-docs/src/commands/mutations/functions/delete-apply.ts
@@ -43,7 +43,7 @@ function updateAttributeByDelete(body: IDocumentBody, textLength: number, curren
const startIndex = currentIndex;
- const endIndex = currentIndex + textLength - 1;
+ const endIndex = currentIndex + textLength;
const removeTextRuns = deleteTextRuns(body, textLength, currentIndex);
diff --git a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
index 207b0d60e2..a202c468aa 100644
--- a/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
+++ b/packages/base-render/src/Component/Docs/text-selection-render-manager.ts
@@ -24,6 +24,7 @@ import { DocumentSkeleton } from './DocSkeleton';
import { IDocumentOffsetConfig } from './Document';
export interface ITextSelectionRenderManager {
+ readonly onInputBefore$: Observable<Nullable<IEditorInputConfig>>;
readonly onKeydown$: Observable<Nullable<IEditorInputConfig>>;
readonly onInput$: Observable<Nullable<IEditorInputConfig>>;
readonly onCompositionstart$: Observable<Nullable<IEditorInputConfig>>;
@@ -58,11 +59,13 @@ export interface ITextSelectionRenderManager {
scroll(): void;
- active(x: number, y: number): void;
+ activate(x: number, y: number): void;
focus(): void;
- deactivate(withoutBlur: boolean): void;
+ blur(): void;
+
+ deactivate(): void;
changeRuntime(docSkeleton: DocumentSkeleton, scene: Scene): void;
@@ -70,8 +73,6 @@ export interface ITextSelectionRenderManager {
reset(): void;
- activate(toLastPosition: boolean): void;
-
getRanges(): Array<Nullable<ITextSelectionRangeWithStyle>>;
getActiveRange(): Nullable<ITextSelectionRangeWithStyle>;
@@ -103,6 +104,10 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
// onSelectionStartObservable = new Observable<Nullable<INodePosition>>();
+ private readonly _onInputBefore$ = new BehaviorSubject<Nullable<IEditorInputConfig>>(null);
+
+ readonly onInputBefore$ = this._onInputBefore$.asObservable();
+
private readonly _onKeydown$ = new BehaviorSubject<Nullable<IEditorInputConfig>>(null);
readonly onKeydown$ = this._onKeydown$.asObservable();
@@ -293,48 +298,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
};
}
- activate(toLastPosition = false): void {
- // const documents = this._documents;
- // if (!documents) {
- // return;
- // }
- // setTimeout(() => {
- // documents.makeDirty();
- // });
- // FIXME: I don't know if this is correct. Just trying it.
- // const lastCharIndex = documents.getSkeleton()!.getModel().bodyModel.body.dataStream.length;
- // const span = documents.findNodeByCharIndex(lastCharIndex - 1);
- // const position = documents.findPositionBySpan(span!);
- // const remain = this.remain();
- // if (position && remain) {
- // remain.startNodePosition = { ...position, isBack: true };
- // remain.endNodePosition = undefined;
- // }
- // if (!this._documents) {
- // return;
- // }
- // TODO
- // this.activeViewport = this._documents.getActiveViewportByCoord(evtOffsetX, evtOffsetY);
- // const startNode = this._documents.findNodeByCoord(evtOffsetX, evtOffsetY);
- // console.log('startNode', startNode, position, evtOffsetX, evtOffsetY);
- // if (position == null) {
- // this._deleteAllTextSelection();
- // return;
- // }
- // if (startNode?.node.streamType === DataStreamTreeTokenType.PARAGRAPH) {
- // position.isBack = true;
- // }
- // if (evt.ctrlKey || this._isEmptyTextSelection()) {
- // const newTextSelection = new TextSelection(this._documents.getScene(), position);
- // this._addTextSelection(newTextSelection);
- // } else {
- // this._updateTextSelection(position);
- // }
- // this._activeSelectionRefresh();
- // this._syncDomToSelection();
- }
-
- active(x: number, y: number) {
+ activate(x: number, y: number) {
this._container.style.left = `${x}px`;
this._container.style.top = `${y}px`;
this._container.style.zIndex = `1000`;
@@ -343,7 +307,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._cursor.style.display = 'revert';
setTimeout(() => {
- this._input.focus();
+ this.focus();
}, 0);
}
@@ -351,17 +315,21 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._input.focus();
}
+ blur() {
+ this._input.blur();
+ }
+
// FIXME: for editor cell editor we don't need to blur the input element
- deactivate(withoutBlur: boolean = true) {
+ deactivate() {
this._container.style.left = `0px`;
this._container.style.top = `0px`;
this._cursor.style.animation = '';
this._cursor.style.display = 'none';
- if (!withoutBlur) {
- this._input.blur();
- }
+ // if (!withoutBlur) {
+ // this._input.blur();
+ // }
}
changeRuntime(docSkeleton: DocumentSkeleton, scene: Scene, viewport?: Nullable<Viewport>) {
@@ -749,6 +717,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
const activeTextSelection = this.getActiveTextSelection();
const anchor = activeTextSelection?.getAnchor();
if (!anchor || (anchor && !anchor.visible) || this._activeViewport == null) {
+ this.focus();
return;
}
@@ -768,7 +737,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
canvasTop += y;
- this.active(canvasLeft, canvasTop);
+ this.activate(canvasLeft, canvasTop);
}
private _moving(moveOffsetX: number, moveOffsetY: number, scrollTimer: ScrollTimer) {
@@ -884,6 +853,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
}
this._eventHandle(e, (config) => {
+ this._onInputBefore$.next(config);
this._onInput$.next(config);
});
});
@@ -906,6 +876,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
this._input.addEventListener('compositionupdate', (e) => {
this._eventHandle(e, (config) => {
+ this._onInputBefore$.next(config);
this._onCompositionupdate$.next(config);
});
});
@@ -969,6 +940,7 @@ export class TextSelectionRenderManager extends RxDisposable implements ITextSel
// documents.onPointerLeaveObserver.remove(this._moveOutObserver);
// documents.onPointerDownObserver.remove(this._downObserver);
// this._docSkeleton?.onRecalculateChangeObservable.remove(this._skeletonObserver);
+ this._onInputBefore$.complete();
this._onKeydown$.complete();
this._onInput$.complete();
this._onCompositionstart$.complete();
diff --git a/packages/base-sheets/src/Controller/Selection.controller.ts b/packages/base-sheets/src/Controller/Selection.controller.ts
index 90db5c0ac7..1bae088c2a 100644
--- a/packages/base-sheets/src/Controller/Selection.controller.ts
+++ b/packages/base-sheets/src/Controller/Selection.controller.ts
@@ -18,6 +18,7 @@ import { Inject } from '@wendellhu/redi';
import { getSheetObject, ISheetObjectParam } from '../Basics/component-tools';
import { VIEWPORT_KEY } from '../Basics/Const/DEFAULT_SPREADSHEET_VIEW';
+import { SetWorksheetActivateMutation } from '../commands/mutations/set-worksheet-activate.mutation';
import { SetSelectionsOperation } from '../commands/operations/selection.operation';
import { ISetZoomRatioOperationParams, SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';
import { NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '../services/selection-manager.service';
@@ -192,7 +193,11 @@ export class SelectionController extends Disposable {
if (param == null) {
return;
}
- const { unitId, sheetId, skeleton } = param;
+ const { unitId, sheetId, skeleton, commandId } = param;
+
+ if (commandId != null && commandId !== SetWorksheetActivateMutation.id) {
+ return;
+ }
const currentRender = this._renderManagerService.getRenderById(unitId);
diff --git a/packages/base-sheets/src/Controller/sheet-render.controller.ts b/packages/base-sheets/src/Controller/sheet-render.controller.ts
index 43e6367263..194c9848a6 100644
--- a/packages/base-sheets/src/Controller/sheet-render.controller.ts
+++ b/packages/base-sheets/src/Controller/sheet-render.controller.ts
@@ -39,9 +39,6 @@ interface ISetWorksheetMutationParams {
@OnLifecycle(LifecycleStages.Rendered, SheetRenderController)
export class SheetRenderController extends Disposable {
- private _previousUnitId: string = '';
- private _previousSheetId: string = '';
-
constructor(
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService,
@IUniverInstanceService private readonly _currentUniverService: IUniverInstanceService,
@@ -84,9 +81,6 @@ export class SheetRenderController extends Disposable {
return;
}
- this._previousUnitId = unitId;
- this._previousSheetId = sheetId;
-
const { mainComponent, components, scene } = currentRender;
const spreadsheet = mainComponent as Spreadsheet;
@@ -140,19 +134,12 @@ export class SheetRenderController extends Disposable {
return;
}
- if (this._previousUnitId === unitId && this._previousSheetId === sheetId) {
- return;
- }
-
- this._previousUnitId = unitId;
-
- this._previousSheetId = sheetId;
-
if (command.id !== SetWorksheetActivateMutation.id) {
this._sheetSkeletonManagerService.makeDirty(
{
unitId,
sheetId,
+ commandId: command.id,
},
true
);
@@ -161,6 +148,7 @@ export class SheetRenderController extends Disposable {
this._sheetSkeletonManagerService.setCurrent({
unitId,
sheetId,
+ commandId: command.id,
});
}
diff --git a/packages/base-sheets/src/services/sheet-skeleton-manager.service.ts b/packages/base-sheets/src/services/sheet-skeleton-manager.service.ts
index 843c4b3767..13a806ce42 100644
--- a/packages/base-sheets/src/services/sheet-skeleton-manager.service.ts
+++ b/packages/base-sheets/src/services/sheet-skeleton-manager.service.ts
@@ -8,11 +8,13 @@ export interface ISheetSkeletonManagerParam {
sheetId: string;
skeleton: SpreadsheetSkeleton;
dirty: boolean;
+ commandId?: string;
}
export interface ISheetSkeletonManagerSearch {
unitId: string;
sheetId: string;
+ commandId?: string;
}
/**
@@ -52,8 +54,8 @@ export class SheetSkeletonManagerService implements IDisposable {
return this._getCurrentBySearch(this._currentSkeleton);
}
- setCurrent(searchParm: ISheetSkeletonManagerSearch): Nullable<ISheetSkeletonManagerParam> {
- const param = this._getCurrentBySearch(searchParm);
+ setCurrent(searchParam: ISheetSkeletonManagerSearch): Nullable<ISheetSkeletonManagerParam> {
+ const param = this._getCurrentBySearch(searchParam);
if (param != null) {
if (param.dirty) {
param.skeleton.makeDirty(true);
@@ -61,11 +63,11 @@ export class SheetSkeletonManagerService implements IDisposable {
}
param.skeleton.calculate();
} else {
- const { unitId, sheetId } = searchParm;
+ const { unitId, sheetId } = searchParam;
- const workbook = this._currentUniverService.getUniverSheetInstance(searchParm.unitId);
+ const workbook = this._currentUniverService.getUniverSheetInstance(searchParam.unitId);
- const worksheet = workbook?.getSheetBySheetId(searchParm.sheetId);
+ const worksheet = workbook?.getSheetBySheetId(searchParam.sheetId);
if (worksheet == null || workbook == null) {
return;
@@ -81,11 +83,13 @@ export class SheetSkeletonManagerService implements IDisposable {
});
}
- this._currentSkeleton = searchParm;
+ this._currentSkeleton = searchParam;
+
+ const nextParam = this.getCurrent();
- this._currentSkeletonBefore$.next(this.getCurrent());
+ this._currentSkeletonBefore$.next(nextParam);
- this._currentSkeleton$.next(this.getCurrent());
+ this._currentSkeleton$.next(nextParam);
return this.getCurrent();
}
@@ -103,9 +107,15 @@ export class SheetSkeletonManagerService implements IDisposable {
}
private _getCurrentBySearch(searchParm: ISheetSkeletonManagerSearch): Nullable<ISheetSkeletonManagerParam> {
- return this._sheetSkeletonParam.find(
+ const item = this._sheetSkeletonParam.find(
(param) => param.unitId === searchParm.unitId && param.sheetId === searchParm.sheetId
);
+
+ if (item != null) {
+ item.commandId = searchParm.commandId;
+ }
+
+ return item;
}
private _buildSkeleton(worksheet: Worksheet, workbook: Workbook) {
diff --git a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
index 96b5d52c9c..1b7f1eac70 100644
--- a/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
+++ b/packages/ui-plugin-sheets/src/controller/editor/start-edit.controller.ts
@@ -151,7 +151,7 @@ export class StartEditController extends Disposable {
isStartBack: true,
},
]);
- this._textSelectionRenderManager.active(HIDDEN_EDITOR_POSITION, HIDDEN_EDITOR_POSITION);
+ this._textSelectionRenderManager.activate(HIDDEN_EDITOR_POSITION, HIDDEN_EDITOR_POSITION);
});
}
@@ -421,8 +421,7 @@ export class StartEditController extends Disposable {
}
private _initialKeyboardListener() {
- this._textSelectionRenderManager.onInput$.subscribe(this._showEditorByKeyboard.bind(this));
- this._textSelectionRenderManager.onCompositionstart$.subscribe(this._showEditorByKeyboard.bind(this));
+ this._textSelectionRenderManager.onInputBefore$.subscribe(this._showEditorByKeyboard.bind(this));
}
private _showEditorByKeyboard(config: Nullable<IEditorInputConfig>) {