@blocknote/core 0.52.1 → 0.54.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 +2 -0
- package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
- package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
- package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
- package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
- package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
- package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
- package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
- package/dist/blocknote.cjs +1 -1
- package/dist/blocknote.js +6 -6
- package/dist/{blocks-CiuUGa1E.js → blocks-CzQLehlc.js} +695 -652
- package/dist/blocks-CzQLehlc.js.map +1 -0
- package/dist/blocks-CzkqclGj.cjs +12 -0
- package/dist/blocks-CzkqclGj.cjs.map +1 -0
- package/dist/blocks.cjs +1 -1
- package/dist/blocks.js +2 -2
- package/dist/comments.cjs +1 -1
- package/dist/comments.cjs.map +1 -1
- package/dist/comments.js +7 -7
- package/dist/comments.js.map +1 -1
- package/dist/en-BuzSuuTq.cjs +2 -0
- package/dist/en-BuzSuuTq.cjs.map +1 -0
- package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
- package/dist/en-uzHzj6eH.js.map +1 -0
- package/dist/extensions-CSB9ZvuL.cjs +58 -0
- package/dist/extensions-CSB9ZvuL.cjs.map +1 -0
- package/dist/{extensions-CrUGDOks.js → extensions-DGuhtOgB.js} +717 -452
- package/dist/extensions-DGuhtOgB.js.map +1 -0
- package/dist/extensions.cjs +1 -1
- package/dist/extensions.js +4 -4
- package/dist/locales.cjs +1 -1
- package/dist/locales.cjs.map +1 -1
- package/dist/locales.js +199 -1
- package/dist/locales.js.map +1 -1
- package/dist/{src-DTF83Cl9.js → src-BkfZVfaO.js} +921 -835
- package/dist/src-BkfZVfaO.js.map +1 -0
- package/dist/src-DM3syMdf.cjs +23 -0
- package/dist/src-DM3syMdf.cjs.map +1 -0
- package/dist/style.css +1 -1
- package/dist/webpack-stats.json +1 -1
- package/dist/y.cjs +1 -1
- package/dist/y.cjs.map +1 -1
- package/dist/y.js +65 -62
- package/dist/y.js.map +1 -1
- package/dist/yjs.cjs +1 -1
- package/dist/yjs.cjs.map +1 -1
- package/dist/yjs.js +88 -76
- package/dist/yjs.js.map +1 -1
- package/package.json +29 -30
- package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
- package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
- package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +10 -1
- package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +42 -8
- package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
- package/src/api/exporters/markdown/markdownExporter.ts +10 -1
- package/src/api/nodeConversions/blockToNode.ts +5 -1
- package/src/api/nodeConversions/nodeToBlock.ts +3 -0
- package/src/api/parsers/html/parseHTML.ts +43 -0
- package/src/api/pmUtil.ts +16 -8
- package/src/blocks/Code/CodeBlockOptions.ts +83 -0
- package/src/blocks/Code/block.test.ts +1 -1
- package/src/blocks/Code/block.ts +29 -281
- package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
- package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
- package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
- package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
- package/src/blocks/Table/TableExtension.test.ts +102 -0
- package/src/blocks/ToggleWrapper/createToggleWrapper.ts +16 -5
- package/src/blocks/defaultBlockTypeGuards.ts +6 -0
- package/src/blocks/index.ts +3 -0
- package/src/comments/extension.ts +2 -4
- package/src/editor/Block.css +324 -3
- package/src/editor/BlockNoteEditor.ts +25 -4
- package/src/editor/BlockNoteExtension.ts +1 -1
- package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
- package/src/exporter/ExportImage.ts +50 -0
- package/src/exporter/Exporter.test.ts +48 -0
- package/src/exporter/Exporter.ts +46 -12
- package/src/exporter/index.ts +1 -0
- package/src/exporter/mapping.ts +4 -1
- package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
- package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
- package/src/extensions/SideMenu/SideMenu.ts +20 -3
- package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
- package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
- package/src/extensions/SuggestionMenu/SuggestionMenu.test.ts +118 -0
- package/src/extensions/SuggestionMenu/SuggestionMenu.ts +40 -27
- package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +13 -3
- package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
- package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
- package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
- package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
- package/src/extensions/TableHandles/TableHandles.ts +25 -12
- package/src/extensions/TrailingNode/TrailingNode.ts +98 -32
- package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
- package/src/extensions/index.ts +4 -0
- package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
- package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
- package/src/i18n/locales/ar.ts +9 -0
- package/src/i18n/locales/de.ts +9 -0
- package/src/i18n/locales/en.ts +9 -0
- package/src/i18n/locales/es.ts +9 -0
- package/src/i18n/locales/fa.ts +9 -0
- package/src/i18n/locales/fr.ts +9 -0
- package/src/i18n/locales/he.ts +9 -0
- package/src/i18n/locales/hr.ts +9 -0
- package/src/i18n/locales/is.ts +9 -0
- package/src/i18n/locales/it.ts +9 -0
- package/src/i18n/locales/ja.ts +9 -0
- package/src/i18n/locales/ko.ts +9 -0
- package/src/i18n/locales/nl.ts +9 -0
- package/src/i18n/locales/no.ts +9 -0
- package/src/i18n/locales/pl.ts +9 -0
- package/src/i18n/locales/pt.ts +9 -0
- package/src/i18n/locales/ru.ts +9 -0
- package/src/i18n/locales/sk.ts +9 -0
- package/src/i18n/locales/uk.ts +9 -0
- package/src/i18n/locales/uz.ts +9 -0
- package/src/i18n/locales/vi.ts +9 -0
- package/src/i18n/locales/zh-tw.ts +9 -0
- package/src/i18n/locales/zh.ts +9 -0
- package/src/index.ts +6 -1
- package/src/schema/blocks/createSpec.ts +17 -5
- package/src/schema/blocks/internal.test.ts +86 -0
- package/src/schema/blocks/internal.ts +64 -0
- package/src/schema/blocks/types.ts +53 -3
- package/src/schema/contentTypePropagation.test.ts +1 -1
- package/src/schema/inlineContent/createSpec.test.ts +161 -0
- package/src/schema/inlineContent/createSpec.ts +180 -13
- package/src/schema/inlineContent/internal.ts +21 -2
- package/src/schema/inlineContent/types.ts +35 -5
- package/src/schema/nodeViewMutations.test.ts +121 -0
- package/src/schema/nodeViewMutations.ts +46 -0
- package/src/user/UserStore.ts +1 -1
- package/src/util/Store.test.ts +132 -0
- package/src/util/Store.ts +135 -0
- package/src/y/extensions/AttributionExtension.test.ts +10 -0
- package/src/y/extensions/Versioning.test.ts +20 -7
- package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
- package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
- package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
- package/src/yjs/extensions/ForkYDoc.ts +26 -1
- package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
- package/src/yjs/extensions/Versioning.test.ts +17 -5
- package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +13 -0
- package/types/src/api/pmUtil.d.ts +7 -7
- package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
- package/types/src/blocks/Code/block.d.ts +3 -53
- package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
- package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
- package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
- package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
- package/types/src/blocks/defaultBlockTypeGuards.d.ts +2 -0
- package/types/src/blocks/index.d.ts +3 -0
- package/types/src/comments/extension.d.ts +1 -15
- package/types/src/editor/BlockNoteEditor.d.ts +3 -3
- package/types/src/editor/BlockNoteExtension.d.ts +1 -1
- package/types/src/exporter/ExportImage.d.ts +36 -0
- package/types/src/exporter/Exporter.d.ts +21 -0
- package/types/src/exporter/Exporter.test.d.ts +1 -0
- package/types/src/exporter/index.d.ts +1 -0
- package/types/src/exporter/mapping.d.ts +1 -1
- package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
- package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
- package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
- package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
- package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -1
- package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
- package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
- package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +7 -11
- package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
- package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
- package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
- package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
- package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
- package/types/src/extensions/TrailingNode/TrailingNode.d.ts +5 -4
- package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
- package/types/src/extensions/index.d.ts +4 -0
- package/types/src/i18n/locales/en.d.ts +9 -0
- package/types/src/i18n/locales/fa.d.ts +9 -0
- package/types/src/i18n/locales/sk.d.ts +9 -0
- package/types/src/index.d.ts +3 -1
- package/types/src/schema/blocks/internal.d.ts +21 -0
- package/types/src/schema/blocks/internal.test.d.ts +1 -0
- package/types/src/schema/blocks/types.d.ts +36 -3
- package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
- package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
- package/types/src/schema/inlineContent/internal.d.ts +3 -2
- package/types/src/schema/inlineContent/types.d.ts +22 -3
- package/types/src/schema/nodeViewMutations.d.ts +3 -0
- package/types/src/schema/nodeViewMutations.test.d.ts +1 -0
- package/types/src/user/UserStore.d.ts +1 -1
- package/types/src/util/Store.d.ts +45 -0
- package/types/src/util/Store.test.d.ts +1 -0
- package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
- package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
- package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
- package/types/src/y/extensions/index.d.ts +2 -10
- package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
- package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
- package/types/src/yjs/extensions/index.d.ts +1 -5
- package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
- package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
- package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
- package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
- package/dist/UserStore-QbbPFj0z.js.map +0 -1
- package/dist/blocks-BqHv0xhc.cjs +0 -12
- package/dist/blocks-BqHv0xhc.cjs.map +0 -1
- package/dist/blocks-CiuUGa1E.js.map +0 -1
- package/dist/en-C5Z4Ag9_.cjs +0 -2
- package/dist/en-C5Z4Ag9_.cjs.map +0 -1
- package/dist/en-CGKXfWma.js.map +0 -1
- package/dist/extensions-CrUGDOks.js.map +0 -1
- package/dist/extensions-Ghr9QqqR.cjs +0 -52
- package/dist/extensions-Ghr9QqqR.cjs.map +0 -1
- package/dist/src-DTF83Cl9.js.map +0 -1
- package/dist/src-OfbaVbMw.cjs +0 -21
- package/dist/src-OfbaVbMw.cjs.map +0 -1
- package/src/blocks/Code/shiki.ts +0 -73
- package/types/src/blocks/Code/shiki.d.ts +0 -4
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { GapCursor } from "@tiptap/pm/gapcursor";
|
|
1
2
|
import { TextSelection } from "prosemirror-state";
|
|
2
3
|
import { CellSelection } from "prosemirror-tables";
|
|
3
4
|
import {
|
|
@@ -10,6 +11,7 @@ import {
|
|
|
10
11
|
} from "vite-plus/test";
|
|
11
12
|
|
|
12
13
|
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
14
|
+
import { TableHandlesExtension } from "../../extensions/TableHandles/TableHandles.js";
|
|
13
15
|
import type { PartialBlock } from "../defaultBlocks.js";
|
|
14
16
|
|
|
15
17
|
/**
|
|
@@ -127,3 +129,103 @@ describe("Table Enter keyboard shortcut", () => {
|
|
|
127
129
|
expect(editor.document).toStrictEqual(before);
|
|
128
130
|
});
|
|
129
131
|
});
|
|
132
|
+
|
|
133
|
+
describe("TableHandlesExtension.getCellSelection", () => {
|
|
134
|
+
let editor: BlockNoteEditor;
|
|
135
|
+
const div = document.createElement("div");
|
|
136
|
+
|
|
137
|
+
beforeAll(() => {
|
|
138
|
+
editor = BlockNoteEditor.create();
|
|
139
|
+
editor.mount(div);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
afterAll(() => {
|
|
143
|
+
editor._tiptapEditor.destroy();
|
|
144
|
+
editor = undefined as any;
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
function getCellSelection() {
|
|
148
|
+
return editor.getExtension(TableHandlesExtension)!.getCellSelection();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
it("returns undefined for a gap cursor left of a leading table without crashing", () => {
|
|
152
|
+
// A table as the first block in the document, matching the crash repro
|
|
153
|
+
// where clicking in the left margin places a gap cursor at doc start.
|
|
154
|
+
editor.replaceBlocks(editor.document, [
|
|
155
|
+
{
|
|
156
|
+
type: "table",
|
|
157
|
+
content: {
|
|
158
|
+
type: "tableContent",
|
|
159
|
+
rows: [{ cells: ["Cell 1", "Cell 2"] }],
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
]);
|
|
163
|
+
|
|
164
|
+
editor.transact((tr) => tr.setSelection(new GapCursor(tr.doc.resolve(0))));
|
|
165
|
+
|
|
166
|
+
expect(() => getCellSelection()).not.toThrow();
|
|
167
|
+
expect(getCellSelection()).toBeUndefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("returns undefined for a gap cursor next to a nested block without crashing", () => {
|
|
171
|
+
// A table as the first block, followed by a nested block. Clicking the
|
|
172
|
+
// nested block's left margin places a gap cursor that is not inside a cell.
|
|
173
|
+
editor.replaceBlocks(editor.document, [
|
|
174
|
+
{
|
|
175
|
+
type: "table",
|
|
176
|
+
content: {
|
|
177
|
+
type: "tableContent",
|
|
178
|
+
rows: [{ cells: ["Cell 1", "Cell 2"] }],
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
type: "paragraph",
|
|
183
|
+
content: "Parent",
|
|
184
|
+
children: [{ type: "paragraph", content: "Nested" }],
|
|
185
|
+
},
|
|
186
|
+
]);
|
|
187
|
+
|
|
188
|
+
let nestedPos = -1;
|
|
189
|
+
editor.prosemirrorView.state.doc.descendants((node, pos) => {
|
|
190
|
+
if (nestedPos === -1 && node.isText && node.text === "Nested") {
|
|
191
|
+
nestedPos = pos;
|
|
192
|
+
}
|
|
193
|
+
return true;
|
|
194
|
+
});
|
|
195
|
+
editor.transact((tr) =>
|
|
196
|
+
tr.setSelection(new GapCursor(tr.doc.resolve(nestedPos - 1))),
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
expect(() => getCellSelection()).not.toThrow();
|
|
200
|
+
expect(getCellSelection()).toBeUndefined();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("returns the cell indices for a text selection inside a cell", () => {
|
|
204
|
+
editor.replaceBlocks(editor.document, [
|
|
205
|
+
{
|
|
206
|
+
type: "table",
|
|
207
|
+
content: {
|
|
208
|
+
type: "tableContent",
|
|
209
|
+
rows: [
|
|
210
|
+
{ cells: ["Cell 1", "Cell 2"] },
|
|
211
|
+
{ cells: ["Cell 3", "Cell 4"] },
|
|
212
|
+
],
|
|
213
|
+
},
|
|
214
|
+
},
|
|
215
|
+
]);
|
|
216
|
+
|
|
217
|
+
let cellPos = -1;
|
|
218
|
+
editor.prosemirrorView.state.doc.descendants((node, pos) => {
|
|
219
|
+
if (cellPos === -1 && node.isText && node.text === "Cell 4") {
|
|
220
|
+
cellPos = pos;
|
|
221
|
+
}
|
|
222
|
+
return true;
|
|
223
|
+
});
|
|
224
|
+
editor.transact((tr) =>
|
|
225
|
+
tr.setSelection(TextSelection.create(tr.doc, cellPos + 1)),
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
expect(getCellSelection()?.from).toEqual({ row: 1, col: 1 });
|
|
229
|
+
expect(getCellSelection()?.to).toEqual({ row: 1, col: 1 });
|
|
230
|
+
});
|
|
231
|
+
});
|
|
@@ -50,20 +50,25 @@ export const createToggleWrapper = (
|
|
|
50
50
|
const toggleButtonOnClick = () => {
|
|
51
51
|
// Toggles visibility of child blocks. Also adds/removes the "add block"
|
|
52
52
|
// button if there are no child blocks.
|
|
53
|
+
const currentBlock = editor.getBlock(block);
|
|
54
|
+
if (!currentBlock) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
53
58
|
if (toggleWrapper.getAttribute("data-show-children") === "true") {
|
|
54
59
|
toggleWrapper.setAttribute("data-show-children", "false");
|
|
55
|
-
toggledState.set(
|
|
60
|
+
toggledState.set(currentBlock, false);
|
|
56
61
|
|
|
57
62
|
if (dom.contains(toggleAddBlockButton)) {
|
|
58
63
|
dom.removeChild(toggleAddBlockButton);
|
|
59
64
|
}
|
|
60
65
|
} else {
|
|
61
66
|
toggleWrapper.setAttribute("data-show-children", "true");
|
|
62
|
-
toggledState.set(
|
|
67
|
+
toggledState.set(currentBlock, true);
|
|
63
68
|
|
|
64
69
|
if (
|
|
65
70
|
editor.isEditable &&
|
|
66
|
-
|
|
71
|
+
currentBlock.children.length === 0 &&
|
|
67
72
|
!dom.contains(toggleAddBlockButton)
|
|
68
73
|
) {
|
|
69
74
|
dom.appendChild(toggleAddBlockButton);
|
|
@@ -111,7 +116,10 @@ export const createToggleWrapper = (
|
|
|
111
116
|
// If a child block is added while children are hidden, show children.
|
|
112
117
|
if (toggleWrapper.getAttribute("data-show-children") === "false") {
|
|
113
118
|
toggleWrapper.setAttribute("data-show-children", "true");
|
|
114
|
-
|
|
119
|
+
const currentBlock = editor.getBlock(block);
|
|
120
|
+
if (currentBlock) {
|
|
121
|
+
toggledState.set(currentBlock, true);
|
|
122
|
+
}
|
|
115
123
|
}
|
|
116
124
|
|
|
117
125
|
// Remove the "add block" button as we want to show child blocks and
|
|
@@ -124,7 +132,10 @@ export const createToggleWrapper = (
|
|
|
124
132
|
// children.
|
|
125
133
|
if (toggleWrapper.getAttribute("data-show-children") === "true") {
|
|
126
134
|
toggleWrapper.setAttribute("data-show-children", "false");
|
|
127
|
-
|
|
135
|
+
const currentBlock = editor.getBlock(block);
|
|
136
|
+
if (currentBlock) {
|
|
137
|
+
toggledState.set(currentBlock, false);
|
|
138
|
+
}
|
|
128
139
|
}
|
|
129
140
|
|
|
130
141
|
// Remove the "add block" button as we want to hide child blocks,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Node } from "prosemirror-model";
|
|
1
2
|
import { CellSelection } from "prosemirror-tables";
|
|
2
3
|
import type { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
|
|
3
4
|
import { BlockConfig, PropSchema, PropSpec } from "../schema/index.js";
|
|
@@ -159,3 +160,8 @@ export function isTableCellSelection(
|
|
|
159
160
|
): selection is CellSelection {
|
|
160
161
|
return selection instanceof CellSelection;
|
|
161
162
|
}
|
|
163
|
+
|
|
164
|
+
export function isTableCellNode(node: Node): boolean {
|
|
165
|
+
const tableRole = node.type.spec.tableRole;
|
|
166
|
+
return tableRole === "cell" || tableRole === "header_cell";
|
|
167
|
+
}
|
package/src/blocks/index.ts
CHANGED
|
@@ -16,6 +16,9 @@ export * from "./Table/block.js";
|
|
|
16
16
|
export * from "./Video/block.js";
|
|
17
17
|
|
|
18
18
|
export { EMPTY_CELL_HEIGHT, EMPTY_CELL_WIDTH } from "./Table/TableExtension.js";
|
|
19
|
+
export * from "./Code/helpers/parse/parsePreCode.js";
|
|
20
|
+
export * from "./Code/helpers/render/createCodeBlock.js";
|
|
21
|
+
export * from "./Code/helpers/toExternalHTML/createPreCode.js";
|
|
19
22
|
export * from "./ToggleWrapper/createToggleWrapper.js";
|
|
20
23
|
export * from "./File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.js";
|
|
21
24
|
export * from "./PageBreak/getPageBreakSlashMenuItems.js";
|
|
@@ -117,11 +117,9 @@ export const CommentsExtension = createExtension(
|
|
|
117
117
|
threadPositions: new Map<string, { from: number; to: number }>(),
|
|
118
118
|
},
|
|
119
119
|
{
|
|
120
|
-
onUpdate() {
|
|
120
|
+
onUpdate(state, prevState) {
|
|
121
121
|
// If the selected thread id changed, we need to update the decorations
|
|
122
|
-
if (
|
|
123
|
-
store.state.selectedThreadId !== store.prevState.selectedThreadId
|
|
124
|
-
) {
|
|
122
|
+
if (state.selectedThreadId !== prevState.selectedThreadId) {
|
|
125
123
|
// So, we issue a transaction to update the decorations
|
|
126
124
|
editor.transact((tr) => tr.setMeta(PLUGIN_KEY, true));
|
|
127
125
|
}
|
package/src/editor/Block.css
CHANGED
|
@@ -22,9 +22,27 @@ BASIC STYLES
|
|
|
22
22
|
|
|
23
23
|
.bn-block-content.ProseMirror-selectednode > *,
|
|
24
24
|
/* Case for node view renderers */
|
|
25
|
-
.ProseMirror-selectednode > .bn-block-content >
|
|
25
|
+
.ProseMirror-selectednode > .bn-block-content > *,
|
|
26
|
+
/* Case for blocks/inline content where class is set manually */
|
|
27
|
+
.bn-block-content .ProseMirror-selectednode,
|
|
28
|
+
.bn-inline-content .ProseMirror-selectednode {
|
|
29
|
+
/* Anchor for the `::after` highlight overlay below. */
|
|
30
|
+
position: relative;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* Draws the selection highlight (border + translucent fill) as an overlay on
|
|
34
|
+
top of the element's content */
|
|
35
|
+
.bn-block-content.ProseMirror-selectednode > *::after,
|
|
36
|
+
.ProseMirror-selectednode > .bn-block-content > *::after,
|
|
37
|
+
.bn-block-content .ProseMirror-selectednode::after,
|
|
38
|
+
.bn-inline-content .ProseMirror-selectednode::after {
|
|
39
|
+
content: "";
|
|
40
|
+
position: absolute;
|
|
41
|
+
inset: 0;
|
|
26
42
|
border-radius: 4px;
|
|
27
|
-
|
|
43
|
+
background-color: rgba(100, 160, 255, 0.08);
|
|
44
|
+
box-shadow: inset 0 0 0 4px rgba(100, 160, 255, 0.3);
|
|
45
|
+
pointer-events: none;
|
|
28
46
|
}
|
|
29
47
|
|
|
30
48
|
.bn-block-content::before {
|
|
@@ -468,6 +486,259 @@ NESTED BLOCKS
|
|
|
468
486
|
transition-delay: 0.1s;
|
|
469
487
|
}
|
|
470
488
|
|
|
489
|
+
/* CODE BLOCK PREVIEW */
|
|
490
|
+
.bn-block-content[data-content-type="codeBlock"]:has(
|
|
491
|
+
.bn-preview-with-source-popup
|
|
492
|
+
) {
|
|
493
|
+
background-color: transparent;
|
|
494
|
+
color: inherit;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/* Default to dark theme as the code block has a dark background regardless of theme. */
|
|
498
|
+
.shiki {
|
|
499
|
+
color: var(--shiki-dark);
|
|
500
|
+
}
|
|
501
|
+
.bn-source-block-popup .shiki {
|
|
502
|
+
color: var(--shiki-light);
|
|
503
|
+
}
|
|
504
|
+
.bn-root[data-color-scheme="dark"] .bn-source-block-popup .shiki {
|
|
505
|
+
color: var(--shiki-dark);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
.bn-preview-with-source-popup {
|
|
509
|
+
position: relative;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
.bn-block-content .bn-preview-with-source-popup {
|
|
513
|
+
display: flex;
|
|
514
|
+
padding: 12px;
|
|
515
|
+
width: 100%;
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
.bn-inline-content-section .bn-preview-with-source-popup {
|
|
519
|
+
display: inline-block;
|
|
520
|
+
padding: 0;
|
|
521
|
+
width: fit-content;
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
.bn-preview-container {
|
|
525
|
+
anchor-name: --bn-source-popup-anchor;
|
|
526
|
+
cursor: pointer;
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
.bn-block-content .bn-preview-container {
|
|
530
|
+
overflow: auto;
|
|
531
|
+
width: 100%;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
.bn-inline-content-section .bn-preview-container {
|
|
535
|
+
overflow: visible;
|
|
536
|
+
width: fit-content;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
/* Additional right padding for equation numbers. */
|
|
540
|
+
.bn-block-content .bn-preview-container .katex-html:has(> .tag) {
|
|
541
|
+
box-sizing: border-box;
|
|
542
|
+
width: max-content;
|
|
543
|
+
min-width: 100%;
|
|
544
|
+
padding-right: 2.5em;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
.bn-source-block-popup {
|
|
548
|
+
background-color: transparent;
|
|
549
|
+
border: none;
|
|
550
|
+
border-radius: 0;
|
|
551
|
+
box-shadow: none;
|
|
552
|
+
color: transparent;
|
|
553
|
+
height: 0;
|
|
554
|
+
left: 0;
|
|
555
|
+
margin-top: 4px;
|
|
556
|
+
overflow: clip;
|
|
557
|
+
position: absolute;
|
|
558
|
+
top: 100%;
|
|
559
|
+
width: 0;
|
|
560
|
+
z-index: 0;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
.bn-preview-with-source-popup[data-open="true"] .bn-source-block-popup {
|
|
564
|
+
background-color: var(--bn-colors-menu-background);
|
|
565
|
+
border: var(--bn-border);
|
|
566
|
+
border-radius: var(--bn-border-radius-medium);
|
|
567
|
+
box-shadow: var(--bn-shadow-medium);
|
|
568
|
+
color: var(--bn-colors-menu-text);
|
|
569
|
+
height: unset;
|
|
570
|
+
z-index: 1;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
.bn-block-content
|
|
574
|
+
.bn-preview-with-source-popup[data-open="true"]
|
|
575
|
+
.bn-source-block-popup {
|
|
576
|
+
width: 100%;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
.bn-inline-content-section
|
|
580
|
+
.bn-preview-with-source-popup[data-open="true"]
|
|
581
|
+
.bn-source-block-popup {
|
|
582
|
+
width: 300px;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
.bn-code-block-source-popup-body {
|
|
586
|
+
align-items: flex-end;
|
|
587
|
+
display: flex;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
.bn-code-block-source-popup-body > pre {
|
|
591
|
+
align-self: center;
|
|
592
|
+
flex: 1;
|
|
593
|
+
margin: 0;
|
|
594
|
+
max-height: 200px;
|
|
595
|
+
overflow: auto;
|
|
596
|
+
padding: 16px;
|
|
597
|
+
position: relative;
|
|
598
|
+
tab-size: 2;
|
|
599
|
+
white-space: pre;
|
|
600
|
+
width: 0;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
.bn-code-block-source-popup-input-empty::before {
|
|
604
|
+
color: currentColor;
|
|
605
|
+
content: attr(data-placeholder);
|
|
606
|
+
left: 16px;
|
|
607
|
+
opacity: 0.5;
|
|
608
|
+
overflow: hidden;
|
|
609
|
+
pointer-events: none;
|
|
610
|
+
position: absolute;
|
|
611
|
+
right: 16px;
|
|
612
|
+
text-overflow: ellipsis;
|
|
613
|
+
top: 16px;
|
|
614
|
+
white-space: nowrap;
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
.bn-code-block-source-error {
|
|
618
|
+
border-top: var(--bn-border);
|
|
619
|
+
color: var(--bn-colors-highlights-red-text);
|
|
620
|
+
font-size: 0.8em;
|
|
621
|
+
padding: 8px 16px;
|
|
622
|
+
white-space: pre-wrap !important;
|
|
623
|
+
font-family: monospace;
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
.bn-code-block-source-popup-ok-button-wrapper {
|
|
627
|
+
align-items: flex-end;
|
|
628
|
+
justify-content: flex-end;
|
|
629
|
+
padding: 14px 16px;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
.bn-code-block-source-popup-ok-button {
|
|
633
|
+
align-items: center;
|
|
634
|
+
appearance: none;
|
|
635
|
+
background-color: rgb(37, 99, 235);
|
|
636
|
+
border: none;
|
|
637
|
+
border-radius: var(--bn-border-radius-small);
|
|
638
|
+
color: white;
|
|
639
|
+
cursor: pointer;
|
|
640
|
+
display: flex;
|
|
641
|
+
font-size: 0.8em;
|
|
642
|
+
font-weight: 500;
|
|
643
|
+
gap: 4px;
|
|
644
|
+
padding: 4px 8px;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
.bn-code-block-source-popup-ok-button-icon {
|
|
648
|
+
height: 1.2em;
|
|
649
|
+
width: 1.2em;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
.bn-code-block-source-popup-ok-button:hover {
|
|
653
|
+
background-color: rgb(29, 78, 216);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
.bn-preview-with-source-popup:has(.bn-preview-placeholder) {
|
|
657
|
+
padding: 0;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
.bn-preview-placeholder {
|
|
661
|
+
align-items: center;
|
|
662
|
+
background-color: rgb(242, 241, 238);
|
|
663
|
+
border-radius: 4px;
|
|
664
|
+
color: rgb(95, 91, 92);
|
|
665
|
+
display: flex;
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
.bn-block-content .bn-preview-placeholder {
|
|
669
|
+
gap: 10px;
|
|
670
|
+
padding: 12px;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
.bn-inline-content-section .bn-preview-placeholder {
|
|
674
|
+
gap: 4px;
|
|
675
|
+
padding: 0 4px;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
.bn-preview-placeholder:where(.dark, .dark *) {
|
|
679
|
+
background-color: rgb(70, 70, 70);
|
|
680
|
+
color: rgb(190, 190, 190);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
.bn-editor[contenteditable="true"]
|
|
684
|
+
.bn-preview-placeholder:not(.bn-preview-placeholder-error):hover {
|
|
685
|
+
background-color: rgb(225, 225, 225);
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
.bn-editor[contenteditable="true"]
|
|
689
|
+
.bn-preview-placeholder:not(.bn-preview-placeholder-error):hover:where(
|
|
690
|
+
.dark,
|
|
691
|
+
.dark *
|
|
692
|
+
) {
|
|
693
|
+
background-color: rgb(90, 90, 90);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
.bn-preview-placeholder-error {
|
|
697
|
+
background-color: var(--bn-colors-highlights-red-background);
|
|
698
|
+
color: var(--bn-colors-highlights-red-text);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
.bn-preview-placeholder-icon {
|
|
702
|
+
align-items: center;
|
|
703
|
+
display: flex;
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
.bn-block-content .bn-preview-placeholder-icon {
|
|
707
|
+
height: 24px;
|
|
708
|
+
width: 24px;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
.bn-inline-content-section .bn-preview-placeholder-icon {
|
|
712
|
+
height: 14px;
|
|
713
|
+
width: 14px;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
.bn-preview-placeholder-icon > svg {
|
|
717
|
+
height: 100%;
|
|
718
|
+
width: 100%;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
.bn-preview-placeholder-text {
|
|
722
|
+
margin: 0;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
.bn-block-content .bn-preview-placeholder-text {
|
|
726
|
+
font-size: 0.9rem;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
.bn-inline-content-section .bn-preview-placeholder-text {
|
|
730
|
+
font-size: 0.7rem;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
/* Toggled for a single frame while an up/down arrow press is handled, so the
|
|
734
|
+
browser's geometry-based vertical caret movement skips the popup. */
|
|
735
|
+
.bn-suppress-source-popup-caret
|
|
736
|
+
.bn-inline-content-section
|
|
737
|
+
.bn-preview-with-source-popup:not([data-open="true"])
|
|
738
|
+
.bn-source-block-popup {
|
|
739
|
+
visibility: hidden;
|
|
740
|
+
}
|
|
741
|
+
|
|
471
742
|
/* PAGE BREAK */
|
|
472
743
|
.bn-block-content[data-content-type="pageBreak"] > div {
|
|
473
744
|
width: 100%;
|
|
@@ -735,9 +1006,59 @@ NESTED BLOCKS
|
|
|
735
1006
|
|
|
736
1007
|
.bn-block-column {
|
|
737
1008
|
flex: 1;
|
|
738
|
-
padding: 12px
|
|
1009
|
+
padding: 12px 15px 12px 25px;
|
|
739
1010
|
/* scroll if we overflow, for example when tables or images are in the column */
|
|
740
1011
|
overflow-x: auto;
|
|
1012
|
+
/* Lets the trailing block widget grow to fill the column's leftover height
|
|
1013
|
+
when a sibling column is taller. */
|
|
1014
|
+
display: flex;
|
|
1015
|
+
flex-direction: column;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
/* Shows separators between a column list's columns while the mouse is over
|
|
1019
|
+
it. This clarifies where each column (and its clickable empty space) ends.
|
|
1020
|
+
The class is managed by the multi-column package's column resize plugin. The
|
|
1021
|
+
transition delay stops the separators from flashing in when the mouse just
|
|
1022
|
+
passes through the column list. */
|
|
1023
|
+
.bn-editor[contenteditable="true"]
|
|
1024
|
+
.bn-column-list-hovered
|
|
1025
|
+
> .bn-block-column:not(:last-child) {
|
|
1026
|
+
box-shadow: 4px 0 0 #eee;
|
|
1027
|
+
transition: box-shadow 0.2s ease 0.2s;
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
.bn-editor[contenteditable="true"]
|
|
1031
|
+
.bn-column-list-hovered
|
|
1032
|
+
> .bn-block-column:not(:last-child):where(.dark, .dark *) {
|
|
1033
|
+
box-shadow: 4px 0 0 rgb(70, 70, 70);
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
/* The border between two columns when the cursor is near their boundary or
|
|
1037
|
+
they're being resized, rendered on the boundary's left column. The class is
|
|
1038
|
+
managed by the multi-column package's column resize plugin. */
|
|
1039
|
+
.bn-editor[contenteditable="true"]
|
|
1040
|
+
.bn-column-list-hovered
|
|
1041
|
+
> .bn-block-column.bn-column-resize-border {
|
|
1042
|
+
box-shadow: 4px 0 0 #ccc;
|
|
1043
|
+
cursor: col-resize;
|
|
1044
|
+
transition: box-shadow 0.2s ease;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
.bn-editor[contenteditable="true"]
|
|
1048
|
+
.bn-column-list-hovered
|
|
1049
|
+
> .bn-block-column.bn-column-resize-border:where(.dark, .dark *) {
|
|
1050
|
+
box-shadow: 4px 0 0 rgb(110, 110, 110);
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
.bn-editor[contenteditable="true"]
|
|
1054
|
+
.bn-block-column.bn-column-resize-border
|
|
1055
|
+
+ .bn-block-column {
|
|
1056
|
+
cursor: col-resize;
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
.bn-block-column > .bn-trailing-block {
|
|
1060
|
+
height: auto;
|
|
1061
|
+
flex-grow: 1;
|
|
741
1062
|
}
|
|
742
1063
|
|
|
743
1064
|
.bn-block-column:first-child {
|
|
@@ -41,7 +41,11 @@ import "../style.css";
|
|
|
41
41
|
import { mergeCSSClasses } from "../util/browser.js";
|
|
42
42
|
import { EventEmitter } from "../util/EventEmitter.js";
|
|
43
43
|
import type { NoInfer } from "../util/typescript.js";
|
|
44
|
-
import {
|
|
44
|
+
import {
|
|
45
|
+
Extension,
|
|
46
|
+
ExtensionFactory,
|
|
47
|
+
ExtensionFactoryInstance,
|
|
48
|
+
} from "./BlockNoteExtension.js";
|
|
45
49
|
import type { TextCursorPosition } from "./cursorPositionTypes.js";
|
|
46
50
|
import {
|
|
47
51
|
BlockManager,
|
|
@@ -690,9 +694,26 @@ export class BlockNoteEditor<
|
|
|
690
694
|
/**
|
|
691
695
|
* Get an extension from the editor
|
|
692
696
|
*/
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
697
|
+
// Declared as an explicit intersection of the two `ExtensionManager`
|
|
698
|
+
// overloads rather than `ExtensionManager["getExtension"]`: indexed access on
|
|
699
|
+
// an overloaded method collapses the signatures, which widened the factory
|
|
700
|
+
// overload's `ReturnType<ReturnType<T>>` result to `any` (losing e.g. a
|
|
701
|
+
// returned extension's `store` type).
|
|
702
|
+
public getExtension: (<
|
|
703
|
+
const Ext extends Extension | ExtensionFactory = Extension,
|
|
704
|
+
>(
|
|
705
|
+
extension: string,
|
|
706
|
+
) =>
|
|
707
|
+
| (Ext extends Extension
|
|
708
|
+
? Ext
|
|
709
|
+
: Ext extends ExtensionFactory
|
|
710
|
+
? ReturnType<ReturnType<Ext>>
|
|
711
|
+
: never)
|
|
712
|
+
| undefined) &
|
|
713
|
+
(<const T extends ExtensionFactory>(
|
|
714
|
+
extension: T,
|
|
715
|
+
) => ReturnType<ReturnType<T>> | undefined) = ((extension: any) =>
|
|
716
|
+
this._extensionManager.getExtension(extension)) as any;
|
|
696
717
|
|
|
697
718
|
/**
|
|
698
719
|
* Mount the editor to a DOM element.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Store, StoreOptions } from "@tanstack/store";
|
|
2
1
|
import { type AnyExtension } from "@tiptap/core";
|
|
3
2
|
import type { Plugin as ProsemirrorPlugin } from "prosemirror-state";
|
|
4
3
|
import type { PartialBlockNoDefaults } from "../schema/index.js";
|
|
4
|
+
import { Store, StoreOptions } from "../util/Store.js";
|
|
5
5
|
import type { BlockNoteEditor } from "./BlockNoteEditor.js";
|
|
6
6
|
import { originalFactorySymbol } from "./managers/ExtensionManager/symbol.js";
|
|
7
7
|
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
FilePanelExtension,
|
|
16
16
|
FormattingToolbarExtension,
|
|
17
17
|
HistoryExtension,
|
|
18
|
+
InlineContentBoundaryEditExtension,
|
|
18
19
|
LinkToolbarExtension,
|
|
19
20
|
NodeSelectionKeyboardExtension,
|
|
20
21
|
PlaceholderExtension,
|
|
@@ -22,6 +23,8 @@ import {
|
|
|
22
23
|
PreviousBlockTypeExtension,
|
|
23
24
|
ShowSelectionExtension,
|
|
24
25
|
SideMenuExtension,
|
|
26
|
+
SourceBlockWithPreviewExtension,
|
|
27
|
+
SourceInlineContentWithPreviewExtension,
|
|
25
28
|
SuggestionMenu,
|
|
26
29
|
TableHandlesExtension,
|
|
27
30
|
TrailingNodeExtension,
|
|
@@ -167,8 +170,11 @@ export function getDefaultExtensions(
|
|
|
167
170
|
PlaceholderExtension(options),
|
|
168
171
|
ShowSelectionExtension(options),
|
|
169
172
|
SideMenuExtension(options),
|
|
173
|
+
SourceBlockWithPreviewExtension(),
|
|
174
|
+
SourceInlineContentWithPreviewExtension(),
|
|
170
175
|
SuggestionMenu(options),
|
|
171
176
|
HistoryExtension(),
|
|
177
|
+
InlineContentBoundaryEditExtension(),
|
|
172
178
|
PositionMappingExtension(),
|
|
173
179
|
...(options.trailingBlock !== false ? [TrailingNodeExtension()] : []),
|
|
174
180
|
] as ExtensionFactoryInstance[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An image generated during export (e.g. a rendered formula or diagram): the
|
|
3
|
+
* encoded image bytes plus the dimensions to display it at.
|
|
4
|
+
*
|
|
5
|
+
* The bytes (rather than e.g. a data URL string or a `Blob`) are the source
|
|
6
|
+
* of truth: they carry no encoding ambiguity, work in every environment, and
|
|
7
|
+
* are readable synchronously - each output format converts them at its own
|
|
8
|
+
* boundary (data URL for HTML-based targets, raw bytes for DOCX, base64 for
|
|
9
|
+
* email attachments).
|
|
10
|
+
*/
|
|
11
|
+
export type ExportImage = {
|
|
12
|
+
/** MIME type of `data`, e.g. `"image/png"` or `"image/svg+xml"`. */
|
|
13
|
+
mimeType: string;
|
|
14
|
+
/** The encoded image bytes. */
|
|
15
|
+
data: Uint8Array;
|
|
16
|
+
/**
|
|
17
|
+
* Dimensions to display the image at, in the target format's units (CSS
|
|
18
|
+
* pixels, points, ...). For raster images, `data`'s own pixel dimensions
|
|
19
|
+
* may be larger - images are often rendered at 2-4x for sharpness.
|
|
20
|
+
*/
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Encodes bytes as base64. This papers over a platform gap: until
|
|
27
|
+
* `Uint8Array.prototype.toBase64()` (ES2026) is available in every runtime
|
|
28
|
+
* BlockNote supports, the only universal built-in is `btoa`, which takes
|
|
29
|
+
* binary *strings*. Uses `toBase64` when the runtime has it.
|
|
30
|
+
*/
|
|
31
|
+
export function bytesToBase64(bytes: Uint8Array): string {
|
|
32
|
+
if ("toBase64" in bytes && typeof bytes.toBase64 === "function") {
|
|
33
|
+
return (bytes as Uint8Array & { toBase64(): string }).toBase64();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let binary = "";
|
|
37
|
+
for (const byte of bytes) {
|
|
38
|
+
binary += String.fromCharCode(byte);
|
|
39
|
+
}
|
|
40
|
+
return btoa(binary);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Encodes an {@link ExportImage}'s bytes as a base64 data URL, for targets
|
|
45
|
+
* that take image sources as URLs (HTML `src` attributes, react-pdf image
|
|
46
|
+
* sources, ...).
|
|
47
|
+
*/
|
|
48
|
+
export function exportImageToDataURL(image: ExportImage): string {
|
|
49
|
+
return `data:${image.mimeType};base64,${bytesToBase64(image.data)}`;
|
|
50
|
+
}
|