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(sheets-ui): data-validation dropdown hidden when selection change (#3119)

zhangwb8bf3b150c65
diff --git a/packages/sheets-data-validation/src/services/dropdown-manager.service.ts b/packages/sheets-data-validation/src/services/dropdown-manager.service.ts
index 79569dedf9..24c4eda23b 100644
--- a/packages/sheets-data-validation/src/services/dropdown-manager.service.ts
+++ b/packages/sheets-data-validation/src/services/dropdown-manager.service.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import type { IDisposable, Nullable } from '@univerjs/core';
-import { Disposable, DisposableCollection, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, Inject, IUniverInstanceService } from '@univerjs/core';
-import type { ISheetLocation } from '@univerjs/sheets';
+import type { IDisposable, Nullable, Workbook } from '@univerjs/core';
+import { Disposable, DisposableCollection, DOCS_FORMULA_BAR_EDITOR_UNIT_ID_KEY, Inject, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { type ISheetLocation, SheetsSelectionsService } from '@univerjs/sheets';
import { Subject } from 'rxjs';
import { SheetCanvasPopManagerService } from '@univerjs/sheets-ui';
import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';
@@ -57,11 +57,13 @@ export class DataValidationDropdownManagerService extends Disposable {
@Inject(DataValidatorRegistryService) private readonly _dataValidatorRegistryService: DataValidatorRegistryService,
@IZenZoneService private readonly _zenZoneService: IZenZoneService,
@IRenderManagerService private readonly _renderManagerService: IRenderManagerService,
- @Inject(DataValidationModel) private readonly _dataValidationModel: DataValidationModel
+ @Inject(DataValidationModel) private readonly _dataValidationModel: DataValidationModel,
+ @Inject(SheetsSelectionsService) private readonly _sheetsSelectionsService: SheetsSelectionsService
) {
super();
this._init();
+ this._initSelectionChange();
this.disposeWithMe(() => {
this._activeDropdown$.complete();
});
@@ -76,6 +78,38 @@ export class DataValidationDropdownManagerService extends Disposable {
}));
}
+ private _getDropdownByCell(unitId: string | undefined, subUnitId: string | undefined, row: number, col: number) {
+ const workbook = unitId ?
+ this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
+ : this._univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) {
+ return;
+ }
+
+ const worksheet = subUnitId ? workbook.getSheetBySheetId(subUnitId) : workbook.getActiveSheet();
+ if (!worksheet) {
+ return;
+ }
+
+ const manager = this._dataValidationModel.ensureManager(workbook.getUnitId(), worksheet.getSheetId()) as SheetDataValidationManager;
+ const rule = manager.getRuleByLocation(row, col);
+ if (!rule) {
+ return;
+ }
+
+ const validator = this._dataValidatorRegistryService.getValidatorItem(rule.type);
+
+ return validator?.dropdown;
+ }
+
+ private _initSelectionChange() {
+ this.disposeWithMe(this._sheetsSelectionsService.selectionMoveEnd$.subscribe((selections) => {
+ if (selections && selections.every((selection) => !(selection.primary && this._getDropdownByCell(selection.primary.unitId, selection.primary.sheetId, selection.primary.actualRow, selection.primary.actualColumn)))) {
+ this.hideDropdown();
+ }
+ }));
+ }
+
showDropdown(param: IDropdownParam, closeOnOutSide = true) {
const { location } = param;
const { row, col } = location;
@@ -133,7 +167,7 @@ export class DataValidationDropdownManagerService extends Disposable {
}
showDataValidationDropdown(unitId: string, subUnitId: string, row: number, col: number, onHide?: () => void) {
- const workbook = this._univerInstanceService.getUniverSheetInstance(unitId);
+ const workbook = this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);
if (!workbook) {
return;
}
diff --git a/packages/sheets-data-validation/src/services/dv-validator-service.ts b/packages/sheets-data-validation/src/services/dv-validator-service.ts
index 80e2b0d738..a4c0e09bc9 100644
--- a/packages/sheets-data-validation/src/services/dv-validator-service.ts
+++ b/packages/sheets-data-validation/src/services/dv-validator-service.ts
@@ -15,7 +15,7 @@
*/
import type { IDataValidationRule, IRange, Nullable, ObjectMatrix, Workbook } from '@univerjs/core';
-import { DataValidationStatus, Inject, IUniverInstanceService, Range, UniverInstanceType } from '@univerjs/core';
+import { DataValidationStatus, Inject, IUniverInstanceService, Range, Tools, UniverInstanceType } from '@univerjs/core';
import { DataValidationModel } from '@univerjs/data-validation';
import type { SheetDataValidationManager } from '../models/sheet-data-validation-manager';
import type { IDataValidationResCache } from './dv-cache.service';
@@ -41,6 +41,10 @@ export class SheetsDataValidationValidatorService {
throw new Error(`cannot find current worksheet, sheetId: ${subUnitId}`);
}
+ if (!Tools.isDefine(row) || !Tools.isDefine(col)) {
+ throw new Error(`row or col is not defined, row: ${row}, col: ${col}`);
+ }
+
const manager = this._dataValidationModel.ensureManager(unitId, subUnitId) as SheetDataValidationManager;
const cell = worksheet.getCell(row, col);
const rule = manager.getRuleByLocation(row, col);
diff --git a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
index 4c988d19ac..08d13f2ccb 100644
--- a/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
+++ b/packages/sheets-thread-comment/src/controllers/sheets-thread-comment.controller.ts
@@ -15,7 +15,7 @@
*/
import type { DependencyOverride, Nullable, Workbook } from '@univerjs/core';
-import { Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LifecycleStages, OnLifecycle, UniverInstanceType } from '@univerjs/core';
+import { Disposable, ICommandService, Inject, Injector, IUniverInstanceService, LifecycleStages, OnLifecycle, RANGE_TYPE, UniverInstanceType } from '@univerjs/core';
import type { MenuConfig } from '@univerjs/ui';
import { ComponentManager, IMenuService, IShortcutService } from '@univerjs/ui';
import { CommentSingle } from '@univerjs/icons';
@@ -89,30 +89,33 @@ export class SheetsThreadCommentController extends Disposable {
}
const params = commandInfo.params as ISetSelectionsOperationParams;
const { unitId, subUnitId, selections, type } = params;
- if ((type === SelectionMoveType.MOVE_END || type === undefined) && selections[0]?.primary) {
+ if ((type === SelectionMoveType.MOVE_END || type === undefined)) {
const range = selections[0].range;
- if (range.endColumn - range.startColumn > 0 || range.endRow - range.startRow > 0) {
+ const rangeType = range.rangeType ?? RANGE_TYPE.NORMAL;
+ if (rangeType !== RANGE_TYPE.NORMAL || range.endColumn - range.startColumn > 0 || range.endRow - range.startRow > 0) {
if (this._threadCommentPanelService.activeCommentId) {
this._commandService.executeCommand(SetActiveCommentOperation.id);
}
return;
}
- const row = selections[0].primary.actualRow;
- const col = selections[0].primary.actualColumn;
- if (!this._sheetsThreadCommentModel.showCommentMarker(unitId, subUnitId, row, col)) {
- if (this._threadCommentPanelService.activeCommentId) {
- this._commandService.executeCommand(SetActiveCommentOperation.id);
+ if (selections[0]?.primary) {
+ const row = selections[0].primary.actualRow;
+ const col = selections[0].primary.actualColumn;
+ if (!this._sheetsThreadCommentModel.showCommentMarker(unitId, subUnitId, row, col)) {
+ if (this._threadCommentPanelService.activeCommentId) {
+ this._commandService.executeCommand(SetActiveCommentOperation.id);
+ }
+ return;
}
- return;
- }
- const commentId = this._sheetsThreadCommentModel.getByLocation(unitId, subUnitId, row, col);
- if (commentId) {
- this._commandService.executeCommand(SetActiveCommentOperation.id, {
- unitId,
- subUnitId,
- commentId,
- });
+ const commentId = this._sheetsThreadCommentModel.getByLocation(unitId, subUnitId, row, col);
+ if (commentId) {
+ this._commandService.executeCommand(SetActiveCommentOperation.id, {
+ unitId,
+ subUnitId,
+ commentId,
+ });
+ }
}
}
}
@@ -243,25 +246,30 @@ export class SheetsThreadCommentController extends Disposable {
const { row, column } = location;
if (Number.isNaN(row) || Number.isNaN(column)) {
- return;
+ return null;
}
- const shapeId = this._markSelectionService.addShape({
- range: {
- startColumn: column,
- endColumn: column,
- startRow: row,
- endRow: row,
- },
- style: {
- hasAutoFill: false,
- fill: 'rgb(255, 189, 55, 0.35)',
- strokeWidth: 1,
- stroke: '#FFBD37',
- widgets: {},
+ // TODO: use evented: false to solve this problem later
+ const shapeId = this._markSelectionService.addShape(
+ {
+ range: {
+ startColumn: column,
+ endColumn: column,
+ startRow: row,
+ endRow: row,
+ },
+ style: {
+ hasAutoFill: false,
+ fill: 'rgb(255, 189, 55, 0.35)',
+ strokeWidth: 1,
+ stroke: '#FFBD37',
+ widgets: {},
+ },
+ primary: null,
},
- primary: null,
- });
+ [],
+ -1
+ );
if (!shapeId) {
return;
}