fix(sheets): fix some APIs, such as setGridLinesColor, were ineffective when setting for inactive worksheets (#6348)

This commit is contained in:
wpxp123456
2025-12-19 15:40:32 +08:00
committed by GitHub
parent 1f77bf43f5
commit a6cc5b5a91
19 changed files with 82 additions and 108 deletions
@@ -890,7 +890,7 @@ export class SheetsFilterController extends Disposable {
private _initErrorHandling() {
this.disposeWithMe(this._commandService.beforeCommandExecuted((command) => {
const params = command.params as IMoveRowsCommandParams;
const target = getSheetCommandTarget(this._univerInstanceService);
const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) return;
const { subUnitId, unitId } = target;
@@ -98,7 +98,7 @@ export class SheetHyperLinkSetRangeController extends Disposable {
const redos: IMutationInfo[] = [];
const undos: IMutationInfo[] = [];
const selection = this._selectionManagerService.getCurrentLastSelection();
const target = getSheetCommandTarget(this._univerInstanceService);
const target = getSheetCommandTarget(this._univerInstanceService, command.params);
if (selection && target) {
const { unitId, subUnitId } = target;
Range.foreach(selection.range, (row, col) => {
@@ -356,7 +356,7 @@ export const SheetTableRemoveRowCommand: ICommand<ISheetTableRowColOperationComm
}
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) {
return false;
}
@@ -460,7 +460,7 @@ export const SheetTableRemoveColCommand: ICommand<ISheetTableRowColOperationComm
return false;
}
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) {
return false;
}
@@ -71,7 +71,7 @@ export class SheetTableRefRangeController extends Disposable {
const undos: IMutationInfo[] = [];
const redos: IMutationInfo[] = [];
const target = getSheetCommandTarget(this._univerInstanceService);
const target = getSheetCommandTarget(this._univerInstanceService, insertParams);
if (!target) {
return { undos, redos };
}
@@ -129,7 +129,7 @@ export class SheetTableRefRangeController extends Disposable {
const undos: IMutationInfo[] = [];
const redos: IMutationInfo[] = [];
const target = getSheetCommandTarget(this._univerInstanceService);
const target = getSheetCommandTarget(this._univerInstanceService, insertParams);
if (!target) {
return { undos, redos };
}
@@ -65,7 +65,7 @@ export class AutoWidthController extends Disposable {
const defaultValue = { redos: [], undos: [] };
const { _univerInstanceService: univerInstanceService } = this;
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return defaultValue;
const { unitId, subUnitId, worksheet } = target;
@@ -230,7 +230,7 @@ export class SheetPermissionCheckUIController extends Disposable {
const { targetRange } = params;
const target = getSheetCommandTarget(this._univerInstanceService);
const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) {
return false;
}
@@ -38,7 +38,7 @@ export const RegisterWorksheetRangeThemeStyleCommand: ICommand<IRegisterWorkshee
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;
const redoParam = {
@@ -209,17 +209,17 @@ export const RemoveColCommandId = 'sheet.command.remove-col';
export const RemoveColByRangeCommand: ICommand<IRemoveColByRangeCommandParams> = {
type: CommandType.COMMAND,
id: 'sheet.command.remove-col-by-range',
handler: (accessor, parmas) => {
if (!parmas) {
handler: (accessor, params) => {
if (!params) {
return false;
}
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService, parmas);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;
const { workbook, worksheet } = target;
const sheetInterceptorService = accessor.get(SheetInterceptorService);
const { range, unitId, subUnitId } = parmas;
const { range, unitId, subUnitId } = params;
// col count
const removeColParams: IRemoveColMutationParams = {
unitId,
@@ -34,7 +34,7 @@ export const SetGridlinesColorCommand: ICommand = {
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;
const { worksheet } = target;
@@ -14,11 +14,10 @@
* limitations under the License.
*/
import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
import type { IAccessor, ICommand } from '@univerjs/core';
import type { ISetWorksheetHideMutationParams } from '../mutations/set-worksheet-hide.mutation';
import type { ISetWorksheetActiveOperationParams } from '../operations/set-worksheet-active.operation';
import { BooleanNumber, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
import { BooleanNumber, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
import { SetWorksheetHideMutation, SetWorksheetHideMutationFactory } from '../mutations/set-worksheet-hide.mutation';
import {
SetWorksheetActiveOperation,
@@ -36,22 +35,15 @@ export const SetWorksheetShowCommand: ICommand = {
id: 'sheet.command.set-worksheet-show',
handler: (accessor: IAccessor, params: ISetWorksheetShowCommandParams) => {
const { unitId, subUnitId } = params;
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const { unitId, subUnitId, worksheet } = target;
const hidden = worksheet.getConfig().hidden;
if (hidden === BooleanNumber.FALSE) return false;
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
if (!target) return false;
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
if (!workbook) return false;
const worksheet = workbook.getSheetBySheetId(subUnitId);
if (!worksheet) return false;
const hidden = worksheet.getConfig().hidden;
if (hidden === BooleanNumber.FALSE) return false;
const redoMutationParams: ISetWorksheetHideMutationParams = {
unitId,
@@ -14,11 +14,11 @@
* limitations under the License.
*/
import type { IAccessor, ICellData, IRange, Nullable, Workbook } from '@univerjs/core';
import type { IAccessor, ICellData, IRange, Nullable } from '@univerjs/core';
import type { IInsertColMutationParams, IRemoveColMutationParams } from '../../basics';
import type { SplitDelimiterEnum } from '../../basics/split-range-text';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, ObjectMatrix, Range, sequenceExecute, UniverInstanceType } from '@univerjs/core';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, ObjectMatrix, Range, sequenceExecute } from '@univerjs/core';
import { splitRangeText } from '../../basics/split-range-text';
import { InsertColMutation, InsertColMutationUndoFactory } from '../mutations/insert-row-col.mutation';
import { RemoveColMutation } from '../mutations/remove-row-col.mutation';
@@ -39,20 +39,11 @@ export const SplitTextToColumnsCommand = {
id: 'sheet.command.split-text-to-columns',
// eslint-disable-next-line max-lines-per-function
handler: (accessor: IAccessor, params: ISplitTextToColumnsCommandParams) => {
const { unitId, subUnitId, range, delimiter, customDelimiter, treatMultipleDelimitersAsOne } = params;
const commandService = accessor.get(ICommandService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const undoRedoService = accessor.get(IUndoRedoService);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
if (!workbook) return false;
const worksheet = workbook.getSheetBySheetId(subUnitId);
if (!worksheet) return false;
const { unitId, subUnitId, worksheet } = target;
const { range, delimiter, customDelimiter, treatMultipleDelimitersAsOne } = params;
const { lastRow, rs, maxLength } = splitRangeText(worksheet, range, delimiter, customDelimiter, treatMultipleDelimitersAsOne);
const maxColumn = worksheet.getColumnCount();
const { startColumn } = Range.transformRange(range, worksheet);
@@ -61,6 +52,8 @@ export const SplitTextToColumnsCommand = {
return false;
}
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const redoMutations = [];
const undoMutations = [];
@@ -32,9 +32,8 @@ export const ToggleGridlinesCommand: ICommand = {
handler: (accessor: IAccessor, params?: IToggleGridlinesCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const { worksheet } = target;
@@ -16,7 +16,6 @@
import type { ICommand } from '@univerjs/core';
import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
import { SheetRangeThemeModel } from '../../model/range-theme-model';
import { getSheetCommandTarget } from '../commands/utils/target-util';
import { RegisterWorksheetRangeThemeStyleMutation } from '../mutations/register-range-theme.mutation';
@@ -31,16 +30,15 @@ export const UnregisterWorksheetRangeThemeStyleCommand: ICommand<IUnregisterWork
id: 'sheet.command.unregister-worksheet-range-theme-style',
type: CommandType.COMMAND,
handler: (accessor, params) => {
if (!params) {
return false;
}
const { unitId, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
if (!params) return false;
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const target = getSheetCommandTarget(univerInstanceService);
if (!target) return false;
const { unitId, themeName } = params;
const redoParam = {
unitId,
@@ -51,6 +49,7 @@ export const UnregisterWorksheetRangeThemeStyleCommand: ICommand<IUnregisterWork
themeName,
rangeThemeStyleJson: sheetRangeThemeModel.getRangeThemeStyle(unitId, themeName)?.toJson(),
};
const result = commandService.syncExecuteCommand(RegisterWorksheetRangeThemeStyleMutation.id, params);
if (result) {
undoRedoService.pushUndoRedo({
@@ -58,7 +57,9 @@ export const UnregisterWorksheetRangeThemeStyleCommand: ICommand<IUnregisterWork
undoMutations: [{ id: RegisterWorksheetRangeThemeStyleMutation.id, params: undoParam }],
redoMutations: [{ id: UnregisterWorksheetRangeThemeStyleMutation.id, params: redoParam }],
});
return true;
}
return true;
return false;
},
};
@@ -24,12 +24,12 @@ export const SetWorksheetRangeThemeStyleMutation: IMutation<IWorksheetRangeTheme
id: 'sheet.mutation.set-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, subUnitId, range, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const { unitId, subUnitId, range, themeName } = params;
sheetRangeThemeModel.registerRangeThemeRule(themeName, { range, unitId, subUnitId });
return true;
},
@@ -24,12 +24,12 @@ export const DeleteWorksheetRangeThemeStyleMutation: IMutation<IWorksheetRangeTh
id: 'sheet.mutation.remove-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, subUnitId, range, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const { unitId, subUnitId, range, themeName } = params;
sheetRangeThemeModel.removeRangeThemeRule(themeName, { range, unitId, subUnitId });
return true;
},
@@ -31,12 +31,12 @@ export const RegisterWorksheetRangeThemeStyleMutation: IMutation<IRegisterWorksh
id: 'sheet.mutation.register-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, rangeThemeStyleJson, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const { unitId, rangeThemeStyleJson, themeName } = params;
const rangeThemeStyle = new RangeThemeStyle(themeName, rangeThemeStyleJson);
sheetRangeThemeModel.registerRangeThemeStyle(unitId, rangeThemeStyle);
return true;
@@ -28,15 +28,11 @@ export const SetWorksheetDefaultStyleMutation: IMutation<ISetWorksheetDefaultSty
id: 'sheet.mutation.set-worksheet-default-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { defaultStyle } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const { worksheet } = target;
if (!worksheet) {
return false;
}
const { defaultStyle } = params;
worksheet.setDefaultCellStyle(defaultStyle);
return true;
},
@@ -28,12 +28,12 @@ export const UnregisterWorksheetRangeThemeStyleMutation: IMutation<IUnregisterWo
id: 'sheet.mutation.unregister-worksheet-range-theme-style',
type: CommandType.MUTATION,
handler: (accessor, params) => {
const { unitId, themeName } = params;
const univerInstanceService = accessor.get(IUniverInstanceService);
const target = getSheetCommandTarget(univerInstanceService);
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
if (!target) return false;
const sheetRangeThemeModel = accessor.get(SheetRangeThemeModel);
const { unitId, themeName } = params;
sheetRangeThemeModel.unregisterRangeThemeStyle(unitId, themeName);
return true;
},
@@ -361,42 +361,33 @@ export class SheetPermissionCheckController extends Disposable {
}
public permissionCheckWithRanges(permissionTypes: IPermissionTypes, selectionRanges?: IRange[], unitId?: string, subUnitId?: string) {
const target = getSheetCommandTarget(this._univerInstanceService);
if (!target) {
return false;
}
const { workbook, worksheet } = target;
if (!unitId) {
unitId = workbook.getUnitId();
}
if (!subUnitId) {
subUnitId = worksheet.getSheetId();
}
const target = getSheetCommandTarget(this._univerInstanceService, { unitId, subUnitId });
if (!target) return false;
const ranges = selectionRanges ?? this._selectionManagerService.getCurrentSelections()?.map((selection) => {
return selection.range;
});
if (!ranges) return false;
if (!ranges) {
return false;
}
const { unitId: _unitId, subUnitId: _subUnitId } = target;
const { workbookTypes, worksheetTypes, rangeTypes } = permissionTypes;
const permissionIds = [];
if (workbookTypes) {
permissionIds.push(...workbookTypes.map((F) => new F(unitId).id));
permissionIds.push(...workbookTypes.map((F) => new F(_unitId).id));
}
if (worksheetTypes) {
permissionIds.push(...worksheetTypes.map((F) => new F(unitId, subUnitId).id));
permissionIds.push(...worksheetTypes.map((F) => new F(_unitId, _subUnitId).id));
}
if (rangeTypes) {
this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).forEach((rule) => {
this._rangeProtectionRuleModel.getSubunitRuleList(_unitId, _subUnitId).forEach((rule) => {
const overlap = ranges.some((range) => {
return rule.ranges.some((r) => {
return Rectangle.intersects(r, range);
});
});
if (overlap) {
permissionIds.push(...rangeTypes.map((F) => new F(unitId, subUnitId, rule.permissionId).id));
permissionIds.push(...rangeTypes.map((F) => new F(_unitId, _subUnitId, rule.permissionId).id));
}
});
}
@@ -409,17 +400,18 @@ export class SheetPermissionCheckController extends Disposable {
}
private _permissionCheckByMoveCommand(params: IMoveRowsCommandParams | IMoveColsCommandParams) {
const target = getSheetCommandTarget(this._univerInstanceService);
if (!target) {
return false;
}
const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) return false;
const { worksheet, unitId, subUnitId } = target;
const toRange = params.toRange;
if (toRange.endRow === worksheet.getRowCount() - 1) {
toRange.endColumn = toRange.startColumn;
} else {
toRange.endRow = toRange.startRow;
}
const permissionLapRanges = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).reduce((p, c) => {
return [...p, ...c.ranges];
}, [] as IRange[]).filter((range) => {
@@ -429,6 +421,7 @@ export class SheetPermissionCheckController extends Disposable {
if (permissionLapRanges.length > 0) {
return false;
}
permissionLapRanges.forEach((range) => {
for (let row = range.startRow; row <= range.endRow; row++) {
for (let col = range.startColumn; col <= range.endColumn; col++) {
@@ -562,18 +555,18 @@ export class SheetPermissionCheckController extends Disposable {
}
}
if (range) {
const target = getSheetCommandTarget(this._univerInstanceService);
if (!target) {
return false;
}
const unitId = params.unitId || target.unitId;
const subunitId = params.subUnitId || target.subUnitId;
const permissionList = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subunitId).filter((rule) => {
const target = getSheetCommandTarget(this._univerInstanceService, params);
if (!target) return false;
const { unitId, subUnitId } = target;
const permissionList = this._rangeProtectionRuleModel.getSubunitRuleList(unitId, subUnitId).filter((rule) => {
return rule.ranges.some((ruleRange) => {
return Rectangle.intersects(ruleRange, range);
});
});
const permissionIds = permissionList.map((rule) => new RangeProtectionPermissionEditPoint(unitId, subunitId, rule.permissionId).id);
const permissionIds = permissionList.map((rule) => new RangeProtectionPermissionEditPoint(unitId, subUnitId, rule.permissionId).id);
const editPermission = this._permissionService.composePermission(permissionIds).every((permission) => permission.value);
if (!editPermission) {
return false;