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(ui): fix the issue where disabling shortcuts through the facade API does not work (#4618)

Dushusir684db12b6428
diff --git a/packages/ui/src/facade/f-shortcut.ts b/packages/ui/src/facade/f-shortcut.ts
index e3dfbcb451..34e871ab7a 100644
--- a/packages/ui/src/facade/f-shortcut.ts
+++ b/packages/ui/src/facade/f-shortcut.ts
@@ -25,7 +25,7 @@ import { IShortcutService } from '@univerjs/ui';
* @hideconstructor
*/
export class FShortcut extends FBase {
- private _forceEscapeDisposable: IDisposable | null = null;
+ private _forceDisableDisposable: IDisposable | null = null;
constructor(
@Inject(Injector) protected readonly _injector: Injector,
@@ -37,20 +37,31 @@ export class FShortcut extends FBase {
/**
* Enable shortcuts of Univer.
* @returns {FShortcut} The Facade API instance itself for chaining.
+ *
+ * @example
+ * ```typescript
+ * fShortcut.enableShortcut(); // Use the FShortcut instance used by disableShortcut before, do not create a new instance
+ * ```
*/
enableShortcut(): this {
- this._forceEscapeDisposable?.dispose();
- this._forceEscapeDisposable = null;
+ this._forceDisableDisposable?.dispose();
+ this._forceDisableDisposable = null;
return this;
}
/**
* Disable shortcuts of Univer.
* @returns {FShortcut} The Facade API instance itself for chaining.
+ *
+ * @example
+ * ```typescript
+ * const fShortcut = univerAPI.getShortcut();
+ * fShortcut.disableShortcut();
+ * ```
*/
disableShortcut(): this {
- if (!this._forceEscapeDisposable) {
- this._forceEscapeDisposable = this._shortcutService.forceEscape();
+ if (!this._forceDisableDisposable) {
+ this._forceDisableDisposable = this._shortcutService.forceDisable();
}
return this;
diff --git a/packages/ui/src/services/shortcut/shortcut.service.ts b/packages/ui/src/services/shortcut/shortcut.service.ts
index b26b066cc1..a1b119479a 100644
--- a/packages/ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/ui/src/services/shortcut/shortcut.service.ts
@@ -100,6 +100,12 @@ export interface IShortcutService {
*/
forceEscape(): IDisposable;
+ /**
+ * Used by API to force disable all shortcut keys, which will not be restored by selection
+ * @returns {IDisposable} a disposable that could be used to cancel the force disabling.
+ */
+ forceDisable(): IDisposable;
+
/**
* Dispatch a keyboard event to the shortcut service and check if there is a shortcut that matches the event.
* @param e - the keyboard event to be dispatched.
@@ -142,6 +148,8 @@ export class ShortcutService extends Disposable implements IShortcutService {
private _forceEscaped = false;
+ private _forceDisabled = false;
+
constructor(
@ICommandService private readonly _commandService: ICommandService,
@IPlatformService private readonly _platformService: IPlatformService,
@@ -239,6 +247,13 @@ export class ShortcutService extends Disposable implements IShortcutService {
return toDisposable(() => (this._forceEscaped = false));
}
+ forceDisable(): IDisposable {
+ this._forceDisabled = true;
+ return toDisposable(() => {
+ this._forceDisabled = false;
+ });
+ }
+
private _resolveKeyboardEvent(e: KeyboardEvent): void {
const candidate = this.dispatch(e);
if (candidate) {
@@ -251,8 +266,9 @@ export class ShortcutService extends Disposable implements IShortcutService {
// Should get the container element of the Univer instance and see if
// the event target is a descendant of the container element.
// Also we should check through escape list and force catching list.
- // if the target is not focused on the univer instance we should ignore the keyboard event
- if (this._forceEscaped) return;
+ // if the target is not focused on the univer instance we should ignore the keyboard event.
+ // Maybe the user has forcibly disabled the shortcut keys, and the shortcut keys should not be processed at this time.
+ if (this._forceEscaped || this._forceDisabled) return;
if (
this._layoutService &&