forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultBlockHelpers.ts
More file actions
97 lines (86 loc) · 3.02 KB
/
Copy pathdefaultBlockHelpers.ts
File metadata and controls
97 lines (86 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { blockToNode } from "../api/nodeConversions/blockToNode.js";
import type { BlockNoteEditor } from "../editor/BlockNoteEditor.js";
import type {
BlockNoDefaults,
BlockSchema,
InlineContentSchema,
StyleSchema,
} from "../schema/index.js";
import { mergeCSSClasses } from "../util/browser.js";
// Function that creates a ProseMirror `DOMOutputSpec` for a default block.
// Since all default blocks have the same structure (`blockContent` div with a
// `inlineContent` element inside), this function only needs the block's name
// for the `data-content-type` attribute of the `blockContent` element and the
// HTML tag of the `inlineContent` element, as well as any HTML attributes to
// add to those.
export function createDefaultBlockDOMOutputSpec(
blockName: string,
htmlTag: string,
blockContentHTMLAttributes: Record<string, string>,
inlineContentHTMLAttributes: Record<string, string>
) {
const blockContent = document.createElement("div");
blockContent.className = mergeCSSClasses(
"bn-block-content",
blockContentHTMLAttributes.class
);
blockContent.setAttribute("data-content-type", blockName);
for (const [attribute, value] of Object.entries(blockContentHTMLAttributes)) {
if (attribute !== "class") {
blockContent.setAttribute(attribute, value);
}
}
const inlineContent = document.createElement(htmlTag);
inlineContent.className = mergeCSSClasses(
"bn-inline-content",
inlineContentHTMLAttributes.class
);
for (const [attribute, value] of Object.entries(
inlineContentHTMLAttributes
)) {
if (attribute !== "class") {
inlineContent.setAttribute(attribute, value);
}
}
blockContent.appendChild(inlineContent);
return {
dom: blockContent,
contentDOM: inlineContent,
};
}
// Function used to convert default blocks to HTML. It uses the corresponding
// node's `renderHTML` method to do the conversion by using a default
// `DOMSerializer`.
export const defaultBlockToHTML = <
BSchema extends BlockSchema,
I extends InlineContentSchema,
S extends StyleSchema
>(
block: BlockNoDefaults<BSchema, I, S>,
editor: BlockNoteEditor<BSchema, I, S>
): {
dom: HTMLElement;
contentDOM?: HTMLElement;
} => {
let node = blockToNode(block, editor.pmSchema, editor.schema.styleSchema);
if (node.type.name === "blockContainer") {
// for regular blocks, get the toDOM spec from the blockContent node
node = node.firstChild!;
}
const toDOM = editor.pmSchema.nodes[node.type.name].spec.toDOM;
if (toDOM === undefined) {
throw new Error(
"This block has no default HTML serialization as its corresponding TipTap node doesn't implement `renderHTML`."
);
}
const renderSpec = toDOM(node);
if (typeof renderSpec !== "object" || !("dom" in renderSpec)) {
throw new Error(
"Cannot use this block's default HTML serialization as its corresponding TipTap node's `renderHTML` function does not return an object with the `dom` property."
);
}
return renderSpec as {
dom: HTMLElement;
contentDOM?: HTMLElement;
};
};