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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
chore: add custom-shortcut example (#5465)
wpxp12345681ca5dc7e7f6
diff --git a/examples/src/sheets/custom-plugin/custom-shortcut/commands/commands/custom.command.ts b/examples/src/sheets/custom-plugin/custom-shortcut/commands/commands/custom.command.tsnew file mode 100644index 0000000000..e456f628f0--- /dev/null+++ b/examples/src/sheets/custom-plugin/custom-shortcut/commands/commands/custom.command.ts@@ -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,+ },+ ],+ });+ }+ },+};diff --git a/examples/src/sheets/custom-plugin/custom-shortcut/controllers/shortcuts/custom.shortcut.ts b/examples/src/sheets/custom-plugin/custom-shortcut/controllers/shortcuts/custom.shortcut.tsnew file mode 100644index 0000000000..50c4860d7d--- /dev/null+++ b/examples/src/sheets/custom-plugin/custom-shortcut/controllers/shortcuts/custom.shortcut.ts@@ -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,+};diff --git a/examples/src/sheets/custom-plugin/custom-shortcut/index.ts b/examples/src/sheets/custom-plugin/custom-shortcut/index.tsnew file mode 100644index 0000000000..bb7cafec43--- /dev/null+++ b/examples/src/sheets/custom-plugin/custom-shortcut/index.ts@@ -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';diff --git a/examples/src/sheets/custom-plugin/custom-shortcut/plugin.ts b/examples/src/sheets/custom-plugin/custom-shortcut/plugin.tsnew file mode 100644index 0000000000..3d9c7a4a05--- /dev/null+++ b/examples/src/sheets/custom-plugin/custom-shortcut/plugin.ts@@ -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)));+ }+}diff --git a/examples/src/sheets/main.ts b/examples/src/sheets/main.tsindex 6aaf48e3ed..9c17782aee 100644--- a/examples/src/sheets/main.ts+++ b/examples/src/sheets/main.ts@@ -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) {