@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
@@ -0,0 +1,116 @@
1
+ import type { HighlighterGeneric } from "@shikijs/types";
2
+ import { Parser, createHighlightPlugin } from "prosemirror-highlight";
3
+ import { createParser } from "prosemirror-highlight/shiki";
4
+ import type { SyntaxHighlightingOptions } from "./SyntaxHighlighting.js";
5
+ import { CustomBlockNoteSchema } from "../../schema/schema.js";
6
+
7
+ export const shikiParserSymbol = Symbol.for("blocknote.shikiParser");
8
+ export const shikiHighlighterPromiseSymbol = Symbol.for(
9
+ "blocknote.shikiHighlighterPromise",
10
+ );
11
+
12
+ // Languages that represent "no highlighting" - skipped without asking Shiki to
13
+ // load a grammar for them.
14
+ const PLAIN_TEXT_LANGUAGES = ["text", "none", "plaintext", "txt"];
15
+
16
+ /**
17
+ * Creates the syntax highlighting plugin for the given block types, lazily
18
+ * loading the highlighter on first use.
19
+ *
20
+ * Each spec's `meta.highlight` callback resolves a node to a language, which is
21
+ * passed straight to Shiki - it resolves aliases and loads the grammar from its
22
+ * bundle, so any language the provided highlighter bundles can be highlighted.
23
+ */
24
+ export function lazyShikiPlugin(
25
+ options: SyntaxHighlightingOptions,
26
+ nodeTypes: string[],
27
+ schema: CustomBlockNoteSchema<any, any, any>,
28
+ ) {
29
+ const globalThisForShiki = globalThis as {
30
+ [shikiHighlighterPromiseSymbol]?: Promise<HighlighterGeneric<any, any>>;
31
+ [shikiParserSymbol]?: Parser;
32
+ };
33
+
34
+ let highlighter: HighlighterGeneric<any, any> | undefined;
35
+ let parser: Parser | undefined;
36
+ // Languages the highlighter failed to load (e.g. not in its bundle). Tracked
37
+ // so we don't keep retrying - and re-triggering re-highlights - forever.
38
+ const unsupportedLanguages = new Set<string>();
39
+ const lazyParser: Parser = (parserOptions) => {
40
+ if (!options.createHighlighter) {
41
+ return [];
42
+ }
43
+ if (!highlighter) {
44
+ globalThisForShiki[shikiHighlighterPromiseSymbol] =
45
+ globalThisForShiki[shikiHighlighterPromiseSymbol] ||
46
+ options.createHighlighter();
47
+
48
+ return globalThisForShiki[shikiHighlighterPromiseSymbol].then(
49
+ (createdHighlighter) => {
50
+ highlighter = createdHighlighter;
51
+ },
52
+ );
53
+ }
54
+ const language = parserOptions.language;
55
+
56
+ if (
57
+ !language ||
58
+ PLAIN_TEXT_LANGUAGES.includes(language) ||
59
+ unsupportedLanguages.has(language)
60
+ ) {
61
+ return [];
62
+ }
63
+
64
+ if (!highlighter.getLoadedLanguages().includes(language)) {
65
+ return highlighter.loadLanguage(language as any).catch(() => {
66
+ // The highlighter doesn't bundle this language - give up on it so we
67
+ // don't loop trying to load it on every re-highlight.
68
+ unsupportedLanguages.add(language);
69
+ });
70
+ }
71
+
72
+ if (!parser) {
73
+ parser =
74
+ globalThisForShiki[shikiParserSymbol] ||
75
+ createParser(highlighter as any, pickThemeOptions(highlighter));
76
+ globalThisForShiki[shikiParserSymbol] = parser;
77
+ }
78
+
79
+ return parser(parserOptions);
80
+ };
81
+
82
+ return createHighlightPlugin({
83
+ parser: lazyParser,
84
+ // The highlight plugin only gives us the block content node, so we can only
85
+ // reconstruct the block's `type` and `props` (which is all a spec's
86
+ // `meta.highlight` needs to pick a language).
87
+ languageExtractor: (node) => {
88
+ const nodeShape = {
89
+ type: node.type.name,
90
+ props: node.attrs,
91
+ };
92
+ // search for the node in the blockSpec or inlineContentSpecs
93
+ const spec =
94
+ schema.blockSpecs[nodeShape.type] ||
95
+ schema.inlineContentSpecs[nodeShape.type];
96
+
97
+ return spec?.implementation?.meta?.highlight?.(nodeShape) ?? undefined;
98
+ },
99
+ nodeTypes,
100
+ });
101
+ }
102
+
103
+ // If a light and dark theme is added to the highlighter, this function specifies them in
104
+ // `createParser`. This lets us use `--shiki-light` and `--shiki-dark` CSS variables for correct
105
+ // styling for both light & dark editor themes.
106
+ function pickThemeOptions(highlighter: HighlighterGeneric<any, any>) {
107
+ const themes = highlighter.getLoadedThemes();
108
+ const light = themes.find((t) => /light/i.test(t));
109
+ const dark = themes.find((t) => /dark/i.test(t));
110
+
111
+ if (light && dark) {
112
+ return { themes: { light, dark }, defaultColor: false as const };
113
+ }
114
+
115
+ return undefined;
116
+ }
@@ -28,6 +28,7 @@ import { nodeToBlock } from "../../api/nodeConversions/nodeToBlock.js";
28
28
  import { getNodeById } from "../../api/nodeUtil.js";
29
29
  import {
30
30
  editorHasBlockWithType,
31
+ isTableCellNode,
31
32
  isTableCellSelection,
32
33
  } from "../../blocks/defaultBlockTypeGuards.js";
33
34
  import { DefaultBlockSchema } from "../../blocks/defaultBlocks.js";
@@ -533,10 +534,10 @@ export class TableHandlesView implements PluginView {
533
534
  }
534
535
 
535
536
  // Hide handles if the table block has been removed.
536
- this.state.block = this.editor.getBlock(this.state.block.id)!;
537
+ const refreshedBlock = this.editor.getBlock(this.state.block.id);
537
538
  if (
538
- !this.state.block ||
539
- this.state.block.type !== "table" ||
539
+ !refreshedBlock ||
540
+ refreshedBlock.type !== "table" ||
540
541
  // when collaborating, the table element might be replaced and out of date
541
542
  // because yjs replaces the element when for example you change the color via the side menu
542
543
  !this.tableElement?.isConnected
@@ -548,6 +549,7 @@ export class TableHandlesView implements PluginView {
548
549
 
549
550
  return;
550
551
  }
552
+ this.state.block = refreshedBlock as typeof this.state.block;
551
553
 
552
554
  const { height: rowCount, width: colCount } = getDimensionsOfTable(
553
555
  this.state.block,
@@ -1106,15 +1108,26 @@ export const TableHandlesExtension = createExtension(({ editor }) => {
1106
1108
  // When the selection is a normal text selection
1107
1109
  // Assumes we are within a tableParagraph
1108
1110
  // And find the from and to cells by resolving the positions
1109
- $fromCell = tr.doc.resolve(
1110
- selection.$from.pos - selection.$from.parentOffset - 1,
1111
- );
1112
- $toCell = tr.doc.resolve(
1113
- selection.$to.pos - selection.$to.parentOffset - 1,
1114
- );
1115
-
1116
- // Opt-out when the selection is not pointing into cells
1117
- if ($fromCell.pos === 0 || $toCell.pos === 0) {
1111
+ const fromCellPos =
1112
+ selection.$from.pos - selection.$from.parentOffset - 1;
1113
+ const toCellPos = selection.$to.pos - selection.$to.parentOffset - 1;
1114
+
1115
+ // Opt-out when the selection is not pointing into cells. This happens when the selection
1116
+ // is at the start of the table's `blockContainer` node and therefore just before the
1117
+ // actual `table` node.
1118
+ if (fromCellPos < 0 || toCellPos < 0) {
1119
+ return undefined;
1120
+ }
1121
+
1122
+ $fromCell = tr.doc.resolve(fromCellPos);
1123
+ $toCell = tr.doc.resolve(toCellPos);
1124
+
1125
+ // Opt-out when the selection is not actually pointing into table
1126
+ // cells (e.g. a gap cursor next to a nested block).
1127
+ if (
1128
+ !isTableCellNode($fromCell.parent) ||
1129
+ !isTableCellNode($toCell.parent)
1130
+ ) {
1118
1131
  return undefined;
1119
1132
  }
1120
1133
  }
@@ -13,15 +13,10 @@ import {
13
13
 
14
14
  const PLUGIN_KEY = new PluginKey<DecorationSet>("trailingNode");
15
15
 
16
- // Skip the widget when the editor isn't editable, or when the document already
17
- // ends with an empty paragraph block (since the user can just type into it).
18
- function shouldShowTrailingWidget(doc: PMNode, isEditable: boolean): boolean {
19
- if (!isEditable) {
20
- return false;
21
- }
22
-
23
- const rootGroup = doc.lastChild;
24
- const lastBlock = rootGroup?.lastChild;
16
+ // Skip the widget when the container already ends with an empty paragraph
17
+ // block (since the user can just type into it).
18
+ function containerNeedsTrailingWidget(container: PMNode): boolean {
19
+ const lastBlock = container.lastChild;
25
20
  const lastContent = lastBlock?.firstChild;
26
21
 
27
22
  return !(
@@ -31,12 +26,48 @@ function shouldShowTrailingWidget(doc: PMNode, isEditable: boolean): boolean {
31
26
  );
32
27
  }
33
28
 
29
+ // Returns the position at the end of each container that should render a
30
+ // trailing widget: the root blockGroup, and columns from the multi-column
31
+ // package. Nested blockGroups (a block's children) are excluded, as they have
32
+ // no empty space below them for a widget to occupy.
33
+ function getTrailingWidgetPositions(doc: PMNode): number[] {
34
+ // When the schema has no columns, the root blockGroup is the only possible
35
+ // container, so traversing the doc to find others can be skipped.
36
+ if (!doc.type.schema.nodes["column"]) {
37
+ const rootGroup = doc.lastChild;
38
+ return rootGroup && containerNeedsTrailingWidget(rootGroup)
39
+ ? [doc.content.size - 1]
40
+ : [];
41
+ }
42
+
43
+ const positions: number[] = [];
44
+
45
+ doc.descendants((node, pos, parent) => {
46
+ if (node.isTextblock) {
47
+ return false;
48
+ }
49
+
50
+ const isContainer =
51
+ node.type.name === "column" ||
52
+ (node.type.name === "blockGroup" && parent?.type.name === "doc");
53
+
54
+ if (isContainer && containerNeedsTrailingWidget(node)) {
55
+ positions.push(pos + node.nodeSize - 1);
56
+ }
57
+
58
+ return true;
59
+ });
60
+
61
+ return positions;
62
+ }
63
+
34
64
  /**
35
65
  * Renders a fake trailing block as a widget decoration after the last block of
36
- * the document. Clicking it inserts a real trailing block and moves the
37
- * selection into it. This way the trailing block is not part of the document
38
- * content, so it doesn't appear when the editor is read-only or when the
39
- * content is exported.
66
+ * the document, as well as of any other container that blocks can be appended
67
+ * to (e.g. columns). Clicking it inserts a real trailing block in the
68
+ * container and moves the selection into it. This way the trailing block is
69
+ * not part of the document content, so it doesn't appear when the editor is
70
+ * read-only or when the content is exported.
40
71
  */
41
72
  export const TrailingNodeExtension = createExtension(
42
73
  ({ editor }: ExtensionOptions) => {
@@ -52,17 +83,33 @@ export const TrailingNodeExtension = createExtension(
52
83
  // based on this click.
53
84
  event.preventDefault();
54
85
 
86
+ const view = editor.prosemirrorView;
87
+ if (!view) {
88
+ return;
89
+ }
90
+
91
+ // The widget may have been remapped since it was created, so its
92
+ // container is resolved from its current DOM position instead of
93
+ // captured up front.
94
+ const container = view.state.doc.resolve(
95
+ view.posAtDOM(el, 0),
96
+ ).parent;
97
+ const lastBlockId = container.lastChild?.attrs["id"];
98
+ if (!lastBlockId) {
99
+ return;
100
+ }
101
+
55
102
  editor.transact((tr) => {
56
103
  const [insertedBlock] = editor.insertBlocks(
57
104
  [{ type: "paragraph" }],
58
- editor.document[editor.document.length - 1],
105
+ lastBlockId,
59
106
  "after",
60
107
  );
61
108
  editor.setTextCursorPosition(insertedBlock, "start");
62
109
  tr.scrollIntoView();
63
110
  });
64
111
 
65
- editor.prosemirrorView?.focus();
112
+ view.focus();
66
113
  });
67
114
  return el;
68
115
  },
@@ -70,29 +117,44 @@ export const TrailingNodeExtension = createExtension(
70
117
  );
71
118
  }
72
119
 
73
- // Maps the existing DecorationSet through the transaction, then
74
- // incrementally adds or removes the widget only if the show/hide state
75
- // crossed over. The underlying Decoration (and its rendered DOM) stays
76
- // reference-stable across transactions.
120
+ // Maps the existing DecorationSet through the transaction, then diffs it
121
+ // against the containers that should currently show a widget, only adding
122
+ // and removing where the two differ. Decorations (and their rendered DOM)
123
+ // stay reference-stable across transactions for unchanged containers.
77
124
  function nextDecorationSet(
78
125
  tr: Transaction,
79
126
  oldSet: DecorationSet,
80
127
  isEditable: boolean,
81
128
  ): DecorationSet {
82
129
  const mapped = oldSet.map(tr.mapping, tr.doc);
83
- const existing = mapped.find();
84
- const wasShowing = existing.length > 0;
85
- const shouldShow = shouldShowTrailingWidget(tr.doc, isEditable);
130
+ const desiredPositions = new Set(
131
+ isEditable ? getTrailingWidgetPositions(tr.doc) : [],
132
+ );
133
+
134
+ const keptPositions = new Set<number>();
135
+ const stale: Decoration[] = [];
136
+ for (const decoration of mapped.find()) {
137
+ if (
138
+ desiredPositions.has(decoration.from) &&
139
+ !keptPositions.has(decoration.from)
140
+ ) {
141
+ keptPositions.add(decoration.from);
142
+ } else {
143
+ stale.push(decoration);
144
+ }
145
+ }
146
+ const missing = [...desiredPositions].filter(
147
+ (pos) => !keptPositions.has(pos),
148
+ );
86
149
 
87
- if (wasShowing === shouldShow) {
88
- return mapped;
150
+ let next = mapped;
151
+ if (stale.length > 0) {
152
+ next = next.remove(stale);
89
153
  }
90
- if (wasShowing) {
91
- return mapped.remove(existing);
154
+ if (missing.length > 0) {
155
+ next = next.add(tr.doc, missing.map(createTrailingWidget));
92
156
  }
93
- return mapped.add(tr.doc, [
94
- createTrailingWidget(tr.doc.content.size - 1),
95
- ]);
157
+ return next;
96
158
  }
97
159
 
98
160
  return {
@@ -133,8 +195,9 @@ export const TrailingNodeExtension = createExtension(
133
195
  props: {
134
196
  decorations: (state) => PLUGIN_KEY.getState(state),
135
197
  // Prevents ProseMirror from trying to move the selection into the
136
- // trailing block, which causes the text caret to flicker in it
137
- // before returning to its previous position.
198
+ // trailing block at the end of the document, which causes the text
199
+ // caret to flicker in it before returning to its previous
200
+ // position.
138
201
  handleKeyDown: (view, event) => {
139
202
  if (event.key !== "ArrowRight" && event.key !== "ArrowDown") {
140
203
  return false;
@@ -150,8 +213,11 @@ export const TrailingNodeExtension = createExtension(
150
213
  return false;
151
214
  }
152
215
 
216
+ const rootGroup = view.state.doc.lastChild;
153
217
  if (
154
- !shouldShowTrailingWidget(view.state.doc, editor.isEditable)
218
+ !editor.isEditable ||
219
+ !rootGroup ||
220
+ !containerNeedsTrailingWidget(rootGroup)
155
221
  ) {
156
222
  return false;
157
223
  }
@@ -136,13 +136,23 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
136
136
  const contents = new Map<string, Block<any, any, any>[]>();
137
137
  let nextId = 1;
138
138
 
139
+ // `Date.now()` only has millisecond resolution, so two snapshots created in
140
+ // the same tick would share a timestamp and `sortSnapshotsNewestFirst` (which
141
+ // has nothing else to order on) could list them oldest-first. Hand out
142
+ // strictly increasing timestamps so creation order is always preserved.
143
+ let lastTimestamp = 0;
144
+ function nextTimestamp() {
145
+ lastTimestamp = Math.max(Date.now(), lastTimestamp + 1);
146
+ return lastTimestamp;
147
+ }
148
+
139
149
  return {
140
150
  async list() {
141
151
  return sortSnapshotsNewestFirst([...snapshots]);
142
152
  },
143
153
 
144
154
  async create(currentDoc, options) {
145
- const now = Date.now();
155
+ const now = nextTimestamp();
146
156
  const id = String(nextId++);
147
157
  const snapshot: VersionSnapshot = {
148
158
  id,
@@ -166,7 +176,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
166
176
 
167
177
  // Create a "Restored from …" snapshot of the current state before
168
178
  // restoring, so the user can undo the restore.
169
- const now = Date.now();
179
+ const now = nextTimestamp();
170
180
  const backupId = String(nextId++);
171
181
  const backup: VersionSnapshot = {
172
182
  id: backupId,
@@ -196,7 +206,7 @@ export function createInMemoryVersioningEndpoints(): VersioningEndpoints<
196
206
  throw new Error(`Snapshot ${String(snapshot.id)} not found`);
197
207
  }
198
208
  stored.name = name;
199
- stored.updatedAt = Date.now();
209
+ stored.updatedAt = nextTimestamp();
200
210
  },
201
211
 
202
212
  async remove(snapshot) {
@@ -3,6 +3,7 @@ export * from "./DropCursor/DropCursor.js";
3
3
  export * from "./FilePanel/FilePanel.js";
4
4
  export * from "./FormattingToolbar/FormattingToolbar.js";
5
5
  export * from "./History/History.js";
6
+ export * from "./InlineContentBoundaryEdit/InlineContentBoundaryEdit.js";
6
7
  export * from "./LinkToolbar/LinkToolbar.js";
7
8
  export * from "./LinkToolbar/protocols.js";
8
9
  export * from "./NodeSelectionKeyboard/NodeSelectionKeyboard.js";
@@ -11,7 +12,10 @@ export * from "./PositionMapping/PositionMapping.js";
11
12
  export * from "./PreviousBlockType/PreviousBlockType.js";
12
13
  export * from "./ShowSelection/ShowSelection.js";
13
14
  export * from "./SideMenu/SideMenu.js";
15
+ export * from "./SourceBlockWithPreview/SourceBlockWithPreview.js";
16
+ export * from "./SourceInlineContentWithPreview/SourceInlineContentWithPreview.js";
14
17
  export * from "./SuggestionMenu/DefaultGridSuggestionItem.js";
18
+ export * from "./SyntaxHighlighting/SyntaxHighlighting.js";
15
19
  export * from "./SuggestionMenu/DefaultSuggestionItem.js";
16
20
  export * from "./SuggestionMenu/getDefaultEmojiPickerItems.js";
17
21
  export * from "./SuggestionMenu/getDefaultSlashMenuItems.js";
@@ -18,15 +18,17 @@ import { createBlockSpec } from "../../../schema/index.js";
18
18
  const createHardBreakTestBlockSpec = <
19
19
  const T extends string,
20
20
  const S extends "shift+enter" | "enter" | "none",
21
+ const C extends "inline" | "plain",
21
22
  >(
22
23
  type: T,
23
24
  hardBreakShortcut: S,
25
+ content: C = "inline" as C,
24
26
  ) =>
25
27
  createBlockSpec(
26
28
  {
27
29
  type,
28
30
  propSchema: {},
29
- content: "inline",
31
+ content,
30
32
  },
31
33
  {
32
34
  meta: {
@@ -47,11 +49,22 @@ const schema = BlockNoteSchema.create({
47
49
  ...defaultBlockSpecs,
48
50
  hardBreakEnter: createHardBreakTestBlockSpec("hardBreakEnter", "enter"),
49
51
  hardBreakNone: createHardBreakTestBlockSpec("hardBreakNone", "none"),
52
+ // "plain" content (`text*`) can't hold a `hardBreak` node, so these blocks
53
+ // insert a literal newline character instead - e.g. code/math/diagram source.
54
+ hardBreakEnterPlain: createHardBreakTestBlockSpec(
55
+ "hardBreakEnterPlain",
56
+ "enter",
57
+ "plain",
58
+ ),
50
59
  },
51
60
  });
52
61
 
53
62
  function createEditor(
54
- blockType: "paragraph" | "hardBreakEnter" | "hardBreakNone",
63
+ blockType:
64
+ | "paragraph"
65
+ | "hardBreakEnter"
66
+ | "hardBreakNone"
67
+ | "hardBreakEnterPlain",
55
68
  ) {
56
69
  const editor = BlockNoteEditor.create({
57
70
  schema,
@@ -87,6 +100,16 @@ function countHardBreaks(editor: BlockNoteEditor<any, any, any>) {
87
100
  return count;
88
101
  }
89
102
 
103
+ function getTextContent(editor: BlockNoteEditor<any, any, any>) {
104
+ let text = "";
105
+ editor._tiptapEditor.state.doc.descendants((node) => {
106
+ if (node.isText) {
107
+ text += node.text;
108
+ }
109
+ });
110
+ return text;
111
+ }
112
+
90
113
  describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
91
114
  it("inserts a hard break on Shift-Enter by default", () => {
92
115
  const editor = createEditor("paragraph");
@@ -152,4 +175,30 @@ describe("KeyboardShortcutsExtension hardBreakShortcut", () => {
152
175
 
153
176
  editor._tiptapEditor.destroy();
154
177
  });
178
+
179
+ it('inserts a newline character on Enter when content is "plain"', () => {
180
+ const editor = createEditor("hardBreakEnterPlain");
181
+
182
+ pressKeys(editor, "Enter");
183
+
184
+ // A "plain" block can't hold a `hardBreak` node, so no node is inserted and
185
+ // the block is not split - a literal newline is added to its text instead.
186
+ expect(countHardBreaks(editor)).toBe(0);
187
+ expect(editor.document.length).toBe(1);
188
+ expect(getTextContent(editor)).toBe("Hello world\n");
189
+
190
+ editor._tiptapEditor.destroy();
191
+ });
192
+
193
+ it('inserts a newline character on Shift-Enter when content is "plain"', () => {
194
+ const editor = createEditor("hardBreakEnterPlain");
195
+
196
+ pressKeys(editor, "Shift-Enter");
197
+
198
+ expect(countHardBreaks(editor)).toBe(0);
199
+ expect(editor.document.length).toBe(1);
200
+ expect(getTextContent(editor)).toBe("Hello world\n");
201
+
202
+ editor._tiptapEditor.destroy();
203
+ });
155
204
  });
@@ -804,9 +804,20 @@ export const KeyboardShortcutsExtension = Extension.create<{
804
804
  commands.command(({ state }) => {
805
805
  const blockInfo = getBlockInfoFromSelection(state);
806
806
 
807
+ const blockSpec =
808
+ this.options.editor.schema.blockSpecs[blockInfo.blockNoteType];
809
+
810
+ // NOTE: This likely doesn't work as intended - `blockSchema[type]`
811
+ // holds the block *config* (type/propSchema/content), which carries
812
+ // no `meta`, so `meta?.hardBreakShortcut` is always `undefined` and
813
+ // this falls back to the default. It should read from the block
814
+ // spec's implementation instead (i.e.
815
+ // `editor.schema.blockSpecs[type].implementation.meta`), the way the
816
+ // syntax-highlighting extension reads `meta.highlight`. Left as-is
817
+ // for a follow-up pass.
807
818
  const blockHardBreakShortcut =
808
- this.options.editor.schema.blockSpecs[blockInfo.blockNoteType]
809
- ?.implementation?.meta?.hardBreakShortcut ?? "shift+enter";
819
+ blockSpec?.implementation?.meta?.hardBreakShortcut ??
820
+ "shift+enter";
810
821
 
811
822
  if (blockHardBreakShortcut === "none") {
812
823
  return false;
@@ -820,6 +831,15 @@ export const KeyboardShortcutsExtension = Extension.create<{
820
831
  // both enter and shift+enter.
821
832
  blockHardBreakShortcut === "enter"
822
833
  ) {
834
+ // "plain" blocks (e.g. code/math/diagram source) hold text only
835
+ // (their content is `text*`), which can't contain a `hardBreak`
836
+ // node - inserting one would split the block into a new one.
837
+ // They represent line breaks as literal newline characters.
838
+ if (blockSpec?.config?.content === "plain") {
839
+ tr.insertText("\n", tr.selection.head);
840
+ return true;
841
+ }
842
+
823
843
  const marks =
824
844
  tr.storedMarks ||
825
845
  tr.selection.$head
@@ -185,6 +185,10 @@ export const ar: Dictionary = {
185
185
  toggle_blocks: {
186
186
  add_block_button: "تبديل فارغ. انقر لإضافة كتلة.",
187
187
  },
188
+ code_block: {
189
+ add_source_button_text: "إضافة كود المصدر",
190
+ ok_button_text: "موافق",
191
+ },
188
192
  // from react package:
189
193
  side_menu: {
190
194
  add_block_label: "إضافة محتوي",
@@ -395,6 +399,11 @@ export const ar: Dictionary = {
395
399
  formatting_change_by: (formats: string, users: string) =>
396
400
  `تغيير التنسيق (${formats}) بواسطة: ${users}`,
397
401
  },
402
+ exporter: {
403
+ open_file: "فتح الملف",
404
+ open_video_file: "فتح الفيديو",
405
+ open_audio_file: "فتح الصوت",
406
+ },
398
407
  generic: {
399
408
  ctrl_shortcut: "Ctrl",
400
409
  },
@@ -221,6 +221,10 @@ export const de: Dictionary = {
221
221
  add_block_button:
222
222
  "Leerer aufklappbarer Bereich. Klicken, um einen Block hinzuzufügen.",
223
223
  },
224
+ code_block: {
225
+ add_source_button_text: "Quellcode hinzufügen",
226
+ ok_button_text: "OK",
227
+ },
224
228
  side_menu: {
225
229
  add_block_label: "Block hinzufügen",
226
230
  drag_handle_label: "Blockmenü öffnen",
@@ -429,6 +433,11 @@ export const de: Dictionary = {
429
433
  formatting_change_by: (formats: string, users: string) =>
430
434
  `Formatierungsänderung (${formats}) von: ${users}`,
431
435
  },
436
+ exporter: {
437
+ open_file: "Datei öffnen",
438
+ open_video_file: "Video öffnen",
439
+ open_audio_file: "Audio öffnen",
440
+ },
432
441
  generic: {
433
442
  ctrl_shortcut: "Strg",
434
443
  },
@@ -200,6 +200,10 @@ export const en = {
200
200
  toggle_blocks: {
201
201
  add_block_button: "Empty toggle. Click to add a block.",
202
202
  },
203
+ code_block: {
204
+ add_source_button_text: "Add source code",
205
+ ok_button_text: "OK",
206
+ },
203
207
  // from react package:
204
208
  side_menu: {
205
209
  add_block_label: "Add block",
@@ -410,6 +414,11 @@ export const en = {
410
414
  formatting_change_by: (formats: string, users: string) =>
411
415
  `Formatting change (${formats}) by: ${users}`,
412
416
  },
417
+ exporter: {
418
+ open_file: "Open file",
419
+ open_video_file: "Open video",
420
+ open_audio_file: "Open audio",
421
+ },
413
422
  generic: {
414
423
  ctrl_shortcut: "Ctrl",
415
424
  },
@@ -200,6 +200,10 @@ export const es: Dictionary = {
200
200
  toggle_blocks: {
201
201
  add_block_button: "Toggle vacío. Haz clic para añadir un bloque.",
202
202
  },
203
+ code_block: {
204
+ add_source_button_text: "Agregar código fuente",
205
+ ok_button_text: "Aceptar",
206
+ },
203
207
  side_menu: {
204
208
  add_block_label: "Agregar bloque",
205
209
  drag_handle_label: "Abrir menú de bloque",
@@ -408,6 +412,11 @@ export const es: Dictionary = {
408
412
  formatting_change_by: (formats: string, users: string) =>
409
413
  `Cambio de formato (${formats}) por: ${users}`,
410
414
  },
415
+ exporter: {
416
+ open_file: "Abrir archivo",
417
+ open_video_file: "Abrir vídeo",
418
+ open_audio_file: "Abrir audio",
419
+ },
411
420
  generic: {
412
421
  ctrl_shortcut: "Ctrl",
413
422
  },