@blocknote/core 0.50.0 → 0.51.1
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/blocknote.cjs +1 -1
- package/dist/blocknote.js +4 -4
- package/dist/{blocks-UU7EM-QM.js → blocks-D0lMxErU.js} +397 -368
- package/dist/blocks-D0lMxErU.js.map +1 -0
- package/dist/blocks-KyMOxPT3.cjs +11 -0
- package/dist/blocks-KyMOxPT3.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/extensions-Cutrafjg.cjs +52 -0
- package/dist/extensions-Cutrafjg.cjs.map +1 -0
- package/dist/{extensions-CkLT--Nc.js → extensions-D2mx2C9y.js} +622 -353
- package/dist/extensions-D2mx2C9y.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +3 -3
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +1 -1
- package/dist/locales.js.map +1 -1
- package/dist/src-D5R5YzV7.cjs +21 -0
- package/dist/src-D5R5YzV7.cjs.map +1 -0
- package/dist/{src-SVFpMAWE.js → src-Dvmif2FY.js} +1070 -564
- package/dist/src-Dvmif2FY.js.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.js +8 -8
- package/package.json +1 -13
- package/src/api/blockManipulation/commands/insertBlocks/__snapshots__/insertBlocks.test.ts.snap +7 -7
- package/src/api/blockManipulation/commands/insertBlocks/insertBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/mergeBlocks/__snapshots__/mergeBlocks.test.ts.snap +5 -5
- package/src/api/blockManipulation/commands/moveBlocks/__snapshots__/moveBlocks.test.ts.snap +2550 -291
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.test.ts +83 -2
- package/src/api/blockManipulation/commands/moveBlocks/moveBlocks.ts +103 -42
- package/src/api/blockManipulation/commands/nestBlock/__snapshots__/nestBlock.test.ts.snap +0 -143
- package/src/api/blockManipulation/commands/replaceBlocks/__snapshots__/replaceBlocks.test.ts.snap +12 -12
- package/src/api/blockManipulation/commands/replaceBlocks/replaceBlocks.ts +5 -3
- package/src/api/blockManipulation/commands/splitBlock/__snapshots__/splitBlock.test.ts.snap +6 -6
- package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +22 -22
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +12 -8
- package/src/api/blockManipulation/setupTestEnv.ts +1 -1
- package/src/api/clipboard/fromClipboard/handleVSCodePaste.ts +2 -6
- package/src/api/clipboard/fromClipboard/pasteExtension.ts +7 -2
- package/src/api/clipboard/toClipboard/copyExtension.ts +12 -1
- package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +12 -4
- package/src/api/exporters/markdown/htmlToMarkdown.ts +813 -0
- package/src/api/exporters/markdown/markdownExporter.ts +2 -21
- package/src/api/nodeConversions/blockToNode.ts +3 -2
- package/src/api/parsers/html/util/__snapshots__/nestedLists.test.ts.snap +122 -107
- package/src/api/parsers/html/util/nestedLists.test.ts +1 -12
- package/src/api/parsers/markdown/markdownToHtml.ts +1194 -0
- package/src/api/parsers/markdown/parseMarkdown.ts +2 -101
- package/src/blocks/Audio/block.ts +1 -4
- package/src/blocks/Code/block.test.ts +258 -0
- package/src/blocks/File/block.ts +1 -4
- package/src/blocks/File/helpers/render/createFileBlockWrapper.ts +7 -2
- package/src/blocks/Image/block.ts +9 -6
- package/src/blocks/ListItem/BulletListItem/block.ts +7 -4
- package/src/blocks/ListItem/NumberedListItem/IndexingPlugin.ts +3 -4
- package/src/blocks/Video/block.ts +4 -5
- package/src/blocks/Video/parseVideoElement.ts +2 -1
- package/src/editor/Block.css +8 -0
- package/src/editor/BlockNoteEditor.test.ts +10 -4
- package/src/editor/BlockNoteEditor.ts +30 -13
- package/src/editor/managers/BlockManager.ts +10 -6
- package/src/editor/managers/ExtensionManager/index.ts +80 -25
- package/src/editor/performance.test.ts +2 -2
- package/src/editor/transformPasted.test.ts +295 -0
- package/src/editor/transformPasted.ts +77 -0
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor-forked.json +1 -12
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-editor.json +0 -11
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap-forked.html +1 -1
- package/src/extensions/Collaboration/__snapshots__/fork-yjs-snap.html +1 -1
- package/src/extensions/SideMenu/dragging.ts +7 -10
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +21 -0
- package/src/extensions/TrailingNode/TrailingNode.ts +124 -65
- package/src/i18n/locales/fr.ts +1 -1
- package/types/src/api/blockManipulation/commands/moveBlocks/moveBlocks.d.ts +13 -2
- package/types/src/api/clipboard/fromClipboard/handleVSCodePaste.d.ts +1 -1
- package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +1 -0
- package/types/src/api/exporters/markdown/htmlToMarkdown.d.ts +11 -0
- package/types/src/api/parsers/markdown/markdownToHtml.d.ts +5 -0
- package/types/src/blocks/Code/block.test.d.ts +1 -0
- package/types/src/blocks/Video/block.d.ts +2 -0
- package/types/src/blocks/Video/parseVideoElement.d.ts +1 -0
- package/types/src/editor/BlockNoteEditor.d.ts +20 -6
- package/types/src/editor/managers/BlockManager.d.ts +8 -4
- package/types/src/editor/transformPasted.test.d.ts +1 -0
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +8 -3
- package/dist/blocks-BFnTypT-.cjs +0 -11
- package/dist/blocks-BFnTypT-.cjs.map +0 -1
- package/dist/blocks-UU7EM-QM.js.map +0 -1
- package/dist/extensions-CYsFi5Is.cjs +0 -2
- package/dist/extensions-CYsFi5Is.cjs.map +0 -1
- package/dist/extensions-CkLT--Nc.js.map +0 -1
- package/dist/src-DsJ6yzs9.cjs +0 -5
- package/dist/src-DsJ6yzs9.cjs.map +0 -1
- package/dist/src-SVFpMAWE.js.map +0 -1
- package/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.ts +0 -42
- package/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.ts +0 -19
- package/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.ts +0 -39
- package/types/src/api/exporters/markdown/util/addSpacesToCheckboxesRehypePlugin.d.ts +0 -7
- package/types/src/api/exporters/markdown/util/convertVideoToMarkdownRehypePlugin.d.ts +0 -2
- package/types/src/api/exporters/markdown/util/removeUnderlinesRehypePlugin.d.ts +0 -6
|
@@ -1,11 +1,4 @@
|
|
|
1
1
|
import { Schema } from "prosemirror-model";
|
|
2
|
-
import remarkGfm from "remark-gfm";
|
|
3
|
-
import remarkParse from "remark-parse";
|
|
4
|
-
import remarkRehype, {
|
|
5
|
-
defaultHandlers as remarkRehypeDefaultHandlers,
|
|
6
|
-
} from "remark-rehype";
|
|
7
|
-
import rehypeStringify from "rehype-stringify";
|
|
8
|
-
import { unified } from "unified";
|
|
9
2
|
|
|
10
3
|
import { Block } from "../../../blocks/defaultBlocks.js";
|
|
11
4
|
import {
|
|
@@ -14,102 +7,10 @@ import {
|
|
|
14
7
|
StyleSchema,
|
|
15
8
|
} from "../../../schema/index.js";
|
|
16
9
|
import { HTMLToBlocks } from "../html/parseHTML.js";
|
|
17
|
-
import {
|
|
18
|
-
|
|
19
|
-
// modified version of https://github.com/syntax-tree/mdast-util-to-hast/blob/main/lib/handlers/code.js
|
|
20
|
-
// that outputs a data-language attribute instead of a CSS class (e.g.: language-typescript)
|
|
21
|
-
function code(state: any, node: any) {
|
|
22
|
-
const value = node.value ? node.value : "";
|
|
23
|
-
/** @type {Properties} */
|
|
24
|
-
const properties: any = {};
|
|
25
|
-
|
|
26
|
-
if (node.lang) {
|
|
27
|
-
// changed line
|
|
28
|
-
properties["data-language"] = node.lang;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
// Create `<code>`.
|
|
32
|
-
/** @type {Element} */
|
|
33
|
-
let result: any = {
|
|
34
|
-
type: "element",
|
|
35
|
-
tagName: "code",
|
|
36
|
-
properties,
|
|
37
|
-
children: [{ type: "text", value }],
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
if (node.meta) {
|
|
41
|
-
result.data = { meta: node.meta };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
state.patch(node, result);
|
|
45
|
-
result = state.applyData(node, result);
|
|
46
|
-
|
|
47
|
-
// Create `<pre>`.
|
|
48
|
-
result = {
|
|
49
|
-
type: "element",
|
|
50
|
-
tagName: "pre",
|
|
51
|
-
properties: {},
|
|
52
|
-
children: [result],
|
|
53
|
-
};
|
|
54
|
-
state.patch(node, result);
|
|
55
|
-
return result;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function video(state: any, node: any) {
|
|
59
|
-
const url = String(node?.url || "");
|
|
60
|
-
const title = node?.title ? String(node.title) : undefined;
|
|
61
|
-
|
|
62
|
-
let result: any = {
|
|
63
|
-
type: "element",
|
|
64
|
-
tagName: "video",
|
|
65
|
-
properties: {
|
|
66
|
-
src: url,
|
|
67
|
-
"data-name": title,
|
|
68
|
-
"data-url": url,
|
|
69
|
-
controls: true,
|
|
70
|
-
},
|
|
71
|
-
children: [],
|
|
72
|
-
};
|
|
73
|
-
state.patch?.(node, result);
|
|
74
|
-
result = state.applyData ? state.applyData(node, result) : result;
|
|
75
|
-
|
|
76
|
-
return result;
|
|
77
|
-
}
|
|
10
|
+
import { markdownToHtml } from "./markdownToHtml.js";
|
|
78
11
|
|
|
79
12
|
export function markdownToHTML(markdown: string): string {
|
|
80
|
-
|
|
81
|
-
.use(remarkParse)
|
|
82
|
-
.use(remarkGfm)
|
|
83
|
-
.use(remarkRehype, {
|
|
84
|
-
handlers: {
|
|
85
|
-
...(remarkRehypeDefaultHandlers as any),
|
|
86
|
-
image: (state: any, node: any) => {
|
|
87
|
-
const url = String(node?.url || "");
|
|
88
|
-
|
|
89
|
-
if (isVideoUrl(url)) {
|
|
90
|
-
return video(state, node);
|
|
91
|
-
} else {
|
|
92
|
-
return remarkRehypeDefaultHandlers.image(state, node);
|
|
93
|
-
}
|
|
94
|
-
},
|
|
95
|
-
code,
|
|
96
|
-
blockquote: (state: any, node: any) => {
|
|
97
|
-
const result = {
|
|
98
|
-
type: "element",
|
|
99
|
-
tagName: "blockquote",
|
|
100
|
-
properties: {},
|
|
101
|
-
// The only difference from the original is that we don't wrap the children with line endings
|
|
102
|
-
children: state.wrap(state.all(node), false),
|
|
103
|
-
};
|
|
104
|
-
state.patch(node, result);
|
|
105
|
-
return state.applyData(node, result);
|
|
106
|
-
},
|
|
107
|
-
},
|
|
108
|
-
})
|
|
109
|
-
.use(rehypeStringify)
|
|
110
|
-
.processSync(markdown);
|
|
111
|
-
|
|
112
|
-
return htmlString.value as string;
|
|
13
|
+
return markdownToHtml(markdown);
|
|
113
14
|
}
|
|
114
15
|
|
|
115
16
|
export function markdownToBlocks<
|
|
@@ -129,11 +129,8 @@ export const audioToExternalHTML =
|
|
|
129
129
|
>,
|
|
130
130
|
) => {
|
|
131
131
|
if (!block.props.url) {
|
|
132
|
-
const div = document.createElement("p");
|
|
133
|
-
div.textContent = "Add audio";
|
|
134
|
-
|
|
135
132
|
return {
|
|
136
|
-
dom:
|
|
133
|
+
dom: document.createElement("audio"),
|
|
137
134
|
};
|
|
138
135
|
}
|
|
139
136
|
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";
|
|
2
|
+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
3
|
+
import type { PartialBlock } from "../defaultBlocks.js";
|
|
4
|
+
import { getLanguageId, type CodeBlockOptions } from "./block.js";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @vitest-environment jsdom
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Simulate typing text into the editor at the current cursor position.
|
|
12
|
+
* This triggers input rules by calling the view's handleTextInput prop,
|
|
13
|
+
* which is how ProseMirror processes keyboard text input.
|
|
14
|
+
*/
|
|
15
|
+
function simulateTextInput(editor: BlockNoteEditor, text: string) {
|
|
16
|
+
const view = editor.prosemirrorView;
|
|
17
|
+
const { from, to } = view.state.selection;
|
|
18
|
+
const deflt = () => view.state.tr.insertText(text, from, to);
|
|
19
|
+
const handled = view.someProp("handleTextInput", (f) =>
|
|
20
|
+
f(view, from, to, text, deflt),
|
|
21
|
+
);
|
|
22
|
+
if (!handled) {
|
|
23
|
+
view.dispatch(deflt());
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function typeString(editor: BlockNoteEditor, str: string) {
|
|
28
|
+
for (const char of str) {
|
|
29
|
+
simulateTextInput(editor, char);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Simulate a keyboard shortcut by invoking the view's handleKeyDown prop,
|
|
35
|
+
* which is how ProseMirror routes keymap-based handlers like Enter.
|
|
36
|
+
*/
|
|
37
|
+
function pressKey(editor: BlockNoteEditor, key: string) {
|
|
38
|
+
const view = editor.prosemirrorView;
|
|
39
|
+
const event = new KeyboardEvent("keydown", { key });
|
|
40
|
+
view.someProp("handleKeyDown", (f) => f(view, event));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe("Code block input rule", () => {
|
|
44
|
+
let editor: BlockNoteEditor;
|
|
45
|
+
const div = document.createElement("div");
|
|
46
|
+
|
|
47
|
+
beforeAll(() => {
|
|
48
|
+
editor = BlockNoteEditor.create();
|
|
49
|
+
editor.mount(div);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
afterAll(() => {
|
|
53
|
+
editor._tiptapEditor.destroy();
|
|
54
|
+
editor = undefined as any;
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
const testDoc: PartialBlock[] = [
|
|
59
|
+
{
|
|
60
|
+
id: "test-paragraph",
|
|
61
|
+
type: "paragraph",
|
|
62
|
+
content: "",
|
|
63
|
+
},
|
|
64
|
+
];
|
|
65
|
+
editor.replaceBlocks(editor.document, testDoc);
|
|
66
|
+
editor.setTextCursorPosition("test-paragraph", "start");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("converts ```ts + space into a codeBlock", () => {
|
|
70
|
+
typeString(editor, "```ts ");
|
|
71
|
+
|
|
72
|
+
const block = editor.document[0];
|
|
73
|
+
expect(block.type).toBe("codeBlock");
|
|
74
|
+
// Without supportedLanguages configured, the raw alias is used
|
|
75
|
+
expect((block.props as any).language).toBe("ts");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("converts ``` + space into a codeBlock with empty language", () => {
|
|
79
|
+
typeString(editor, "``` ");
|
|
80
|
+
|
|
81
|
+
const block = editor.document[0];
|
|
82
|
+
expect(block.type).toBe("codeBlock");
|
|
83
|
+
expect((block.props as any).language).toBe("");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("converts ```javascript + space into a codeBlock", () => {
|
|
87
|
+
typeString(editor, "```javascript ");
|
|
88
|
+
|
|
89
|
+
const block = editor.document[0];
|
|
90
|
+
expect(block.type).toBe("codeBlock");
|
|
91
|
+
expect((block.props as any).language).toBe("javascript");
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("does not trigger input rule without trailing space", () => {
|
|
95
|
+
typeString(editor, "```ts");
|
|
96
|
+
|
|
97
|
+
const block = editor.document[0];
|
|
98
|
+
expect(block.type).toBe("paragraph");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("does not trigger with only two backticks", () => {
|
|
102
|
+
typeString(editor, "``ts ");
|
|
103
|
+
|
|
104
|
+
const block = editor.document[0];
|
|
105
|
+
expect(block.type).toBe("paragraph");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("does not trigger in non-empty paragraph with preceding text", () => {
|
|
109
|
+
typeString(editor, "some text ```ts ");
|
|
110
|
+
|
|
111
|
+
const block = editor.document[0];
|
|
112
|
+
// The ^ anchor in the regex means it only triggers at the start of a block
|
|
113
|
+
expect(block.type).toBe("paragraph");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("code block content is empty after conversion", () => {
|
|
117
|
+
typeString(editor, "```ts ");
|
|
118
|
+
|
|
119
|
+
const block = editor.document[0];
|
|
120
|
+
expect(block.type).toBe("codeBlock");
|
|
121
|
+
expect(block.content).toEqual([]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("converts ```ts + Enter into a codeBlock", () => {
|
|
125
|
+
typeString(editor, "```ts");
|
|
126
|
+
pressKey(editor, "Enter");
|
|
127
|
+
|
|
128
|
+
const block = editor.document[0];
|
|
129
|
+
expect(block.type).toBe("codeBlock");
|
|
130
|
+
expect((block.props as any).language).toBe("ts");
|
|
131
|
+
expect(block.content).toEqual([]);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("converts ``` + Enter into a codeBlock with empty language", () => {
|
|
135
|
+
typeString(editor, "```");
|
|
136
|
+
pressKey(editor, "Enter");
|
|
137
|
+
|
|
138
|
+
const block = editor.document[0];
|
|
139
|
+
expect(block.type).toBe("codeBlock");
|
|
140
|
+
expect((block.props as any).language).toBe("");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("converts ```javascript + Enter into a codeBlock", () => {
|
|
144
|
+
typeString(editor, "```javascript");
|
|
145
|
+
pressKey(editor, "Enter");
|
|
146
|
+
|
|
147
|
+
const block = editor.document[0];
|
|
148
|
+
expect(block.type).toBe("codeBlock");
|
|
149
|
+
expect((block.props as any).language).toBe("javascript");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("does not trigger Enter conversion in non-empty paragraph with preceding text", () => {
|
|
153
|
+
typeString(editor, "some text ```ts");
|
|
154
|
+
pressKey(editor, "Enter");
|
|
155
|
+
|
|
156
|
+
const block = editor.document[0];
|
|
157
|
+
expect(block.type).toBe("paragraph");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("does not trigger Enter conversion with only two backticks", () => {
|
|
161
|
+
typeString(editor, "``ts");
|
|
162
|
+
pressKey(editor, "Enter");
|
|
163
|
+
|
|
164
|
+
const block = editor.document[0];
|
|
165
|
+
expect(block.type).toBe("paragraph");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("places cursor inside the new code block after space conversion", () => {
|
|
169
|
+
typeString(editor, "```ts ");
|
|
170
|
+
|
|
171
|
+
const block = editor.document[0];
|
|
172
|
+
expect(block.type).toBe("codeBlock");
|
|
173
|
+
|
|
174
|
+
const { block: cursorBlock } = editor.getTextCursorPosition();
|
|
175
|
+
expect(cursorBlock.id).toBe(block.id);
|
|
176
|
+
|
|
177
|
+
// Typing should now go into the code block, not after it.
|
|
178
|
+
typeString(editor, "hello");
|
|
179
|
+
const after = editor.document[0];
|
|
180
|
+
expect(after.type).toBe("codeBlock");
|
|
181
|
+
expect(after.id).toBe(block.id);
|
|
182
|
+
expect((after.content as Array<{ type: string; text: string }>)[0].text).toBe(
|
|
183
|
+
"hello",
|
|
184
|
+
);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("places cursor inside the new code block after Enter conversion", () => {
|
|
188
|
+
typeString(editor, "```ts");
|
|
189
|
+
pressKey(editor, "Enter");
|
|
190
|
+
|
|
191
|
+
const block = editor.document[0];
|
|
192
|
+
expect(block.type).toBe("codeBlock");
|
|
193
|
+
|
|
194
|
+
const { block: cursorBlock } = editor.getTextCursorPosition();
|
|
195
|
+
expect(cursorBlock.id).toBe(block.id);
|
|
196
|
+
|
|
197
|
+
typeString(editor, "world");
|
|
198
|
+
const after = editor.document[0];
|
|
199
|
+
expect(after.type).toBe("codeBlock");
|
|
200
|
+
expect(after.id).toBe(block.id);
|
|
201
|
+
expect((after.content as Array<{ type: string; text: string }>)[0].text).toBe(
|
|
202
|
+
"world",
|
|
203
|
+
);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("Enter inside an existing code block does not retrigger conversion", () => {
|
|
207
|
+
typeString(editor, "```ts ");
|
|
208
|
+
|
|
209
|
+
const block = editor.document[0];
|
|
210
|
+
expect(block.type).toBe("codeBlock");
|
|
211
|
+
|
|
212
|
+
typeString(editor, "```js");
|
|
213
|
+
pressKey(editor, "Enter");
|
|
214
|
+
|
|
215
|
+
// Enter inside a code block should insert a newline, not convert again.
|
|
216
|
+
const after = editor.document[0];
|
|
217
|
+
expect(after.type).toBe("codeBlock");
|
|
218
|
+
expect((after.props as any).language).toBe("ts");
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe("getLanguageId", () => {
|
|
223
|
+
const options: CodeBlockOptions = {
|
|
224
|
+
supportedLanguages: {
|
|
225
|
+
typescript: {
|
|
226
|
+
name: "TypeScript",
|
|
227
|
+
aliases: ["ts", "typescript"],
|
|
228
|
+
},
|
|
229
|
+
javascript: {
|
|
230
|
+
name: "JavaScript",
|
|
231
|
+
aliases: ["js", "javascript"],
|
|
232
|
+
},
|
|
233
|
+
python: {
|
|
234
|
+
name: "Python",
|
|
235
|
+
aliases: ["py", "python"],
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
it("resolves alias to language id", () => {
|
|
241
|
+
expect(getLanguageId(options, "ts")).toBe("typescript");
|
|
242
|
+
expect(getLanguageId(options, "js")).toBe("javascript");
|
|
243
|
+
expect(getLanguageId(options, "py")).toBe("python");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("resolves language id directly", () => {
|
|
247
|
+
expect(getLanguageId(options, "typescript")).toBe("typescript");
|
|
248
|
+
expect(getLanguageId(options, "javascript")).toBe("javascript");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it("returns undefined for unknown language", () => {
|
|
252
|
+
expect(getLanguageId(options, "unknown")).toBeUndefined();
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("returns undefined with no supportedLanguages", () => {
|
|
256
|
+
expect(getLanguageId({}, "ts")).toBeUndefined();
|
|
257
|
+
});
|
|
258
|
+
});
|
package/src/blocks/File/block.ts
CHANGED
|
@@ -75,11 +75,8 @@ export const createFileBlockSpec = createBlockSpec(createFileBlockConfig, {
|
|
|
75
75
|
},
|
|
76
76
|
toExternalHTML(block) {
|
|
77
77
|
if (!block.props.url) {
|
|
78
|
-
const div = document.createElement("p");
|
|
79
|
-
div.textContent = "Add file";
|
|
80
|
-
|
|
81
78
|
return {
|
|
82
|
-
dom:
|
|
79
|
+
dom: document.createElement("embed"),
|
|
83
80
|
};
|
|
84
81
|
}
|
|
85
82
|
|
|
@@ -26,7 +26,12 @@ export const createFileBlockWrapper = (
|
|
|
26
26
|
element?: { dom: HTMLElement; destroy?: () => void },
|
|
27
27
|
buttonIcon?: HTMLElement,
|
|
28
28
|
) => {
|
|
29
|
-
|
|
29
|
+
// Use a <figure>/<figcaption> when the block has a caption, so the caption
|
|
30
|
+
// is semantically associated with its content for assistive tech. Falls back
|
|
31
|
+
// to a plain <div> when there is no caption (or the file has not been
|
|
32
|
+
// uploaded yet, since the upload UI never shows the caption).
|
|
33
|
+
const useFigure = block.props.url !== "" && !!block.props.caption;
|
|
34
|
+
const wrapper = document.createElement(useFigure ? "figure" : "div");
|
|
30
35
|
wrapper.className = "bn-file-block-content-wrapper";
|
|
31
36
|
|
|
32
37
|
// Show the add file button if the file has not been uploaded yet. Change to
|
|
@@ -73,7 +78,7 @@ export const createFileBlockWrapper = (
|
|
|
73
78
|
|
|
74
79
|
// Show the caption if there is one.
|
|
75
80
|
if (block.props.caption) {
|
|
76
|
-
const caption = document.createElement("
|
|
81
|
+
const caption = document.createElement("figcaption");
|
|
77
82
|
caption.className = "bn-file-caption";
|
|
78
83
|
caption.textContent = block.props.caption;
|
|
79
84
|
wrapper.appendChild(caption);
|
|
@@ -116,9 +116,15 @@ export const imageRender =
|
|
|
116
116
|
image.src = block.props.url;
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
// alt describes image content (per WCAG H86); figcaption (when present)
|
|
120
|
+
// is the contextual caption. Fall back to "" so unlabelled images are
|
|
121
|
+
// marked decorative rather than getting a noisy generic fallback.
|
|
122
|
+
image.alt = block.props.name || "";
|
|
120
123
|
image.contentEditable = "false";
|
|
121
124
|
image.draggable = false;
|
|
125
|
+
if (block.props.previewWidth) {
|
|
126
|
+
image.width = block.props.previewWidth;
|
|
127
|
+
}
|
|
122
128
|
imageWrapper.appendChild(image);
|
|
123
129
|
|
|
124
130
|
return createResizableFileBlockWrapper(
|
|
@@ -141,11 +147,8 @@ export const imageToExternalHTML =
|
|
|
141
147
|
>,
|
|
142
148
|
) => {
|
|
143
149
|
if (!block.props.url) {
|
|
144
|
-
const div = document.createElement("p");
|
|
145
|
-
div.textContent = "Add image";
|
|
146
|
-
|
|
147
150
|
return {
|
|
148
|
-
dom:
|
|
151
|
+
dom: document.createElement("img"),
|
|
149
152
|
};
|
|
150
153
|
}
|
|
151
154
|
|
|
@@ -153,7 +156,7 @@ export const imageToExternalHTML =
|
|
|
153
156
|
if (block.props.showPreview) {
|
|
154
157
|
image = document.createElement("img");
|
|
155
158
|
image.src = block.props.url;
|
|
156
|
-
image.alt = block.props.name ||
|
|
159
|
+
image.alt = block.props.name || "";
|
|
157
160
|
if (block.props.previewWidth) {
|
|
158
161
|
image.width = block.props.previewWidth;
|
|
159
162
|
}
|
|
@@ -37,17 +37,20 @@ export const createBulletListItemBlockSpec = createBlockSpec(
|
|
|
37
37
|
|
|
38
38
|
const parent = element.parentElement;
|
|
39
39
|
|
|
40
|
-
if (parent === null) {
|
|
41
|
-
return undefined;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
40
|
if (
|
|
41
|
+
parent === null ||
|
|
45
42
|
parent.tagName === "UL" ||
|
|
46
43
|
(parent.tagName === "DIV" && parent.parentElement?.tagName === "UL")
|
|
47
44
|
) {
|
|
48
45
|
return parseDefaultProps(element);
|
|
49
46
|
}
|
|
50
47
|
|
|
48
|
+
// Orphan `<li>` (no <ul>/<ol> ancestor) — match as bulletListItem so
|
|
49
|
+
// pasting bare `<li>` HTML doesn't fall back to a paragraph.
|
|
50
|
+
if (!element.closest("ul, ol")) {
|
|
51
|
+
return parseDefaultProps(element);
|
|
52
|
+
}
|
|
53
|
+
|
|
51
54
|
return undefined;
|
|
52
55
|
},
|
|
53
56
|
// As `li` elements can contain multiple paragraphs, we need to merge their contents
|
|
@@ -135,10 +135,9 @@ function getDecorations(
|
|
|
135
135
|
// Find the start of the first change to limit traversal scope.
|
|
136
136
|
// We only need to check from the change point forward, since earlier
|
|
137
137
|
// blocks are unaffected and their mapped decorations remain correct.
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
138
|
+
// On init (no steps), changedRange() returns null — fall back to a
|
|
139
|
+
// full scan so initial content gets indexed.
|
|
140
|
+
const range = tr.changedRange() ?? { from: 0, to: tr.doc.nodeSize - 2 };
|
|
142
141
|
const decorationsToAdd = [] as Deco[];
|
|
143
142
|
|
|
144
143
|
// Track blockGroups where we've verified a decoration match past the
|
|
@@ -92,7 +92,9 @@ export const createVideoBlockSpec = createBlockSpec(
|
|
|
92
92
|
video.controls = true;
|
|
93
93
|
video.contentEditable = "false";
|
|
94
94
|
video.draggable = false;
|
|
95
|
-
|
|
95
|
+
if (block.props.previewWidth) {
|
|
96
|
+
video.width = block.props.previewWidth;
|
|
97
|
+
}
|
|
96
98
|
videoWrapper.appendChild(video);
|
|
97
99
|
|
|
98
100
|
return createResizableFileBlockWrapper(
|
|
@@ -105,11 +107,8 @@ export const createVideoBlockSpec = createBlockSpec(
|
|
|
105
107
|
},
|
|
106
108
|
toExternalHTML(block) {
|
|
107
109
|
if (!block.props.url) {
|
|
108
|
-
const div = document.createElement("p");
|
|
109
|
-
div.textContent = "Add video";
|
|
110
|
-
|
|
111
110
|
return {
|
|
112
|
-
dom:
|
|
111
|
+
dom: document.createElement("video"),
|
|
113
112
|
};
|
|
114
113
|
}
|
|
115
114
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export const parseVideoElement = (videoElement: HTMLVideoElement) => {
|
|
2
2
|
const url = videoElement.src || undefined;
|
|
3
3
|
const previewWidth = videoElement.width || undefined;
|
|
4
|
+
const name = videoElement.getAttribute("data-name") || undefined;
|
|
4
5
|
|
|
5
|
-
return { url, previewWidth };
|
|
6
|
+
return { url, previewWidth, name };
|
|
6
7
|
};
|
package/src/editor/Block.css
CHANGED
|
@@ -49,6 +49,11 @@ BASIC STYLES
|
|
|
49
49
|
white-space: pre-wrap;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
.bn-trailing-block {
|
|
53
|
+
cursor: text;
|
|
54
|
+
height: 30px;
|
|
55
|
+
}
|
|
56
|
+
|
|
52
57
|
/*
|
|
53
58
|
NESTED BLOCKS
|
|
54
59
|
*/
|
|
@@ -468,6 +473,9 @@ NESTED BLOCKS
|
|
|
468
473
|
cursor: pointer;
|
|
469
474
|
display: flex;
|
|
470
475
|
flex-direction: column;
|
|
476
|
+
/* Reset default <figure> browser margins (the wrapper becomes a <figure>
|
|
477
|
+
when the block has a caption). */
|
|
478
|
+
margin: 0;
|
|
471
479
|
user-select: none;
|
|
472
480
|
}
|
|
473
481
|
|
|
@@ -187,7 +187,7 @@ it("sets an initial block id when using Y.js", async () => {
|
|
|
187
187
|
expect(transactionCount).toBe(2);
|
|
188
188
|
// Only after a real modification is made, will the fragment be updated
|
|
189
189
|
expect(fragment.toJSON()).toMatchInlineSnapshot(
|
|
190
|
-
`"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer
|
|
190
|
+
`"<blockgroup><blockcontainer id="0"><paragraph backgroundColor="default" textAlignment="left" textColor="default">Hello</paragraph></blockcontainer></blockgroup>"`,
|
|
191
191
|
);
|
|
192
192
|
});
|
|
193
193
|
|
|
@@ -213,8 +213,14 @@ it("onBeforeChange", () => {
|
|
|
213
213
|
{
|
|
214
214
|
"block": {
|
|
215
215
|
"children": [],
|
|
216
|
-
"content": [
|
|
217
|
-
|
|
216
|
+
"content": [
|
|
217
|
+
{
|
|
218
|
+
"styles": {},
|
|
219
|
+
"text": "Hello",
|
|
220
|
+
"type": "text",
|
|
221
|
+
},
|
|
222
|
+
],
|
|
223
|
+
"id": "1",
|
|
218
224
|
"props": {
|
|
219
225
|
"backgroundColor": "default",
|
|
220
226
|
"textAlignment": "left",
|
|
@@ -232,7 +238,7 @@ it("onBeforeChange", () => {
|
|
|
232
238
|
"block": {
|
|
233
239
|
"children": [],
|
|
234
240
|
"content": [],
|
|
235
|
-
"id": "
|
|
241
|
+
"id": "0",
|
|
236
242
|
"props": {
|
|
237
243
|
"backgroundColor": "default",
|
|
238
244
|
"textAlignment": "left",
|
|
@@ -306,7 +306,8 @@ export interface BlockNoteEditorOptions<
|
|
|
306
306
|
};
|
|
307
307
|
|
|
308
308
|
/**
|
|
309
|
-
*
|
|
309
|
+
* When the editor document doesn't end in an empty paragraph block, this option causes the editor to render an element simulating one.
|
|
310
|
+
* When clicked by the user, it gets turned into an actual block at the end of the document. This element is not shown when the option is `false`.
|
|
310
311
|
*
|
|
311
312
|
* @default true
|
|
312
313
|
*/
|
|
@@ -731,15 +732,27 @@ export class BlockNoteEditor<
|
|
|
731
732
|
/**
|
|
732
733
|
* Mount the editor to a DOM element.
|
|
733
734
|
*
|
|
735
|
+
* @param element The DOM element to mount the editor's contenteditable into.
|
|
736
|
+
* @param options.portalTarget Where to mount `editor.portalElement` — the
|
|
737
|
+
* container that floating UI (toolbars, menus, etc) portals into. When
|
|
738
|
+
* omitted, defaults to `element.parentElement` (which is the editor's
|
|
739
|
+
* `bn-container` in typical React usage), or to `document.body` /
|
|
740
|
+
* the surrounding shadow root when no parent is available.
|
|
741
|
+
*
|
|
734
742
|
* @warning Not needed to call manually when using React, use BlockNoteView to take care of mounting
|
|
735
743
|
*/
|
|
736
|
-
public mount = (
|
|
744
|
+
public mount = (
|
|
745
|
+
element: HTMLElement,
|
|
746
|
+
options?: { portalTarget?: HTMLElement | null },
|
|
747
|
+
) => {
|
|
737
748
|
const root = element.getRootNode();
|
|
738
|
-
|
|
739
|
-
root
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
749
|
+
const isInShadowRoot =
|
|
750
|
+
typeof ShadowRoot !== "undefined" && root instanceof ShadowRoot;
|
|
751
|
+
const target =
|
|
752
|
+
options?.portalTarget ??
|
|
753
|
+
element.parentElement ??
|
|
754
|
+
(isInShadowRoot ? (root as ShadowRoot) : document.body);
|
|
755
|
+
target.appendChild(this.portalElement);
|
|
743
756
|
this._tiptapEditor.mount({ mount: element });
|
|
744
757
|
};
|
|
745
758
|
|
|
@@ -1240,19 +1253,23 @@ export class BlockNoteEditor<
|
|
|
1240
1253
|
/**
|
|
1241
1254
|
* Moves the selected blocks up. If the previous block has children, moves
|
|
1242
1255
|
* them to the end of its children. If there is no previous block, but the
|
|
1243
|
-
* current blocks share a common parent, moves them out of & before it.
|
|
1256
|
+
* current blocks share a common parent, moves them out of & before it. If a
|
|
1257
|
+
* `blockIdentifier` is provided, that block is moved instead of the
|
|
1258
|
+
* selection, and the selection is left unchanged.
|
|
1244
1259
|
*/
|
|
1245
|
-
public moveBlocksUp() {
|
|
1246
|
-
return this._blockManager.moveBlocksUp();
|
|
1260
|
+
public moveBlocksUp(blockIdentifier?: BlockIdentifier) {
|
|
1261
|
+
return this._blockManager.moveBlocksUp(blockIdentifier);
|
|
1247
1262
|
}
|
|
1248
1263
|
|
|
1249
1264
|
/**
|
|
1250
1265
|
* Moves the selected blocks down. If the next block has children, moves
|
|
1251
1266
|
* them to the start of its children. If there is no next block, but the
|
|
1252
|
-
* current blocks share a common parent, moves them out of & after it.
|
|
1267
|
+
* current blocks share a common parent, moves them out of & after it. If a
|
|
1268
|
+
* `blockIdentifier` is provided, that block is moved instead of the
|
|
1269
|
+
* selection, and the selection is left unchanged.
|
|
1253
1270
|
*/
|
|
1254
|
-
public moveBlocksDown() {
|
|
1255
|
-
return this._blockManager.moveBlocksDown();
|
|
1271
|
+
public moveBlocksDown(blockIdentifier?: BlockIdentifier) {
|
|
1272
|
+
return this._blockManager.moveBlocksDown(blockIdentifier);
|
|
1256
1273
|
}
|
|
1257
1274
|
|
|
1258
1275
|
/**
|