Skip to content

Commit bd5c631

Browse files
CopilotneSpecc
andauthored
fix: allow multiple editor instances to register inline shortcuts on document
Two bugs caused inline shortcuts to fail when multiple EditorJS instances were on the same page: 1. Mismatch: shortcuts were registered on `document` but removed from `this.Editor.UI.nodes.redactor`, so they were never actually removed. 2. Because shortcuts accumulated on `document` forever, the second editor's attempt to register the same shortcuts threw an error (silently caught), meaning the second editor's inline shortcuts were never registered at all. Fixes: - Remove the throw in `Shortcuts.add()` to allow multiple editors to each register their own handler for the same shortcut on `document`. Each handler already guards via `if (!currentBlock) return`, so only the focused editor responds. - Fix `close()` to call `Shortcuts.remove(document, shortcut)` matching where they were registered. - Update type signatures from `Element` to `HTMLElement | Document` to match actual usage. Agent-Logs-Url: https://github.com/codex-team/editor.js/sessions/6c11dfbe-e93a-4fd6-a88e-ef0409aa3d73 Co-authored-by: neSpecc <3684889+neSpecc@users.noreply.github.com>
1 parent 10b7045 commit bd5c631

2 files changed

Lines changed: 5 additions & 13 deletions

File tree

src/components/modules/toolbar/inline.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export default class InlineToolbar extends Module<InlineToolbarNodes> {
110110
const shortcut = this.getToolShortcut(tool.name);
111111

112112
if (shortcut !== undefined) {
113-
Shortcuts.remove(this.Editor.UI.nodes.redactor, shortcut);
113+
Shortcuts.remove(document, shortcut);
114114
}
115115

116116
/**

src/components/utils/shortcuts.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,16 @@ class Shortcuts {
4141
/**
4242
* All registered shortcuts
4343
*
44-
* @type {Map<Element, Shortcut[]>}
44+
* @type {Map<HTMLElement | Document, Shortcut[]>}
4545
*/
46-
private registeredShortcuts: Map<Element, Shortcut[]> = new Map();
46+
private registeredShortcuts: Map<HTMLElement | Document, Shortcut[]> = new Map();
4747

4848
/**
4949
* Register shortcut
5050
*
5151
* @param shortcut - shortcut options
5252
*/
5353
public add(shortcut: ShortcutData): void {
54-
const foundShortcut = this.findShortcut(shortcut.on, shortcut.name);
55-
56-
if (foundShortcut) {
57-
throw Error(
58-
`Shortcut ${shortcut.name} is already registered for ${shortcut.on}. Please remove it before add a new handler.`
59-
);
60-
}
61-
6254
const newShortcut = new Shortcut({
6355
name: shortcut.name,
6456
on: shortcut.on,
@@ -75,7 +67,7 @@ class Shortcuts {
7567
* @param element - Element shortcut is set for
7668
* @param name - shortcut name
7769
*/
78-
public remove(element: Element, name: string): void {
70+
public remove(element: HTMLElement | Document, name: string): void {
7971
const shortcut = this.findShortcut(element, name);
8072

8173
if (!shortcut) {
@@ -104,7 +96,7 @@ class Shortcuts {
10496
* @param shortcut - shortcut name
10597
* @returns {number} index - shortcut index if exist
10698
*/
107-
private findShortcut(element: Element, shortcut: string): Shortcut | void {
99+
private findShortcut(element: HTMLElement | Document, shortcut: string): Shortcut | void {
108100
const shortcuts = this.registeredShortcuts.get(element) || [];
109101

110102
return shortcuts.find(({ name }) => name === shortcut);

0 commit comments

Comments
 (0)