forked from TypeCellOS/BlockNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTableOfContents.tsx.bak
More file actions
90 lines (78 loc) · 2.05 KB
/
TableOfContents.tsx.bak
File metadata and controls
90 lines (78 loc) · 2.05 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
import {
Block,
BlockSchemaWithBlock,
createBlockSpec,
InlineContent,
} from "@blocknote/core";
import { ReactSlashMenuItem } from "@blocknote/react";
import { RiLayout5Fill } from "react-icons/ri";
function inlineContentToText(inlineContent: InlineContent<any, any>[]) {
return inlineContent.reduce((string, content) => {
if (content.type === "link") {
return (
string +
content.content.reduce((string, content) => {
return string + content.text;
}, "")
);
}
return string + content.text;
}, "");
}
function createHeadingElements(block: Block<any, any, any>) {
const heading: HTMLElement = document.createElement("li");
const text = document.createElement("p");
text.innerText = inlineContentToText(block.content);
heading.appendChild(text);
const subheadings: HTMLElement = document.createElement("ol");
for (const child of block.children) {
subheadings.appendChild(createHeadingElements(child));
}
if (block.children.length > 0) {
heading.appendChild(subheadings);
}
return heading;
}
export const TableOfContents = createBlockSpec(
{
type: "toc" as const,
propSchema: {} as const,
content: "none",
},
{
render: (_, editor) => {
const toc = document.createElement("ol");
editor.onEditorContentChange(() => {
toc.innerHTML = "";
for (const block of editor.document) {
if (block.type === "heading") {
toc.appendChild(createHeadingElements(block));
}
}
});
return {
dom: toc,
};
},
}
);
export const insertTableOfContents: ReactSlashMenuItem<
BlockSchemaWithBlock<"toc", typeof TableOfContents.config>
> = {
name: "Insert Separator",
execute: (editor) => {
editor.insertBlocks(
[
{
type: "toc",
},
],
editor.getTextCursorPosition().block,
"after"
);
},
aliases: ["toc", "table", "contents", "navigation", "headings"],
group: "Media",
icon: <RiLayout5Fill />,
hint: "Insert a table of contents",
};