Skip to content

Commit

Permalink
fix: mv auto col cmd into sheets-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
lumixraku committed Oct 28, 2024
1 parent 0700f7e commit f7c9056
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright 2023-present DreamNum Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type { IAccessor, ICommand, IRange } from '@univerjs/core';

import {
CommandType,
ICommandService,
IUndoRedoService,
IUniverInstanceService,
sequenceExecute,
} from '@univerjs/core';
import {
getSheetCommandTarget,
SheetsSelectionsService,
} from '@univerjs/sheets';
import { AutoWidthController } from '../../controllers/auto-width.controller';

export interface ISetWorksheetColIsAutoWidthCommandParams {
unitId?: string;
subUnitId?: string;
ranges?: IRange[]; // For Facade API
}

export const SetWorksheetColAutoWidthCommand: ICommand = {
type: CommandType.COMMAND,
id: 'sheet.command.set-col-auto-width',
handler: async (accessor: IAccessor, params?: ISetWorksheetColIsAutoWidthCommandParams) => {
const commandService = accessor.get(ICommandService);
const undoRedoService = accessor.get(IUndoRedoService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
const univerInstanceService = accessor.get(IUniverInstanceService);

const target = getSheetCommandTarget(univerInstanceService, params);
if (!target) return false;

const { unitId, subUnitId } = target;

let ranges = [];
if (params?.ranges) {
ranges = [...params.ranges];
} else {
const selections = selectionManagerService.getCurrentSelections();
for (let i = 0; i < selections.length; i++) {
ranges.push(selections[i].range);
}
}

if (!ranges?.length) {
return false;
}

const redoMutationParams: Required<ISetWorksheetColIsAutoWidthCommandParams> = {
unitId,
subUnitId,
ranges,
};

// undos redos comes from auto-width.controller
const { redos, undos } = accessor.get(AutoWidthController).getUndoRedoParamsOfColWidth(redoMutationParams);

const result = sequenceExecute([...redos], commandService);
if (result.result) {
undoRedoService.pushUndoRedo({
unitID: unitId,
undoMutations: [...undos],
redoMutations: [...redos],
});

return true;
}

return false;
},
};
23 changes: 2 additions & 21 deletions packages/sheets-ui/src/controllers/auto-width.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
import type { IColAutoWidthInfo, IObjectArrayPrimitiveType, Nullable, Workbook } from '@univerjs/core';
import type { RenderManagerService } from '@univerjs/engine-render';
import type {
ISetWorksheetColIsAutoWidthCommandParams,
ISetWorksheetColWidthMutationParams,
} from '@univerjs/sheets';
import type { ISetWorksheetColIsAutoWidthCommandParams } from '../commands/commands/set-worksheet-auto-col-width.command';
import { Disposable, Inject, IUniverInstanceService } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import {
createAutoColWidthUndoMutationsByRedos,
SetWorksheetColIsAutoWidthCommand,
SetWorksheetColWidthMutation,
SheetInterceptorService,
} from '@univerjs/sheets';
import { SetWorksheetColAutoWidthCommand } from '../commands/commands/set-worksheet-auto-col-width.command';

Check failure on line 30 in packages/sheets-ui/src/controllers/auto-width.controller.ts

View workflow job for this annotation

GitHub Actions / eslint

'SetWorksheetColAutoWidthCommand' is defined but never used
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';

export const AFFECT_LAYOUT_STYLES = ['ff', 'fs', 'tr', 'tb'];
Expand All @@ -39,7 +39,6 @@ export class AutoWidthController extends Disposable {
@Inject(IUniverInstanceService) private readonly _univerInstanceService: IUniverInstanceService
) {
super();
this._initialize();
}

getUndoRedoParamsOfColWidth(params: Required<ISetWorksheetColIsAutoWidthCommandParams>) {
Expand Down Expand Up @@ -88,22 +87,4 @@ export class AutoWidthController extends Disposable {
],
};
}

private _initialize() {
const { _sheetInterceptorService: sheetInterceptorService } = this;

// intercept 'sheet.command.set-col-is-auto-width' command.
this.disposeWithMe(sheetInterceptorService.interceptCommand({
getMutations: (command: { id: string; params: Required<ISetWorksheetColIsAutoWidthCommandParams> }) => {
if (command.id !== SetWorksheetColIsAutoWidthCommand.id) {
return {
redos: [],
undos: [],
};
}

return this.getUndoRedoParamsOfColWidth(command.params);
},
}));
}
}
5 changes: 3 additions & 2 deletions packages/sheets-ui/src/controllers/menu.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
SetSelectedRowsVisibleCommand, SetTabColorCommand,
SetTextRotationCommand,
SetTextWrapCommand,
SetVerticalTextAlignCommand, SetWorksheetColIsAutoWidthCommand, SetWorksheetHideCommand,
SetVerticalTextAlignCommand, SetWorksheetHideCommand,
SetWorksheetRowIsAutoHeightCommand,
ToggleGridlinesCommand,
} from '@univerjs/sheets';
Expand Down Expand Up @@ -80,6 +80,7 @@ import { RemoveColConfirmCommand, RemoveRowConfirmCommand } from '../commands/co
import { RemoveSheetConfirmCommand } from '../commands/commands/remove-sheet-confirm.command';
import { SetOnceFormatPainterCommand } from '../commands/commands/set-format-painter.command';
import { CancelFrozenCommand, SetColumnFrozenCommand, SetRowFrozenCommand, SetSelectionFrozenCommand } from '../commands/commands/set-frozen.command';
import { SetWorksheetColAutoWidthCommand } from '../commands/commands/set-worksheet-auto-col-width.command';
import { ShowMenuListCommand } from '../commands/commands/unhide.command';
import {
ChangeSheetProtectionFromSheetBarCommand,
Expand Down Expand Up @@ -487,7 +488,7 @@ export const menuSchema: MenuSchemaType = {
order: 3,
menuItemFactory: SetColWidthMenuItemFactory,
},
[SetWorksheetColIsAutoWidthCommand.id]: {
[SetWorksheetColAutoWidthCommand.id]: {
order: 4,
menuItemFactory: ColAutoWidthMenuItemFactory,
},
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets-ui/src/controllers/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ import {
SetTextWrapCommand,
SetVerticalTextAlignCommand,
SetWorksheetActiveOperation,
SetWorksheetColIsAutoWidthCommand,
SetWorksheetColWidthMutation,
SetWorksheetRowIsAutoHeightCommand,
SetWorksheetRowIsAutoHeightMutation,
Expand Down Expand Up @@ -111,6 +110,7 @@ import {
SetRowFrozenCommand,
SetSelectionFrozenCommand,
} from '../../commands/commands/set-frozen.command';
import { SetWorksheetColAutoWidthCommand } from '../../commands/commands/set-worksheet-auto-col-width.command';
import { COLOR_PICKER_COMPONENT } from '../../components/color-picker';
import { FONT_FAMILY_COMPONENT, FONT_FAMILY_ITEM_COMPONENT } from '../../components/font-family';
import { FONT_SIZE_COMPONENT } from '../../components/font-size';
Expand Down Expand Up @@ -994,7 +994,7 @@ export function FitContentMenuItemFactory(accessor: IAccessor): IMenuButtonItem

export function ColAutoWidthMenuItemFactory(accessor: IAccessor): IMenuButtonItem {
return {
id: SetWorksheetColIsAutoWidthCommand.id,
id: SetWorksheetColAutoWidthCommand.id,
type: MenuItemType.BUTTON,
icon: 'AutoWidth',
title: 'rightClick.fitContent',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import type { IMouseEvent, IPointerEvent, IRenderContext, IRenderModule, Spreads
import type {
IDeltaColumnWidthCommandParams,
IDeltaRowHeightCommand,
ISetWorksheetColIsAutoWidthCommandParams,
ISetWorksheetRowIsAutoHeightCommandParams,
} from '@univerjs/sheets';
import type { ISetWorksheetColIsAutoWidthCommandParams } from '../../commands/commands/set-worksheet-auto-col-width.command';
import {
createInterceptorKey,
Disposable,
Expand All @@ -34,12 +34,13 @@ import {
RANGE_TYPE,
} from '@univerjs/core';
import { CURSOR_TYPE, Rect, SHEET_VIEWPORT_KEY, Vector2 } from '@univerjs/engine-render';

import {
DeltaColumnWidthCommand,
DeltaRowHeightCommand, SetWorksheetColIsAutoWidthCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService,
DeltaRowHeightCommand, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService,
} from '@univerjs/sheets';

import { Subscription } from 'rxjs';
import { SetWorksheetColAutoWidthCommand } from '../../commands/commands/set-worksheet-auto-col-width.command';
import { SHEET_COMPONENT_HEADER_LAYER_INDEX, SHEET_VIEW_KEY } from '../../common/keys';
import { SheetSkeletonManagerService } from '../../services/sheet-skeleton-manager.service';
import {
Expand Down Expand Up @@ -522,7 +523,7 @@ export class HeaderResizeRenderController extends Disposable implements IRenderM
}

this._commandService.executeCommand<ISetWorksheetColIsAutoWidthCommandParams>(
SetWorksheetColIsAutoWidthCommand.id, { ranges }
SetWorksheetColAutoWidthCommand.id, { ranges }
);
this._columnResizeRect?.hide();
break;
Expand Down
4 changes: 3 additions & 1 deletion packages/sheets-ui/src/controllers/sheet-ui.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
MoveSelectionEnterAndTabCommand,
SelectAllCommand,
} from '../commands/commands/set-selection.command';
import { SetWorksheetColAutoWidthCommand } from '../commands/commands/set-worksheet-auto-col-width.command';
import { ChangeZoomRatioCommand, SetZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { ShowMenuListCommand } from '../commands/commands/unhide.command';
import { AddWorksheetProtectionCommand, ChangeSheetProtectionFromSheetBarCommand, DeleteWorksheetProtectionCommand, DeleteWorksheetProtectionFormSheetBarCommand, SetWorksheetProtectionCommand } from '../commands/commands/worksheet-protection.command';
Expand All @@ -93,8 +94,8 @@ import { SheetPermissionOpenDialogOperation } from '../commands/operations/sheet
import { SheetPermissionOpenPanelOperation } from '../commands/operations/sheet-permission-open-panel.operation';
import { SidebarDefinedNameOperation } from '../commands/operations/sidebar-defined-name.operation';
import { BorderPanel } from '../components/border-panel/BorderPanel';
import { BORDER_PANEL_COMPONENT } from '../components/border-panel/interface';

import { BORDER_PANEL_COMPONENT } from '../components/border-panel/interface';
import { COLOR_PICKER_COMPONENT, ColorPicker } from '../components/color-picker';
import {
FONT_FAMILY_COMPONENT,
Expand Down Expand Up @@ -280,6 +281,7 @@ export class SheetUIController extends Disposable {
SetWorksheetProtectionCommand,
DeleteWorksheetProtectionFormSheetBarCommand,
SetProtectionCommand,
SetWorksheetColAutoWidthCommand,
].forEach((c) => {
this.disposeWithMe(this._commandService.registerCommand(c));
});
Expand Down
4 changes: 2 additions & 2 deletions packages/sheets/src/controllers/basic-worksheet.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import {
import { SetTabColorCommand } from '../commands/commands/set-tab-color.command';
import { SetWorkbookNameCommand } from '../commands/commands/set-workbook-name.command';
import { SetWorksheetActivateCommand } from '../commands/commands/set-worksheet-activate.command';
import { DeltaColumnWidthCommand, SetColWidthCommand, SetWorksheetColIsAutoWidthCommand } from '../commands/commands/set-worksheet-col-width.command';
import { DeltaColumnWidthCommand, SetColWidthCommand } from '../commands/commands/set-worksheet-col-width.command';
import { SetWorksheetHideCommand } from '../commands/commands/set-worksheet-hide.command';
import { SetWorksheetNameCommand } from '../commands/commands/set-worksheet-name.command';
import { SetWorksheetOrderCommand } from '../commands/commands/set-worksheet-order.command';
Expand Down Expand Up @@ -255,7 +255,7 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetWorksheetRowIsAutoHeightCommand,
SetWorksheetRowIsAutoHeightMutation,
SetWorksheetColWidthMutation,
SetWorksheetColIsAutoWidthCommand,
// SetWorksheetColIsAutoWidthCommand,

SetSelectionsOperation,
ScrollToCellOperation,
Expand Down
3 changes: 1 addition & 2 deletions packages/sheets/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ export {
export {
DeltaColumnWidthCommand,
type IDeltaColumnWidthCommandParams,
type ISetWorksheetColIsAutoWidthCommandParams,
SetColWidthCommand,
SetWorksheetColIsAutoWidthCommand,
} from './commands/commands/set-worksheet-col-width.command';
export { SetWorksheetHideCommand } from './commands/commands/set-worksheet-hide.command';
export {
Expand Down Expand Up @@ -381,6 +379,7 @@ export {
createAutoColWidthUndoMutationsByRedos,
type ISetWorksheetColWidthMutationParams,
SetWorksheetColWidthMutation,
SetWorksheetColWidthMutationFactory,
} from './commands/mutations/set-worksheet-col-width.mutation';
export { type ISetWorksheetHideMutationParams, SetWorksheetHideMutation } from './commands/mutations/set-worksheet-hide.mutation';
export { type ISetWorksheetNameMutationParams, SetWorksheetNameMutation } from './commands/mutations/set-worksheet-name.mutation';
Expand Down

0 comments on commit f7c9056

Please sign in to comment.