@blocknote/core 0.53.0 → 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-BEJW9OM_.js → blocks-CzQLehlc.js} +571 -568
- 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-BR5hkAio.js → extensions-DGuhtOgB.js} +662 -427
- 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-BUXut68d.js → src-BkfZVfaO.js} +967 -899
- 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 +59 -56
- 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 +17 -18
- 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/exporters/markdown/htmlToMarkdown.ts +45 -10
- package/src/api/nodeConversions/blockToNode.ts +5 -1
- package/src/api/nodeConversions/nodeToBlock.ts +3 -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/index.ts +3 -0
- package/src/comments/extension.ts +2 -4
- package/src/editor/Block.css +273 -2
- 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/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
- package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
- 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/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 +5 -1
- package/src/schema/blocks/createSpec.ts +2 -3
- 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 +171 -12
- package/src/schema/inlineContent/internal.ts +21 -2
- package/src/schema/inlineContent/types.ts +35 -5
- 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/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/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 +1 -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 +1 -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/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 +2 -1
- 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/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-BEJW9OM_.js.map +0 -1
- package/dist/blocks-BPmJglFo.cjs +0 -12
- package/dist/blocks-BPmJglFo.cjs.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-AitWbYdJ.cjs +0 -52
- package/dist/extensions-AitWbYdJ.cjs.map +0 -1
- package/dist/extensions-BR5hkAio.js.map +0 -1
- package/dist/src-BHPuOo7W.cjs +0 -21
- package/dist/src-BHPuOo7W.cjs.map +0 -1
- package/dist/src-BUXut68d.js.map +0 -1
- package/src/blocks/Code/shiki.ts +0 -73
- package/types/src/blocks/Code/shiki.d.ts +0 -4
|
@@ -24,7 +24,7 @@ import type { StyleSchema } from "./styles/types.js";
|
|
|
24
24
|
* `undefined`.
|
|
25
25
|
*
|
|
26
26
|
* The assertions are the type annotations and `@ts-expect-error` directives: if
|
|
27
|
-
* propagation breaks, this file stops compiling, which `vp lint` / `
|
|
27
|
+
* propagation breaks, this file stops compiling, which `vp lint` / `tsc`
|
|
28
28
|
* catches in CI (note that `vp test`, which strips types via esbuild, does not
|
|
29
29
|
* — these tests are guarded by the type-checker, not the runner). The `it`
|
|
30
30
|
* bodies otherwise contain no meaningful runtime logic, mirroring the existing
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { describe, expect, it } from "vite-plus/test";
|
|
2
|
+
|
|
3
|
+
import { BlockNoteSchema } from "../../blocks/BlockNoteSchema.js";
|
|
4
|
+
import {
|
|
5
|
+
defaultBlockSpecs,
|
|
6
|
+
defaultInlineContentSpecs,
|
|
7
|
+
} from "../../blocks/defaultBlocks.js";
|
|
8
|
+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
9
|
+
import { YAttributionMarksExtension } from "../../y/extensions/YAttributionMarks.js";
|
|
10
|
+
import { createInlineContentSpec } from "./createSpec.js";
|
|
11
|
+
|
|
12
|
+
// A minimal "plain" inline content, matched from external HTML by a `parse`
|
|
13
|
+
// function (the `tag: "*"` parse rule).
|
|
14
|
+
const customPlainIC = createInlineContentSpec(
|
|
15
|
+
{
|
|
16
|
+
type: "customPlainIC",
|
|
17
|
+
propSchema: {},
|
|
18
|
+
content: "plain",
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
parse: (el) => (el.classList?.contains("custom-plain-ic") ? {} : undefined),
|
|
22
|
+
render: () => {
|
|
23
|
+
const dom = document.createElement("span");
|
|
24
|
+
const contentDOM = document.createElement("span");
|
|
25
|
+
dom.append(contentDOM);
|
|
26
|
+
return { dom, contentDOM };
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
const createEditor = () =>
|
|
32
|
+
BlockNoteEditor.create({
|
|
33
|
+
schema: BlockNoteSchema.create({
|
|
34
|
+
blockSpecs: defaultBlockSpecs,
|
|
35
|
+
inlineContentSpecs: {
|
|
36
|
+
...defaultInlineContentSpecs,
|
|
37
|
+
customPlainIC,
|
|
38
|
+
},
|
|
39
|
+
}),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("plain inline content", () => {
|
|
43
|
+
it("holds its text as a string in the block model", () => {
|
|
44
|
+
const editor = createEditor();
|
|
45
|
+
|
|
46
|
+
editor.replaceBlocks(editor.document, [
|
|
47
|
+
{
|
|
48
|
+
type: "paragraph",
|
|
49
|
+
content: [
|
|
50
|
+
{
|
|
51
|
+
type: "customPlainIC",
|
|
52
|
+
props: {},
|
|
53
|
+
content: "hello world",
|
|
54
|
+
} as any,
|
|
55
|
+
],
|
|
56
|
+
},
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
const inlineContent = (editor.document[0] as any).content[0];
|
|
60
|
+
expect(inlineContent.type).toBe("customPlainIC");
|
|
61
|
+
expect(inlineContent.content).toBe("hello world");
|
|
62
|
+
|
|
63
|
+
editor._tiptapEditor.destroy();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("round-trips a newline in its string content", () => {
|
|
67
|
+
const editor = createEditor();
|
|
68
|
+
|
|
69
|
+
// Plain content holds raw text, so newlines are kept as characters rather
|
|
70
|
+
// than being split into (disallowed) `hardBreak` nodes.
|
|
71
|
+
editor.replaceBlocks(editor.document, [
|
|
72
|
+
{
|
|
73
|
+
type: "paragraph",
|
|
74
|
+
content: [
|
|
75
|
+
{
|
|
76
|
+
type: "customPlainIC",
|
|
77
|
+
props: {},
|
|
78
|
+
content: "first\nsecond",
|
|
79
|
+
} as any,
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
const inlineContent = (editor.document[0] as any).content[0];
|
|
85
|
+
expect(inlineContent.type).toBe("customPlainIC");
|
|
86
|
+
expect(inlineContent.content).toBe("first\nsecond");
|
|
87
|
+
|
|
88
|
+
editor._tiptapEditor.destroy();
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("keeps text and drops formatting marks when parsed", () => {
|
|
92
|
+
const editor = createEditor();
|
|
93
|
+
|
|
94
|
+
const blocks = editor.tryParseHTMLToBlocks(
|
|
95
|
+
`<p><span class="custom-plain-ic">hello <b>world</b></span></p>`,
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const inlineContent = (blocks[0] as any).content[0];
|
|
99
|
+
expect(inlineContent.type).toBe("customPlainIC");
|
|
100
|
+
expect(inlineContent.content).toBe("hello world");
|
|
101
|
+
|
|
102
|
+
editor._tiptapEditor.destroy();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("converts line breaks to newline characters when parsed", () => {
|
|
106
|
+
const editor = createEditor();
|
|
107
|
+
|
|
108
|
+
const blocks = editor.tryParseHTMLToBlocks(
|
|
109
|
+
`<p><span class="custom-plain-ic">first<br>second</span></p>`,
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const inlineContent = (blocks[0] as any).content[0];
|
|
113
|
+
expect(inlineContent.type).toBe("customPlainIC");
|
|
114
|
+
expect(inlineContent.content).toBe("first\nsecond");
|
|
115
|
+
|
|
116
|
+
editor._tiptapEditor.destroy();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("disallows formatting marks but allows annotation marks", () => {
|
|
120
|
+
// Checked at the ProseMirror level because the block model intentionally
|
|
121
|
+
// represents plain content as a bare string, without marks.
|
|
122
|
+
//
|
|
123
|
+
// Every non-formatting mark comes from an optional extension. The Yjs
|
|
124
|
+
// attribution marks are the ones reachable from core, so they stand in for
|
|
125
|
+
// the `"annotation"` group here; register them so the group is non-empty
|
|
126
|
+
// (mirroring the "plain" block test).
|
|
127
|
+
const editor = BlockNoteEditor.create({
|
|
128
|
+
schema: BlockNoteSchema.create({
|
|
129
|
+
blockSpecs: defaultBlockSpecs,
|
|
130
|
+
inlineContentSpecs: {
|
|
131
|
+
...defaultInlineContentSpecs,
|
|
132
|
+
customPlainIC,
|
|
133
|
+
},
|
|
134
|
+
}),
|
|
135
|
+
extensions: [YAttributionMarksExtension()],
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const plainType = editor.pmSchema.nodes["customPlainIC"];
|
|
139
|
+
const insertMark = editor.pmSchema.marks["y-attributed-insert"];
|
|
140
|
+
const boldMark = editor.pmSchema.marks["bold"];
|
|
141
|
+
|
|
142
|
+
// The plain inline content allows the non-formatting mark but not the
|
|
143
|
+
// formatting one.
|
|
144
|
+
expect(plainType.allowsMarkType(insertMark)).toBe(true);
|
|
145
|
+
expect(plainType.allowsMarkType(boldMark)).toBe(false);
|
|
146
|
+
|
|
147
|
+
// ...and so `allowedMarks` keeps the former while dropping the latter.
|
|
148
|
+
const markNames = new Set(
|
|
149
|
+
plainType
|
|
150
|
+
.allowedMarks([
|
|
151
|
+
insertMark.create({ userIds: ["test-user"] }),
|
|
152
|
+
boldMark.create(),
|
|
153
|
+
])
|
|
154
|
+
.map((m) => m.type.name),
|
|
155
|
+
);
|
|
156
|
+
expect(markNames.has("y-attributed-insert")).toBe(true);
|
|
157
|
+
expect(markNames.has("bold")).toBe(false);
|
|
158
|
+
|
|
159
|
+
editor._tiptapEditor.destroy();
|
|
160
|
+
});
|
|
161
|
+
});
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { Node } from "@tiptap/core";
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
DOMParser,
|
|
5
|
+
Fragment,
|
|
6
|
+
Node as ProsemirrorNode,
|
|
7
|
+
Schema,
|
|
8
|
+
TagParseRule,
|
|
9
|
+
} from "@tiptap/pm/model";
|
|
4
10
|
import { inlineContentToNodes } from "../../api/nodeConversions/blockToNode.js";
|
|
5
11
|
import { nodeToCustomInlineContent } from "../../api/nodeConversions/nodeToBlock.js";
|
|
6
12
|
import type { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
7
13
|
import { ignoreNonContentMutations } from "../nodeViewMutations.js";
|
|
8
14
|
import { propsToAttributes } from "../blocks/internal.js";
|
|
15
|
+
import { nonFormattingMarks } from "../markGroups.js";
|
|
9
16
|
import { Props } from "../propTypes.js";
|
|
10
17
|
import { StyleSchema } from "../styles/types.js";
|
|
11
18
|
import {
|
|
@@ -26,6 +33,23 @@ export type CustomInlineContentImplementation<
|
|
|
26
33
|
> = {
|
|
27
34
|
meta?: {
|
|
28
35
|
draggable?: boolean;
|
|
36
|
+
code?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* When {@link code} is `true`, this can syntax highlight the contents of the
|
|
39
|
+
* inline content with the result of this callback.
|
|
40
|
+
*/
|
|
41
|
+
// Method syntax (rather than an arrow-function property) so its parameter is
|
|
42
|
+
// checked bivariantly, keeping a specific implementation assignable to the
|
|
43
|
+
// generic spec record type.
|
|
44
|
+
highlight?(
|
|
45
|
+
inlineContent: Pick<InlineContentFromConfig<T, S>, "type" | "props">,
|
|
46
|
+
): string | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Marks the inline content as rendering a preview with an editable source
|
|
49
|
+
* popup, driven by the editor-wide
|
|
50
|
+
* `SourceInlineContentWithPreviewExtension`.
|
|
51
|
+
*/
|
|
52
|
+
hasPreview?: boolean;
|
|
29
53
|
};
|
|
30
54
|
|
|
31
55
|
/**
|
|
@@ -33,6 +57,17 @@ export type CustomInlineContentImplementation<
|
|
|
33
57
|
*/
|
|
34
58
|
parse?: (el: HTMLElement) => Partial<Props<T["propSchema"]>> | undefined;
|
|
35
59
|
|
|
60
|
+
/**
|
|
61
|
+
* Advanced parsing function that controls how the content within the inline
|
|
62
|
+
* content is parsed. This is not recommended to use, and is only useful for
|
|
63
|
+
* advanced use cases. Only applies to inline content with `content: "styled"`.
|
|
64
|
+
* Return `undefined` to fall through to the default inline content parsing.
|
|
65
|
+
*/
|
|
66
|
+
parseContent?: (options: {
|
|
67
|
+
el: HTMLElement;
|
|
68
|
+
schema: Schema;
|
|
69
|
+
}) => Fragment | undefined;
|
|
70
|
+
|
|
36
71
|
/**
|
|
37
72
|
* Renders an inline content to DOM elements
|
|
38
73
|
*/
|
|
@@ -55,6 +90,16 @@ export type CustomInlineContentImplementation<
|
|
|
55
90
|
editor: BlockNoteEditor<any, any, S>,
|
|
56
91
|
// (note) if we want to fix the manual cast, we need to prevent circular references and separate block definition and render implementations
|
|
57
92
|
// or allow manually passing <BSchema>, but that's not possible without passing the other generics because Typescript doesn't support partial inferred generics
|
|
93
|
+
/**
|
|
94
|
+
* The ProseMirror node backing this inline content.
|
|
95
|
+
*/
|
|
96
|
+
node: ProsemirrorNode,
|
|
97
|
+
/**
|
|
98
|
+
* Returns this inline content's position in the document. When rendered
|
|
99
|
+
* outside the editor (i.e. serialized to HTML), this is a no-op that returns
|
|
100
|
+
* `undefined`.
|
|
101
|
+
*/
|
|
102
|
+
getPos: () => number | undefined,
|
|
58
103
|
) => {
|
|
59
104
|
dom: HTMLElement;
|
|
60
105
|
contentDOM?: HTMLElement;
|
|
@@ -86,22 +131,98 @@ export type CustomInlineContentImplementation<
|
|
|
86
131
|
runsBefore?: string[];
|
|
87
132
|
};
|
|
88
133
|
|
|
134
|
+
// Resolves the element whose children hold the inline content's editable
|
|
135
|
+
// content, i.e. the `[data-editable]` element (or the element itself if it is /
|
|
136
|
+
// contains none).
|
|
137
|
+
function getEditableElement(element: HTMLElement) {
|
|
138
|
+
if (element.matches("[data-editable]")) {
|
|
139
|
+
return element;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return element.querySelector<HTMLElement>("[data-editable]") || element;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Parses an element's children as inline content.
|
|
146
|
+
function parseInlineContent(el: HTMLElement, schema: Schema) {
|
|
147
|
+
return DOMParser.fromSchema(schema).parse(el, {
|
|
148
|
+
topNode: schema.nodes.paragraph.create(),
|
|
149
|
+
preserveWhitespace: true,
|
|
150
|
+
}).content;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Flattens parsed inline content into text nodes only. "plain" inline content
|
|
154
|
+
// holds text only, so non-text inline nodes are flattened: line breaks become
|
|
155
|
+
// newline characters and other nodes (e.g. mentions) are kept as their text.
|
|
156
|
+
function flattenToText(content: Fragment, schema: Schema) {
|
|
157
|
+
const textNodes: ProsemirrorNode[] = [];
|
|
158
|
+
content.forEach((child) => {
|
|
159
|
+
if (child.isText) {
|
|
160
|
+
textNodes.push(child);
|
|
161
|
+
} else {
|
|
162
|
+
const text =
|
|
163
|
+
child.type === schema.linebreakReplacement ? "\n" : child.textContent;
|
|
164
|
+
if (text) {
|
|
165
|
+
textNodes.push(schema.text(text, child.marks));
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
return Fragment.fromArray(textNodes);
|
|
171
|
+
}
|
|
172
|
+
|
|
89
173
|
export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
|
|
90
174
|
config: C,
|
|
91
175
|
customParseFunction?: CustomInlineContentImplementation<C, any>["parse"],
|
|
176
|
+
customParseContentFunction?: CustomInlineContentImplementation<
|
|
177
|
+
C,
|
|
178
|
+
any
|
|
179
|
+
>["parseContent"],
|
|
92
180
|
) {
|
|
181
|
+
// When a custom `parseContent` function is provided (and this inline content
|
|
182
|
+
// actually holds content), it controls how content within the inline content
|
|
183
|
+
// is parsed. This applies to _both_ parse rules below, as content copied from
|
|
184
|
+
// within the editor is tagged with `data-inline-content-type` (matched by the
|
|
185
|
+
// first rule), while content pasted from outside is matched by the custom
|
|
186
|
+
// `parse` function (the second rule). `resolveContentElement` locates the
|
|
187
|
+
// element whose children to parse as a fallback when `parseContent` returns
|
|
188
|
+
// `undefined`.
|
|
189
|
+
// "plain" inline content always needs `getContent` so its parsed content is
|
|
190
|
+
// flattened to text (`<br>`/line breaks become newline characters), regardless
|
|
191
|
+
// of whether a custom `parseContent` is provided — mirroring "plain" blocks.
|
|
192
|
+
// "styled" inline content only needs it to run a custom `parseContent`.
|
|
193
|
+
const getContent =
|
|
194
|
+
config.content === "plain" ||
|
|
195
|
+
(customParseContentFunction && config.content === "styled")
|
|
196
|
+
? (resolveContentElement: (el: HTMLElement) => HTMLElement) =>
|
|
197
|
+
(node: HTMLElement, schema: Schema) => {
|
|
198
|
+
const result = customParseContentFunction?.({ el: node, schema });
|
|
199
|
+
|
|
200
|
+
// `parseContent` may return `undefined` to fall through to the
|
|
201
|
+
// default inline content parsing.
|
|
202
|
+
if (result !== undefined) {
|
|
203
|
+
return config.content === "plain"
|
|
204
|
+
? flattenToText(result, schema)
|
|
205
|
+
: result;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const parsed = parseInlineContent(
|
|
209
|
+
resolveContentElement(node),
|
|
210
|
+
schema,
|
|
211
|
+
);
|
|
212
|
+
return config.content === "plain"
|
|
213
|
+
? flattenToText(parsed, schema)
|
|
214
|
+
: parsed;
|
|
215
|
+
}
|
|
216
|
+
: undefined;
|
|
217
|
+
|
|
93
218
|
const rules: TagParseRule[] = [
|
|
94
219
|
{
|
|
95
220
|
tag: `[data-inline-content-type="${config.type}"]`,
|
|
96
|
-
contentElement: (element) =>
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return htmlElement.querySelector("[data-editable]") || htmlElement;
|
|
104
|
-
},
|
|
221
|
+
contentElement: (element) => getEditableElement(element as HTMLElement),
|
|
222
|
+
getContent: getContent
|
|
223
|
+
? (node, schema) =>
|
|
224
|
+
getContent(getEditableElement)(node as HTMLElement, schema)
|
|
225
|
+
: undefined,
|
|
105
226
|
},
|
|
106
227
|
];
|
|
107
228
|
|
|
@@ -121,6 +242,12 @@ export function getInlineContentParseRules<C extends CustomInlineContentConfig>(
|
|
|
121
242
|
|
|
122
243
|
return props;
|
|
123
244
|
},
|
|
245
|
+
// Because we do the parsing ourselves, we want to preserve whitespace for
|
|
246
|
+
// content we've parsed.
|
|
247
|
+
preserveWhitespace: getContent ? true : undefined,
|
|
248
|
+
getContent: getContent
|
|
249
|
+
? (node, schema) => getContent((el) => el)(node as HTMLElement, schema)
|
|
250
|
+
: undefined,
|
|
124
251
|
});
|
|
125
252
|
}
|
|
126
253
|
return rules;
|
|
@@ -138,9 +265,27 @@ export function createInlineContentSpec<
|
|
|
138
265
|
inline: true,
|
|
139
266
|
group: "inline",
|
|
140
267
|
draggable: inlineContentImplementation.meta?.draggable,
|
|
141
|
-
selectable: inlineContentConfig.content
|
|
268
|
+
selectable: inlineContentConfig.content !== "none",
|
|
142
269
|
atom: inlineContentConfig.content === "none",
|
|
143
|
-
|
|
270
|
+
code: inlineContentImplementation.meta?.code,
|
|
271
|
+
content:
|
|
272
|
+
inlineContentConfig.content === "styled"
|
|
273
|
+
? "inline*"
|
|
274
|
+
: inlineContentConfig.content === "plain"
|
|
275
|
+
? "text*"
|
|
276
|
+
: "",
|
|
277
|
+
// "plain" inline content holds unstyled text, so it disallows formatting
|
|
278
|
+
// marks (mirroring "plain" blocks). It still allows the non-formatting marks
|
|
279
|
+
// (comments and suggestions/diffs), which annotate content without changing
|
|
280
|
+
// it and are ignored by the content model. `nonFormattingMarks` resolves the
|
|
281
|
+
// group only when at least one such mark is registered, so a plain inline
|
|
282
|
+
// content in an editor without any of them doesn't reference an empty
|
|
283
|
+
// (unknown) mark group.
|
|
284
|
+
marks() {
|
|
285
|
+
return inlineContentConfig.content === "plain"
|
|
286
|
+
? nonFormattingMarks(this.editor)
|
|
287
|
+
: undefined;
|
|
288
|
+
},
|
|
144
289
|
|
|
145
290
|
addAttributes() {
|
|
146
291
|
return propsToAttributes(inlineContentConfig.propSchema);
|
|
@@ -154,6 +299,7 @@ export function createInlineContentSpec<
|
|
|
154
299
|
return getInlineContentParseRules(
|
|
155
300
|
inlineContentConfig,
|
|
156
301
|
inlineContentImplementation.parse,
|
|
302
|
+
inlineContentImplementation.parseContent,
|
|
157
303
|
);
|
|
158
304
|
},
|
|
159
305
|
|
|
@@ -171,6 +317,8 @@ export function createInlineContentSpec<
|
|
|
171
317
|
// No-op
|
|
172
318
|
},
|
|
173
319
|
editor,
|
|
320
|
+
node,
|
|
321
|
+
() => undefined,
|
|
174
322
|
);
|
|
175
323
|
|
|
176
324
|
return addInlineContentAttributes(
|
|
@@ -207,6 +355,8 @@ export function createInlineContentSpec<
|
|
|
207
355
|
);
|
|
208
356
|
},
|
|
209
357
|
editor,
|
|
358
|
+
node,
|
|
359
|
+
getPos,
|
|
210
360
|
);
|
|
211
361
|
|
|
212
362
|
const nodeView = addInlineContentAttributes(
|
|
@@ -233,10 +383,19 @@ export function createInlineContentSpec<
|
|
|
233
383
|
...inlineContentImplementation,
|
|
234
384
|
toExternalHTML: inlineContentImplementation.toExternalHTML,
|
|
235
385
|
render(inlineContent, updateInlineContent, editor) {
|
|
386
|
+
// Rendered outside the editor (serialization), so there's no live node
|
|
387
|
+
// view - derive the node from the content and stub out `getPos`.
|
|
388
|
+
const node = inlineContentToNodes(
|
|
389
|
+
[inlineContent] as any,
|
|
390
|
+
editor.pmSchema,
|
|
391
|
+
)[0];
|
|
392
|
+
|
|
236
393
|
const output = inlineContentImplementation.render(
|
|
237
394
|
inlineContent,
|
|
238
395
|
updateInlineContent,
|
|
239
396
|
editor,
|
|
397
|
+
node,
|
|
398
|
+
() => undefined,
|
|
240
399
|
);
|
|
241
400
|
|
|
242
401
|
return addInlineContentAttributes(
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { KeyboardShortcutCommand, Node } from "@tiptap/core";
|
|
2
2
|
|
|
3
|
+
import {
|
|
4
|
+
Extension,
|
|
5
|
+
ExtensionFactoryInstance,
|
|
6
|
+
} from "../../editor/BlockNoteExtension.js";
|
|
3
7
|
import { camelToDataKebab } from "../../util/string.js";
|
|
4
8
|
import { PropSchema, Props } from "../propTypes.js";
|
|
5
9
|
import {
|
|
@@ -77,10 +81,12 @@ export function createInternalInlineContentSpec<
|
|
|
77
81
|
>(
|
|
78
82
|
config: T,
|
|
79
83
|
implementation: InlineContentImplementation<NoInfer<T>>,
|
|
84
|
+
extensions?: (Extension | ExtensionFactoryInstance)[],
|
|
80
85
|
): InlineContentSpec<T> {
|
|
81
86
|
return {
|
|
82
87
|
config,
|
|
83
88
|
implementation,
|
|
89
|
+
extensions,
|
|
84
90
|
} as const;
|
|
85
91
|
}
|
|
86
92
|
|
|
@@ -99,12 +105,25 @@ export function createInlineContentSpecFromTipTapNode<
|
|
|
99
105
|
{
|
|
100
106
|
type: node.name as T["name"],
|
|
101
107
|
propSchema,
|
|
102
|
-
content:
|
|
108
|
+
content:
|
|
109
|
+
node.config.content === "inline*"
|
|
110
|
+
? "styled"
|
|
111
|
+
: node.config.content === "text*"
|
|
112
|
+
? "plain"
|
|
113
|
+
: "none",
|
|
103
114
|
},
|
|
115
|
+
// Cast needed because `implementation` is typed against the generic
|
|
116
|
+
// `CustomInlineContentConfig`, while `createInternalInlineContentSpec`
|
|
117
|
+
// expects the implementation for the specific (still-generic) config
|
|
118
|
+
// inferred from `node`/`propSchema` above.
|
|
104
119
|
{
|
|
105
120
|
...implementation,
|
|
106
121
|
node,
|
|
107
|
-
}
|
|
122
|
+
} as unknown as InlineContentImplementation<{
|
|
123
|
+
type: T["name"];
|
|
124
|
+
propSchema: P;
|
|
125
|
+
content: "styled" | "none" | "plain";
|
|
126
|
+
}>,
|
|
108
127
|
);
|
|
109
128
|
}
|
|
110
129
|
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import { Node } from "@tiptap/core";
|
|
2
2
|
import { ViewMutationRecord } from "prosemirror-view";
|
|
3
|
-
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
4
3
|
import { PropSchema, Props } from "../propTypes.js";
|
|
5
4
|
import { StyleSchema, Styles } from "../styles/types.js";
|
|
5
|
+
import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
|
|
6
|
+
import {
|
|
7
|
+
Extension,
|
|
8
|
+
ExtensionFactoryInstance,
|
|
9
|
+
} from "../../editor/BlockNoteExtension.js";
|
|
6
10
|
|
|
7
11
|
export type CustomInlineContentConfig = {
|
|
8
12
|
type: string;
|
|
@@ -16,11 +20,35 @@ export type InlineContentConfig = CustomInlineContentConfig | "text" | "link";
|
|
|
16
20
|
// InlineContentImplementation contains the "implementation" info about an InlineContent element
|
|
17
21
|
// such as the functions / Nodes required to render and / or serialize it
|
|
18
22
|
export type InlineContentImplementation<T extends InlineContentConfig> =
|
|
19
|
-
T extends
|
|
20
|
-
?
|
|
21
|
-
: {
|
|
23
|
+
T extends CustomInlineContentConfig
|
|
24
|
+
? {
|
|
22
25
|
meta?: {
|
|
26
|
+
/**
|
|
27
|
+
* Whether the inline content is a {@link https://prosemirror.net/docs/ref/#model.NodeSpec.code} block
|
|
28
|
+
*/
|
|
29
|
+
code?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* When {@link code} is `true`, this can syntax highlight the contents of the block with the result of this callback
|
|
32
|
+
*/
|
|
33
|
+
// Method syntax (rather than an arrow-function property) so its
|
|
34
|
+
// parameter is checked bivariantly, keeping a specific
|
|
35
|
+
// implementation assignable to the generic spec record type.
|
|
36
|
+
highlight?(
|
|
37
|
+
inlineContent: Pick<
|
|
38
|
+
CustomInlineContentFromConfig<T, any>,
|
|
39
|
+
"type" | "props"
|
|
40
|
+
>,
|
|
41
|
+
): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the inline content is draggable
|
|
44
|
+
*/
|
|
23
45
|
draggable?: boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Marks the inline content as rendering a preview with an editable
|
|
48
|
+
* source popup, driven by the editor-wide
|
|
49
|
+
* `SourceInlineContentWithPreviewExtension`.
|
|
50
|
+
*/
|
|
51
|
+
hasPreview?: boolean;
|
|
24
52
|
};
|
|
25
53
|
node: Node;
|
|
26
54
|
toExternalHTML?: (
|
|
@@ -43,7 +71,8 @@ export type InlineContentImplementation<T extends InlineContentConfig> =
|
|
|
43
71
|
destroy?: () => void;
|
|
44
72
|
};
|
|
45
73
|
runsBefore?: string[];
|
|
46
|
-
}
|
|
74
|
+
}
|
|
75
|
+
: undefined;
|
|
47
76
|
|
|
48
77
|
export type InlineContentSchemaWithInlineContent<
|
|
49
78
|
IType extends string,
|
|
@@ -57,6 +86,7 @@ export type InlineContentSchemaWithInlineContent<
|
|
|
57
86
|
export type InlineContentSpec<T extends InlineContentConfig> = {
|
|
58
87
|
config: T;
|
|
59
88
|
implementation: InlineContentImplementation<T>;
|
|
89
|
+
extensions?: (Extension | ExtensionFactoryInstance)[];
|
|
60
90
|
};
|
|
61
91
|
|
|
62
92
|
// A Schema contains all the types (Configs) supported in an editor
|