@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
package/src/blocks/Code/block.ts
CHANGED
|
@@ -1,57 +1,14 @@
|
|
|
1
|
-
import type { HighlighterGeneric } from "@shikijs/types";
|
|
2
|
-
import { DOMParser } from "@tiptap/pm/model";
|
|
3
|
-
import { createExtension } from "../../editor/BlockNoteExtension.js";
|
|
4
1
|
import { createBlockConfig, createBlockSpec } from "../../schema/index.js";
|
|
5
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
parsePreCode,
|
|
4
|
+
parsePreCodeContent,
|
|
5
|
+
} from "./helpers/parse/parsePreCode.js";
|
|
6
|
+
import { createPreCode } from "./helpers/toExternalHTML/createPreCode.js";
|
|
7
|
+
import { CodeKeyboardShortcutsExtension } from "./helpers/extensions/CodeKeyboardShortcutsExtension.js";
|
|
8
|
+
import { CodeBlockOptions } from "./CodeBlockOptions.js";
|
|
9
|
+
import { createCodeBlock } from "./helpers/render/createCodeBlock.js";
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Whether to indent lines with a tab when the user presses `Tab` in a code block.
|
|
10
|
-
*
|
|
11
|
-
* @default true
|
|
12
|
-
*/
|
|
13
|
-
indentLineWithTab?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* The default language to use for code blocks.
|
|
16
|
-
*
|
|
17
|
-
* @default "text"
|
|
18
|
-
*/
|
|
19
|
-
defaultLanguage?: string;
|
|
20
|
-
/**
|
|
21
|
-
* The languages that are supported in the editor.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* {
|
|
25
|
-
* javascript: {
|
|
26
|
-
* name: "JavaScript",
|
|
27
|
-
* aliases: ["js"],
|
|
28
|
-
* },
|
|
29
|
-
* typescript: {
|
|
30
|
-
* name: "TypeScript",
|
|
31
|
-
* aliases: ["ts"],
|
|
32
|
-
* },
|
|
33
|
-
* }
|
|
34
|
-
*/
|
|
35
|
-
supportedLanguages?: Record<
|
|
36
|
-
string,
|
|
37
|
-
{
|
|
38
|
-
/**
|
|
39
|
-
* The display name of the language.
|
|
40
|
-
*/
|
|
41
|
-
name: string;
|
|
42
|
-
/**
|
|
43
|
-
* Aliases for this language.
|
|
44
|
-
*/
|
|
45
|
-
aliases?: string[];
|
|
46
|
-
}
|
|
47
|
-
>;
|
|
48
|
-
/**
|
|
49
|
-
* The highlighter to use for code blocks.
|
|
50
|
-
*/
|
|
51
|
-
createHighlighter?: () => Promise<HighlighterGeneric<any, any>>;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
export type CodeBlockConfig = ReturnType<typeof createCodeBlockConfig>;
|
|
11
|
+
const CODE_BLOCK_KEYBOARD_SHORTCUTS_KEY = "code-block-keyboard-shortcuts";
|
|
55
12
|
|
|
56
13
|
export const createCodeBlockConfig = createBlockConfig(
|
|
57
14
|
({ defaultLanguage = "text" }: CodeBlockOptions) =>
|
|
@@ -66,6 +23,8 @@ export const createCodeBlockConfig = createBlockConfig(
|
|
|
66
23
|
}) as const,
|
|
67
24
|
);
|
|
68
25
|
|
|
26
|
+
export type CodeBlockConfig = ReturnType<typeof createCodeBlockConfig>;
|
|
27
|
+
|
|
69
28
|
export const createCodeBlockSpec = createBlockSpec(
|
|
70
29
|
createCodeBlockConfig,
|
|
71
30
|
(options) => ({
|
|
@@ -73,236 +32,25 @@ export const createCodeBlockSpec = createBlockSpec(
|
|
|
73
32
|
code: true,
|
|
74
33
|
defining: true,
|
|
75
34
|
isolating: false,
|
|
35
|
+
highlight: (block) => block.props.language,
|
|
76
36
|
},
|
|
77
|
-
parse: (
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return undefined;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const code = e.firstElementChild!;
|
|
90
|
-
const language =
|
|
91
|
-
code.getAttribute("data-language") ||
|
|
92
|
-
code.className
|
|
93
|
-
.split(" ")
|
|
94
|
-
.find((name) => name.includes("language-"))
|
|
95
|
-
?.replace("language-", "");
|
|
96
|
-
|
|
97
|
-
return { language };
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
parseContent: ({ el, schema }) => {
|
|
101
|
-
const parser = DOMParser.fromSchema(schema);
|
|
102
|
-
const code = el.firstElementChild!;
|
|
103
|
-
|
|
104
|
-
return parser.parse(code, {
|
|
105
|
-
preserveWhitespace: "full",
|
|
106
|
-
topNode: schema.nodes["codeBlock"].create(),
|
|
107
|
-
}).content;
|
|
108
|
-
},
|
|
109
|
-
|
|
110
|
-
render(block, editor) {
|
|
111
|
-
const wrapper = document.createDocumentFragment();
|
|
112
|
-
const pre = document.createElement("pre");
|
|
113
|
-
const code = document.createElement("code");
|
|
114
|
-
pre.appendChild(code);
|
|
115
|
-
|
|
116
|
-
let removeSelectChangeListener = undefined;
|
|
117
|
-
|
|
118
|
-
if (options.supportedLanguages) {
|
|
119
|
-
const select = document.createElement("select");
|
|
120
|
-
|
|
121
|
-
Object.entries(options.supportedLanguages ?? {}).forEach(
|
|
122
|
-
([id, { name }]) => {
|
|
123
|
-
const option = document.createElement("option");
|
|
124
|
-
|
|
125
|
-
option.value = id;
|
|
126
|
-
option.text = name;
|
|
127
|
-
select.appendChild(option);
|
|
128
|
-
},
|
|
129
|
-
);
|
|
130
|
-
select.value =
|
|
131
|
-
block.props.language || options.defaultLanguage || "text";
|
|
132
|
-
|
|
133
|
-
if (editor.isEditable) {
|
|
134
|
-
const handleLanguageChange = (event: Event) => {
|
|
135
|
-
const language = (event.target as HTMLSelectElement).value;
|
|
136
|
-
|
|
137
|
-
editor.updateBlock(block.id, { props: { language } });
|
|
138
|
-
};
|
|
139
|
-
select.addEventListener("change", handleLanguageChange);
|
|
140
|
-
removeSelectChangeListener = () =>
|
|
141
|
-
select.removeEventListener("change", handleLanguageChange);
|
|
142
|
-
} else {
|
|
143
|
-
select.disabled = true;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const selectWrapper = document.createElement("div");
|
|
147
|
-
selectWrapper.contentEditable = "false";
|
|
148
|
-
|
|
149
|
-
selectWrapper.appendChild(select);
|
|
150
|
-
wrapper.appendChild(selectWrapper);
|
|
151
|
-
}
|
|
152
|
-
wrapper.appendChild(pre);
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
dom: wrapper,
|
|
156
|
-
contentDOM: code,
|
|
157
|
-
destroy: () => {
|
|
158
|
-
removeSelectChangeListener?.();
|
|
37
|
+
parse: (el) => parsePreCode(el),
|
|
38
|
+
parseContent: (opts) => parsePreCodeContent(opts, "codeBlock"),
|
|
39
|
+
render: (block, editor) =>
|
|
40
|
+
createCodeBlock(
|
|
41
|
+
block,
|
|
42
|
+
editor,
|
|
43
|
+
options.supportedLanguages && {
|
|
44
|
+
selectedLanguage: block.props.language,
|
|
45
|
+
supportedLanguages: options.supportedLanguages,
|
|
159
46
|
},
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
toExternalHTML(block) {
|
|
163
|
-
const pre = document.createElement("pre");
|
|
164
|
-
const code = document.createElement("code");
|
|
165
|
-
code.className = `language-${block.props.language}`;
|
|
166
|
-
code.dataset.language = block.props.language;
|
|
167
|
-
pre.appendChild(code);
|
|
168
|
-
return {
|
|
169
|
-
dom: pre,
|
|
170
|
-
contentDOM: code,
|
|
171
|
-
};
|
|
172
|
-
},
|
|
47
|
+
),
|
|
48
|
+
toExternalHTML: (block) => createPreCode(block),
|
|
173
49
|
}),
|
|
174
|
-
(options) =>
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
createExtension({
|
|
181
|
-
key: "code-block-keyboard-shortcuts",
|
|
182
|
-
keyboardShortcuts: {
|
|
183
|
-
Delete: ({ editor }) => {
|
|
184
|
-
return editor.transact((tr) => {
|
|
185
|
-
const { block } = editor.getTextCursorPosition();
|
|
186
|
-
if (block.type !== "codeBlock") {
|
|
187
|
-
return false;
|
|
188
|
-
}
|
|
189
|
-
const { $from } = tr.selection;
|
|
190
|
-
|
|
191
|
-
// When inside empty codeblock, on `DELETE` key press, delete the codeblock
|
|
192
|
-
if (!$from.parent.textContent) {
|
|
193
|
-
editor.removeBlocks([block]);
|
|
194
|
-
|
|
195
|
-
return true;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return false;
|
|
199
|
-
});
|
|
200
|
-
},
|
|
201
|
-
Tab: ({ editor }) => {
|
|
202
|
-
if (options.indentLineWithTab === false) {
|
|
203
|
-
return false;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
return editor.transact((tr) => {
|
|
207
|
-
const { block } = editor.getTextCursorPosition();
|
|
208
|
-
if (block.type === "codeBlock") {
|
|
209
|
-
// TODO should probably only tab when at a line start or already tabbed in
|
|
210
|
-
tr.insertText(" ");
|
|
211
|
-
return true;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return false;
|
|
215
|
-
});
|
|
216
|
-
},
|
|
217
|
-
Enter: ({ editor }) => {
|
|
218
|
-
return editor.transact((tr) => {
|
|
219
|
-
const { block, nextBlock } = editor.getTextCursorPosition();
|
|
220
|
-
if (block.type !== "codeBlock") {
|
|
221
|
-
return false;
|
|
222
|
-
}
|
|
223
|
-
const { $from } = tr.selection;
|
|
224
|
-
|
|
225
|
-
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
|
|
226
|
-
const endsWithDoubleNewline =
|
|
227
|
-
$from.parent.textContent.endsWith("\n\n");
|
|
228
|
-
|
|
229
|
-
// The user is trying to exit the code block by pressing enter at the end of the code block
|
|
230
|
-
if (isAtEnd && endsWithDoubleNewline) {
|
|
231
|
-
// Remove the double newline
|
|
232
|
-
tr.delete($from.pos - 2, $from.pos);
|
|
233
|
-
|
|
234
|
-
// If there is a next block, move the cursor to it
|
|
235
|
-
if (nextBlock) {
|
|
236
|
-
editor.setTextCursorPosition(nextBlock, "start");
|
|
237
|
-
return true;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// If there is no next block, insert a new paragraph
|
|
241
|
-
const [newBlock] = editor.insertBlocks(
|
|
242
|
-
[{ type: "paragraph" }],
|
|
243
|
-
block,
|
|
244
|
-
"after",
|
|
245
|
-
);
|
|
246
|
-
// Move the cursor to the new block
|
|
247
|
-
editor.setTextCursorPosition(newBlock, "start");
|
|
248
|
-
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
tr.insertText("\n");
|
|
253
|
-
return true;
|
|
254
|
-
});
|
|
255
|
-
},
|
|
256
|
-
"Shift-Enter": ({ editor }) => {
|
|
257
|
-
return editor.transact(() => {
|
|
258
|
-
const { block } = editor.getTextCursorPosition();
|
|
259
|
-
if (block.type !== "codeBlock") {
|
|
260
|
-
return false;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
const [newBlock] = editor.insertBlocks(
|
|
264
|
-
// insert a new paragraph
|
|
265
|
-
[{ type: "paragraph" }],
|
|
266
|
-
block,
|
|
267
|
-
"after",
|
|
268
|
-
);
|
|
269
|
-
// move the cursor to the new block
|
|
270
|
-
editor.setTextCursorPosition(newBlock, "start");
|
|
271
|
-
return true;
|
|
272
|
-
});
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
inputRules: [
|
|
276
|
-
{
|
|
277
|
-
find: /^```(.*?)\s$/,
|
|
278
|
-
replace: ({ match }) => {
|
|
279
|
-
const languageName = match[1].trim();
|
|
280
|
-
const attributes = {
|
|
281
|
-
language: getLanguageId(options, languageName) ?? languageName,
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
return {
|
|
285
|
-
type: "codeBlock",
|
|
286
|
-
props: {
|
|
287
|
-
language: attributes.language,
|
|
288
|
-
},
|
|
289
|
-
content: [],
|
|
290
|
-
};
|
|
291
|
-
},
|
|
292
|
-
},
|
|
293
|
-
],
|
|
294
|
-
}),
|
|
295
|
-
];
|
|
296
|
-
},
|
|
50
|
+
(options) => [
|
|
51
|
+
CodeKeyboardShortcutsExtension(options)(
|
|
52
|
+
CODE_BLOCK_KEYBOARD_SHORTCUTS_KEY,
|
|
53
|
+
"codeBlock",
|
|
54
|
+
),
|
|
55
|
+
],
|
|
297
56
|
);
|
|
298
|
-
|
|
299
|
-
export function getLanguageId(
|
|
300
|
-
options: CodeBlockOptions,
|
|
301
|
-
languageName: string,
|
|
302
|
-
): string | undefined {
|
|
303
|
-
return Object.entries(options.supportedLanguages ?? {}).find(
|
|
304
|
-
([id, { aliases }]) => {
|
|
305
|
-
return aliases?.includes(languageName) || id === languageName;
|
|
306
|
-
},
|
|
307
|
-
)?.[0];
|
|
308
|
-
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { createExtension } from "../../../../editor/BlockNoteExtension.js";
|
|
2
|
+
import { CodeBlockOptions, getLanguageId } from "../../CodeBlockOptions.js";
|
|
3
|
+
|
|
4
|
+
export const CodeKeyboardShortcutsExtension =
|
|
5
|
+
(options: CodeBlockOptions) => (key: string, blockType: string) =>
|
|
6
|
+
createExtension({
|
|
7
|
+
key,
|
|
8
|
+
keyboardShortcuts: {
|
|
9
|
+
Delete: ({ editor }) => {
|
|
10
|
+
return editor.transact((tr) => {
|
|
11
|
+
const { block } = editor.getTextCursorPosition();
|
|
12
|
+
if (block.type !== blockType) {
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
const { $from } = tr.selection;
|
|
16
|
+
|
|
17
|
+
// When inside empty codeblock, on `DELETE` key press, delete the codeblock
|
|
18
|
+
if (!$from.parent.textContent) {
|
|
19
|
+
editor.removeBlocks([block]);
|
|
20
|
+
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return false;
|
|
25
|
+
});
|
|
26
|
+
},
|
|
27
|
+
Tab: ({ editor }) => {
|
|
28
|
+
if (options.indentLineWithTab === false) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return editor.transact((tr) => {
|
|
33
|
+
const { block } = editor.getTextCursorPosition();
|
|
34
|
+
if (block.type === blockType) {
|
|
35
|
+
// TODO should probably only tab when at a line start or already tabbed in
|
|
36
|
+
tr.insertText(" ");
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return false;
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
Enter: ({ editor }) => {
|
|
44
|
+
return editor.transact((tr) => {
|
|
45
|
+
const { block, nextBlock } = editor.getTextCursorPosition();
|
|
46
|
+
if (block.type !== blockType) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
const { $from } = tr.selection;
|
|
50
|
+
|
|
51
|
+
const isAtEnd = $from.parentOffset === $from.parent.nodeSize - 2;
|
|
52
|
+
const endsWithDoubleNewline =
|
|
53
|
+
$from.parent.textContent.endsWith("\n\n");
|
|
54
|
+
|
|
55
|
+
// The user is trying to exit the code block by pressing enter at the end of the code block
|
|
56
|
+
if (isAtEnd && endsWithDoubleNewline) {
|
|
57
|
+
// Remove the double newline
|
|
58
|
+
tr.delete($from.pos - 2, $from.pos);
|
|
59
|
+
|
|
60
|
+
// If there is a next block, move the cursor to it
|
|
61
|
+
if (nextBlock) {
|
|
62
|
+
editor.setTextCursorPosition(nextBlock, "start");
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// If there is no next block, insert a new paragraph
|
|
67
|
+
const [newBlock] = editor.insertBlocks(
|
|
68
|
+
[{ type: "paragraph" }],
|
|
69
|
+
block,
|
|
70
|
+
"after",
|
|
71
|
+
);
|
|
72
|
+
// Move the cursor to the new block
|
|
73
|
+
editor.setTextCursorPosition(newBlock, "start");
|
|
74
|
+
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
tr.insertText("\n");
|
|
79
|
+
return true;
|
|
80
|
+
});
|
|
81
|
+
},
|
|
82
|
+
"Shift-Enter": ({ editor }) => {
|
|
83
|
+
return editor.transact(() => {
|
|
84
|
+
const { block } = editor.getTextCursorPosition();
|
|
85
|
+
if (block.type !== blockType) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const [newBlock] = editor.insertBlocks(
|
|
90
|
+
// insert a new paragraph
|
|
91
|
+
[{ type: "paragraph" }],
|
|
92
|
+
block,
|
|
93
|
+
"after",
|
|
94
|
+
);
|
|
95
|
+
// move the cursor to the new block
|
|
96
|
+
editor.setTextCursorPosition(newBlock, "start");
|
|
97
|
+
return true;
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
inputRules: [
|
|
102
|
+
{
|
|
103
|
+
find: /^```(.*?)\s$/,
|
|
104
|
+
replace: ({ match }) => {
|
|
105
|
+
const languageName = match[1].trim();
|
|
106
|
+
const attributes = {
|
|
107
|
+
language: getLanguageId(options, languageName) ?? languageName,
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
type: blockType,
|
|
112
|
+
props: {
|
|
113
|
+
language: attributes.language,
|
|
114
|
+
},
|
|
115
|
+
content: [],
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
});
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { DOMParser, Schema } from "@tiptap/pm/model";
|
|
2
|
+
|
|
3
|
+
export const parsePreCode = (el: HTMLElement) => {
|
|
4
|
+
{
|
|
5
|
+
if (el.tagName !== "PRE") {
|
|
6
|
+
return undefined;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (
|
|
10
|
+
el.childElementCount !== 1 ||
|
|
11
|
+
el.firstElementChild?.tagName !== "CODE"
|
|
12
|
+
) {
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const code = el.firstElementChild!;
|
|
17
|
+
const language =
|
|
18
|
+
code.getAttribute("data-language") ||
|
|
19
|
+
code.className
|
|
20
|
+
.split(" ")
|
|
21
|
+
.find((name) => name.startsWith("language-"))
|
|
22
|
+
?.replace("language-", "");
|
|
23
|
+
|
|
24
|
+
return { language };
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export const parsePreCodeContent = (
|
|
29
|
+
{
|
|
30
|
+
el,
|
|
31
|
+
schema,
|
|
32
|
+
}: {
|
|
33
|
+
el: HTMLElement;
|
|
34
|
+
schema: Schema;
|
|
35
|
+
},
|
|
36
|
+
blockType: string,
|
|
37
|
+
) => {
|
|
38
|
+
const parser = DOMParser.fromSchema(schema);
|
|
39
|
+
const code = el.firstElementChild!;
|
|
40
|
+
|
|
41
|
+
return parser.parse(code, {
|
|
42
|
+
preserveWhitespace: "full",
|
|
43
|
+
topNode: schema.nodes[blockType].create(),
|
|
44
|
+
}).content;
|
|
45
|
+
};
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { BlockNoteEditor } from "../../../../editor/BlockNoteEditor.js";
|
|
2
|
+
import type { BlockFromConfig } from "../../../../schema/index.js";
|
|
3
|
+
|
|
4
|
+
// Select dropdown to change the block's language. Assumes `block` has a `language` prop.
|
|
5
|
+
export const createLanguageSelect = (
|
|
6
|
+
block: BlockFromConfig<any, any, any>,
|
|
7
|
+
editor: BlockNoteEditor<any>,
|
|
8
|
+
selectedLanguage: string,
|
|
9
|
+
supportedLanguages: Record<
|
|
10
|
+
string,
|
|
11
|
+
{
|
|
12
|
+
name: string;
|
|
13
|
+
}
|
|
14
|
+
>,
|
|
15
|
+
) => {
|
|
16
|
+
if (!(selectedLanguage in supportedLanguages)) {
|
|
17
|
+
throw new Error(`Language ${selectedLanguage} is not supported.`);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const select = document.createElement("select");
|
|
21
|
+
Object.entries(supportedLanguages).forEach(([id, { name }]) => {
|
|
22
|
+
const option = document.createElement("option");
|
|
23
|
+
option.value = id;
|
|
24
|
+
option.text = name;
|
|
25
|
+
select.appendChild(option);
|
|
26
|
+
});
|
|
27
|
+
select.value = selectedLanguage;
|
|
28
|
+
|
|
29
|
+
const handleLanguageChange = (event: Event) => {
|
|
30
|
+
if (!editor.isEditable) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
editor.updateBlock(block.id, {
|
|
35
|
+
props: { language: (event.target as HTMLSelectElement).value },
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
if (editor.isEditable) {
|
|
40
|
+
select.addEventListener("change", handleLanguageChange);
|
|
41
|
+
} else {
|
|
42
|
+
select.disabled = true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const selectWrapper = document.createElement("div");
|
|
46
|
+
selectWrapper.contentEditable = "false";
|
|
47
|
+
selectWrapper.appendChild(select);
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
dom: selectWrapper,
|
|
51
|
+
destroy: () => select.removeEventListener("change", handleLanguageChange),
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// Renders the block's inline content as code, alongside a language picker, if multiple languages
|
|
56
|
+
// are supported.
|
|
57
|
+
export const createCodeBlock = (
|
|
58
|
+
block: BlockFromConfig<any, any, any>,
|
|
59
|
+
editor: BlockNoteEditor<any>,
|
|
60
|
+
options?: {
|
|
61
|
+
selectedLanguage: string;
|
|
62
|
+
supportedLanguages: Record<
|
|
63
|
+
string,
|
|
64
|
+
{
|
|
65
|
+
name: string;
|
|
66
|
+
}
|
|
67
|
+
>;
|
|
68
|
+
},
|
|
69
|
+
) => {
|
|
70
|
+
const pre = document.createElement("pre");
|
|
71
|
+
const code = document.createElement("code");
|
|
72
|
+
pre.appendChild(code);
|
|
73
|
+
|
|
74
|
+
const sourceBlock = document.createDocumentFragment();
|
|
75
|
+
|
|
76
|
+
let languageSelect: ReturnType<typeof createLanguageSelect> | undefined =
|
|
77
|
+
undefined;
|
|
78
|
+
if (options && Object.keys(options.supportedLanguages).length > 1) {
|
|
79
|
+
languageSelect = createLanguageSelect(
|
|
80
|
+
block,
|
|
81
|
+
editor,
|
|
82
|
+
options.selectedLanguage,
|
|
83
|
+
options.supportedLanguages,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
sourceBlock.appendChild(languageSelect.dom);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
sourceBlock.appendChild(pre);
|
|
90
|
+
|
|
91
|
+
return {
|
|
92
|
+
dom: sourceBlock,
|
|
93
|
+
contentDOM: code,
|
|
94
|
+
destroy: () => {
|
|
95
|
+
languageSelect?.destroy();
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { BlockFromConfig } from "../../../../schema/index.js";
|
|
2
|
+
|
|
3
|
+
export const createPreCode = (block: BlockFromConfig<any, any, any>) => {
|
|
4
|
+
const pre = document.createElement("pre");
|
|
5
|
+
const code = document.createElement("code");
|
|
6
|
+
code.className = `language-${block.props.language}`;
|
|
7
|
+
code.dataset.language = block.props.language;
|
|
8
|
+
pre.appendChild(code);
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
dom: pre,
|
|
12
|
+
contentDOM: code,
|
|
13
|
+
};
|
|
14
|
+
};
|