@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.
Files changed (221) hide show
  1. package/dist/BlockNoteExtension-BJCLnwrE.cjs +2 -0
  2. package/dist/BlockNoteExtension-BJCLnwrE.cjs.map +1 -0
  3. package/dist/BlockNoteExtension-CbvDA0F7.js +80 -0
  4. package/dist/BlockNoteExtension-CbvDA0F7.js.map +1 -0
  5. package/dist/{UserStore-QbbPFj0z.js → UserStore-C4TzkGEn.js} +3 -3
  6. package/dist/{UserStore-DCG6lsla.cjs.map → UserStore-C4TzkGEn.js.map} +1 -1
  7. package/dist/{UserStore-DCG6lsla.cjs → UserStore-mj3fXW4D.cjs} +2 -2
  8. package/dist/UserStore-mj3fXW4D.cjs.map +1 -0
  9. package/dist/blocknote.cjs +1 -1
  10. package/dist/blocknote.js +6 -6
  11. package/dist/{blocks-CiuUGa1E.js → blocks-CzQLehlc.js} +695 -652
  12. package/dist/blocks-CzQLehlc.js.map +1 -0
  13. package/dist/blocks-CzkqclGj.cjs +12 -0
  14. package/dist/blocks-CzkqclGj.cjs.map +1 -0
  15. package/dist/blocks.cjs +1 -1
  16. package/dist/blocks.js +2 -2
  17. package/dist/comments.cjs +1 -1
  18. package/dist/comments.cjs.map +1 -1
  19. package/dist/comments.js +7 -7
  20. package/dist/comments.js.map +1 -1
  21. package/dist/en-BuzSuuTq.cjs +2 -0
  22. package/dist/en-BuzSuuTq.cjs.map +1 -0
  23. package/dist/{en-CGKXfWma.js → en-uzHzj6eH.js} +10 -1
  24. package/dist/en-uzHzj6eH.js.map +1 -0
  25. package/dist/extensions-CSB9ZvuL.cjs +58 -0
  26. package/dist/extensions-CSB9ZvuL.cjs.map +1 -0
  27. package/dist/{extensions-CrUGDOks.js → extensions-DGuhtOgB.js} +717 -452
  28. package/dist/extensions-DGuhtOgB.js.map +1 -0
  29. package/dist/extensions.cjs +1 -1
  30. package/dist/extensions.js +4 -4
  31. package/dist/locales.cjs +1 -1
  32. package/dist/locales.cjs.map +1 -1
  33. package/dist/locales.js +199 -1
  34. package/dist/locales.js.map +1 -1
  35. package/dist/{src-DTF83Cl9.js → src-BkfZVfaO.js} +921 -835
  36. package/dist/src-BkfZVfaO.js.map +1 -0
  37. package/dist/src-DM3syMdf.cjs +23 -0
  38. package/dist/src-DM3syMdf.cjs.map +1 -0
  39. package/dist/style.css +1 -1
  40. package/dist/webpack-stats.json +1 -1
  41. package/dist/y.cjs +1 -1
  42. package/dist/y.cjs.map +1 -1
  43. package/dist/y.js +65 -62
  44. package/dist/y.js.map +1 -1
  45. package/dist/yjs.cjs +1 -1
  46. package/dist/yjs.cjs.map +1 -1
  47. package/dist/yjs.js +88 -76
  48. package/dist/yjs.js.map +1 -1
  49. package/package.json +29 -30
  50. package/src/api/blockManipulation/commands/updateBlock/__snapshots__/updateBlock.test.ts.snap +2505 -763
  51. package/src/api/blockManipulation/commands/updateBlock/updateBlock.test.ts +42 -0
  52. package/src/api/blockManipulation/commands/updateBlock/updateBlock.ts +16 -4
  53. package/src/api/clipboard/fromClipboard/handleFileInsertion.ts +10 -1
  54. package/src/api/exporters/html/util/serializeBlocksExternalHTML.ts +42 -8
  55. package/src/api/exporters/markdown/htmlToMarkdown.ts +45 -10
  56. package/src/api/exporters/markdown/markdownExporter.ts +10 -1
  57. package/src/api/nodeConversions/blockToNode.ts +5 -1
  58. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  59. package/src/api/parsers/html/parseHTML.ts +43 -0
  60. package/src/api/pmUtil.ts +16 -8
  61. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  62. package/src/blocks/Code/block.test.ts +1 -1
  63. package/src/blocks/Code/block.ts +29 -281
  64. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  65. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  66. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  67. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  68. package/src/blocks/Table/TableExtension.test.ts +102 -0
  69. package/src/blocks/ToggleWrapper/createToggleWrapper.ts +16 -5
  70. package/src/blocks/defaultBlockTypeGuards.ts +6 -0
  71. package/src/blocks/index.ts +3 -0
  72. package/src/comments/extension.ts +2 -4
  73. package/src/editor/Block.css +324 -3
  74. package/src/editor/BlockNoteEditor.ts +25 -4
  75. package/src/editor/BlockNoteExtension.ts +1 -1
  76. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  77. package/src/exporter/ExportImage.ts +50 -0
  78. package/src/exporter/Exporter.test.ts +48 -0
  79. package/src/exporter/Exporter.ts +46 -12
  80. package/src/exporter/index.ts +1 -0
  81. package/src/exporter/mapping.ts +4 -1
  82. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  83. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  84. package/src/extensions/SideMenu/SideMenu.ts +20 -3
  85. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  86. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  87. package/src/extensions/SuggestionMenu/SuggestionMenu.test.ts +118 -0
  88. package/src/extensions/SuggestionMenu/SuggestionMenu.ts +40 -27
  89. package/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.ts +13 -3
  90. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  91. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  92. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  93. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  94. package/src/extensions/TableHandles/TableHandles.ts +25 -12
  95. package/src/extensions/TrailingNode/TrailingNode.ts +98 -32
  96. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  97. package/src/extensions/index.ts +4 -0
  98. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  99. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  100. package/src/i18n/locales/ar.ts +9 -0
  101. package/src/i18n/locales/de.ts +9 -0
  102. package/src/i18n/locales/en.ts +9 -0
  103. package/src/i18n/locales/es.ts +9 -0
  104. package/src/i18n/locales/fa.ts +9 -0
  105. package/src/i18n/locales/fr.ts +9 -0
  106. package/src/i18n/locales/he.ts +9 -0
  107. package/src/i18n/locales/hr.ts +9 -0
  108. package/src/i18n/locales/is.ts +9 -0
  109. package/src/i18n/locales/it.ts +9 -0
  110. package/src/i18n/locales/ja.ts +9 -0
  111. package/src/i18n/locales/ko.ts +9 -0
  112. package/src/i18n/locales/nl.ts +9 -0
  113. package/src/i18n/locales/no.ts +9 -0
  114. package/src/i18n/locales/pl.ts +9 -0
  115. package/src/i18n/locales/pt.ts +9 -0
  116. package/src/i18n/locales/ru.ts +9 -0
  117. package/src/i18n/locales/sk.ts +9 -0
  118. package/src/i18n/locales/uk.ts +9 -0
  119. package/src/i18n/locales/uz.ts +9 -0
  120. package/src/i18n/locales/vi.ts +9 -0
  121. package/src/i18n/locales/zh-tw.ts +9 -0
  122. package/src/i18n/locales/zh.ts +9 -0
  123. package/src/index.ts +6 -1
  124. package/src/schema/blocks/createSpec.ts +17 -5
  125. package/src/schema/blocks/internal.test.ts +86 -0
  126. package/src/schema/blocks/internal.ts +64 -0
  127. package/src/schema/blocks/types.ts +53 -3
  128. package/src/schema/contentTypePropagation.test.ts +1 -1
  129. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  130. package/src/schema/inlineContent/createSpec.ts +180 -13
  131. package/src/schema/inlineContent/internal.ts +21 -2
  132. package/src/schema/inlineContent/types.ts +35 -5
  133. package/src/schema/nodeViewMutations.test.ts +121 -0
  134. package/src/schema/nodeViewMutations.ts +46 -0
  135. package/src/user/UserStore.ts +1 -1
  136. package/src/util/Store.test.ts +132 -0
  137. package/src/util/Store.ts +135 -0
  138. package/src/y/extensions/AttributionExtension.test.ts +10 -0
  139. package/src/y/extensions/Versioning.test.ts +20 -7
  140. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  141. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  142. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  143. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  144. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  145. package/src/yjs/extensions/Versioning.test.ts +17 -5
  146. package/types/src/api/exporters/html/util/serializeBlocksExternalHTML.d.ts +13 -0
  147. package/types/src/api/pmUtil.d.ts +7 -7
  148. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  149. package/types/src/blocks/Code/block.d.ts +3 -53
  150. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  151. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  152. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  153. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  154. package/types/src/blocks/defaultBlockTypeGuards.d.ts +2 -0
  155. package/types/src/blocks/index.d.ts +3 -0
  156. package/types/src/comments/extension.d.ts +1 -15
  157. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  158. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  159. package/types/src/exporter/ExportImage.d.ts +36 -0
  160. package/types/src/exporter/Exporter.d.ts +21 -0
  161. package/types/src/exporter/Exporter.test.d.ts +1 -0
  162. package/types/src/exporter/index.d.ts +1 -0
  163. package/types/src/exporter/mapping.d.ts +1 -1
  164. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  165. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  166. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  167. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  168. package/types/src/extensions/SideMenu/SideMenu.d.ts +6 -1
  169. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  170. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  171. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +7 -11
  172. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  173. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  174. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  175. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  176. package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
  177. package/types/src/extensions/TrailingNode/TrailingNode.d.ts +5 -4
  178. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  179. package/types/src/extensions/index.d.ts +4 -0
  180. package/types/src/i18n/locales/en.d.ts +9 -0
  181. package/types/src/i18n/locales/fa.d.ts +9 -0
  182. package/types/src/i18n/locales/sk.d.ts +9 -0
  183. package/types/src/index.d.ts +3 -1
  184. package/types/src/schema/blocks/internal.d.ts +21 -0
  185. package/types/src/schema/blocks/internal.test.d.ts +1 -0
  186. package/types/src/schema/blocks/types.d.ts +36 -3
  187. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  188. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  189. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  190. package/types/src/schema/inlineContent/types.d.ts +22 -3
  191. package/types/src/schema/nodeViewMutations.d.ts +3 -0
  192. package/types/src/schema/nodeViewMutations.test.d.ts +1 -0
  193. package/types/src/user/UserStore.d.ts +1 -1
  194. package/types/src/util/Store.d.ts +45 -0
  195. package/types/src/util/Store.test.d.ts +1 -0
  196. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  197. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  198. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  199. package/types/src/y/extensions/index.d.ts +2 -10
  200. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  201. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  202. package/types/src/yjs/extensions/index.d.ts +1 -5
  203. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  204. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  205. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  206. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  207. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  208. package/dist/blocks-BqHv0xhc.cjs +0 -12
  209. package/dist/blocks-BqHv0xhc.cjs.map +0 -1
  210. package/dist/blocks-CiuUGa1E.js.map +0 -1
  211. package/dist/en-C5Z4Ag9_.cjs +0 -2
  212. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  213. package/dist/en-CGKXfWma.js.map +0 -1
  214. package/dist/extensions-CrUGDOks.js.map +0 -1
  215. package/dist/extensions-Ghr9QqqR.cjs +0 -52
  216. package/dist/extensions-Ghr9QqqR.cjs.map +0 -1
  217. package/dist/src-DTF83Cl9.js.map +0 -1
  218. package/dist/src-OfbaVbMw.cjs +0 -21
  219. package/dist/src-OfbaVbMw.cjs.map +0 -1
  220. package/src/blocks/Code/shiki.ts +0 -73
  221. package/types/src/blocks/Code/shiki.d.ts +0 -4
@@ -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 { lazyShikiPlugin } from "./shiki.js";
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
- export type CodeBlockOptions = {
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: (e) => {
78
- if (e.tagName !== "PRE") {
79
- return undefined;
80
- }
81
-
82
- if (
83
- e.childElementCount !== 1 ||
84
- e.firstElementChild?.tagName !== "CODE"
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
- return [
176
- createExtension({
177
- key: "code-block-highlighter",
178
- prosemirrorPlugins: [lazyShikiPlugin(options)],
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
+ };