chore: add custom-shortcut example (#5465)

This commit is contained in:
wpxp123456
2025-06-30 17:17:37 +08:00
committed by GitHub
parent 3b5aff2698
commit 81ca5dc7e7
5 changed files with 172 additions and 0 deletions
@@ -0,0 +1,73 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* 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 } from '@univerjs/core';
import { CommandType, ICommandService, IUniverInstanceService } from '@univerjs/core';
import { ClearSelectionContentCommand, getSheetCommandTarget, SheetsSelectionsService } from '@univerjs/sheets';
/**
* The command to clear content in current selected ranges.
*/
export const CustomClearSelectionContentCommand: ICommand = {
id: 'sheet.command.custom-clear-selection-content',
type: CommandType.COMMAND,
handler: (accessor: IAccessor) => {
const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
if (!target) return false;
const { unitId, subUnitId, worksheet } = target;
const selectionManagerService = accessor.get(SheetsSelectionsService);
const range = selectionManagerService.getCurrentLastSelection()?.range;
if (!range) return false;
const commandService = accessor.get(ICommandService);
const { startRow, endRow, startColumn, endColumn } = range;
const isSingleCell = startRow === endRow && startColumn === endColumn;
if (isSingleCell && startRow === 2 && startColumn === 2) {
// If the range is cell C3, clear the entire row.
return commandService.executeCommand(ClearSelectionContentCommand.id, {
unitId,
subUnitId,
ranges: [
{
startRow,
endRow,
startColumn: 0,
endColumn: worksheet.getMaxColumns() - 1,
},
],
});
} else {
// Clear the selected range.
return commandService.executeCommand(ClearSelectionContentCommand.id, {
unitId,
subUnitId,
ranges: [
{
startRow,
endRow,
startColumn,
endColumn,
},
],
});
}
},
};
@@ -0,0 +1,30 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* 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 { IShortcutItem } from '@univerjs/ui';
import { whenSheetEditorFocused } from '@univerjs/sheets-ui';
import { KeyCode } from '@univerjs/ui';
import { CustomClearSelectionContentCommand } from '../../commands/commands/custom.command';
export const CustomClearSelectionValueShortcutItem: IShortcutItem = {
id: CustomClearSelectionContentCommand.id,
// high priority to ensure it is checked first
priority: 9999,
// when focusing on any other input tag do not trigger this shortcut
preconditions: whenSheetEditorFocused,
binding: KeyCode.DELETE,
mac: KeyCode.BACKSPACE,
};
@@ -0,0 +1,17 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* 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.
*/
export { UniverSheetsCustomShortcutPlugin } from './plugin';
@@ -0,0 +1,50 @@
/**
* Copyright 2023-present DreamNum Co., Ltd.
*
* 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 { ICommandService, Inject, Injector, Plugin, UniverInstanceType } from '@univerjs/core';
import { IShortcutService } from '@univerjs/ui';
import { CustomClearSelectionContentCommand } from './commands/commands/custom.command';
import { CustomClearSelectionValueShortcutItem } from './controllers/shortcuts/custom.shortcut';
const SHEET_CUSTOM_SHORTCUT_PLUGIN = 'SHEET_CUSTOM_SHORTCUT_PLUGIN';
export class UniverSheetsCustomShortcutPlugin extends Plugin {
static override type = UniverInstanceType.UNIVER_SHEET;
static override pluginName = SHEET_CUSTOM_SHORTCUT_PLUGIN;
constructor(
@Inject(Injector) protected readonly _injector: Injector,
@ICommandService private readonly _commandService: ICommandService,
@IShortcutService private readonly _shortcutService: IShortcutService
) {
super();
this._initCommands();
this._initShortcuts();
}
private _initCommands() {
[
CustomClearSelectionContentCommand,
].forEach((command) => this.disposeWithMe(this._commandService.registerCommand(command)));
}
private _initShortcuts() {
[
CustomClearSelectionValueShortcutItem,
].forEach((item) => this.disposeWithMe(this._shortcutService.registerShortcut(item)));
}
}
+2
View File
@@ -41,6 +41,7 @@ import { UniverSheetsZenEditorPlugin } from '@univerjs/sheets-zen-editor';
import { UniverUIPlugin } from '@univerjs/ui';
import { UniverVue3AdapterPlugin } from '@univerjs/ui-adapter-vue3';
import { UniverWebComponentAdapterPlugin } from '@univerjs/ui-adapter-web-component';
import { UniverSheetsCustomShortcutPlugin } from './custom-plugin/custom-shortcut';
import ImportCSVButtonPlugin from './custom-plugin/import-csv-button';
import '@univerjs/sheets/facade';
@@ -126,6 +127,7 @@ function createNewInstance() {
univer.registerPlugin(UniverNetworkPlugin);
univer.registerPlugin(UniverSheetsNotePlugin);
univer.registerPlugin(ImportCSVButtonPlugin);
univer.registerPlugin(UniverSheetsCustomShortcutPlugin);
// If we are running in e2e platform, we should immediately register the debugger plugin.
if (IS_E2E) {