@blocknote/core 0.54.1 → 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,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vitest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
|
|
5
|
+
import { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
|
|
6
|
+
import { CommentEditorSubmitExtension } from "./CommentEditorSubmitExtension.js";
|
|
7
|
+
|
|
8
|
+
const editors: BlockNoteEditor[] = [];
|
|
9
|
+
|
|
10
|
+
function createEditor(
|
|
11
|
+
options: Parameters<typeof CommentEditorSubmitExtension>[0],
|
|
12
|
+
content = "A comment",
|
|
13
|
+
) {
|
|
14
|
+
const editor = BlockNoteEditor.create({
|
|
15
|
+
trailingBlock: false,
|
|
16
|
+
initialContent: [{ type: "paragraph", content }],
|
|
17
|
+
extensions: [CommentEditorSubmitExtension(options)],
|
|
18
|
+
});
|
|
19
|
+
editor.mount(document.createElement("div"));
|
|
20
|
+
editors.push(editor);
|
|
21
|
+
return editor;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function pressEnter(editor: BlockNoteEditor, options: KeyboardEventInit = {}) {
|
|
25
|
+
const event = new KeyboardEvent("keydown", {
|
|
26
|
+
key: "Enter",
|
|
27
|
+
keyCode: 13,
|
|
28
|
+
bubbles: true,
|
|
29
|
+
cancelable: true,
|
|
30
|
+
...options,
|
|
31
|
+
});
|
|
32
|
+
editor.prosemirrorView.dom.dispatchEvent(event);
|
|
33
|
+
return event;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
afterEach(() => {
|
|
37
|
+
for (const editor of editors) {
|
|
38
|
+
editor.unmount();
|
|
39
|
+
}
|
|
40
|
+
editors.length = 0;
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe("CommentEditorSubmitExtension", () => {
|
|
44
|
+
it("passes the editor to the callback provided at initialization", () => {
|
|
45
|
+
const onSubmit = vi.fn();
|
|
46
|
+
const editor = createEditor({ onSubmit });
|
|
47
|
+
pressEnter(editor);
|
|
48
|
+
expect(onSubmit).toHaveBeenCalledExactlyOnceWith(editor);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("shares the pending guard between keyboard and button submission", async () => {
|
|
52
|
+
let finish = () => {};
|
|
53
|
+
const pending = new Promise<void>((resolve) => {
|
|
54
|
+
finish = resolve;
|
|
55
|
+
});
|
|
56
|
+
const onSubmit = vi.fn(() => pending);
|
|
57
|
+
const editor = createEditor({ onSubmit });
|
|
58
|
+
const submit = editor.getExtension(CommentEditorSubmitExtension)!.submit;
|
|
59
|
+
pressEnter(editor);
|
|
60
|
+
await submit();
|
|
61
|
+
pressEnter(editor, { ctrlKey: true });
|
|
62
|
+
expect(onSubmit).toHaveBeenCalledOnce();
|
|
63
|
+
finish();
|
|
64
|
+
await pending;
|
|
65
|
+
await submit();
|
|
66
|
+
expect(onSubmit).toHaveBeenCalledTimes(2);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("takes precedence over code block Enter behavior", () => {
|
|
70
|
+
const onSubmit = vi.fn();
|
|
71
|
+
const editor = createEditor({ onSubmit });
|
|
72
|
+
editor.updateBlock(editor.document[0], { type: "codeBlock" });
|
|
73
|
+
pressEnter(editor);
|
|
74
|
+
expect(onSubmit).toHaveBeenCalledOnce();
|
|
75
|
+
expect(editor.prosemirrorState.doc.textContent).toBe("A comment");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("submits on Enter by default without inserting a block", () => {
|
|
79
|
+
const onSubmit = vi.fn();
|
|
80
|
+
const editor = createEditor({ onSubmit });
|
|
81
|
+
expect(pressEnter(editor).defaultPrevented).toBe(true);
|
|
82
|
+
expect(onSubmit).toHaveBeenCalledOnce();
|
|
83
|
+
expect(editor.document).toHaveLength(1);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("allows normal Enter when disabled, but still submits on Mod-Enter", () => {
|
|
87
|
+
const onSubmit = vi.fn();
|
|
88
|
+
const editor = createEditor({ onSubmit, submitOnEnter: false });
|
|
89
|
+
pressEnter(editor);
|
|
90
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
91
|
+
expect(editor.document).toHaveLength(2);
|
|
92
|
+
pressEnter(editor, { ctrlKey: true });
|
|
93
|
+
expect(onSubmit).toHaveBeenCalledOnce();
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("preserves Shift-Enter line breaks", () => {
|
|
97
|
+
const onSubmit = vi.fn();
|
|
98
|
+
const editor = createEditor({ onSubmit });
|
|
99
|
+
pressEnter(editor, { shiftKey: true });
|
|
100
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
101
|
+
expect(editor.document).toHaveLength(1);
|
|
102
|
+
let lineBreaks = 0;
|
|
103
|
+
editor.prosemirrorState.doc.descendants((node) => {
|
|
104
|
+
if (node.type.name === "hardBreak") {
|
|
105
|
+
lineBreaks++;
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
expect(lineBreaks).toBe(1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("consumes submission shortcuts on empty comments", () => {
|
|
112
|
+
const onSubmit = vi.fn();
|
|
113
|
+
const editor = createEditor({ onSubmit }, "");
|
|
114
|
+
pressEnter(editor);
|
|
115
|
+
pressEnter(editor, { ctrlKey: true });
|
|
116
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
117
|
+
expect(editor.document).toHaveLength(1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("ignores composition and held keys", () => {
|
|
121
|
+
const onSubmit = vi.fn();
|
|
122
|
+
const editor = createEditor({ onSubmit });
|
|
123
|
+
pressEnter(editor, { isComposing: true });
|
|
124
|
+
pressEnter(editor, { keyCode: 229 });
|
|
125
|
+
const documentBeforeRepeat = editor.document;
|
|
126
|
+
expect(pressEnter(editor, { repeat: true }).defaultPrevented).toBe(true);
|
|
127
|
+
expect(editor.document).toEqual(documentBeforeRepeat);
|
|
128
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("does not submit read-only editors", () => {
|
|
132
|
+
const onSubmit = vi.fn();
|
|
133
|
+
const editor = createEditor({ onSubmit });
|
|
134
|
+
editor.isEditable = false;
|
|
135
|
+
pressEnter(editor);
|
|
136
|
+
expect(onSubmit).not.toHaveBeenCalled();
|
|
137
|
+
editor.isEditable = true;
|
|
138
|
+
pressEnter(editor);
|
|
139
|
+
expect(onSubmit).toHaveBeenCalledOnce();
|
|
140
|
+
});
|
|
141
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createExtension,
|
|
3
|
+
type ExtensionOptions,
|
|
4
|
+
} from "../editor/BlockNoteExtension.js";
|
|
5
|
+
|
|
6
|
+
/** Keyboard submission for a comment editor. */
|
|
7
|
+
export const CommentEditorSubmitExtension = createExtension(
|
|
8
|
+
({
|
|
9
|
+
editor,
|
|
10
|
+
options: { submitOnEnter = true, onSubmit },
|
|
11
|
+
}: ExtensionOptions<{
|
|
12
|
+
/** Submit on plain Enter. Mod-Enter always submits. @default true */
|
|
13
|
+
submitOnEnter?: boolean;
|
|
14
|
+
onSubmit: (editor: ExtensionOptions["editor"]) => void | Promise<void>;
|
|
15
|
+
}>) => {
|
|
16
|
+
let submitting = false;
|
|
17
|
+
|
|
18
|
+
async function submit() {
|
|
19
|
+
if (!editor.isEditable || editor.isEmpty || submitting) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
submitting = true;
|
|
23
|
+
try {
|
|
24
|
+
await onSubmit(editor);
|
|
25
|
+
} finally {
|
|
26
|
+
submitting = false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function handleSubmit({ event }: { event: KeyboardEvent }) {
|
|
31
|
+
const view = editor.prosemirrorView;
|
|
32
|
+
if (
|
|
33
|
+
!view.editable ||
|
|
34
|
+
view.composing ||
|
|
35
|
+
event.isComposing ||
|
|
36
|
+
event.keyCode === 229
|
|
37
|
+
) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
if (!event.repeat) {
|
|
41
|
+
void submit();
|
|
42
|
+
}
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
key: "commentEditorSubmit",
|
|
48
|
+
submit,
|
|
49
|
+
runsBefore: ["default"],
|
|
50
|
+
keyboardShortcuts: {
|
|
51
|
+
"Mod-Enter": handleSubmit,
|
|
52
|
+
...(submitOnEnter ? { Enter: handleSubmit } : {}),
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
},
|
|
56
|
+
);
|
|
@@ -64,6 +64,7 @@ export const CommentsExtension = createExtension(
|
|
|
64
64
|
threadStore,
|
|
65
65
|
resolveUsers,
|
|
66
66
|
confirmBeforeDiscard = true,
|
|
67
|
+
submitOnEnter = true,
|
|
67
68
|
},
|
|
68
69
|
}: ExtensionOptions<{
|
|
69
70
|
/**
|
|
@@ -93,6 +94,12 @@ export const CommentsExtension = createExtension(
|
|
|
93
94
|
* @default true
|
|
94
95
|
*/
|
|
95
96
|
confirmBeforeDiscard?: boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Submit comments, replies, and edits on Enter. Shift-Enter inserts a line
|
|
99
|
+
* break, and Mod-Enter always submits, regardless of this setting.
|
|
100
|
+
* @default true
|
|
101
|
+
*/
|
|
102
|
+
submitOnEnter?: boolean;
|
|
96
103
|
}>) => {
|
|
97
104
|
if (!resolveUsers) {
|
|
98
105
|
throw new Error(
|
|
@@ -383,6 +390,7 @@ export const CommentsExtension = createExtension(
|
|
|
383
390
|
},
|
|
384
391
|
commentEditorSchema,
|
|
385
392
|
confirmBeforeDiscard,
|
|
393
|
+
submitOnEnter,
|
|
386
394
|
} as const;
|
|
387
395
|
},
|
|
388
396
|
);
|
package/src/comments/index.ts
CHANGED
|
@@ -49,6 +49,7 @@ import {
|
|
|
49
49
|
import type { TextCursorPosition } from "./cursorPositionTypes.js";
|
|
50
50
|
import {
|
|
51
51
|
BlockManager,
|
|
52
|
+
EditorFocusOptions,
|
|
52
53
|
EventManager,
|
|
53
54
|
ExportManager,
|
|
54
55
|
ExtensionManager,
|
|
@@ -719,26 +720,14 @@ export class BlockNoteEditor<
|
|
|
719
720
|
* Mount the editor to a DOM element.
|
|
720
721
|
*
|
|
721
722
|
* @param element The DOM element to mount the editor's contenteditable into.
|
|
722
|
-
*
|
|
723
|
-
*
|
|
724
|
-
*
|
|
725
|
-
*
|
|
726
|
-
* the surrounding shadow root when no parent is available.
|
|
723
|
+
*
|
|
724
|
+
* Floating UI rendered next to the contenteditable counts as within the
|
|
725
|
+
* editor already; UI rendered outside its DOM tree has to be registered with
|
|
726
|
+
* {@link registerPortalElement} so {@link isWithinEditor} recognizes it.
|
|
727
727
|
*
|
|
728
728
|
* @warning Not needed to call manually when using React, use BlockNoteView to take care of mounting
|
|
729
729
|
*/
|
|
730
|
-
public mount = (
|
|
731
|
-
element: HTMLElement,
|
|
732
|
-
options?: { portalTarget?: HTMLElement | null },
|
|
733
|
-
) => {
|
|
734
|
-
const root = element.getRootNode();
|
|
735
|
-
const isInShadowRoot =
|
|
736
|
-
typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot;
|
|
737
|
-
const target =
|
|
738
|
-
options?.portalTarget ??
|
|
739
|
-
element.parentElement ??
|
|
740
|
-
(isInShadowRoot ? (root as ShadowRoot) : document.body);
|
|
741
|
-
target.appendChild(this.portalElement);
|
|
730
|
+
public mount = (element: HTMLElement) => {
|
|
742
731
|
this._tiptapEditor.mount({ mount: element });
|
|
743
732
|
};
|
|
744
733
|
|
|
@@ -746,7 +735,6 @@ export class BlockNoteEditor<
|
|
|
746
735
|
* Unmount the editor from the DOM element it is bound to
|
|
747
736
|
*/
|
|
748
737
|
public unmount = () => {
|
|
749
|
-
this.portalElement?.remove();
|
|
750
738
|
this._tiptapEditor.unmount();
|
|
751
739
|
};
|
|
752
740
|
|
|
@@ -774,42 +762,84 @@ export class BlockNoteEditor<
|
|
|
774
762
|
return this.prosemirrorView?.dom as HTMLDivElement | undefined;
|
|
775
763
|
}
|
|
776
764
|
|
|
777
|
-
|
|
765
|
+
// Portal elements registered by the view layer, with reference counts so
|
|
766
|
+
// several UI elements can share one (e.g. multiple popovers portalling into
|
|
767
|
+
// the same custom element).
|
|
768
|
+
private _portalElements = new Map<HTMLElement, number>();
|
|
778
769
|
|
|
779
770
|
/**
|
|
780
|
-
*
|
|
781
|
-
*
|
|
782
|
-
*
|
|
771
|
+
* Registers an element as a portal element for this editor's floating UI, so
|
|
772
|
+
* {@link isWithinEditor} treats its contents as part of the editor. The view
|
|
773
|
+
* layer calls this for each portal element it designates (see
|
|
774
|
+
* `PortalElementOverride` in `@blocknote/react`) — without it, UI portalled outside
|
|
775
|
+
* the editor's DOM tree would be considered outside the editor.
|
|
776
|
+
* Registrations are reference-counted; release with
|
|
777
|
+
* {@link unregisterPortalElement}.
|
|
783
778
|
*/
|
|
784
|
-
public
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
779
|
+
public registerPortalElement = (element: HTMLElement) => {
|
|
780
|
+
this._portalElements.set(
|
|
781
|
+
element,
|
|
782
|
+
(this._portalElements.get(element) ?? 0) + 1,
|
|
783
|
+
);
|
|
784
|
+
};
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Releases a registration made with {@link registerPortalElement}. The element
|
|
788
|
+
* stops counting as part of the editor once every registration for it has
|
|
789
|
+
* been released.
|
|
790
|
+
*/
|
|
791
|
+
public unregisterPortalElement = (element: HTMLElement) => {
|
|
792
|
+
const count = this._portalElements.get(element);
|
|
793
|
+
if (count === undefined) {
|
|
794
|
+
return;
|
|
789
795
|
}
|
|
790
|
-
|
|
791
|
-
|
|
796
|
+
|
|
797
|
+
if (count <= 1) {
|
|
798
|
+
this._portalElements.delete(element);
|
|
799
|
+
} else {
|
|
800
|
+
this._portalElements.set(element, count - 1);
|
|
792
801
|
}
|
|
793
|
-
|
|
794
|
-
}
|
|
802
|
+
};
|
|
795
803
|
|
|
796
804
|
/**
|
|
797
|
-
* Checks whether a DOM element belongs to this editor —
|
|
798
|
-
*
|
|
799
|
-
* elements like menus and
|
|
805
|
+
* Checks whether a DOM element belongs to this editor — inside the editor's
|
|
806
|
+
* DOM tree, or inside any portal element registered via
|
|
807
|
+
* {@link registerPortalElement} (used for floating UI elements like menus and
|
|
808
|
+
* toolbars, which may portal outside the editor's DOM tree).
|
|
809
|
+
*
|
|
810
|
+
* The DOM-tree check starts at the content area's *parent*, so that UI the
|
|
811
|
+
* host app renders as `BlockNoteView` children counts too — React places
|
|
812
|
+
* those beside the content (see the "Static Formatting Toolbar" example).
|
|
813
|
+
* The boundary is whatever the element passed to `editor.mount()` has as
|
|
814
|
+
* its parent: `BlockNoteView` always provides a wrapper; mounting bare into
|
|
815
|
+
* `<body>`, as the vanilla-JS docs do, makes the whole page count as within
|
|
816
|
+
* the editor.
|
|
800
817
|
*/
|
|
801
818
|
public isWithinEditor = (element: Element): boolean => {
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
819
|
+
if (this.domElement?.parentElement?.contains(element)) {
|
|
820
|
+
return true;
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
for (const portalElement of this._portalElements.keys()) {
|
|
824
|
+
if (portalElement.contains(element)) {
|
|
825
|
+
return true;
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
return false;
|
|
806
830
|
};
|
|
807
831
|
|
|
808
|
-
public isFocused() {
|
|
832
|
+
public isFocused(options?: EditorFocusOptions) {
|
|
809
833
|
if (this.headless) {
|
|
810
834
|
return false;
|
|
811
835
|
}
|
|
812
|
-
|
|
836
|
+
const contentFocused = this.prosemirrorView?.hasFocus() || false;
|
|
837
|
+
if (!options?.includeEditorUI) {
|
|
838
|
+
return contentFocused;
|
|
839
|
+
}
|
|
840
|
+
const active =
|
|
841
|
+
typeof document !== "undefined" ? document.activeElement : null;
|
|
842
|
+
return contentFocused || (!!active && this.isWithinEditor(active));
|
|
813
843
|
}
|
|
814
844
|
|
|
815
845
|
public headless = true;
|
|
@@ -1167,7 +1197,8 @@ export class BlockNoteEditor<
|
|
|
1167
1197
|
}
|
|
1168
1198
|
|
|
1169
1199
|
/**
|
|
1170
|
-
* Gets the URL of the
|
|
1200
|
+
* Gets the URL of the link the current selection starts in, or `undefined`
|
|
1201
|
+
* if it does not start in one.
|
|
1171
1202
|
*/
|
|
1172
1203
|
public getSelectedLinkUrl() {
|
|
1173
1204
|
return this._styleManager.getSelectedLinkUrl();
|
|
@@ -1365,6 +1396,28 @@ export class BlockNoteEditor<
|
|
|
1365
1396
|
);
|
|
1366
1397
|
}
|
|
1367
1398
|
|
|
1399
|
+
/**
|
|
1400
|
+
* A callback function that runs when focus changes. By default, this reports
|
|
1401
|
+
* when the editor's content area gains or loses DOM focus.
|
|
1402
|
+
*
|
|
1403
|
+
* Note that `focused: false` only means the content area itself blurred —
|
|
1404
|
+
* focus may have moved into the editor's own UI (e.g. a toolbar
|
|
1405
|
+
* popover's input). Pass `includeEditorUI: true` to report changes to
|
|
1406
|
+
* combined content and UI focus, allowing focus handoffs to settle.
|
|
1407
|
+
*
|
|
1408
|
+
* @param callback The callback to execute.
|
|
1409
|
+
* @returns A function to remove the callback.
|
|
1410
|
+
*/
|
|
1411
|
+
public onFocusChange(
|
|
1412
|
+
callback: (
|
|
1413
|
+
editor: BlockNoteEditor<BSchema, ISchema, SSchema>,
|
|
1414
|
+
context: { focused: boolean; event: FocusEvent },
|
|
1415
|
+
) => void,
|
|
1416
|
+
options?: EditorFocusOptions,
|
|
1417
|
+
) {
|
|
1418
|
+
return this._eventManager.onFocusChange(callback, options);
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1368
1421
|
/**
|
|
1369
1422
|
* A callback function that runs when the editor has been mounted.
|
|
1370
1423
|
*
|
|
@@ -55,6 +55,8 @@ export interface Extension<State = any, Key extends string = string> {
|
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
57
|
* A mapping of a keyboard shortcut to a function that will be called when the shortcut is pressed
|
|
58
|
+
* The callback receives the editor and the original keyboard event. Return
|
|
59
|
+
* true to consume the shortcut, or false to let other handlers run.
|
|
58
60
|
*
|
|
59
61
|
* The keys are in the format:
|
|
60
62
|
* - Key names may be strings like `Shift-Ctrl-Enter`—a key identifier prefixed with zero or more modifiers
|
|
@@ -77,7 +79,10 @@ export interface Extension<State = any, Key extends string = string> {
|
|
|
77
79
|
*/
|
|
78
80
|
readonly keyboardShortcuts?: Record<
|
|
79
81
|
string,
|
|
80
|
-
(ctx: {
|
|
82
|
+
(ctx: {
|
|
83
|
+
editor: BlockNoteEditor<any, any, any>;
|
|
84
|
+
event: KeyboardEvent;
|
|
85
|
+
}) => boolean
|
|
81
86
|
>;
|
|
82
87
|
|
|
83
88
|
/**
|