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: ref selection should call updateSelection after skelenton change (#2839)

lumixd37f6692dbe7
diff --git a/packages/sheets-formula/src/controllers/prompt.controller.ts b/packages/sheets-formula/src/controllers/prompt.controller.ts
index ad32b0bddb..de37302711 100644
--- a/packages/sheets-formula/src/controllers/prompt.controller.ts
+++ b/packages/sheets-formula/src/controllers/prompt.controller.ts
@@ -964,10 +964,6 @@ export class PromptController extends Disposable {
const gridRange = deserializeRangeWithSheet(token);
const { unitId: refUnitId, sheetName, range: rawRange } = gridRange;
- if (!isSelf && (!refUnitId || !sheetName)) {
- continue;
- }
-
/**
* pro/issues/436
* When the range is an entire row or column, NaN values need to be corrected.
@@ -978,6 +974,7 @@ export class PromptController extends Disposable {
continue;
}
+ // sheet name is designed to be unique.
const refSheetId = this._getSheetIdByName(unitId, sheetName.trim());
if (sheetName.length !== 0 && refSheetId !== sheetId) {
@@ -1527,6 +1524,8 @@ export class PromptController extends Disposable {
}
}
+ const unitId = skeleton?.worksheet.getUnitId();
+ const sheetId = skeleton?.worksheet.getSheetId();
const refString = this._generateRefString({
range: {
startRow: Math.min(startRow, endRow),
@@ -1534,6 +1533,8 @@ export class PromptController extends Disposable {
startColumn: Math.min(startColumn, endColumn),
endColumn: Math.max(startColumn, endColumn),
...refType,
+ sheetId,
+ unitId,
},
primary,
style: null,
diff --git a/packages/sheets-formula/src/services/render-services/ref-selections.render-service.ts b/packages/sheets-formula/src/services/render-services/ref-selections.render-service.ts
index e399047bcd..10588fc544 100644
--- a/packages/sheets-formula/src/services/render-services/ref-selections.render-service.ts
+++ b/packages/sheets-formula/src/services/render-services/ref-selections.render-service.ts
@@ -18,8 +18,8 @@ import type { IDisposable, Nullable, Workbook } from '@univerjs/core';
import { DisposableCollection, Inject, Injector, RANGE_TYPE, ThemeService, toDisposable } from '@univerjs/core';
import type { IMouseEvent, IPointerEvent, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { IRenderManagerService, ScrollTimerType, SHEET_VIEWPORT_KEY, Vector2 } from '@univerjs/engine-render';
-import { convertSelectionDataToRange, getNormalSelectionStyle, IRefSelectionsService, type ISelectionWithCoordAndStyle, type SheetsSelectionsService, type WorkbookSelections } from '@univerjs/sheets';
-import { BaseSelectionRenderService, checkInHeaderRanges, getAllSelection, getCoordByOffset, getSheetObject, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
+import { convertSelectionDataToRange, getNormalSelectionStyle, IRefSelectionsService, type ISelectionWithCoordAndStyle, type ISelectionWithStyle, type SheetsSelectionsService, type WorkbookSelections } from '@univerjs/sheets';
+import { attachSelectionWithCoord, BaseSelectionRenderService, checkInHeaderRanges, getAllSelection, getCoordByOffset, getSheetObject, SheetSkeletonManagerService } from '@univerjs/sheets-ui';
import { IShortcutService } from '@univerjs/ui';
/**
@@ -40,7 +40,7 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
@Inject(ThemeService) themeService: ThemeService,
@IShortcutService shortcutService: IShortcutService,
@IRenderManagerService renderManagerService: IRenderManagerService,
- @IRefSelectionsService refSelectionsService: SheetsSelectionsService,
+ @IRefSelectionsService private readonly _refSelectionsService: SheetsSelectionsService,
@Inject(SheetSkeletonManagerService) private readonly _sheetSkeletonManagerService: SheetSkeletonManagerService
) {
super(
@@ -50,13 +50,13 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
renderManagerService
);
- this._workbookSelections = refSelectionsService.getWorkbookSelections(this._context.unitId);
+ this._workbookSelections = this._refSelectionsService.getWorkbookSelections(this._context.unitId);
this._initSelectionChangeListener();
this._initSkeletonChangeListener();
this._initUserActionSyncListener();
- this._setStyle(getRefSelectionStyle(this._themeService));
+ this._setStyle(getDefaultRefSelectionStyle(this._themeService));
this._remainLastEnabled = true; // For ref range selections, we should always remain others.
}
@@ -154,9 +154,6 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
}
private _initUserActionSyncListener() {
- // When user completes range selection, we should update the range into the `IRefSelectionService`,
- // and later the selections would be updated to the `SelectionManagerService`.
- // Note that it would also trigger the callback in `_initSelectionChangeListener`.
this.disposeWithMe(this.selectionMoveEnd$.subscribe((params) => this._updateSelections(params)));
}
@@ -173,13 +170,14 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
}
private _initSelectionChangeListener(): void {
- // When selection completes, we need to update the selections' rendering and clear event handlers.
- this.disposeWithMe(this._workbookSelections.beforeSelectionMoveEnd$.subscribe((selectionsWithStyles) => {
- // Clear already existed selections.
+ // selectionMoveEnd$ beforeSelectionMoveEnd$ was triggered when pointerup after dragging to change selection area.
+ // Changing the selection area through the 8 control points of the ref selection will not trigger this subscriber.
+
+ // beforeSelectionMoveEnd$ & selectionMoveEnd$ would triggered when change skeleton(change sheet).
+ this.disposeWithMe(this._workbookSelections.selectionMoveEnd$.subscribe((selectionsWithStyles) => {
this._reset();
- // The selections' style would be colorful here. PromptController would change the color of
- // selections later.
+ // The selections' style would be colorful here. PromptController would change the color of selections later.
for (const selectionWithStyle of selectionsWithStyles) {
const selectionData = this.attachSelectionWithCoord(selectionWithStyle);
this._addSelectionControlBySelectionData(selectionData);
@@ -188,6 +186,9 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
}
private _initSkeletonChangeListener(): void {
+ // changing sheet is not the only way cause currentSkeleton$ emit, a lot of cmds will emit currentSkeleton$
+ // COMMAND_LISTENER_SKELETON_CHANGE ---> currentSkeleton$.next
+ // 'sheet.mutation.set-worksheet-row-auto-height' is one of COMMAND_LISTENER_SKELETON_CHANGE
this.disposeWithMe(this._sheetSkeletonManagerService.currentSkeleton$.subscribe((param) => {
if (!param) {
return;
@@ -196,10 +197,26 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
const { skeleton } = param;
const { scene } = this._context;
const viewportMain = scene.getViewport(SHEET_VIEWPORT_KEY.VIEW_MAIN);
+
+ if (this._skeleton && this._skeleton.worksheet.getSheetId() !== skeleton.worksheet.getSheetId()) {
+ this._reset();
+ }
this._changeRuntime(skeleton, scene, viewportMain);
+
+ // for col width & row height resize
+ const currentSelections = this._workbookSelections.getCurrentSelections();
+ this._refreshSelectionControl(currentSelections || []);
}));
}
+ protected override _refreshSelectionControl(selectionsData: readonly ISelectionWithStyle[]) {
+ const selections = selectionsData.map((selectionWithStyle) => {
+ const selectionData = attachSelectionWithCoord(selectionWithStyle, this._skeleton);
+ return selectionData;
+ });
+ this.updateControlForCurrentByRangeData(selections);
+ }
+
private _getActiveViewport(evt: IPointerEvent | IMouseEvent) {
const sheetObject = this._getSheetObject();
return sheetObject?.scene.getActiveViewportByCoord(Vector2.FromArray([evt.offsetX, evt.offsetY]));
@@ -215,7 +232,7 @@ export class RefSelectionsRenderService extends BaseSelectionRenderService imple
* @param themeService
* @returns The selection's style.
*/
-function getRefSelectionStyle(themeService: ThemeService) {
+function getDefaultRefSelectionStyle(themeService: ThemeService) {
const style = getNormalSelectionStyle(themeService);
style.strokeDash = 8;
style.hasAutoFill = false;
diff --git a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
index ec04e8b37b..a0b9cf19a6 100644
--- a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
+++ b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
@@ -101,8 +101,10 @@ export function HelpFunction() {
}
function getPosition() {
- const documentDataModel = univerInstanceService.getCurrentUniverDocInstance()!;
-
+ const documentDataModel = univerInstanceService.getCurrentUniverDocInstance();
+ if (!documentDataModel) {
+ return;
+ }
const editorUnitId = documentDataModel.getUnitId();
if (!editorService.isEditor(editorUnitId)) {
diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.ts
index c23845d1e2..2aec698828 100644
--- a/packages/sheets-ui/src/index.ts
+++ b/packages/sheets-ui/src/index.ts
@@ -166,5 +166,6 @@ export { SetZoomRatioOperation } from './commands/operations/set-zoom-ratio.oper
export { SheetPermissionOpenDialogOperation } from './commands/operations/sheet-permission-open-dialog.operation';
export { SheetPermissionOpenPanelOperation } from './commands/operations/sheet-permission-open-panel.operation';
export { SidebarDefinedNameOperation } from './commands/operations/sidebar-defined-name.operation';
+export { attachSelectionWithCoord, attachPrimaryWithCoord } from './services/selection/util';
// #endregion
diff --git a/packages/sheets-ui/src/services/selection/base-selection-render.service.ts b/packages/sheets-ui/src/services/selection/base-selection-render.service.ts
index 91c642401a..2155ed24dd 100644
--- a/packages/sheets-ui/src/services/selection/base-selection-render.service.ts
+++ b/packages/sheets-ui/src/services/selection/base-selection-render.service.ts
@@ -144,6 +144,12 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
// #endregion
protected readonly _selectionMoveEnd$ = new BehaviorSubject<ISelectionWithCoordAndStyle[]>([]);
+
+ /**
+ * trigger when selection move end(pointerup)
+ * and then update selection model in selectionManagerService
+ * selectionMoveEnd$ ---> _updateSelections --> [email protected]
+ */
readonly selectionMoveEnd$ = this._selectionMoveEnd$.asObservable();
protected readonly _selectionMoving$ = new Subject<ISelectionWithCoordAndStyle[]>();
readonly selectionMoving$ = this._selectionMoving$.asObservable();
@@ -168,7 +174,6 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
protected readonly _renderManagerService: IRenderManagerService
) {
super();
-
this._resetStyle();
this._initMoving();
}
@@ -224,7 +229,6 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
* selectionManagerService@_emitOnEnd -->
* _workbookSelections.selectionMoveEnd$ --> _addSelectionControlBySelectionData
*
- *
* @param selectionData
*/
protected _addSelectionControlBySelectionData(selection: ISelectionWithCoordAndStyle) {
@@ -242,7 +246,11 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
// TODO: memory leak? This extension seems never released.
// eslint-disable-next-line no-new
- new SelectionShapeExtension(control, skeleton, scene, this._themeService, this._injector);
+ new SelectionShapeExtension(control, skeleton, scene, this._themeService, this._injector, {
+ selectionMoveEnd: () => {
+ this._selectionMoveEnd$.next(this.getSelectionDataWithStyle());
+ },
+ });
const { rowHeaderWidth, columnHeaderHeight } = skeleton;
@@ -258,9 +266,14 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
return control;
}
+ /**
+ * Update the corresponding selectionControl based on selectionsData.
+ * selectionData[i] ---- selectionControls[i]
+ * @param selections
+ */
updateControlForCurrentByRangeData(selections: ISelectionWithCoordAndStyle[]) {
- const currentControls = this.getSelectionControls();
- if (!currentControls) {
+ const selectionControls = this.getSelectionControls();
+ if (!selectionControls) {
return;
}
@@ -272,12 +285,14 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
const { rowHeaderWidth, columnHeaderHeight } = skeleton;
+ // TODO @lumixraku This is awful!
+ // selectionControls should create & remove base on selections.
for (let i = 0, len = selections.length; i < len; i++) {
const { rangeWithCoord, primaryWithCoord, style } = selections[i];
- const control = currentControls[i];
+ const control = selectionControls[i];
- control.update(rangeWithCoord, rowHeaderWidth, columnHeaderHeight, style, primaryWithCoord);
+ control && control.update(rangeWithCoord, rowHeaderWidth, columnHeaderHeight, style, primaryWithCoord);
}
}
@@ -389,6 +404,9 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
this._shortcutService.setDisable(false);
}
+ /**
+ * Clear existed selections.
+ */
protected _reset() {
this._clearSelectionControls();
this._downObserver?.unsubscribe();
@@ -982,6 +1000,15 @@ export class BaseSelectionRenderService extends Disposable implements ISheetSele
};
this._updateSelectionControlRange(activeControl, newSelectionRange, currentCell);
}
+
+ protected _refreshSelectionControl(selectionsData: readonly ISelectionWithStyle[]) {
+ const selections = selectionsData.map((selectionWithStyle) => {
+ const selectionData = attachSelectionWithCoord(selectionWithStyle, this._skeleton);
+ selectionData.style = getNormalSelectionStyle(this._themeService);
+ return selectionData;
+ });
+ this.updateControlForCurrentByRangeData(selections);
+ }
}
export function getAllSelection(skeleton: SpreadsheetSkeleton): ISelectionWithStyle {
diff --git a/packages/sheets-ui/src/services/selection/selection-render.service.ts b/packages/sheets-ui/src/services/selection/selection-render.service.ts
index c28e4cd5ad..d2973149fc 100644
--- a/packages/sheets-ui/src/services/selection/selection-render.service.ts
+++ b/packages/sheets-ui/src/services/selection/selection-render.service.ts
@@ -18,8 +18,8 @@ import type { IDisposable, Nullable, Workbook } from '@univerjs/core';
import { ICommandService, IContextService, ILogService, Inject, Injector, RANGE_TYPE, ThemeService, toDisposable } from '@univerjs/core';
import type { IMouseEvent, IPointerEvent, IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { IRenderManagerService, ScrollTimerType, SHEET_VIEWPORT_KEY, Vector2 } from '@univerjs/engine-render';
-import type { ISelectionWithCoordAndStyle, ISelectionWithStyle, ISetSelectionsOperationParams, WorkbookSelections } from '@univerjs/sheets';
-import { convertSelectionDataToRange, DISABLE_NORMAL_SELECTIONS, getNormalSelectionStyle, SelectionMoveType, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
+import type { ISelectionWithCoordAndStyle, ISetSelectionsOperationParams, WorkbookSelections } from '@univerjs/sheets';
+import { convertSelectionDataToRange, DISABLE_NORMAL_SELECTIONS, SelectionMoveType, SetSelectionsOperation, SheetsSelectionsService } from '@univerjs/sheets';
import { IShortcutService } from '@univerjs/ui';
import { distinctUntilChanged, startWith } from 'rxjs';
import { SheetSkeletonManagerService } from '../sheet-skeleton-manager.service';
@@ -45,7 +45,7 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
@Inject(ThemeService) themeService: ThemeService,
@IShortcutService shortcutService: IShortcutService,
@IRenderManagerService renderManagerService: IRenderManagerService,
- @Inject(SheetsSelectionsService) selectionManagerService: SheetsSelectionsService,
+ @Inject(SheetsSelectionsService) private readonly _selectionManagerService: SheetsSelectionsService,
@ILogService private readonly _logService: ILogService,
@ICommandService private readonly _commandService: ICommandService,
@IContextService private readonly _contextService: IContextService,
@@ -58,7 +58,7 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
renderManagerService
);
- this._workbookSelections = selectionManagerService.getWorkbookSelections(this._context.unitId);
+ this._workbookSelections = _selectionManagerService.getWorkbookSelections(this._context.unitId);
this._init();
}
@@ -138,20 +138,10 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
const param = this._workbookSelections.getCurrentSelections();
if (!param) return;
- this._refreshSelection(param);
+ this._refreshSelectionControl(param);
}));
}
- private _refreshSelection(params: readonly ISelectionWithStyle[]) {
- const selections = params.map((selectionWithStyle) => {
- const selectionData = attachSelectionWithCoord(selectionWithStyle, this._skeleton);
- selectionData.style = getNormalSelectionStyle(this._themeService);
- return selectionData;
- });
-
- this.updateControlForCurrentByRangeData(selections);
- }
-
private _normalSelectionDisabled(): boolean {
return this._contextService.getContextValue(DISABLE_NORMAL_SELECTIONS);
}
@@ -186,6 +176,12 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
}));
}
+ /**
+ * set new selection range in seletion model
+ * selectionMoveEnd$ ---> _updateSelections --> [email protected]
+ * @param selectionDataWithStyleList
+ * @param type
+ */
private _updateSelections(selectionDataWithStyleList: ISelectionWithCoordAndStyle[], type: SelectionMoveType) {
const workbook = this._context.unit;
const unitId = workbook.getUnitId();
@@ -219,19 +215,24 @@ export class SheetSelectionRenderService extends BaseSelectionRenderService impl
this._changeRuntime(skeleton, scene, viewportMain);
// If there is no initial selection, add one by default in the top left corner.
- const firstSelection = this._workbookSelections.getCurrentLastSelection();
- if (!firstSelection) {
- this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
- unitId,
- subUnitId: sheetId,
- selections: [getTopLeftSelection(skeleton)],
- } as ISetSelectionsOperationParams);
- }
+ // const firstSelection = this._workbookSelections.getCurrentLastSelection();
+ // Dont do that above! Ref selection also need syncExecCmd to reset selection.
+ // TODO @lumixraku why use such weird a way to clear existing selection? subscribe to currentSkeleton$ is much better?
+
+ if (this._normalSelectionDisabled()) return;
+
+ // SetSelectionsOperation would clear all exists selections
+ // SetSelectionsOperation ---> selectionManager@setSelections ---> moveEnd$ ---> selectionRenderService@_reset
+ this._commandService.syncExecuteCommand(SetSelectionsOperation.id, {
+ unitId,
+ subUnitId: sheetId,
+ selections: [getTopLeftSelection(skeleton)],
+ } as ISetSelectionsOperationParams);
- // for col width & row height resize
const currentSelections = this._workbookSelections.getCurrentSelections();
+ // for col width & row height resize
if (currentSelections != null) {
- this._refreshSelection(currentSelections);
+ this._refreshSelectionControl(currentSelections);
}
}));
}
diff --git a/packages/sheets-ui/src/services/selection/selection-shape-extension.ts b/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
index 894cfdb598..042b18a957 100644
--- a/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
+++ b/packages/sheets-ui/src/services/selection/selection-shape-extension.ts
@@ -86,7 +86,8 @@ export class SelectionShapeExtension {
/** @deprecated injection in extensions should be strictly limited. */
// TODO@ybzky: remove injector here, permission control should be update from the outside.
- private readonly _injector: Injector
+ private readonly _injector: Injector,
+ private _selectionHooks: Record<string, () => void>
) {
this._initialControl();
@@ -369,7 +370,12 @@ export class SelectionShapeExtension {
this._clearObserverEvent();
scene.enableEvent();
this._scrollTimer?.dispose();
+ // selectionScaled$ --> disableLockedSelectionChange
this._control.selectionMoved$.next(this._targetSelection);
+ // this._selectionMoveEnd$.next(this.getSelectionDataWithStyle());
+ // this function should placed after this._control.selectionMoved$,
+ // because selectionMoveEnd will dispose all selectionControls, then this._control will be null.
+ this._selectionHooks.selectionMoveEnd?.();
});
}
@@ -516,6 +522,11 @@ export class SelectionShapeExtension {
this._control.selectionScaling$.next(this._targetSelection);
}
+ /**
+ * Events for 8 control point
+ * @param evt
+ * @param cursor
+ */
private _widgetEvent(evt: IMouseEvent | IPointerEvent, cursor: CURSOR_TYPE) {
const { offsetX: evtOffsetX, offsetY: evtOffsetY } = evt;
@@ -588,6 +599,12 @@ export class SelectionShapeExtension {
scene.enableEvent();
this._scrollTimer?.dispose();
this._control.selectionScaled$.next(this._targetSelection);
+ // selectionMoveEnd$ update selection model
+ // _selectionHooks.selectionMoveEnd --> SelectionRenderService._selectionMoveEnd$.next(this.getSelectionDataWithStyle());
+
+ // this function should placed after this._control.selectionMoved$,
+ // because selectionMoveEnd will dispose all selectionControls, then this._control will be null.
+ this._selectionHooks.selectionMoveEnd?.();
});
}
diff --git a/packages/sheets-ui/src/services/selection/selection-shape.ts b/packages/sheets-ui/src/services/selection/selection-shape.ts
index 07c71319bf..a86b9d4546 100644
--- a/packages/sheets-ui/src/services/selection/selection-shape.ts
+++ b/packages/sheets-ui/src/services/selection/selection-shape.ts
@@ -555,7 +555,7 @@ export class SelectionControl extends Disposable {
stroke: SELECTION_CONTROL_BORDER_BUFFER_COLOR,
});
- if (strokeDash == null) {
+ if (strokeDash === null || strokeDash === undefined) {
this.dashRect.hide();
this._stopAntLineAnimation();
} else {