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: route embedded editor shortcuts explicitly (#7337)

Univerd4c32e5909b3
diff --git a/packages/docs-ui/src/views/list-type-picker/Picker.tsx b/packages/docs-ui/src/views/list-type-picker/Picker.tsx
index 3d0d43cb6e..cf7dceb5eb 100644
--- a/packages/docs-ui/src/views/list-type-picker/Picker.tsx
+++ b/packages/docs-ui/src/views/list-type-picker/Picker.tsx
@@ -29,7 +29,10 @@ interface IListTypePickerProps extends IListTypePickerBaseProps {
export const ListTypePicker = (props: IListTypePickerProps) => {
const { value, onChange, options } = props;
return (
- <div className="univer-grid univer-grid-cols-3 univer-gap-2 univer-p-1.5">
+ <div
+ className="univer-grid univer-grid-cols-3 univer-gap-2 univer-p-1.5"
+ data-u-editor-interaction-boundary="true"
+ >
{options.map((item) => {
return (
<a
diff --git a/packages/docs-ui/src/views/rich-text-editor/hooks/use-keyboard-event.ts b/packages/docs-ui/src/views/rich-text-editor/hooks/use-keyboard-event.ts
index 4924033702..f87cd690fb 100644
--- a/packages/docs-ui/src/views/rich-text-editor/hooks/use-keyboard-event.ts
+++ b/packages/docs-ui/src/views/rich-text-editor/hooks/use-keyboard-event.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { KeyCode, MetaKeys } from '@univerjs/ui';
+import type { KeyCode, MetaKeys, NativeTextEditorShortcutBehavior } from '@univerjs/ui';
import type { Editor } from '../../../services/editor/editor';
import { CommandType, DisposableCollection, generateRandomId, ICommandService } from '@univerjs/core';
import { DeviceInputEventType } from '@univerjs/engine-render';
@@ -22,7 +22,11 @@ import { IShortcutService, useDependency } from '@univerjs/ui';
import { useEffect, useMemo } from 'react';
export interface IKeyboardEventConfig {
- keyCodes: { keyCode: KeyCode; metaKey?: MetaKeys }[];
+ keyCodes: {
+ keyCode: KeyCode;
+ metaKey?: MetaKeys;
+ nativeTextEditorBehavior?: NativeTextEditorShortcutBehavior;
+ }[];
handler: (keyCode: KeyCode, metaKey?: MetaKeys) => void;
}
@@ -52,6 +56,12 @@ export function useKeyboardEvent(isNeed: boolean, config?: IKeyboardEventConfig,
return {
id: operationId,
binding: keyCode.metaKey ? keyCode.keyCode | keyCode.metaKey : keyCode.keyCode,
+ eventPreconditions: keyCode.nativeTextEditorBehavior == null
+ ? undefined
+ : (event: KeyboardEvent) =>
+ event.target instanceof HTMLElement &&
+ event.target.id === `__editor_${editorId}`,
+ nativeTextEditorBehavior: keyCode.nativeTextEditorBehavior,
preconditions: () => true,
priority: 901,
staticParameters: {
diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts
index 04ff1fc759..5223cb091e 100644
--- a/packages/ui/src/index.ts
+++ b/packages/ui/src/index.ts
@@ -120,7 +120,7 @@ export { IUIRuntimeScopeService, UIRuntimeScopeService } from './services/runtim
export type { IUIRuntimeScope } from './services/runtime-scope/ui-runtime-scope.service';
export { KeyCode, MetaKeys } from './services/shortcut/keycode';
export { ShortcutPanelService } from './services/shortcut/shortcut-panel.service';
-export { IShortcutService, ShortcutService } from './services/shortcut/shortcut.service';
+export { IShortcutService, NativeTextEditorShortcutBehavior, ShortcutService } from './services/shortcut/shortcut.service';
export type { IShortcutItem } from './services/shortcut/shortcut.service';
export { DesktopSidebarService } from './services/sidebar/desktop-sidebar.service';
export { useSidebarClick } from './services/sidebar/hooks/use-sidebar-click';
diff --git a/packages/ui/src/services/shortcut/__tests__/shortcut.service.spec.ts b/packages/ui/src/services/shortcut/__tests__/shortcut.service.spec.ts
index c82a76c37b..9cd1c912c2 100644
--- a/packages/ui/src/services/shortcut/__tests__/shortcut.service.spec.ts
+++ b/packages/ui/src/services/shortcut/__tests__/shortcut.service.spec.ts
@@ -25,7 +25,7 @@ import { ILayoutService } from '../../layout/layout.service';
import { IPlatformService } from '../../platform/platform.service';
import { IUIRuntimeScopeService } from '../../runtime-scope/ui-runtime-scope.service';
import { KeyCode, MetaKeys } from '../keycode';
-import { ShortcutService } from '../shortcut.service';
+import { NativeTextEditorShortcutBehavior, ShortcutService } from '../shortcut.service';
function createKeyboardEvent(
keyCode: number,
@@ -164,6 +164,12 @@ describe('ShortcutService', () => {
priority: 10,
preconditions: () => false,
};
+ const eventBlocked = {
+ id: 'cmd.event-blocked',
+ binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
+ priority: 11,
+ eventPreconditions: () => false,
+ };
const highAllowed = {
id: 'cmd.high',
binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
@@ -173,6 +179,7 @@ describe('ShortcutService', () => {
service.registerShortcut(low);
service.registerShortcut(highBlocked);
+ service.registerShortcut(eventBlocked);
service.registerShortcut(highAllowed);
const event = createKeyboardEvent(KeyCode.A, { ctrlKey: true });
@@ -319,6 +326,7 @@ describe('ShortcutService', () => {
service.registerShortcut({
id: 'cmd.select-all',
binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
+ nativeTextEditorBehavior: NativeTextEditorShortcutBehavior.ALLOW_NATIVE,
});
const event = createKeyboardEvent(KeyCode.A, { ctrlKey: true });
@@ -334,6 +342,63 @@ describe('ShortcutService', () => {
service.dispose();
});
+ it('lets an explicit shortcut override native select-all in an embedded text editor', () => {
+ const embedRoot = document.createElement('div');
+ embedRoot.setAttribute('data-embed-interaction-boundary-owner', 'embed-1');
+ embedRoot.setAttribute(EMBED_CHILD_UNIT_ID_ATTRIBUTE, 'child-doc');
+ const textEditor = document.createElement('div');
+ textEditor.contentEditable = 'true';
+ Object.defineProperty(textEditor, 'isContentEditable', {
+ configurable: true,
+ get: () => true,
+ });
+ embedRoot.appendChild(textEditor);
+ const otherTextEditor = document.createElement('div');
+ otherTextEditor.contentEditable = 'true';
+ embedRoot.appendChild(otherTextEditor);
+
+ const scopedExecuteCommand = vi.fn(() => Promise.resolve(true));
+ const scopedContextService = {
+ getContextValue: vi.fn(),
+ };
+ const scopedCommandService = {
+ executeCommand: scopedExecuteCommand,
+ };
+ const runtimeScope = {
+ has: vi.fn(() => true),
+ get: vi.fn((identifier: unknown) => identifier === IContextService ? scopedContextService : scopedCommandService),
+ };
+ const { service, executeCommand, getRuntimeScope } = createService();
+ getRuntimeScope.mockReturnValue(runtimeScope);
+ service.registerShortcut({
+ id: 'doc.command.select-all',
+ binding: KeyCode.A | MetaKeys.CTRL_COMMAND,
+ eventPreconditions: (event) => event.target === textEditor,
+ nativeTextEditorBehavior: NativeTextEditorShortcutBehavior.OVERRIDE_NATIVE,
+ });
+
+ const event = createKeyboardEvent(KeyCode.A, { ctrlKey: true });
+ Object.defineProperty(event, 'target', {
+ configurable: true,
+ get: () => textEditor,
+ });
+ window.dispatchEvent(event);
+
+ expect(getRuntimeScope).toHaveBeenCalledWith('child-doc');
+ expect(scopedExecuteCommand).toHaveBeenCalledWith('doc.command.select-all', undefined);
+ expect(executeCommand).not.toHaveBeenCalled();
+ expect(event.defaultPrevented).toBe(true);
+
+ const otherEvent = createKeyboardEvent(KeyCode.A, { ctrlKey: true });
+ Object.defineProperty(otherEvent, 'target', {
+ configurable: true,
+ get: () => otherTextEditor,
+ });
+
+ expect(service.dispatch(otherEvent)).toBeUndefined();
+ service.dispose();
+ });
+
it('lets embed-owned Univer internal editors handle select-all without dispatching global shortcuts', () => {
const embedRoot = document.createElement('div');
embedRoot.setAttribute('data-embed-interaction-boundary-owner', 'embed-1');
diff --git a/packages/ui/src/services/shortcut/shortcut.service.ts b/packages/ui/src/services/shortcut/shortcut.service.ts
index 13ddbd9826..2fe9e6dd10 100644
--- a/packages/ui/src/services/shortcut/shortcut.service.ts
+++ b/packages/ui/src/services/shortcut/shortcut.service.ts
@@ -25,6 +25,15 @@ import { IPlatformService } from '../platform/platform.service';
import { IUIRuntimeScopeService } from '../runtime-scope/ui-runtime-scope.service';
import { KeyCode, KeyCodeToChar, MetaKeys } from './keycode';
+/**
+ * Defines whether a Univer shortcut should yield to the browser's native behavior
+ * when the keyboard event originates from an editable text element.
+ */
+export enum NativeTextEditorShortcutBehavior {
+ ALLOW_NATIVE,
+ OVERRIDE_NATIVE,
+}
+
/**
* A shortcut item that could be registered to the {@link IShortcutService}.
*/
@@ -44,6 +53,11 @@ export interface IShortcutItem<P extends object = object> {
*/
preconditions?: (contextService: IContextService) => boolean;
+ /**
+ * A callback that examines the keyboard event before this shortcut is selected.
+ */
+ eventPreconditions?: (event: KeyboardEvent) => boolean;
+
/**
* The binding of the shortcut. It should be a combination of {@link KeyCode} and {@link MetaKeys}.
*
@@ -65,6 +79,12 @@ export interface IShortcutItem<P extends object = object> {
*/
linux?: number;
+ /**
+ * Controls whether this shortcut can override the browser's native behavior
+ * for editable text elements. Native behavior is preserved by default.
+ */
+ nativeTextEditorBehavior?: NativeTextEditorShortcutBehavior;
+
/**
* The group of the menu item should belong to. The shortcut item would be rendered in the
* panel if this is set.
@@ -296,10 +316,6 @@ export class ShortcutService extends Disposable implements IShortcutService {
return undefined;
}
- if (this._shouldLetEmbedTextEditorHandleNativeShortcut(e, binding)) {
- return undefined;
- }
-
const shortcuts = this._shortCutMapping.get(binding);
if (shortcuts === undefined) {
return undefined;
@@ -308,7 +324,17 @@ export class ShortcutService extends Disposable implements IShortcutService {
const contextService = this._getRuntimeService<IContextService>(e, IContextService) ?? this._contextService;
const candidateShortcut = Array.from(shortcuts)
.sort((s1, s2) => (s2.priority ?? 0) - (s1.priority ?? 0))
- .find((s) => s.preconditions?.(contextService) ?? true);
+ .find((s) =>
+ (s.eventPreconditions?.(e) ?? true) &&
+ (s.preconditions?.(contextService) ?? true)
+ );
+
+ if (
+ this._shouldLetEmbedTextEditorHandleNativeShortcut(e, binding) &&
+ candidateShortcut?.nativeTextEditorBehavior !== NativeTextEditorShortcutBehavior.OVERRIDE_NATIVE
+ ) {
+ return undefined;
+ }
return candidateShortcut;
}