@blocknote/core 0.54.2 → 0.55.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -1
- package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -1
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +6 -6
- package/dist/blocks-DLgTreVu.cjs +12 -0
- package/dist/blocks-DLgTreVu.cjs.map +1 -0
- package/dist/{blocks-Dol85hpH.js → blocks-DaEUT0O1.js} +346 -340
- package/dist/blocks-DaEUT0O1.js.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +1 -1
- package/dist/comments.cjs +1 -1
- package/dist/comments.cjs.map +1 -1
- package/dist/comments.js +62 -36
- package/dist/comments.js.map +1 -1
- package/dist/{en-uzHzj6eH.js → en-BAn9gxr-.js} +5 -2
- package/dist/{en-uzHzj6eH.js.map → en-BAn9gxr-.js.map} +1 -1
- package/dist/{en-BuzSuuTq.cjs → en-CHnrV5l8.cjs} +2 -2
- package/dist/{en-BuzSuuTq.cjs.map → en-CHnrV5l8.cjs.map} +1 -1
- package/dist/{extensions-D6JcJCwd.js → extensions-B1JQURVF.js} +221 -221
- package/dist/extensions-B1JQURVF.js.map +1 -0
- package/dist/{extensions-B7Rsp_Ft.cjs → extensions-CZkqKzJO.cjs} +3 -3
- package/dist/extensions-CZkqKzJO.cjs.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +2 -2
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +89 -23
- package/dist/locales.js.map +1 -1
- package/dist/{src-Buuo5l7X.js → src-D5w0XAHX.js} +702 -640
- package/dist/src-D5w0XAHX.js.map +1 -0
- package/dist/{src-C54Ok9E0.cjs → src-th5uZEvx.cjs} +8 -8
- package/dist/src-th5uZEvx.cjs.map +1 -0
- package/dist/webpack-stats.json +1 -1
- package/dist/y.cjs +1 -1
- package/dist/y.js +3 -3
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +1 -1
- package/package.json +12 -12
- package/src/comments/CommentEditorSubmitExtension.test.ts +141 -0
- package/src/comments/CommentEditorSubmitExtension.ts +56 -0
- package/src/comments/extension.ts +8 -0
- package/src/comments/index.ts +1 -0
- package/src/editor/BlockNoteEditor.ts +94 -41
- package/src/editor/BlockNoteExtension.ts +6 -1
- package/src/editor/managers/EventManager.browser.test.ts +334 -0
- package/src/editor/managers/EventManager.ts +128 -31
- package/src/editor/managers/ExtensionManager/ExtensionManager.test.ts +88 -1
- package/src/editor/managers/ExtensionManager/index.ts +30 -7
- package/src/editor/managers/StyleManager.browser.test.ts +122 -0
- package/src/editor/managers/StyleManager.test.ts +98 -0
- package/src/editor/managers/StyleManager.ts +31 -15
- package/src/editor/managers/index.ts +1 -0
- package/src/extensions/FormattingToolbar/FormattingToolbar.ts +23 -0
- package/src/extensions/SideMenu/SideMenu.ts +15 -0
- package/src/extensions/TableHandles/TableHandles.browser.test.ts +3 -1
- package/src/i18n/locales/ar.ts +1 -0
- package/src/i18n/locales/de.ts +1 -0
- package/src/i18n/locales/en.ts +1 -0
- package/src/i18n/locales/es.ts +1 -0
- package/src/i18n/locales/fa.ts +1 -0
- package/src/i18n/locales/fr.ts +1 -0
- package/src/i18n/locales/he.ts +1 -0
- package/src/i18n/locales/hr.ts +1 -0
- package/src/i18n/locales/is.ts +1 -0
- package/src/i18n/locales/it.ts +1 -0
- package/src/i18n/locales/ja.ts +1 -0
- package/src/i18n/locales/ko.ts +1 -0
- package/src/i18n/locales/nl.ts +1 -0
- package/src/i18n/locales/no.ts +1 -0
- package/src/i18n/locales/pl.ts +1 -0
- package/src/i18n/locales/pt.ts +1 -0
- package/src/i18n/locales/ru.ts +1 -0
- package/src/i18n/locales/sk.ts +1 -0
- package/src/i18n/locales/uk.ts +1 -0
- package/src/i18n/locales/uz.ts +1 -0
- package/src/i18n/locales/vi.ts +1 -0
- package/src/i18n/locales/zh-tw.ts +1 -0
- package/src/i18n/locales/zh.ts +1 -0
- package/src/index.ts +1 -0
- package/src/util/browser.ts +24 -0
- package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +4 -0
- package/types/src/comments/CommentEditorSubmitExtension.d.ts +19 -0
- package/types/src/comments/CommentEditorSubmitExtension.test.d.ts +1 -0
- package/types/src/comments/extension.d.ts +7 -0
- package/types/src/comments/index.d.ts +1 -0
- package/types/src/editor/BlockNoteEditor.d.ts +52 -19
- package/types/src/editor/BlockNoteExtension.d.ts +3 -0
- package/types/src/editor/managers/EventManager.browser.test.d.ts +1 -0
- package/types/src/editor/managers/EventManager.d.ts +55 -0
- package/types/src/editor/managers/StyleManager.browser.test.d.ts +1 -0
- package/types/src/editor/managers/StyleManager.d.ts +4 -2
- package/types/src/editor/managers/StyleManager.test.d.ts +1 -0
- package/types/src/editor/managers/index.d.ts +1 -0
- package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +3 -0
- package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +1 -0
- package/types/src/i18n/locales/en.d.ts +1 -0
- package/types/src/i18n/locales/fa.d.ts +1 -0
- package/types/src/i18n/locales/sk.d.ts +1 -0
- package/types/src/index.d.ts +1 -0
- package/types/src/util/browser.d.ts +2 -0
- package/dist/blocks-CTB_5Vyj.cjs +0 -12
- package/dist/blocks-CTB_5Vyj.cjs.map +0 -1
- package/dist/blocks-Dol85hpH.js.map +0 -1
- package/dist/extensions-B7Rsp_Ft.cjs.map +0 -1
- package/dist/extensions-D6JcJCwd.js.map +0 -1
- package/dist/src-Buuo5l7X.js.map +0 -1
- package/dist/src-C54Ok9E0.cjs.map +0 -1
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
|
|
2
|
+
|
|
3
|
+
import { BlockNoteEditor } from "../BlockNoteEditor.js";
|
|
4
|
+
|
|
5
|
+
function appendToRegisteredPortalElement(
|
|
6
|
+
editor: { registerPortalElement(element: HTMLElement): void },
|
|
7
|
+
...elements: HTMLElement[]
|
|
8
|
+
) {
|
|
9
|
+
const root = document.createElement("div");
|
|
10
|
+
document.body.append(root);
|
|
11
|
+
editor.registerPortalElement(root);
|
|
12
|
+
root.append(...elements);
|
|
13
|
+
return root;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Focus tracking is almost entirely DOM semantics — event ordering, what
|
|
17
|
+
// `document.activeElement` reads as at each step, and how focus behaves when
|
|
18
|
+
// it moves into UI that is portalled outside the editor. None of that is
|
|
19
|
+
// reproducible in jsdom, so these run in the browser suite across all three
|
|
20
|
+
// engines.
|
|
21
|
+
|
|
22
|
+
/** Resolves once the deferred focus settle has run (see attachUIFocusTracker). */
|
|
23
|
+
function settle() {
|
|
24
|
+
return new Promise((resolve) => setTimeout(resolve, 20));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe("Focus events", () => {
|
|
28
|
+
let editor: BlockNoteEditor<any, any, any>;
|
|
29
|
+
let container: HTMLElement;
|
|
30
|
+
let outside: HTMLInputElement;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Mounts an editor inside its own container, mirroring how `BlockNoteView`
|
|
34
|
+
* renders it. The nesting matters: `isWithinEditor` — which `isFocused`
|
|
35
|
+
* and the focus events are built on — treats the mount element's *parent*
|
|
36
|
+
* as the editor's boundary, so that it also covers UI rendered as a
|
|
37
|
+
* sibling of the content area. Mounting straight into `<body>` would make
|
|
38
|
+
* that boundary the whole page.
|
|
39
|
+
*/
|
|
40
|
+
function mountEditor() {
|
|
41
|
+
const editorContainer = document.createElement("div");
|
|
42
|
+
const mountPoint = document.createElement("div");
|
|
43
|
+
editorContainer.append(mountPoint);
|
|
44
|
+
document.body.append(editorContainer);
|
|
45
|
+
const instance = BlockNoteEditor.create();
|
|
46
|
+
instance.mount(mountPoint);
|
|
47
|
+
return { editor: instance, container: editorContainer };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
beforeEach(() => {
|
|
51
|
+
outside = document.createElement("input");
|
|
52
|
+
outside.id = "outside";
|
|
53
|
+
document.body.append(outside);
|
|
54
|
+
|
|
55
|
+
({ editor, container } = mountEditor());
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
editor.unmount();
|
|
60
|
+
container.remove();
|
|
61
|
+
outside.remove();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* The DOM contract the tracker is built on
|
|
66
|
+
* (https://developer.mozilla.org/en-US/docs/Web/API/FocusEvent#order_of_events).
|
|
67
|
+
* If a future engine changes this ordering, the tracker's "settle
|
|
68
|
+
* immediately on focusin, defer on focusout" split stops being valid — so
|
|
69
|
+
* it's asserted rather than assumed.
|
|
70
|
+
*/
|
|
71
|
+
it("follows the documented focus event order", async () => {
|
|
72
|
+
const a = document.createElement("input");
|
|
73
|
+
const b = document.createElement("input");
|
|
74
|
+
document.body.append(a, b);
|
|
75
|
+
const order: string[] = [];
|
|
76
|
+
for (const [name, element] of [
|
|
77
|
+
["a", a],
|
|
78
|
+
["b", b],
|
|
79
|
+
] as const) {
|
|
80
|
+
for (const type of ["blur", "focusout", "focus", "focusin"]) {
|
|
81
|
+
element.addEventListener(type, () => order.push(`${type}:${name}`));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
a.focus();
|
|
86
|
+
order.length = 0;
|
|
87
|
+
b.focus();
|
|
88
|
+
|
|
89
|
+
expect(order).toEqual(["blur:a", "focusout:a", "focus:b", "focusin:b"]);
|
|
90
|
+
|
|
91
|
+
a.remove();
|
|
92
|
+
b.remove();
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Why the focusout side has to be deferred: at focusout time the outgoing
|
|
97
|
+
* element has *already* lost focus and `document.activeElement` reads as
|
|
98
|
+
* `<body>`, so the destination isn't knowable yet. (`relatedTarget` can't
|
|
99
|
+
* substitute — MDN documents it as null in cases like tabbing out of the
|
|
100
|
+
* page, and it is unreliable on mobile.)
|
|
101
|
+
*/
|
|
102
|
+
it("reports <body> as the active element during focusout", async () => {
|
|
103
|
+
const a = document.createElement("input");
|
|
104
|
+
document.body.append(a);
|
|
105
|
+
let activeDuringFocusOut: Element | null = null;
|
|
106
|
+
a.addEventListener("focusout", () => {
|
|
107
|
+
activeDuringFocusOut = document.activeElement;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
a.focus();
|
|
111
|
+
a.blur();
|
|
112
|
+
|
|
113
|
+
expect(activeDuringFocusOut).toBe(document.body);
|
|
114
|
+
a.remove();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("isFocused() tracks the content area", async () => {
|
|
118
|
+
expect(editor.isFocused()).toBe(false);
|
|
119
|
+
|
|
120
|
+
editor.focus();
|
|
121
|
+
expect(editor.isFocused()).toBe(true);
|
|
122
|
+
|
|
123
|
+
outside.focus();
|
|
124
|
+
expect(editor.isFocused()).toBe(false);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("isFocused({ includeEditorUI }) counts the editor's own UI", async () => {
|
|
128
|
+
// The portal element is where menus, toolbars and popovers render — it
|
|
129
|
+
// lives outside the content area, so plain content focus can't see it.
|
|
130
|
+
const popoverInput = document.createElement("input");
|
|
131
|
+
appendToRegisteredPortalElement(editor, popoverInput);
|
|
132
|
+
|
|
133
|
+
popoverInput.focus();
|
|
134
|
+
|
|
135
|
+
expect(editor.isFocused()).toBe(false);
|
|
136
|
+
expect(editor.isFocused({ includeEditorUI: true })).toBe(true);
|
|
137
|
+
|
|
138
|
+
outside.focus();
|
|
139
|
+
expect(editor.isFocused({ includeEditorUI: true })).toBe(false);
|
|
140
|
+
|
|
141
|
+
popoverInput.remove();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("onFocusChange reports content focus and blur", async () => {
|
|
145
|
+
const events: boolean[] = [];
|
|
146
|
+
const unsubscribe = editor.onFocusChange((_editor, ctx) =>
|
|
147
|
+
events.push(ctx.focused),
|
|
148
|
+
);
|
|
149
|
+
|
|
150
|
+
editor.focus();
|
|
151
|
+
await settle();
|
|
152
|
+
outside.focus();
|
|
153
|
+
await settle();
|
|
154
|
+
|
|
155
|
+
expect(events).toEqual([true, false]);
|
|
156
|
+
unsubscribe();
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("onFocusChange({ includeEditorUI }) stays focused across a handoff into the editor's UI", async () => {
|
|
160
|
+
const popoverInput = document.createElement("input");
|
|
161
|
+
appendToRegisteredPortalElement(editor, popoverInput);
|
|
162
|
+
|
|
163
|
+
const events: boolean[] = [];
|
|
164
|
+
const unsubscribe = editor.onFocusChange(
|
|
165
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
166
|
+
{ includeEditorUI: true },
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
editor.focus();
|
|
170
|
+
await settle();
|
|
171
|
+
// Content -> a popover input. This is the handoff that matters: the raw
|
|
172
|
+
// channel would report a blur here, which is what used to tear the mobile
|
|
173
|
+
// toolbar (and the popover with it) down mid-interaction.
|
|
174
|
+
popoverInput.focus();
|
|
175
|
+
await settle();
|
|
176
|
+
|
|
177
|
+
expect(events.at(-1)).toBe(true);
|
|
178
|
+
expect(events).not.toContain(false);
|
|
179
|
+
|
|
180
|
+
// Leaving the editor entirely does report a blur.
|
|
181
|
+
outside.focus();
|
|
182
|
+
await settle();
|
|
183
|
+
expect(events.at(-1)).toBe(false);
|
|
184
|
+
|
|
185
|
+
unsubscribe();
|
|
186
|
+
popoverInput.remove();
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("does not report a spurious blur while focus moves between UI elements", async () => {
|
|
190
|
+
const first = document.createElement("input");
|
|
191
|
+
const second = document.createElement("input");
|
|
192
|
+
appendToRegisteredPortalElement(editor, first, second);
|
|
193
|
+
|
|
194
|
+
editor.focus();
|
|
195
|
+
await settle();
|
|
196
|
+
|
|
197
|
+
const events: boolean[] = [];
|
|
198
|
+
const unsubscribe = editor.onFocusChange(
|
|
199
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
200
|
+
{ includeEditorUI: true },
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
first.focus();
|
|
204
|
+
second.focus();
|
|
205
|
+
editor.focus();
|
|
206
|
+
await settle();
|
|
207
|
+
|
|
208
|
+
expect(events).not.toContain(false);
|
|
209
|
+
|
|
210
|
+
unsubscribe();
|
|
211
|
+
first.remove();
|
|
212
|
+
second.remove();
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
it("ignores focus changes that never involve the editor", async () => {
|
|
216
|
+
// The tracker listens at the document level, so it sees every focus
|
|
217
|
+
// change on the page — including ones with nothing to do with this
|
|
218
|
+
// editor. Those must not reach subscribers.
|
|
219
|
+
const otherA = document.createElement("input");
|
|
220
|
+
const otherB = document.createElement("input");
|
|
221
|
+
document.body.append(otherA, otherB);
|
|
222
|
+
|
|
223
|
+
const events: boolean[] = [];
|
|
224
|
+
const unsubscribe = editor.onFocusChange(
|
|
225
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
226
|
+
{ includeEditorUI: true },
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
otherA.focus();
|
|
230
|
+
await settle();
|
|
231
|
+
otherB.focus();
|
|
232
|
+
await settle();
|
|
233
|
+
|
|
234
|
+
expect(events).toEqual([]);
|
|
235
|
+
|
|
236
|
+
unsubscribe();
|
|
237
|
+
otherA.remove();
|
|
238
|
+
otherB.remove();
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("keeps two editors on one page independent", async () => {
|
|
242
|
+
const { editor: other, container: otherContainer } = mountEditor();
|
|
243
|
+
|
|
244
|
+
const events: boolean[] = [];
|
|
245
|
+
const unsubscribe = editor.onFocusChange(
|
|
246
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
247
|
+
{ includeEditorUI: true },
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
other.focus();
|
|
251
|
+
await settle();
|
|
252
|
+
|
|
253
|
+
expect(other.isFocused()).toBe(true);
|
|
254
|
+
expect(editor.isFocused({ includeEditorUI: true })).toBe(false);
|
|
255
|
+
expect(events).toEqual([]);
|
|
256
|
+
|
|
257
|
+
unsubscribe();
|
|
258
|
+
other.unmount();
|
|
259
|
+
otherContainer.remove();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it("stops delivering events after unsubscribing", async () => {
|
|
263
|
+
const events: boolean[] = [];
|
|
264
|
+
const unsubscribe = editor.onFocusChange(
|
|
265
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
266
|
+
{ includeEditorUI: true },
|
|
267
|
+
);
|
|
268
|
+
|
|
269
|
+
editor.focus();
|
|
270
|
+
await settle();
|
|
271
|
+
const countWhileSubscribed = events.length;
|
|
272
|
+
expect(countWhileSubscribed).toBeGreaterThan(0);
|
|
273
|
+
|
|
274
|
+
unsubscribe();
|
|
275
|
+
outside.focus();
|
|
276
|
+
await settle();
|
|
277
|
+
editor.focus();
|
|
278
|
+
await settle();
|
|
279
|
+
|
|
280
|
+
expect(events.length).toBe(countWhileSubscribed);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("survives an unsubscribe being called twice", async () => {
|
|
284
|
+
// The count backing the shared document listeners is reference-counted.
|
|
285
|
+
// A second call to the same unsubscribe used to drive it negative, so it
|
|
286
|
+
// never reached 1 again and the tracker silently stopped attaching for
|
|
287
|
+
// every later subscriber — with no error to notice.
|
|
288
|
+
const stale = editor.onFocusChange(() => {}, { includeEditorUI: true });
|
|
289
|
+
stale();
|
|
290
|
+
stale();
|
|
291
|
+
|
|
292
|
+
const events: boolean[] = [];
|
|
293
|
+
const unsubscribe = editor.onFocusChange(
|
|
294
|
+
(_editor, ctx) => events.push(ctx.focused),
|
|
295
|
+
{ includeEditorUI: true },
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
editor.focus();
|
|
299
|
+
await settle();
|
|
300
|
+
|
|
301
|
+
expect(events).toEqual([true]);
|
|
302
|
+
unsubscribe();
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("supports several subscribers independently", async () => {
|
|
306
|
+
const first: boolean[] = [];
|
|
307
|
+
const second: boolean[] = [];
|
|
308
|
+
const unsubscribeFirst = editor.onFocusChange(
|
|
309
|
+
(_editor, ctx) => first.push(ctx.focused),
|
|
310
|
+
{ includeEditorUI: true },
|
|
311
|
+
);
|
|
312
|
+
const unsubscribeSecond = editor.onFocusChange(
|
|
313
|
+
(_editor, ctx) => second.push(ctx.focused),
|
|
314
|
+
{ includeEditorUI: true },
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
editor.focus();
|
|
318
|
+
await settle();
|
|
319
|
+
expect(first.length).toBeGreaterThan(0);
|
|
320
|
+
expect(second.length).toBe(first.length);
|
|
321
|
+
|
|
322
|
+
// The document listeners are shared and reference-counted, so dropping
|
|
323
|
+
// one subscriber must not stop the other's events.
|
|
324
|
+
unsubscribeFirst();
|
|
325
|
+
const firstCount = first.length;
|
|
326
|
+
outside.focus();
|
|
327
|
+
await settle();
|
|
328
|
+
|
|
329
|
+
expect(first.length).toBe(firstCount);
|
|
330
|
+
expect(second.at(-1)).toBe(false);
|
|
331
|
+
|
|
332
|
+
unsubscribeSecond();
|
|
333
|
+
});
|
|
334
|
+
});
|
|
@@ -16,6 +16,24 @@ import {
|
|
|
16
16
|
*/
|
|
17
17
|
export type Unsubscribe = () => void;
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Options shared by the focus APIs (`isFocused`, `onFocusChange`).
|
|
21
|
+
*/
|
|
22
|
+
export type EditorFocusOptions = {
|
|
23
|
+
/**
|
|
24
|
+
* When true, the editor's own UI - toolbars, menus and popovers inside
|
|
25
|
+
* its DOM boundary or registered portal elements - counts as focused,
|
|
26
|
+
* answering "is the user still interacting with this editor?" rather than
|
|
27
|
+
* "does the content area hold DOM focus?".
|
|
28
|
+
*
|
|
29
|
+
* Events then fire only when that combined state changes, and only once
|
|
30
|
+
* focus movement has settled, so a handoff from the content area into a
|
|
31
|
+
* popover's input reports no blur at all. The default reports raw
|
|
32
|
+
* content-area focus.
|
|
33
|
+
*/
|
|
34
|
+
includeEditorUI?: boolean;
|
|
35
|
+
};
|
|
36
|
+
|
|
19
37
|
/**
|
|
20
38
|
* EventManager is a class which manages the events of the editor
|
|
21
39
|
*/
|
|
@@ -34,6 +52,20 @@ export class EventManager<
|
|
|
34
52
|
onSelectionChange: [
|
|
35
53
|
ctx: { editor: BlockNoteEditor<BSchema, I, S>; transaction: Transaction },
|
|
36
54
|
];
|
|
55
|
+
onFocusChange: [
|
|
56
|
+
ctx: {
|
|
57
|
+
editor: BlockNoteEditor<BSchema, I, S>;
|
|
58
|
+
focused: boolean;
|
|
59
|
+
event: FocusEvent;
|
|
60
|
+
},
|
|
61
|
+
];
|
|
62
|
+
onFocusChangeWithinUI: [
|
|
63
|
+
ctx: {
|
|
64
|
+
editor: BlockNoteEditor<BSchema, I, S>;
|
|
65
|
+
focused: boolean;
|
|
66
|
+
event: FocusEvent;
|
|
67
|
+
},
|
|
68
|
+
];
|
|
37
69
|
onMount: [ctx: { editor: BlockNoteEditor<BSchema, I, S> }];
|
|
38
70
|
onUnmount: [ctx: { editor: BlockNoteEditor<BSchema, I, S> }];
|
|
39
71
|
}> {
|
|
@@ -51,15 +83,83 @@ export class EventManager<
|
|
|
51
83
|
editor._tiptapEditor.on("selectionUpdate", ({ transaction }) => {
|
|
52
84
|
this.emit("onSelectionChange", { editor, transaction });
|
|
53
85
|
});
|
|
86
|
+
editor._tiptapEditor.on("focus", ({ event }) => {
|
|
87
|
+
this.emit("onFocusChange", { editor, focused: true, event });
|
|
88
|
+
});
|
|
89
|
+
editor._tiptapEditor.on("blur", ({ event }) => {
|
|
90
|
+
this.emit("onFocusChange", { editor, focused: false, event });
|
|
91
|
+
});
|
|
54
92
|
editor._tiptapEditor.on("mount", () => {
|
|
55
93
|
this.emit("onMount", { editor });
|
|
56
94
|
});
|
|
57
95
|
editor._tiptapEditor.on("unmount", () => {
|
|
58
96
|
this.emit("onUnmount", { editor });
|
|
59
97
|
});
|
|
98
|
+
|
|
99
|
+
let unsubscribeUIFocusTracker: Unsubscribe | undefined;
|
|
100
|
+
this.onMount(() => {
|
|
101
|
+
unsubscribeUIFocusTracker = this.attachUIFocusTracker();
|
|
102
|
+
});
|
|
103
|
+
this.onUnmount(() => {
|
|
104
|
+
if (unsubscribeUIFocusTracker) {
|
|
105
|
+
unsubscribeUIFocusTracker();
|
|
106
|
+
}
|
|
107
|
+
});
|
|
60
108
|
});
|
|
61
109
|
}
|
|
62
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Settled focus-within-UI tracking. Document-level listeners (attached on
|
|
113
|
+
* editor mount, detached on unmount — a no-op per focus event is too
|
|
114
|
+
* cheap to be worth gating on subscribers) cover the case tiptap events
|
|
115
|
+
* can't: focus moving from the editor's own UI outside the content area
|
|
116
|
+
* (including registered portal elements) to somewhere else entirely.
|
|
117
|
+
* Blur-side changes are re-checked after the current task because
|
|
118
|
+
* `document.activeElement` transiently becomes `<body>` during focus
|
|
119
|
+
* handoffs (and `relatedTarget` is unreliable on mobile).
|
|
120
|
+
*/
|
|
121
|
+
private attachUIFocusTracker(): Unsubscribe {
|
|
122
|
+
if (typeof document === "undefined") {
|
|
123
|
+
return () => {};
|
|
124
|
+
}
|
|
125
|
+
let wasLastFocused = this.editor.isFocused({ includeEditorUI: true });
|
|
126
|
+
let settleUiFocusedTimeout: ReturnType<typeof setTimeout> | undefined;
|
|
127
|
+
|
|
128
|
+
const settleUIFocus = (event: FocusEvent) => {
|
|
129
|
+
const focused = this.editor.isFocused({ includeEditorUI: true });
|
|
130
|
+
if (focused !== wasLastFocused) {
|
|
131
|
+
wasLastFocused = focused;
|
|
132
|
+
this.emit("onFocusChangeWithinUI", {
|
|
133
|
+
editor: this.editor,
|
|
134
|
+
focused,
|
|
135
|
+
event,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
// On focusin the new element already holds focus, so the state can be
|
|
140
|
+
// read immediately.
|
|
141
|
+
const onFocusIn = (event: FocusEvent) => settleUIFocus(event);
|
|
142
|
+
|
|
143
|
+
// On focusout it can't: `document.activeElement` is still the outgoing
|
|
144
|
+
// element (and passes through `<body>` mid-handoff), and some UI
|
|
145
|
+
// libraries restore focus asynchronously — the ariakit and shadcn link
|
|
146
|
+
// popovers both do. The check therefore has to wait for the current task
|
|
147
|
+
// to finish. A microtask is too early (verified: those popover tests go
|
|
148
|
+
// red), and a frame would work but doesn't run in a background tab.
|
|
149
|
+
const onFocusOut = (event: FocusEvent) => {
|
|
150
|
+
clearTimeout(settleUiFocusedTimeout);
|
|
151
|
+
settleUiFocusedTimeout = setTimeout(() => settleUIFocus(event));
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
document.addEventListener("focusin", onFocusIn, true);
|
|
155
|
+
document.addEventListener("focusout", onFocusOut, true);
|
|
156
|
+
return () => {
|
|
157
|
+
clearTimeout(settleUiFocusedTimeout);
|
|
158
|
+
document.removeEventListener("focusin", onFocusIn, true);
|
|
159
|
+
document.removeEventListener("focusout", onFocusOut, true);
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
63
163
|
/**
|
|
64
164
|
* Register a callback that will be called when the editor changes.
|
|
65
165
|
*/
|
|
@@ -76,13 +176,7 @@ export class EventManager<
|
|
|
76
176
|
*/
|
|
77
177
|
includeUpdatesFromRemote = true,
|
|
78
178
|
): Unsubscribe {
|
|
79
|
-
|
|
80
|
-
transaction,
|
|
81
|
-
appendedTransactions,
|
|
82
|
-
}: {
|
|
83
|
-
transaction: Transaction;
|
|
84
|
-
appendedTransactions: Transaction[];
|
|
85
|
-
}) => {
|
|
179
|
+
return this.on("onChange", ({ transaction, appendedTransactions }) => {
|
|
86
180
|
if (!includeUpdatesFromRemote && isRemoteTransaction(transaction)) {
|
|
87
181
|
// don't trigger the callback if the changes are caused by a remote user
|
|
88
182
|
return;
|
|
@@ -95,12 +189,7 @@ export class EventManager<
|
|
|
95
189
|
);
|
|
96
190
|
},
|
|
97
191
|
});
|
|
98
|
-
};
|
|
99
|
-
this.on("onChange", cb);
|
|
100
|
-
|
|
101
|
-
return () => {
|
|
102
|
-
this.off("onChange", cb);
|
|
103
|
-
};
|
|
192
|
+
});
|
|
104
193
|
}
|
|
105
194
|
|
|
106
195
|
/**
|
|
@@ -113,22 +202,38 @@ export class EventManager<
|
|
|
113
202
|
*/
|
|
114
203
|
includeSelectionChangedByRemote = false,
|
|
115
204
|
): Unsubscribe {
|
|
116
|
-
|
|
205
|
+
return this.on("onSelectionChange", ({ transaction }) => {
|
|
117
206
|
if (
|
|
118
207
|
!includeSelectionChangedByRemote &&
|
|
119
|
-
isRemoteTransaction(
|
|
208
|
+
isRemoteTransaction(transaction)
|
|
120
209
|
) {
|
|
121
210
|
// don't trigger the callback if the selection changed because of a remote user
|
|
122
211
|
return;
|
|
123
212
|
}
|
|
124
213
|
callback(this.editor);
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
this.on("onSelectionChange", cb);
|
|
214
|
+
});
|
|
215
|
+
}
|
|
128
216
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
217
|
+
/**
|
|
218
|
+
* Register a callback for focus changes. By default, this reports when the
|
|
219
|
+
* editor's content area gains or loses DOM focus.
|
|
220
|
+
*
|
|
221
|
+
* Note that `focused: false` only means the content area itself blurred —
|
|
222
|
+
* focus may have moved into the editor's own UI (e.g. a toolbar
|
|
223
|
+
* popover's input). Pass `includeEditorUI: true` to report changes to
|
|
224
|
+
* combined content and UI focus, allowing focus handoffs to settle.
|
|
225
|
+
*/
|
|
226
|
+
public onFocusChange(
|
|
227
|
+
callback: (
|
|
228
|
+
editor: BlockNoteEditor<BSchema, I, S>,
|
|
229
|
+
ctx: { focused: boolean; event: FocusEvent },
|
|
230
|
+
) => void,
|
|
231
|
+
options?: EditorFocusOptions,
|
|
232
|
+
): Unsubscribe {
|
|
233
|
+
return this.on(
|
|
234
|
+
options?.includeEditorUI ? "onFocusChangeWithinUI" : "onFocusChange",
|
|
235
|
+
({ focused, event }) => callback(this.editor, { focused, event }),
|
|
236
|
+
);
|
|
132
237
|
}
|
|
133
238
|
|
|
134
239
|
/**
|
|
@@ -137,11 +242,7 @@ export class EventManager<
|
|
|
137
242
|
public onMount(
|
|
138
243
|
callback: (ctx: { editor: BlockNoteEditor<BSchema, I, S> }) => void,
|
|
139
244
|
): Unsubscribe {
|
|
140
|
-
this.on("onMount", callback);
|
|
141
|
-
|
|
142
|
-
return () => {
|
|
143
|
-
this.off("onMount", callback);
|
|
144
|
-
};
|
|
245
|
+
return this.on("onMount", callback);
|
|
145
246
|
}
|
|
146
247
|
|
|
147
248
|
/**
|
|
@@ -150,11 +251,7 @@ export class EventManager<
|
|
|
150
251
|
public onUnmount(
|
|
151
252
|
callback: (ctx: { editor: BlockNoteEditor<BSchema, I, S> }) => void,
|
|
152
253
|
): Unsubscribe {
|
|
153
|
-
this.on("onUnmount", callback);
|
|
154
|
-
|
|
155
|
-
return () => {
|
|
156
|
-
this.off("onUnmount", callback);
|
|
157
|
-
};
|
|
254
|
+
return this.on("onUnmount", callback);
|
|
158
255
|
}
|
|
159
256
|
}
|
|
160
257
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* @vitest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
import { Plugin, PluginKey } from "prosemirror-state";
|
|
5
|
-
import { describe, expect, it } from "vite-plus/test";
|
|
5
|
+
import { describe, expect, it, vi } from "vite-plus/test";
|
|
6
6
|
|
|
7
7
|
import { createExtension } from "../../BlockNoteExtension.js";
|
|
8
8
|
import { BlockNoteEditor } from "../../BlockNoteEditor.js";
|
|
@@ -15,6 +15,93 @@ function createMountedEditor(
|
|
|
15
15
|
return editor;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
describe("Extension keyboard shortcuts", () => {
|
|
19
|
+
it("validates shortcut names at initialization", () => {
|
|
20
|
+
expect(() =>
|
|
21
|
+
createMountedEditor([
|
|
22
|
+
createExtension({
|
|
23
|
+
key: "invalid-shortcut",
|
|
24
|
+
keyboardShortcuts: { "InvalidModifier-k": () => true },
|
|
25
|
+
}),
|
|
26
|
+
]),
|
|
27
|
+
).toThrow("Unrecognized modifier name: InvalidModifier");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("preserves the outer event for fallback matching after a nested key event", () => {
|
|
31
|
+
const nestedEvent = new KeyboardEvent("keydown", {
|
|
32
|
+
key: "x",
|
|
33
|
+
bubbles: true,
|
|
34
|
+
});
|
|
35
|
+
const nested = vi.fn(() => true);
|
|
36
|
+
const fallback = vi.fn(() => true);
|
|
37
|
+
const editor = createMountedEditor([
|
|
38
|
+
createExtension({
|
|
39
|
+
key: "nested-shortcuts",
|
|
40
|
+
keyboardShortcuts: {
|
|
41
|
+
"Shift-K": ({ editor }) => {
|
|
42
|
+
editor.prosemirrorView.dom.dispatchEvent(nestedEvent);
|
|
43
|
+
return false;
|
|
44
|
+
},
|
|
45
|
+
K: fallback,
|
|
46
|
+
x: nested,
|
|
47
|
+
},
|
|
48
|
+
}),
|
|
49
|
+
]);
|
|
50
|
+
const outerEvent = new KeyboardEvent("keydown", {
|
|
51
|
+
key: "K",
|
|
52
|
+
keyCode: 75,
|
|
53
|
+
shiftKey: true,
|
|
54
|
+
bubbles: true,
|
|
55
|
+
cancelable: true,
|
|
56
|
+
});
|
|
57
|
+
editor.prosemirrorView.dom.dispatchEvent(outerEvent);
|
|
58
|
+
expect(nested).toHaveBeenCalledExactlyOnceWith({
|
|
59
|
+
editor,
|
|
60
|
+
event: nestedEvent,
|
|
61
|
+
});
|
|
62
|
+
expect(fallback).toHaveBeenCalledExactlyOnceWith({
|
|
63
|
+
editor,
|
|
64
|
+
event: outerEvent,
|
|
65
|
+
});
|
|
66
|
+
expect(outerEvent.defaultPrevented).toBe(true);
|
|
67
|
+
editor.unmount();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("forwards the original event and preserves modifier matching and fallthrough", () => {
|
|
71
|
+
const first = vi.fn(() => false);
|
|
72
|
+
const second = vi.fn(() => true);
|
|
73
|
+
const editor = createMountedEditor([
|
|
74
|
+
createExtension({
|
|
75
|
+
key: "first-shortcut",
|
|
76
|
+
runsBefore: ["second-shortcut"],
|
|
77
|
+
keyboardShortcuts: { "Ctrl-Shift-k": first },
|
|
78
|
+
}),
|
|
79
|
+
createExtension({
|
|
80
|
+
key: "second-shortcut",
|
|
81
|
+
keyboardShortcuts: { "Ctrl-Shift-k": second },
|
|
82
|
+
}),
|
|
83
|
+
]);
|
|
84
|
+
const event = new KeyboardEvent("keydown", {
|
|
85
|
+
key: "K",
|
|
86
|
+
keyCode: 75,
|
|
87
|
+
ctrlKey: true,
|
|
88
|
+
shiftKey: true,
|
|
89
|
+
repeat: true,
|
|
90
|
+
bubbles: true,
|
|
91
|
+
cancelable: true,
|
|
92
|
+
});
|
|
93
|
+
editor.prosemirrorView.dom.dispatchEvent(event);
|
|
94
|
+
expect(first).toHaveBeenCalledExactlyOnceWith({ editor, event });
|
|
95
|
+
expect(second).toHaveBeenCalledExactlyOnceWith({ editor, event });
|
|
96
|
+
expect(event.defaultPrevented).toBe(true);
|
|
97
|
+
editor.prosemirrorView.dom.dispatchEvent(
|
|
98
|
+
new KeyboardEvent("keydown", { key: "k", bubbles: true }),
|
|
99
|
+
);
|
|
100
|
+
expect(first).toHaveBeenCalledOnce();
|
|
101
|
+
editor.unmount();
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
18
105
|
/**
|
|
19
106
|
* Returns the index of the plugin identified by `key` within the editor's
|
|
20
107
|
* ProseMirror plugin list. A lower index means it runs/applies earlier.
|