@blocknote/core 0.53.0 → 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 (197) 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-BEJW9OM_.js → blocks-CzQLehlc.js} +571 -568
  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-BR5hkAio.js → extensions-DGuhtOgB.js} +662 -427
  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-BUXut68d.js → src-BkfZVfaO.js} +967 -899
  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 +59 -56
  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 +17 -18
  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/exporters/markdown/htmlToMarkdown.ts +45 -10
  54. package/src/api/nodeConversions/blockToNode.ts +5 -1
  55. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  56. package/src/api/pmUtil.ts +16 -8
  57. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  58. package/src/blocks/Code/block.test.ts +1 -1
  59. package/src/blocks/Code/block.ts +29 -281
  60. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  61. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  62. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  63. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  64. package/src/blocks/index.ts +3 -0
  65. package/src/comments/extension.ts +2 -4
  66. package/src/editor/Block.css +273 -2
  67. package/src/editor/BlockNoteEditor.ts +25 -4
  68. package/src/editor/BlockNoteExtension.ts +1 -1
  69. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  70. package/src/exporter/ExportImage.ts +50 -0
  71. package/src/exporter/Exporter.test.ts +48 -0
  72. package/src/exporter/Exporter.ts +46 -12
  73. package/src/exporter/index.ts +1 -0
  74. package/src/exporter/mapping.ts +4 -1
  75. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  76. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  77. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  78. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  79. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  80. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  81. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  82. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  83. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  84. package/src/extensions/index.ts +4 -0
  85. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  86. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  87. package/src/i18n/locales/ar.ts +9 -0
  88. package/src/i18n/locales/de.ts +9 -0
  89. package/src/i18n/locales/en.ts +9 -0
  90. package/src/i18n/locales/es.ts +9 -0
  91. package/src/i18n/locales/fa.ts +9 -0
  92. package/src/i18n/locales/fr.ts +9 -0
  93. package/src/i18n/locales/he.ts +9 -0
  94. package/src/i18n/locales/hr.ts +9 -0
  95. package/src/i18n/locales/is.ts +9 -0
  96. package/src/i18n/locales/it.ts +9 -0
  97. package/src/i18n/locales/ja.ts +9 -0
  98. package/src/i18n/locales/ko.ts +9 -0
  99. package/src/i18n/locales/nl.ts +9 -0
  100. package/src/i18n/locales/no.ts +9 -0
  101. package/src/i18n/locales/pl.ts +9 -0
  102. package/src/i18n/locales/pt.ts +9 -0
  103. package/src/i18n/locales/ru.ts +9 -0
  104. package/src/i18n/locales/sk.ts +9 -0
  105. package/src/i18n/locales/uk.ts +9 -0
  106. package/src/i18n/locales/uz.ts +9 -0
  107. package/src/i18n/locales/vi.ts +9 -0
  108. package/src/i18n/locales/zh-tw.ts +9 -0
  109. package/src/i18n/locales/zh.ts +9 -0
  110. package/src/index.ts +5 -1
  111. package/src/schema/blocks/createSpec.ts +2 -3
  112. package/src/schema/blocks/types.ts +53 -3
  113. package/src/schema/contentTypePropagation.test.ts +1 -1
  114. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  115. package/src/schema/inlineContent/createSpec.ts +171 -12
  116. package/src/schema/inlineContent/internal.ts +21 -2
  117. package/src/schema/inlineContent/types.ts +35 -5
  118. package/src/user/UserStore.ts +1 -1
  119. package/src/util/Store.test.ts +132 -0
  120. package/src/util/Store.ts +135 -0
  121. package/src/y/extensions/AttributionExtension.test.ts +10 -0
  122. package/src/y/extensions/Versioning.test.ts +20 -7
  123. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  124. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  125. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  126. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  127. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  128. package/src/yjs/extensions/Versioning.test.ts +17 -5
  129. package/types/src/api/pmUtil.d.ts +7 -7
  130. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  131. package/types/src/blocks/Code/block.d.ts +3 -53
  132. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  133. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  134. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  135. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  136. package/types/src/blocks/index.d.ts +3 -0
  137. package/types/src/comments/extension.d.ts +1 -15
  138. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  139. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  140. package/types/src/exporter/ExportImage.d.ts +36 -0
  141. package/types/src/exporter/Exporter.d.ts +21 -0
  142. package/types/src/exporter/Exporter.test.d.ts +1 -0
  143. package/types/src/exporter/index.d.ts +1 -0
  144. package/types/src/exporter/mapping.d.ts +1 -1
  145. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  146. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  147. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  148. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  149. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  150. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  151. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  152. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  153. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  154. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  155. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  156. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  157. package/types/src/extensions/TableHandles/TableHandles.d.ts +1 -1
  158. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  159. package/types/src/extensions/index.d.ts +4 -0
  160. package/types/src/i18n/locales/en.d.ts +9 -0
  161. package/types/src/i18n/locales/fa.d.ts +9 -0
  162. package/types/src/i18n/locales/sk.d.ts +9 -0
  163. package/types/src/index.d.ts +2 -1
  164. package/types/src/schema/blocks/types.d.ts +36 -3
  165. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  166. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  167. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  168. package/types/src/schema/inlineContent/types.d.ts +22 -3
  169. package/types/src/user/UserStore.d.ts +1 -1
  170. package/types/src/util/Store.d.ts +45 -0
  171. package/types/src/util/Store.test.d.ts +1 -0
  172. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  173. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  174. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  175. package/types/src/y/extensions/index.d.ts +2 -10
  176. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  177. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  178. package/types/src/yjs/extensions/index.d.ts +1 -5
  179. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  180. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  181. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  182. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  183. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  184. package/dist/blocks-BEJW9OM_.js.map +0 -1
  185. package/dist/blocks-BPmJglFo.cjs +0 -12
  186. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  187. package/dist/en-C5Z4Ag9_.cjs +0 -2
  188. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  189. package/dist/en-CGKXfWma.js.map +0 -1
  190. package/dist/extensions-AitWbYdJ.cjs +0 -52
  191. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  192. package/dist/extensions-BR5hkAio.js.map +0 -1
  193. package/dist/src-BHPuOo7W.cjs +0 -21
  194. package/dist/src-BHPuOo7W.cjs.map +0 -1
  195. package/dist/src-BUXut68d.js.map +0 -1
  196. package/src/blocks/Code/shiki.ts +0 -73
  197. package/types/src/blocks/Code/shiki.d.ts +0 -4
package/dist/y.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"y.js","names":[],"sources":["../src/y/extensions/YAttributionMarks.ts","../src/y/extensions/AttributionExtension.ts","../src/y/extensions/RelativePositionMapping.ts","../src/y/extensions/blockMatchNodes.ts","../src/y/extensions/YSync.ts","../src/y/utils.ts","../src/y/extensions/Suggestions.ts","../src/y/extensions/Versioning.ts","../src/y/extensions/YCursorPlugin.ts","../src/y/extensions/DiffVersioningExtension.ts","../src/y/extensions/index.ts","../src/y/comments/yjsHelpers.ts","../src/y/comments/YjsThreadStoreBase.ts","../src/y/comments/RESTYjsThreadStore.ts","../src/y/comments/YjsThreadStore.ts","../src/y/versioning/yhub.ts"],"sourcesContent":["import { Mark } from \"@tiptap/core\";\nimport { Mark as PMMark, MarkSpec } from \"prosemirror-model\";\nimport {\n createExtension,\n type ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport { BLOCK_LEVEL_SUGGESTION_GROUP } from \"../../pm-nodes/suggestionMarks.js\";\nimport { NON_FORMATTING_MARK_GROUP } from \"../../schema/markGroups.js\";\nimport {\n fallbackColorForUserId,\n userColorPalette,\n userColorVarNames,\n} from \"../../user/index.js\";\n\n/**\n * Describes a suggestion mark to {@link GetAttributionMarkClassName}: whether it\n * wraps inline content or a whole block, and which kind of change it represents.\n * `modificationType: \"format\"` corresponds to the `y-attributed-format`\n * (modification) mark.\n */\nexport type AttributionMarkStyleInfo = {\n contentType: \"inline-content\" | \"block\";\n modificationType: \"insert\" | \"delete\" | \"format\";\n};\n\n/**\n * The class name(s) an app returns to style a suggestion mark. Either:\n * - a single string applied to *both* the mark content and its hover tooltip, or\n * - `{ content, tooltip }` to style the mark content and the tooltip\n * independently.\n */\nexport type AttributionMarkClassNames =\n | string\n | { content: string; tooltip: string };\n\n/**\n * Optional callback to override suggestion-mark styling. Given a mark's\n * {@link AttributionMarkStyleInfo}, it returns the class name(s) to apply (see\n * {@link AttributionMarkClassNames}). When a class is returned, the default\n * per-user color (the `--user-color-*` custom properties and the built-in\n * `.bn-suggestion-mark` / `.bn-suggestion-node` styling) is *not* applied to the\n * mark content, so the class fully controls its appearance — letting an app\n * color suggestions by change type (e.g. green insertions, red deletions)\n * instead of by author.\n */\nexport type GetAttributionMarkClassName = (\n info: AttributionMarkStyleInfo,\n) => AttributionMarkClassNames;\n\n/**\n * Resolve the {@link AttributionMarkClassNames} returned by a\n * {@link GetAttributionMarkClassName} callback to the class name for a single\n * target (the mark `content` or its `tooltip`).\n */\nexport const resolveAttributionMarkClassName = (\n result: AttributionMarkClassNames | undefined,\n target: \"content\" | \"tooltip\",\n): string | undefined =>\n result === undefined\n ? undefined\n : typeof result === \"string\"\n ? result\n : result[target];\n\n/**\n * Shared mark view for the attribution marks (insert / delete / modification).\n * It renders the marked content and tags the wrapper with the author(s) via\n * `data-*` attributes. The attribution tooltip shown on hover is handled\n * separately by the `AttributionExtension`, which reads those attributes\n * straight from the DOM — keeping this mark view purely presentational and the\n * tooltip state off of module scope.\n *\n * Author colors are *not* baked into the (deterministic) mark attrs. Instead the\n * wrapper sets the generic `--user-color-*` custom properties the Block.css rules\n * read, sourcing them from the author's *per-user* CSS variable via\n * `var(--user-color-<id>-*, <fallback>)`. Those per-user variables are populated\n * on the editor root by `AttributionExtension` once the user resolves, so a mark\n * shows the deterministic palette fallback immediately and recolors to the\n * author's own color purely through the CSS cascade — no decoration, no doc\n * transaction. See `userColors.ts`.\n */\nconst createAttributionMarkView =\n (\n type: \"insert\" | \"delete\" | \"modification\",\n options?: {\n editor?: BlockNoteEditor<any, any, any>;\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n },\n ) =>\n ({ mark, inline }: { mark: PMMark; inline: boolean }) => {\n const editor = options?.editor;\n // `<ins>`/`<del>` are semantic elements. The modification mark has no\n // dedicated element, so it renders as a `<span>` inline or a `<div>` over a\n // block, matching its `parseDOM` rules.\n const tag =\n type === \"insert\"\n ? \"ins\"\n : type === \"delete\"\n ? \"del\"\n : inline\n ? \"span\"\n : \"div\";\n const dom = document.createElement(tag);\n\n Object.assign(dom.dataset, {\n userIds: JSON.stringify(mark.attrs[\"userIds\"]),\n inline: String(inline),\n });\n if (type === \"modification\") {\n dom.dataset[\"type\"] = \"modification\";\n dom.dataset[\"format\"] = JSON.stringify(mark.attrs[\"format\"]);\n }\n\n // Optional app-provided override: `y-attributed-format` is exposed as\n // `\"format\"`. When a class is returned it's applied to the content element\n // *instead of* the built-in `.bn-suggestion-mark` / `.bn-suggestion-node`\n // classes, and the per-user `--user-color-*` properties are omitted, so the\n // class fully controls the appearance (background, text color, etc.) with no\n // per-user color leaking through (see the type doc).\n const contentClassName = resolveAttributionMarkClassName(\n options?.getAttributionMarkClassName?.({\n contentType: inline ? \"inline-content\" : \"block\",\n modificationType: type === \"modification\" ? \"format\" : type,\n }),\n \"content\",\n );\n\n // The wrapper is always `display: contents` so it never generates a box of\n // its own — an inline `<ins>`/`<del>` around block/table content (e.g. a\n // suggestion spanning table cells) would otherwise break the normal layout.\n // Because a `display: contents` element paints nothing, the highlight is\n // applied to the inner content span (see `.bn-suggestion-mark` in Block.css)\n // using the `--user-color-*` custom properties, which cascade down from here.\n // They're sourced from the author's per-user variable (populated on the\n // editor root by `AttributionExtension`) with the deterministic palette as a\n // fallback, so a mark is colored before the user resolves and recolors via\n // the cascade afterward. When an override class owns the styling, no per-user\n // color is applied at all.\n const userIds = (mark.attrs[\"userIds\"] as string[] | null) ?? [];\n const firstId = userIds[0];\n const fallback = firstId\n ? fallbackColorForUserId(firstId)\n : userColorPalette[0];\n if (contentClassName) {\n dom.style.cssText = \"display: contents\";\n } else {\n const light = firstId\n ? `var(${userColorVarNames(firstId).light}, ${fallback.light})`\n : fallback.light;\n const dark = firstId\n ? `var(${userColorVarNames(firstId).dark}, ${fallback.dark})`\n : fallback.dark;\n dom.style.cssText =\n \"display: contents\" +\n `; --user-color-light: ${light}; --user-color-dark: ${dark}`;\n }\n\n const contentDOM = document.createElement(\"span\");\n if (inline) {\n // Inline content: the span is a real inline box that carries the highlight\n // (default path) or the app-provided override class.\n contentDOM.className =\n contentClassName ??\n (type === \"delete\"\n ? \"bn-suggestion-mark bn-suggestion-mark--delete\"\n : \"bn-suggestion-mark\");\n } else {\n // Block-level marks wrap block/table structure (e.g. <tr>/<td>/<p>). The\n // span must be `display: contents` so it doesn't inject an inline box into\n // the table layout (which triggers the browser's anonymous-table fixup and\n // breaks the table). Such a span has no box to paint a background on, so\n // the `.bn-suggestion-node` rule highlights its children (the wrapped\n // nodes) instead. An override class is applied to the same span, so it\n // should likewise target its children (e.g. `.my-class > *`).\n contentDOM.style.display = \"contents\";\n contentDOM.className =\n contentClassName ??\n (type === \"delete\"\n ? \"bn-suggestion-node bn-suggestion-node--delete\"\n : \"bn-suggestion-node\");\n if (type === \"delete\") {\n // A deleted block shows a localized \"Deleted\" badge via a `::before`\n // (see Block.css). The badge text is passed down as a CSS string token\n // in `--deleted-label` so the stylesheet stays locale-agnostic; the\n // wrapper is `display: contents` and can't paint a pseudo-element of its\n // own, so the rule renders the badge on the wrapped node instead, which\n // inherits this custom property.\n const label = editor?.dictionary.suggestion_changes.deleted;\n if (label) {\n contentDOM.style.setProperty(\n \"--deleted-label\",\n JSON.stringify(label),\n );\n }\n }\n }\n dom.appendChild(contentDOM);\n\n return {\n dom,\n contentDOM,\n };\n };\n\nexport const YAttributedInsertion = Mark.create<{\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-insert\",\n inclusive: false,\n excludes: \"\",\n // Two groups: `BLOCK_LEVEL_SUGGESTION_GROUP` lets the mark sit on block nodes\n // (see `suggestionMarks`), so a whole block can be marked as inserted in\n // suggestion mode; `NON_FORMATTING_MARK_GROUP` lets it annotate text inside\n // `\"plain\"` blocks (e.g. code blocks) — see `nonFormattingMarks`.\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"insert\", {\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\nexport const YAttributedDeletion = Mark.create<{\n editor?: BlockNoteEditor<any, any, any>;\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-delete\",\n inclusive: false,\n excludes: \"\",\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"delete\", {\n editor: this.options.editor,\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\nexport const YAttributedFormat = Mark.create<{\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-format\",\n inclusive: false,\n excludes: \"\",\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n format: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"modification\", {\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\n/**\n * Bundles the three `y-attributed-*` suggestion marks into a single BlockNote\n * extension, so they can be registered wherever they're actually needed (the\n * Yjs collaboration extension, or a test that exercises suggestions) instead of\n * living in the default schema. The marks opt into being allowed on block nodes\n * via their `blockLevelSuggestion` option — see `suggestionMarks`.\n */\nexport const YAttributionMarksExtension = createExtension(\n ({\n options,\n }: ExtensionOptions<\n { getAttributionMarkClassName?: GetAttributionMarkClassName } | undefined\n >) => ({\n key: \"yAttributionMarks\",\n tiptapExtensions: [\n YAttributedInsertion.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n YAttributedDeletion.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n YAttributedFormat.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n }),\n);\n","import { getChangedRanges } from \"@tiptap/core\";\nimport { Plugin, PluginKey, type Transaction } from \"prosemirror-state\";\nimport {\n createExtension,\n createStore,\n type ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n colorsForUserIds,\n userColorVarNames,\n normalizeToUserStore,\n type UserStoreOrResolver,\n} from \"../../user/index.js\";\nimport {\n resolveAttributionMarkClassName,\n YAttributionMarksExtension,\n type GetAttributionMarkClassName,\n} from \"./YAttributionMarks.js\";\n\n/** The attribution marks, mapped to their `modificationType`. */\nconst ATTRIBUTION_MARK_TYPES = {\n \"y-attributed-insert\": \"insert\",\n \"y-attributed-delete\": \"delete\",\n \"y-attributed-format\": \"format\",\n} as const;\n\nconst ATTRIBUTION_LOAD_PLUGIN_KEY = new PluginKey(\"attributionLoadUsers\");\n\n/** Wrapper of an attribution mark; carries its author(s) in `data-user-ids`. */\nconst ATTRIBUTION_MARK_SELECTOR = \"[data-user-ids]\";\n\n/** Parse the JSON-encoded `data-user-ids` attribute; `[]` if missing/malformed. */\nconst parseUserIds = (userIdsJSON: string | undefined): string[] => {\n if (!userIdsJSON) {\n return [];\n }\n let userIds: unknown;\n try {\n userIds = JSON.parse(userIdsJSON);\n } catch {\n return [];\n }\n return Array.isArray(userIds) ? userIds.map(String) : [];\n};\n\n/**\n * The changed format keys from a modification mark's `data-format` (e.g.\n * `[\"bold\", \"italic\"]`); `[]` if missing/malformed or an empty change. This is\n * the raw change context — turning it into a localized label (e.g.\n * `\"Bold, Italic\"`) is a view concern owned by the React layer's\n * `formatChangeLabel`, so core stays i18n-agnostic here.\n */\nconst parseFormatKeys = (formatJSON: string | undefined): string[] => {\n if (!formatJSON) {\n return [];\n }\n let format: unknown;\n try {\n format = JSON.parse(formatJSON);\n } catch {\n return [];\n }\n if (typeof format !== \"object\" || format === null) {\n return [];\n }\n return Object.keys(format);\n};\n\n/**\n * The element with a real box to anchor the tooltip to. The wrapper is\n * `display: contents` (no box of its own), so use its content span child,\n * falling back further for block marks.\n */\nconst getReferenceElement = (wrapper: Element): Element => {\n const content = wrapper.firstElementChild ?? wrapper;\n const rect = content.getBoundingClientRect();\n if (rect.width || rect.height) {\n return content;\n }\n return content.firstElementChild ?? content;\n};\n\n/**\n * The box the tooltip anchors to. The wrapper is `display: contents` (no box of\n * its own), so use its content span child, falling back further for block marks.\n * Exported for the React controller's floating-ui `getBoundingClientRect`.\n */\nexport const getReferenceRect = (wrapper: Element): DOMRect =>\n getReferenceElement(wrapper).getBoundingClientRect();\n\n/**\n * The per-line client rects of the reference element, for floating-ui's\n * `inline()` middleware — it needs one rect per line to position off a\n * multi-line mark, and virtual elements don't get a default `getClientRects`.\n */\nexport const getReferenceClientRects = (wrapper: Element): DOMRectList =>\n getReferenceElement(wrapper).getClientRects();\n\n/**\n * State for the currently-hovered suggestion mark's tooltip (`undefined` when\n * none). The extension computes it; a React controller renders + positions it\n * (see `AttributionTooltipController`).\n */\nexport type AttributionTooltipState = {\n /** The wrapper element the tooltip anchors to (floating-ui reference). */\n anchor: HTMLElement;\n /** Per-user background color, resolved from the user store (default path). */\n color: string;\n /** The kind of change — `format` is the modification mark. */\n modificationType: \"insert\" | \"delete\" | \"format\";\n /** Whether the mark wraps inline content or a whole block. */\n contentType: \"inline-content\" | \"block\";\n /** Resolved usernames (falls back to raw ids), for custom renderers. */\n users: string[];\n /**\n * The changed format keys (e.g. `[\"bold\", \"italic\"]`), present only for\n * `format` marks. This is the raw change context — the view layer turns it\n * into a localized label via its `formatChangeLabel`.\n */\n format?: string[];\n /**\n * Class name from the `getAttributionMarkClassName` callback (override path).\n * When present, the tooltip applies this and skips the inline `color`.\n */\n className?: string;\n};\n\n/**\n * Resolves the attribution tooltip state for suggestion marks on hover (exposed\n * via a store for React), and loads each mark's author so its color/username\n * resolves. Marks nest, so a single delegated `mouseover` listener picks the\n * `closest` wrapper to the pointer — the innermost mark wins.\n */\nexport const AttributionExtension = createExtension(\n ({\n options,\n }: ExtensionOptions<\n | {\n /** Resolves authors to usernames. Optional; unresolved ids show raw. */\n resolveUsers?: UserStoreOrResolver;\n /** See {@link GetAttributionMarkClassName}. */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n }\n | undefined\n >) => {\n const userStore = normalizeToUserStore(options?.resolveUsers);\n const getAttributionMarkClassName = options?.getAttributionMarkClassName;\n\n const store = createStore<AttributionTooltipState | undefined>(undefined);\n\n // Load the authors of the attribution marks in `tr`'s changed ranges, so\n // their colors/usernames resolve (colors then flow to marks via `syncRootVars`).\n // `getChangedRanges` covers mark-only steps too — which suggestion mode adds\n // over existing text and `tr.changedRange()` would miss.\n const loadChangedUsers = (tr: Transaction) => {\n const ranges = getChangedRanges(tr);\n if (ranges.length === 0) {\n return;\n }\n // Most changes are local (often several steps in one small span), so scan a\n // single range spanning all of them rather than each range individually.\n let from = Infinity;\n let to = -Infinity;\n for (const { newRange } of ranges) {\n from = Math.min(from, newRange.from);\n to = Math.max(to, newRange.to);\n }\n\n const ids = new Set<string>();\n tr.doc.nodesBetween(from, to, (node) => {\n for (const mark of node.marks) {\n if (\n ATTRIBUTION_MARK_TYPES[\n mark.type.name as keyof typeof ATTRIBUTION_MARK_TYPES\n ]\n ) {\n const userIds = mark.attrs[\"userIds\"] as string[] | null;\n userIds?.forEach((id) => ids.add(id));\n }\n }\n return true;\n });\n if (ids.size > 0) {\n void userStore.loadUsers(Array.from(ids));\n }\n };\n\n return {\n key: \"attribution\",\n userStore,\n store,\n prosemirrorPlugins: [\n // Marks arrive in a transaction (suggestion mode, viewing suggestions,\n // version preview), so resolve their authors as the doc changes.\n new Plugin({\n key: ATTRIBUTION_LOAD_PLUGIN_KEY,\n state: {\n init: () => null,\n apply: (tr) => {\n if (tr.docChanged) {\n loadChangedUsers(tr);\n }\n return null;\n },\n },\n }),\n ],\n mount({ dom, root, signal }) {\n // Write each resolved author's color to the editor root as per-user CSS\n // variables (`--user-color-<id>-{light,dark}`) that the mark wrappers read\n // via `var(..., <fallback>)`, so the cascade recolors marks once a color\n // resolves. Color-less users have theirs removed so the fallback applies.\n const syncRootVars = () => {\n for (const [id, user] of userStore.store.state) {\n const { light, dark } = userColorVarNames(id);\n if (user.color && user.colorLight) {\n dom.style.setProperty(light, user.colorLight);\n dom.style.setProperty(dark, user.color);\n } else {\n dom.style.removeProperty(light);\n dom.style.removeProperty(dark);\n }\n }\n };\n\n // The wrapper currently showing a tooltip, so we don't re-emit on every\n // `mouseover` over the same mark.\n let activeAnchor: HTMLElement | undefined;\n\n // The mark's authors as usernames, falling back to the raw id when not\n // cached (`getUser` is cache-only; ids load on hover, see `onPointerOver`).\n const usersLabelArray = (userIdsJSON: string | undefined): string[] =>\n parseUserIds(userIdsJSON).map(\n (id) => userStore.getUser(id)?.username ?? id,\n );\n\n // A stable identity string for a wrapper (empty if unattributed), used to\n // (a) test whether a mark is attributed and (b) group adjacent marks with\n // the *same* attribution under one tooltip. It's an internal grouping key,\n // not the displayed text — that's composed in the view from `users` and\n // the format label — so it's built from raw `data-*` (ids + format keys)\n // and stays free of i18n/username resolution.\n const attributionIdentity = (wrapper: HTMLElement) => {\n const ids = parseUserIds(wrapper.dataset[\"userIds\"]);\n if (ids.length === 0) {\n return \"\";\n }\n const format = parseFormatKeys(wrapper.dataset[\"format\"]);\n return `${format.join(\",\")}:${ids.join(\",\")}`;\n };\n\n // Build the tooltip state from a wrapper's `data-*` attributes.\n const buildState = (anchor: HTMLElement): AttributionTooltipState => {\n const isModification = anchor.dataset[\"format\"] !== undefined;\n const modificationType: AttributionTooltipState[\"modificationType\"] =\n isModification\n ? \"format\"\n : anchor.tagName === \"INS\"\n ? \"insert\"\n : \"delete\";\n const contentType: AttributionTooltipState[\"contentType\"] =\n anchor.dataset[\"inline\"] === \"false\" ? \"block\" : \"inline-content\";\n\n return {\n anchor,\n // The tooltip is portaled outside the editor root, so it can't read\n // the cascaded per-user vars — resolve a concrete color from the store.\n color: colorsForUserIds(\n userStore,\n parseUserIds(anchor.dataset[\"userIds\"]),\n ).dark,\n modificationType,\n contentType,\n users: usersLabelArray(anchor.dataset[\"userIds\"]),\n format: isModification\n ? parseFormatKeys(anchor.dataset[\"format\"])\n : undefined,\n className: resolveAttributionMarkClassName(\n getAttributionMarkClassName?.({ contentType, modificationType }),\n \"tooltip\",\n ),\n };\n };\n\n const hideTooltip = () => {\n if (!activeAnchor) {\n return;\n }\n activeAnchor = undefined;\n store.setState(undefined);\n };\n\n // The innermost attributed mark at or above `el`, skipping unattributed\n // wrappers so an attributed ancestor still wins.\n const innermostAttributed = (\n el: Element | null,\n ): HTMLElement | undefined => {\n while (el) {\n const wrapper = el.closest<HTMLElement>(ATTRIBUTION_MARK_SELECTOR);\n if (!wrapper) {\n return undefined;\n }\n if (attributionIdentity(wrapper)) {\n return wrapper;\n }\n el = wrapper.parentElement;\n }\n return undefined;\n };\n\n const onPointerOver = (event: Event) => {\n const target = event.target instanceof Element ? event.target : null;\n const innermost = innermostAttributed(target);\n if (!innermost) {\n // Not over an attributed mark — drop the current tooltip.\n hideTooltip();\n return;\n }\n\n const identity = attributionIdentity(innermost);\n // Anchor on the outermost ancestor with the *same* attribution so one\n // tooltip covers the whole region; a differently-attributed ancestor\n // breaks the chain, and unattributed ones are climbed past.\n let anchor = innermost;\n let el: Element | null = innermost.parentElement;\n while (el) {\n const ancestor = el.closest<HTMLElement>(ATTRIBUTION_MARK_SELECTOR);\n if (!ancestor) {\n break;\n }\n const ancestorIdentity = attributionIdentity(ancestor);\n if (ancestorIdentity === identity) {\n anchor = ancestor;\n } else if (ancestorIdentity) {\n break;\n }\n el = ancestor.parentElement;\n }\n\n if (activeAnchor === anchor) {\n return;\n }\n\n activeAnchor = anchor;\n store.setState(buildState(anchor));\n\n // First hover renders raw ids (cache-only); load the authors and refresh\n // the resolved usernames once loaded, if this mark is still active.\n const ids = parseUserIds(anchor.dataset[\"userIds\"]);\n if (ids.length > 0) {\n void userStore.loadUsers(ids).then(() => {\n if (activeAnchor !== anchor) {\n return;\n }\n store.setState(buildState(anchor));\n });\n }\n };\n\n root.addEventListener(\"mouseover\", onPointerOver, { signal });\n signal.addEventListener(\"abort\", hideTooltip);\n\n // Seed from the cache, then keep the vars in sync as users resolve.\n syncRootVars();\n const unsubscribe = userStore.store.subscribe(syncRootVars);\n signal.addEventListener(\"abort\", unsubscribe);\n },\n\n // The `y-attributed-*` marks aren't in the default schema — register them\n // here so the block specs can allow them (collaboration-only).\n blockNoteExtensions: [\n YAttributionMarksExtension({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n };\n },\n);\n","import { relativePositionStore, ySyncPluginKey } from \"@y/prosemirror\";\nimport { createExtension } from \"../../editor/BlockNoteExtension.js\";\n\nexport const RelativePositionMappingExtension = createExtension(\n ({ editor }) => {\n return {\n key: \"yPositionMapping\",\n mapPosition: (position: number, side: \"left\" | \"right\" = \"left\") => {\n const ySyncPluginState = ySyncPluginKey.getState(\n editor.prosemirrorState,\n );\n if (!ySyncPluginState?.ytype) {\n throw new Error(\"YSync plugin state not found\");\n }\n\n // 0 is a special case & always should map to itself\n if (position === 0) {\n return () => 0;\n }\n\n const posStore = relativePositionStore(\n editor.prosemirrorState.doc.resolve(\n position + (side === \"right\" ? 1 : -1),\n ),\n ySyncPluginState.ytype,\n ySyncPluginState.renderer,\n );\n\n return () => {\n const curYSyncPluginState = ySyncPluginKey.getState(\n editor.prosemirrorState,\n ) as typeof ySyncPluginState;\n const pos = posStore(\n editor.prosemirrorState.doc,\n curYSyncPluginState.ytype,\n curYSyncPluginState.renderer,\n );\n\n // This can happen if the element is garbage collected\n if (pos === null) {\n throw new Error(\"Position not found, cannot track positions\");\n }\n\n return pos + (side === \"right\" ? -1 : 1);\n };\n },\n } as const;\n },\n);\n","import { $prosemirrorDelta } from \"@y/prosemirror\";\nimport * as delta from \"lib0/delta\";\nimport * as schema from \"lib0/schema\";\n\n/**\n * Canonical name of a content delta's first block child (the child carried by an\n * insert op), or `null`. For a BlockNote `blockContainer` (content\n * `blockContent blockGroup?`) this is its block-content type (paragraph,\n * heading, image, ...).\n */\nconst firstChild = (\n d: schema.Unwrap<typeof $prosemirrorDelta>,\n): schema.Unwrap<typeof $prosemirrorDelta> | null => {\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const it of op.insert) {\n if (delta.$deltaAny.check(it)) {\n return it;\n }\n }\n }\n }\n return null;\n};\n\n/**\n * Whether a `blockContainer` delta carries a child `blockGroup` — i.e. the block\n * has nested children. A container's content is `blockContent blockGroup?`, so\n * this is what tells a leaf block apart from a parent.\n */\nconst hasBlockGroup = (d: schema.Unwrap<typeof $prosemirrorDelta>): boolean => {\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const it of op.insert) {\n if (delta.$deltaAny.check(it) && it.name === \"blockGroup\") {\n return true;\n }\n }\n }\n }\n return false;\n};\n\nfunction getTableDimensions(\n d: schema.Unwrap<typeof $prosemirrorDelta>,\n): { rows: number; cols: number } | null {\n if (d.name !== \"table\") {\n return null;\n }\n\n // Collect all rows with their cells' colspan/rowspan values.\n const rows: Array<Array<{ colspan: number; rowspan: number }>> = [];\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const tr of op.insert as Array<\n schema.Unwrap<typeof $prosemirrorDelta>\n >) {\n if (tr.name !== \"tableRow\") {\n return null;\n }\n const cells: Array<{ colspan: number; rowspan: number }> = [];\n for (const trOp of (tr as any).children) {\n if (delta.$insertOp.check(trOp)) {\n for (const td of trOp.insert as Array<\n schema.Unwrap<typeof $prosemirrorDelta>\n >) {\n if (td.name !== \"tableCell\" && td.name !== \"tableHeader\") {\n return null;\n }\n cells.push({\n colspan: Number(td.attrs.colspan) || 1,\n rowspan: Number(td.attrs.rowspan) || 1,\n });\n }\n }\n }\n rows.push(cells);\n }\n }\n }\n\n if (rows.length === 0) {\n return null;\n }\n\n // Build an occupancy grid to determine the true column count.\n // Each entry in `grid[r]` tracks which columns are already occupied\n // (by a cell from a previous row with rowspan > 1).\n const grid: boolean[][] = [];\n for (let r = 0; r < rows.length; r++) {\n if (!grid[r]) {\n grid[r] = [];\n }\n let col = 0;\n for (const cell of rows[r]) {\n // Skip columns already occupied by a rowspan from above.\n while (grid[r][col]) {\n col++;\n }\n // Mark all slots this cell occupies.\n for (let dr = 0; dr < cell.rowspan; dr++) {\n if (!grid[r + dr]) {\n grid[r + dr] = [];\n }\n for (let dc = 0; dc < cell.colspan; dc++) {\n grid[r + dr][col + dc] = true;\n }\n }\n col += cell.colspan;\n }\n }\n\n const numCols = Math.max(...grid.map((row) => row.length));\n return { rows: rows.length, cols: numCols };\n}\n\n/**\n * BlockNote's node-pairing policy for y-prosemirror's `matchNodes` option\n * (forwarded to `lib0/delta.diff`). This is the schema-specific bit that lives\n * in userland - the binding itself stays schema-agnostic.\n *\n * A `blockContainer` holds exactly one block content (`blockContent\n * blockGroup?`). Diffing a *type change* of that content as an in-place child\n * delete+insert would, under a suggestion, tombstone the old content next to the\n * new one => two block-contents in one container => schema-invalid. So we\n * declare a container's identity to be its first block-content child's type:\n * when that changes, the two containers are reported as *different*, the PM->Y\n * diff replaces the whole container, and the deleted + inserted containers sit\n * as siblings in the blockGroup (`blockGroupChild+` allows that). Each carries\n * the `y-attributed-*` node mark - which `blockContainer` already whitelists -\n * so no schema change and no storage transform are needed. A plain text edit\n * keeps the same first-child type => same identity => the diff descends and\n * merges as usual.\n *\n * @param a removed (old) node\n * @param b inserted (new) node\n * @returns whether `a` and `b` are the same node (diff in place) vs different (replace)\n */\nexport const blockMatchNodes = (\n a: schema.Unwrap<typeof $prosemirrorDelta>,\n b: schema.Unwrap<typeof $prosemirrorDelta>,\n): boolean => {\n if (a.name !== b.name) {\n return false;\n }\n\n if (a.name !== \"blockContainer\") {\n return true;\n }\n\n const childA = firstChild(a);\n const childB = firstChild(b);\n\n if (childA?.name !== childB?.name) {\n return false;\n }\n\n // A change in nesting is structural too: if one container gains or loses a\n // child `blockGroup`, diffing it in place would insert/delete the blockGroup as\n // a sibling of the block content inside a single container — schema-invalid.\n // Treat it as different so the whole container is replaced instead, same as a\n // content-type change. Keeps concurrent nesting merges (e.g. two users nesting\n // a block under the same parent) from producing a lopsided in-place result.\n if (hasBlockGroup(a) !== hasBlockGroup(b)) {\n return false;\n }\n\n if (childA?.name === \"table\" && childB?.name === \"table\") {\n const dimA = getTableDimensions(childA);\n const dimB = getTableDimensions(childB);\n if (\n dimA !== null &&\n dimB !== null &&\n dimA.rows !== dimB.rows &&\n dimA.cols !== dimB.cols\n ) {\n return false;\n }\n }\n\n return true;\n};\n","import { configureYProsemirror, syncPlugin } from \"@y/prosemirror\";\nimport {\n type ExtensionOptions,\n createExtension,\n} from \"../../editor/BlockNoteExtension.js\";\nimport { blockMatchNodes } from \"./blockMatchNodes.js\";\nimport { CollaborationOptions } from \"./index.js\";\n\n/**\n * Maps a Y attribution to BlockNote's `y-attributed-*` mark attrs.\n *\n * The mapper must be deterministic in `(format, attribution)` and emit attrs\n * that exactly match the declared mark schema in YAttributionMarks.ts. Any\n * mismatch causes the sync plugin to fire phantom reconcile dispatches in a\n * loop. See ATTRIBUTION.md in @y/prosemirror.\n *\n * Crucially the marks carry only stable identity (`userIds`, plus `format` for\n * the modification mark) — *not* user colors. Colors resolve asynchronously\n * from the {@link UserStore}, so baking them in here would make the mapper's\n * output change under a fixed `(format, attribution)` once a user loads, which\n * is exactly the non-determinism that triggers the reconcile loop. Instead the\n * `AttributionExtension` applies colors as a decoration layer that can\n * update independently of the mark representation.\n */\nexport const mapAttributionToMark = (\n format: Record<string, unknown> | null,\n attribution: {\n insert?: readonly string[];\n delete?: readonly string[];\n format?: Record<string, readonly string[]>;\n insertAt?: number;\n deleteAt?: number;\n formatAt?: number;\n },\n): Record<string, unknown> => {\n const out: Record<string, unknown> = { ...format };\n\n if (attribution.insert) {\n out[\"y-attributed-insert\"] = { userIds: attribution.insert };\n }\n\n if (attribution.delete) {\n out[\"y-attributed-delete\"] = { userIds: attribution.delete };\n }\n\n if (attribution.format) {\n const userIds = [...new Set(Object.values(attribution.format).flat())];\n out[\"y-attributed-format\"] = { userIds, format: attribution.format };\n }\n\n return out;\n};\n\nexport const YSyncExtension = createExtension(\n ({\n options,\n editor,\n }: ExtensionOptions<\n Pick<\n CollaborationOptions,\n | \"fragment\"\n | \"renderer\"\n | \"suggestionDoc\"\n | \"provider\"\n | \"getAttributionMarkClassName\"\n >\n >) => {\n return {\n key: \"ySync\",\n fragment: options.fragment,\n mount: () => {\n const configure = () => {\n editor.exec(\n configureYProsemirror({\n ytype: options.fragment,\n // purposefully not passing `renderer` here, this is only for syncing the main doc, not switching to suggestion mode\n // In the future, we may want view suggestion mode to be the default, and then we can decide how to indicate that through the options.\n // For now though, we are leaving suggestion mode as experimental and must be explicitly enabled through the SuggestionsExtension.\n }),\n );\n };\n\n if (\n options.provider &&\n \"synced\" in options.provider &&\n typeof options.provider.synced === \"boolean\"\n ) {\n if (options.provider[\"synced\"]) {\n configure();\n } else if (\n \"on\" in options.provider &&\n typeof options.provider.on === \"function\"\n ) {\n options.provider.on(\"synced\", (synced: boolean) => {\n if (synced) {\n configure();\n }\n });\n } else {\n throw new Error(\n \"YSyncExtension: provider must have a 'synced' boolean or an 'on' method to listen for 'sync'\",\n );\n }\n } else {\n configure();\n }\n },\n prosemirrorPlugins: [\n syncPlugin({\n suggestionDoc: options.suggestionDoc,\n mapAttributionToMark,\n // Node-pairing policy for the PM->Y diff: a `blockContainer` whose\n // block-content type changes is treated as a *different* node, so the\n // diff replaces the whole container (deleted + inserted siblings in\n // the blockGroup) instead of producing two block-contents in one\n // container => schema-invalid. No schema change / storage transform\n // needed; `blockContainer` already whitelists the `y-attributed-*`\n // marks. See blockMatchNodes.ts.\n customCompare: blockMatchNodes,\n }),\n ],\n runsBefore: [\"default\"],\n } as const;\n },\n);\n","import {\n deltaAttributionToFormat,\n deltaToPNode,\n deltaToPSteps,\n docToDelta,\n nodeToDelta,\n pmToFragment,\n} from \"@y/prosemirror\";\nimport * as d from \"lib0/delta\";\nimport { Node } from \"prosemirror-model\";\nimport { Transaction } from \"prosemirror-state\";\nimport {\n type Block,\n type BlockNoteEditor,\n type BlockSchema,\n type InlineContentSchema,\n type PartialBlock,\n type StyleSchema,\n blockToNode,\n docToBlocks,\n} from \"../index.js\";\nimport { blockMatchNodes } from \"./extensions/blockMatchNodes.js\";\nimport { mapAttributionToMark } from \"./extensions/YSync.js\";\n\nimport * as Y from \"@y/y\";\n\n/**\n * Find the equivalent of a Y.Type in another Y.Doc.\n *\n * For root types this looks up the matching shared key; for sub-types it\n * locates the item by its client/clock ID in the target doc's store.\n */\nexport function findTypeInOtherYdoc<T extends Y.Type<any>>(\n ytype: T,\n otherYdoc: Y.Doc,\n): T {\n const ydoc = ytype.doc;\n if (!ydoc) {\n throw new Error(\"type does not have a ydoc\");\n }\n if (ytype._item === null) {\n /**\n * If is a root type, we need to find the root key in the original ydoc\n * and use it to get the type in the other ydoc.\n */\n const rootKey = Array.from(ydoc.share.keys()).find(\n (key) => ydoc.share.get(key) === ytype,\n );\n if (rootKey == null) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n return otherYdoc.get(rootKey as string, ytype.constructor as any) as T;\n } else {\n /**\n * If it is a sub type, we use the item id to find the history type.\n */\n const ytypeItem = ytype._item;\n const otherStructs = otherYdoc.store.clients.get(ytypeItem.id.client) ?? [];\n const itemIndex = Y.findIndexSS(otherStructs, ytypeItem.id.clock);\n const otherItem = otherStructs[itemIndex] as Y.Item | undefined;\n if (!otherItem) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n const otherContent = otherItem.content as Y.ContentType | undefined;\n if (!otherContent) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n return otherContent.type as T;\n }\n}\n\n/**\n * Turn Prosemirror JSON to BlockNote style JSON\n * @param editor BlockNote editor\n * @param json Prosemirror JSON\n * @returns BlockNote style JSON\n */\nexport function _prosemirrorJSONToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(editor: BlockNoteEditor<BSchema, ISchema, SSchema>, json: any) {\n // note: theoretically this should also be possible without creating prosemirror nodes,\n // but this is definitely the easiest way\n const doc = editor.pmSchema.nodeFromJSON(json);\n return docToBlocks<BSchema, ISchema, SSchema>(doc);\n}\n\n/**\n * Turn BlockNote JSON to Prosemirror node / state\n * @param editor BlockNote editor\n * @param blocks BlockNote blocks\n * @returns Prosemirror root node\n */\nexport function _blocksToProsemirrorNode<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: PartialBlock<BSchema, ISchema, SSchema>[],\n) {\n const pmNodes = blocks.map((b) => blockToNode(b, editor.pmSchema));\n\n const doc = editor.pmSchema.topNodeType.create(\n null,\n editor.pmSchema.nodes[\"blockGroup\"].create(null, pmNodes),\n );\n return doc;\n}\n\n/** YJS / BLOCKNOTE conversions */\n\n/**\n * Turn a Y.Type collaborative doc into a BlockNote document (BlockNote style JSON of all blocks)\n * @param editor BlockNote editor\n * @param fragment Y.Type\n * @returns BlockNote document (BlockNote style JSON of all blocks)\n */\nexport function yfragmentToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(editor: BlockNoteEditor<BSchema, ISchema, SSchema>, fragment: Y.Type) {\n const pmNode = deltaToPNode(fragment.toDeltaDeep(), editor.pmSchema, null);\n if (pmNode === null) {\n return [];\n }\n return docToBlocks<BSchema, ISchema, SSchema>(pmNode);\n}\n\n/**\n * Convert blocks to a Y.Type\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param editor BlockNote editor\n * @param blocks the blocks to convert\n * @param fragment XML fragment name\n * @returns Y.Type\n */\nexport function blocksToYType<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: Block<BSchema, ISchema, SSchema>[],\n fragment?: Y.Type,\n) {\n if (!fragment) {\n fragment = new Y.Doc().get(\"prosemirror\");\n }\n return pmToFragment(_blocksToProsemirrorNode(editor, blocks), fragment);\n}\n\n/**\n * Turn a Y.Doc collaborative doc into a BlockNote document (BlockNote style JSON of all blocks)\n * @param editor BlockNote editor\n * @param ydoc Y.Doc\n * @param fragment XML fragment name\n * @returns BlockNote document (BlockNote style JSON of all blocks)\n */\nexport function yDocToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n ydoc: Y.Doc,\n fragment = \"prosemirror\",\n) {\n return yfragmentToBlocks(editor, ydoc.get(fragment));\n}\n\n/**\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param editor BlockNote editor\n * @param blocks the blocks to convert\n * @param fragment XML fragment name\n */\nexport function blocksToYDoc<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: PartialBlock<BSchema, ISchema, SSchema>[],\n fragment = \"prosemirror\",\n) {\n const delta = docToDelta(_blocksToProsemirrorNode(editor, blocks));\n const doc = new Y.Doc();\n doc.get(fragment).applyDelta(delta);\n return doc;\n}\n\n/**\n * Diff two ProseMirror documents into a delta, using BlockNote's node-pairing\n * policy ({@link blockMatchNodes}) so a block's content-type change is reported\n * as a replace rather than a schema-invalid in-place edit.\n */\nexport function docDiffToDelta(previousDoc: Node, newDoc: Node) {\n const initialDelta = nodeToDelta(previousDoc);\n const finalDelta = nodeToDelta(newDoc);\n return d.diff(initialDelta.done(), finalDelta.done(), {\n compare: blockMatchNodes,\n });\n}\n\n/**\n * Build a ProseMirror transaction that turns `tr.doc` into the content of a\n * Y.Type `fragment`, applying the `renderer`'s authorship as\n * `y-attributed-*` marks. Used to render a (read-only) diff of a snapshot / a\n * version comparison into the editor.\n */\nexport function getProseMirrorTrFromYFragment({\n tr,\n fragment,\n renderer,\n}: {\n tr: Transaction;\n fragment: Y.Type;\n renderer?: Y.AbstractRenderer | null;\n}): Transaction {\n const ycontent = deltaAttributionToFormat(\n fragment.toDeltaDeep({ renderer }),\n mapAttributionToMark,\n );\n // @todo it is preferred to apply the minimal diff - at least for debugging purposes. the\n // document replacal is more reliable though\n\n const pcontent = nodeToDelta(tr.doc, undefined, true);\n const diff = d.diff(pcontent.done(), ycontent.done(), {\n compare: blockMatchNodes,\n });\n return deltaToPSteps(tr, diff, undefined, undefined);\n}\n","import { getMarkRange, posToDOMRect } from \"@tiptap/core\";\n\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n acceptChanges,\n rejectAllChanges,\n rejectChanges,\n configureYProsemirror,\n acceptAllChanges,\n} from \"@y/prosemirror\";\nimport { CollaborationOptions } from \"./index.js\";\nimport { findTypeInOtherYdoc } from \"../utils.js\";\nimport { normalizeToUserStore } from \"../../user/index.js\";\n\nexport const SuggestionsExtension = createExtension(\n ({ editor, options }: ExtensionOptions<CollaborationOptions>) => {\n const suggestionDoc = options.suggestionDoc;\n if (!suggestionDoc) {\n throw new Error(\"Suggestion doc not found\");\n }\n // Shared with the rest of collaboration; the collaboration extension passes\n // an already-built store here.\n const userStore = normalizeToUserStore(options.resolveUsers);\n\n function getSuggestionElementAtPos(pos: number) {\n let currentNode = editor.prosemirrorView.nodeDOM(pos);\n while (currentNode && currentNode.parentElement) {\n if (currentNode.nodeName === \"INS\" || currentNode.nodeName === \"DEL\") {\n return currentNode as HTMLElement;\n }\n currentNode = currentNode.parentElement;\n }\n return null;\n }\n\n function getMarkAtPos(pos: number, markType: string) {\n return editor.transact((tr) => {\n const resolvedPos = tr.doc.resolve(pos);\n const mark = resolvedPos\n .marks()\n .find((mark) => mark.type.name === markType);\n\n if (!mark) {\n return;\n }\n\n const markRange = getMarkRange(resolvedPos, mark.type);\n if (!markRange) {\n return;\n }\n\n return {\n range: markRange,\n mark,\n get text() {\n return tr.doc.textBetween(markRange.from, markRange.to);\n },\n get position() {\n // to minimize re-renders, we convert to JSON, which is the same shape anyway\n return posToDOMRect(\n editor.prosemirrorView,\n markRange.from,\n markRange.to,\n ).toJSON() as DOMRect;\n },\n };\n });\n }\n\n function getSuggestionAtSelection() {\n return editor.transact((tr) => {\n const selection = tr.selection;\n if (!selection.empty) {\n return undefined;\n }\n return (\n getMarkAtPos(selection.anchor, \"insertion\") ||\n getMarkAtPos(selection.anchor, \"deletion\") ||\n getMarkAtPos(selection.anchor, \"modification\")\n );\n });\n }\n\n return {\n key: \"suggestions\",\n userStore,\n runsBefore: [\"ySync\"],\n viewSuggestions: () => {\n if (options.renderer) {\n options.renderer.suggestionMode = false;\n }\n editor.exec(\n configureYProsemirror({\n ytype: findTypeInOtherYdoc(options.fragment, suggestionDoc),\n renderer: options.renderer,\n }),\n );\n },\n enableSuggestions: () => {\n if (options.renderer) {\n options.renderer.suggestionMode = true;\n }\n editor.exec(\n configureYProsemirror({\n ytype: findTypeInOtherYdoc(options.fragment, suggestionDoc),\n renderer: options.renderer,\n }),\n );\n },\n disableSuggestions: () => {\n editor.exec(\n configureYProsemirror({\n ytype: options.fragment,\n renderer: null,\n }),\n );\n },\n applyAllSuggestions: () => {\n return editor.exec(acceptAllChanges());\n },\n applySuggestion: (start: number, end?: number) => {\n return editor.exec(acceptChanges(start, end));\n },\n revertSuggestion: (start: number, end?: number) => {\n return editor.exec(rejectChanges(start, end));\n },\n revertAllSuggestions: () => {\n return editor.exec(rejectAllChanges());\n },\n\n getSuggestionElementAtPos,\n getMarkAtPos,\n getSuggestionAtSelection,\n getSuggestionAtCoords: (coords: { left: number; top: number }) => {\n return editor.transact(() => {\n const posAtCoords = editor.prosemirrorView.posAtCoords(coords);\n if (posAtCoords === null || posAtCoords?.inside === -1) {\n return undefined;\n }\n\n return (\n getMarkAtPos(posAtCoords.pos, \"y-attributed-insert\") ||\n getMarkAtPos(posAtCoords.pos, \"y-attributed-delete\") ||\n getMarkAtPos(posAtCoords.pos, \"y-attributed-format\")\n );\n });\n },\n checkUnresolvedSuggestions: () => {\n let hasUnresolvedSuggestions = false;\n\n editor.prosemirrorState.doc.descendants((node) => {\n if (hasUnresolvedSuggestions) {\n return false;\n }\n\n hasUnresolvedSuggestions =\n node.marks.findIndex(\n (mark) =>\n mark.type.name === \"y-attributed-insert\" ||\n mark.type.name === \"y-attributed-delete\" ||\n mark.type.name === \"y-attributed-format\",\n ) !== -1;\n\n return true;\n });\n\n return hasUnresolvedSuggestions;\n },\n } as const;\n },\n);\n","import { configureYProsemirror, pauseSync } from \"@y/prosemirror\";\nimport * as Y from \"@y/y\";\n\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport type { PreviewController } from \"../../extensions/Versioning/index.js\";\nimport {\n findTypeInOtherYdoc,\n getProseMirrorTrFromYFragment,\n} from \"../utils.js\";\n\n/**\n * Empties the document before a {@link configureYProsemirror} refill so\n * ProseMirror rebuilds every node view instead of reusing a stale-positioned one\n * (BlockNote node views resolve their block eagerly via `getPos()` and throw on\n * a moved node). Sync is paused first so the clear never reaches the Y.Doc.\n *\n * TODO: remove once `configureYProsemirror` applies a minimal diff.\n */\nfunction clearDocumentForConfigure(editor: BlockNoteEditor<any, any, any>) {\n // Pause sync (ytype -> null) so the deletion below stays local.\n editor.exec(pauseSync);\n editor.removeBlocks(editor.document);\n}\n\n/**\n * Creates a Yjs-specific adapter that provides the {@link PreviewController}\n * and `getCurrentDocument` callback required by the base\n * {@link VersioningExtension}.\n *\n * This is wired automatically by the {@link CollaborationExtension} when\n * `versioningEndpoints` is provided. You only need to call this directly if\n * you're using the `VersioningExtension` outside of the collaboration wrapper.\n */\nexport function createYjsVersioningAdapter(\n editor: BlockNoteEditor<any, any, any>,\n fragment: Y.Type,\n): {\n preview: PreviewController<Uint8Array, Y.ContentMap>;\n getCurrentDocument: () => Y.Type;\n serializeCurrentContent: () => Uint8Array;\n} {\n return {\n getCurrentDocument: () => fragment,\n // Serialise the live document as a V2 update — the same format that\n // `getContent` returns (via `convertUpdateFormatV1ToV2`) and that\n // `enterPreview` consumes (`applyUpdateV2`). Used to render a read-only\n // diff of the live document against a snapshot.\n serializeCurrentContent: () => Y.encodeStateAsUpdateV2(fragment.doc!),\n preview: {\n enterPreview: (\n snapshotContent: Uint8Array,\n compareToContent?: Uint8Array,\n attributions?: Y.ContentMap,\n ) => {\n let prevSnapshot: { fragment: Y.Type } | undefined;\n if (compareToContent) {\n const compareToDoc = new Y.Doc({ isSuggestionDoc: true });\n Y.applyUpdateV2(compareToDoc, compareToContent);\n prevSnapshot = {\n fragment: findTypeInOtherYdoc(fragment, compareToDoc),\n };\n }\n\n const doc = new Y.Doc();\n Y.applyUpdateV2(doc, snapshotContent);\n // Empty the document before reconfiguring so ProseMirror rebuilds node\n // views from scratch instead of reusing stale-positioned ones. See\n // clearDocumentForConfigure.\n clearDocumentForConfigure(editor);\n\n editor.exec((state, dispatch) => {\n const tr = getProseMirrorTrFromYFragment({\n tr: state.tr,\n fragment: findTypeInOtherYdoc(fragment, doc),\n // Pass the optional content map as `attrs` so the diff renderer\n // knows who/when authored each change. Without it, the renderer\n // only produces \"what changed\" (empty userIds, null timestamps) and\n // downstream mark tooltips show \"unknown / unknown time\".\n renderer: prevSnapshot\n ? Y.createDiffRenderer(\n prevSnapshot.fragment.doc!,\n doc,\n attributions ? { attrs: attributions } : undefined,\n )\n : undefined,\n });\n if (dispatch) {\n dispatch(tr);\n }\n return true;\n });\n },\n exitPreview: () => {\n // Empty the document before reconfiguring so ProseMirror rebuilds node\n // views from scratch instead of reusing stale-positioned ones. See\n // clearDocumentForConfigure.\n clearDocumentForConfigure(editor);\n editor.exec(configureYProsemirror({ ytype: fragment }));\n },\n applyRestore: (_snapshotContent: Uint8Array) => {\n // For Yjs-backed versioning, restoration happens on the server (e.g.\n // YHub's `/rollback` endpoint) which publishes a reverting update to\n // the document's room. That update propagates back to this client over\n // the live sync connection and updates `fragment` automatically, so\n // there is nothing to apply locally — we only need to leave preview\n // mode. `exitPreview` is already called by the base extension before\n // this runs, so this is a no-op.\n //\n // Note: this assumes `endpoints.restore` performs the server-side\n // restore. The default in-memory adapter has no server, which is why\n // this is specific to the Yjs collaboration setup.\n },\n },\n };\n}\n","import { defaultSelectionBuilder, yCursorPlugin } from \"@y/prosemirror\";\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport { CollaborationOptions } from \"./index.js\";\n\nexport type CollaborationUser = {\n id?: string;\n name: string;\n color: string;\n [key: string]: unknown;\n};\n\n/**\n * Determine whether the foreground color should be white or black based on a provided background color\n * Inspired by: https://stackoverflow.com/a/3943023\n */\nfunction isDarkColor(bgColor: string): boolean {\n const color = bgColor.charAt(0) === \"#\" ? bgColor.substring(1, 7) : bgColor;\n const r = parseInt(color.substring(0, 2), 16); // hexToR\n const g = parseInt(color.substring(2, 4), 16); // hexToG\n const b = parseInt(color.substring(4, 6), 16); // hexToB\n const uicolors = [r / 255, g / 255, b / 255];\n const c = uicolors.map((col) => {\n if (col <= 0.03928) {\n return col / 12.92;\n }\n return Math.pow((col + 0.055) / 1.055, 2.4);\n });\n const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];\n return L <= 0.179;\n}\n\nfunction defaultCursorRender(user: CollaborationUser) {\n const cursorElement = document.createElement(\"span\");\n\n cursorElement.classList.add(\"bn-collaboration-cursor__base\");\n\n const caretElement = document.createElement(\"span\");\n caretElement.setAttribute(\"contentedEditable\", \"false\");\n caretElement.classList.add(\"bn-collaboration-cursor__caret\");\n caretElement.setAttribute(\n \"style\",\n `background-color: ${user.color}; color: ${\n isDarkColor(user.color) ? \"white\" : \"black\"\n }`,\n );\n\n const labelElement = document.createElement(\"span\");\n\n labelElement.classList.add(\"bn-collaboration-cursor__label\");\n labelElement.setAttribute(\n \"style\",\n `background-color: ${user.color}; color: ${\n isDarkColor(user.color) ? \"white\" : \"black\"\n }`,\n );\n labelElement.insertBefore(document.createTextNode(user.name), null);\n\n caretElement.insertBefore(labelElement, null);\n\n cursorElement.insertBefore(document.createTextNode(\"\\u2060\"), null); // Non-breaking space\n cursorElement.insertBefore(caretElement, null);\n cursorElement.insertBefore(document.createTextNode(\"\\u2060\"), null); // Non-breaking space\n\n return cursorElement;\n}\n\nexport const YCursorExtension = createExtension(\n ({ options }: ExtensionOptions<CollaborationOptions>) => {\n const recentlyUpdatedCursors = new Map();\n const awareness =\n options.provider &&\n \"awareness\" in options.provider &&\n typeof options.provider.awareness === \"object\"\n ? options.provider.awareness\n : undefined;\n if (awareness) {\n if (\n \"setLocalStateField\" in awareness &&\n typeof awareness.setLocalStateField === \"function\"\n ) {\n awareness.setLocalStateField(\"user\", options.user);\n }\n if (\"on\" in awareness && typeof awareness.on === \"function\") {\n if (options.showCursorLabels !== \"always\") {\n awareness.on(\n \"change\",\n ({\n updated,\n }: {\n added: Array<number>;\n updated: Array<number>;\n removed: Array<number>;\n }) => {\n for (const clientID of updated) {\n const cursor = recentlyUpdatedCursors.get(clientID);\n\n if (cursor) {\n setTimeout(() => {\n cursor.element.setAttribute(\"data-active\", \"\");\n }, 10);\n\n if (cursor.hideTimeout) {\n clearTimeout(cursor.hideTimeout);\n }\n\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: setTimeout(() => {\n cursor.element.removeAttribute(\"data-active\");\n }, 2000),\n });\n }\n }\n },\n );\n }\n }\n }\n\n return {\n key: \"yCursor\",\n prosemirrorPlugins: [\n awareness\n ? yCursorPlugin(awareness, {\n selectionBuilder: defaultSelectionBuilder,\n cursorBuilder(user, clientID) {\n let cursorData = recentlyUpdatedCursors.get(clientID);\n\n if (!cursorData) {\n const cursorElement = (\n options.renderCursor ?? defaultCursorRender\n )(user as CollaborationUser);\n\n if (options.showCursorLabels !== \"always\") {\n cursorElement.addEventListener(\"mouseenter\", () => {\n const cursor = recentlyUpdatedCursors.get(clientID)!;\n cursor.element.setAttribute(\"data-active\", \"\");\n\n if (cursor.hideTimeout) {\n clearTimeout(cursor.hideTimeout);\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: undefined,\n });\n }\n });\n\n cursorElement.addEventListener(\"mouseleave\", () => {\n const cursor = recentlyUpdatedCursors.get(clientID)!;\n\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: setTimeout(() => {\n cursor.element.removeAttribute(\"data-active\");\n }, 2000),\n });\n });\n }\n\n cursorData = {\n element: cursorElement,\n hideTimeout: undefined,\n };\n\n recentlyUpdatedCursors.set(clientID, cursorData);\n }\n\n return cursorData.element;\n },\n })\n : undefined,\n ].filter((a) => a !== undefined),\n dependsOn: [\"ySync\"],\n updateUser(user: CollaborationUser) {\n awareness?.setLocalStateField(\"user\", user);\n },\n getUser(): CollaborationUser | undefined {\n const state = awareness?.getLocalState();\n if (!state) {\n return undefined;\n }\n return state[\"user\"];\n },\n } as const;\n },\n);\n","import { docToDelta } from \"@y/prosemirror\";\nimport * as Y from \"@y/y\";\n\nimport type { Block } from \"../../blocks/defaultBlocks.js\";\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport { createExtension } from \"../../editor/BlockNoteExtension.js\";\nimport type { User } from \"../../user/index.js\";\nimport {\n _blocksToProsemirrorNode,\n docDiffToDelta,\n findTypeInOtherYdoc,\n getProseMirrorTrFromYFragment,\n} from \"../utils.js\";\nimport { AttributionExtension } from \"./AttributionExtension.js\";\nimport type { GetAttributionMarkClassName } from \"./YAttributionMarks.js\";\n\n/**\n * A version diff has a single \"author\" — the version that introduced the changes\n * — not a real user, so all `y-attributed-*` marks carry one synthetic id. That\n * id is derived from the version's label (see {@link diffAuthorId}) so it's stable\n * per version: the user store caches resolved users by id, so a per-label id keeps\n * each version's tooltip showing its own name instead of a stale cached one.\n */\nconst DIFF_AUTHOR_ID_PREFIX = \"version:\";\n\n/** The synthetic author id for a given version label. */\nconst diffAuthorId = (label: string) => DIFF_AUTHOR_ID_PREFIX + label;\n\n/** Fallback label used when a diff is rendered without a version name. */\nconst DEFAULT_DIFF_LABEL = \"This version\";\n\n/** Color used for the version diff marks. */\nconst DIFF_AUTHOR_COLOR = \"#4363d8\";\n\nexport type DiffVersioningExtensionOptions = {\n /**\n * The color used for the diff's attribution marks. Defaults to a blue.\n */\n color?: string;\n /**\n * See {@link GetAttributionMarkClassName}. Forwarded to the underlying\n * {@link AttributionExtension} to override mark styling by change type.\n */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n};\n\n/**\n * Records the author of each transaction on `doc` into a mutable\n * {@link Y.Attributions}, so the resulting attribution marks carry a non-empty\n * `userIds` (and therefore resolve to a color/name). The listener must be\n * attached *before* the attributed transaction runs. Mirrors the store used by\n * the suggestion gallery example (`createAttributionStore`).\n */\nfunction attributeTransactionsTo(doc: Y.Doc, userId: string): Y.Attributions {\n const attrs = new Y.Attributions();\n doc.on(\"beforeObserverCalls\", (tr) => {\n if (!tr.insertSet.isEmpty()) {\n Y.insertIntoIdMap(\n attrs.inserts,\n Y.createIdMapFromIdSet(tr.insertSet, [\n Y.createContentAttribute(\"insert\", userId),\n ]),\n );\n }\n if (!tr.deleteSet.isEmpty()) {\n Y.insertIntoIdMap(\n attrs.deletes,\n Y.createIdMapFromIdSet(tr.deleteSet, [\n Y.createContentAttribute(\"delete\", userId),\n ]),\n );\n }\n });\n return attrs;\n}\n\n/**\n * An opt-in extension that renders a read-only diff between two BlockNote\n * documents (`Block[]`) directly in the editor, marking insertions/deletions\n * with the `y-attributed-*` suggestion marks — the same visual result the Yjs\n * collaboration adapter produces, but driven from two plain block arrays with\n * no server and no live Yjs sync.\n *\n * It composes {@link AttributionExtension} (which registers the attribution\n * marks and drives their colors + hover tooltips from a user store), and adds\n * the {@link renderDiff} / {@link clearDiff} capability.\n *\n * Registering this extension is what makes non-collaborative versioning\n * (`inMemoryVersioning`) capable of showing diffs: the in-memory preview\n * controller looks this up by key (`\"diffVersioning\"`) and delegates to\n * {@link renderDiff}, falling back to a static document swap when it's absent.\n *\n * `renderDiff` is also a standalone, directly-callable API — you can register\n * just this extension and call\n * `editor.getExtension(DiffVersioningExtension).renderDiff(target, baseline)`\n * to render a known diff (e.g. in tests).\n *\n * @example\n * ```ts\n * const editor = BlockNoteEditor.create({\n * extensions: [DiffVersioningExtension()],\n * });\n * editor.getExtension(DiffVersioningExtension)!.renderDiff(target, baseline);\n * ```\n */\nexport const DiffVersioningExtension = createExtension(\n ({\n options,\n editor,\n }: {\n options: DiffVersioningExtensionOptions | undefined;\n editor: BlockNoteEditor<any, any, any>;\n }) => {\n const color = options?.color ?? DIFF_AUTHOR_COLOR;\n\n // Resolve a synthetic author id back to its version label. The id encodes\n // the label (`version:<label>`), so this is a pure decode — no shared mutable\n // state, and the user store caches each version's \"user\" separately (so\n // switching between version comparisons never shows a stale name).\n const resolveUsers = async (ids: string[]): Promise<User[]> =>\n ids\n .filter((id) => id.startsWith(DIFF_AUTHOR_ID_PREFIX))\n .map((id) => ({\n id,\n username: id.slice(DIFF_AUTHOR_ID_PREFIX.length),\n avatarUrl: \"\",\n color,\n }));\n\n /**\n * Render a read-only diff of `baselineBlocks` → `snapshotBlocks` into the\n * editor. The changes are attributed to the version that introduced them:\n * pass `versionLabel` to label the diff marks (shown in their hover tooltip,\n * e.g. \"Edited by: {versionLabel}\"). Uses the \"two-doc fork\" recipe so the\n * two Y.Docs share history — a hard requirement for\n * `createDiffRenderer`, which diffs by Yjs client/clock ids.\n */\n const renderDiff = (\n snapshotBlocks: Block<any, any, any>[],\n baselineBlocks: Block<any, any, any>[],\n versionLabel: string = DEFAULT_DIFF_LABEL,\n ) => {\n const authorId = diffAuthorId(versionLabel);\n\n if (!editor.pmSchema.marks[\"y-attributed-insert\"]) {\n throw new Error(\n \"DiffVersioningExtension: the y-attributed-* marks are missing from \" +\n \"the schema. This should not happen — the extension registers them \" +\n \"via AttributionExtension.\",\n );\n }\n\n const baselineNode = _blocksToProsemirrorNode(editor, baselineBlocks);\n const snapshotNode = _blocksToProsemirrorNode(editor, snapshotBlocks);\n\n // gc must stay off so the attribution manager can read the full struct\n // store (including deleted items) when diffing.\n const prevDoc = new Y.Doc({ gc: false });\n const prevType = prevDoc.get(\"prosemirror\");\n prevDoc.transact(() => {\n prevType.applyDelta(docToDelta(baselineNode) as any);\n });\n\n // Fork prevDoc into nextDoc so they share client/clock ids, then apply the\n // baseline → snapshot delta as a new transaction. New content gets ids\n // that prevDoc lacks (→ inserts); items retained-away become deletes.\n const nextDoc = new Y.Doc({ gc: false });\n Y.applyUpdateV2(nextDoc, Y.encodeStateAsUpdateV2(prevDoc));\n const nextType = findTypeInOtherYdoc(prevType, nextDoc);\n\n // Attach the author store BEFORE applying the delta so the diff\n // transaction's inserts/deletes are attributed.\n const attrs = attributeTransactionsTo(nextDoc, authorId);\n\n const delta = docDiffToDelta(baselineNode, snapshotNode);\n nextDoc.transact(() => {\n nextType.applyDelta(delta as any);\n }, authorId);\n\n const renderer = Y.createDiffRenderer(prevDoc, nextDoc, { attrs });\n\n // Clear the live doc first so ProseMirror rebuilds node views from\n // scratch (BlockNote node views resolve their block eagerly via getPos()\n // and throw on a moved node). The diff then inserts the attributed content\n // against an empty doc.\n editor.replaceBlocks(editor.document, []);\n\n editor.exec((state, dispatch) => {\n const tr = getProseMirrorTrFromYFragment({\n tr: state.tr,\n fragment: nextType,\n renderer,\n });\n if (dispatch) {\n dispatch(tr);\n }\n return true;\n });\n\n prevDoc.destroy();\n nextDoc.destroy();\n };\n\n /**\n * Leave diff view: clear the (mark-carrying) document and restore the given\n * blocks. Clears first so stale node views for block-level marks are torn\n * down instead of reused.\n */\n const clearDiff = (restore: Block<any, any, any>[]) => {\n editor.replaceBlocks(editor.document, []);\n editor.replaceBlocks(editor.document, restore);\n };\n\n return {\n key: \"diffVersioning\",\n // Compose AttributionExtension: registers the y-attributed-* marks and\n // drives their colors + hover-tooltip names from the (static) user store.\n blockNoteExtensions: [\n AttributionExtension({\n resolveUsers,\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n renderDiff,\n clearDiff,\n };\n },\n);\n","import type { Awareness } from \"@y/protocols/awareness\";\nimport type * as Y from \"@y/y\";\nimport { BlockNoteEditorOptions } from \"../../editor/BlockNoteEditor.js\";\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n VersioningEndpoints,\n VersioningEndpointsFactory,\n VersioningExtension,\n} from \"../../extensions/Versioning/index.js\";\nimport { normalizeToUserStore, UserStoreOrResolver } from \"../../user/index.js\";\nimport { AttributionExtension } from \"./AttributionExtension.js\";\nimport { RelativePositionMappingExtension } from \"./RelativePositionMapping.js\";\nimport { SuggestionsExtension } from \"./Suggestions.js\";\nimport { createYjsVersioningAdapter } from \"./Versioning.js\";\nimport { CollaborationUser, YCursorExtension } from \"./YCursorPlugin.js\";\nimport type { GetAttributionMarkClassName } from \"./YAttributionMarks.js\";\nimport { YSyncExtension } from \"./YSync.js\";\n\nexport type CollaborationOptions = {\n /**\n * The Yjs Type that's used for collaboration.\n */\n fragment: Y.Type;\n /**\n * The user info for the current user that's shown to other collaborators.\n */\n user: CollaborationUser;\n /**\n * Resolve user information (usernames, colors) for suggestions and versions,\n * used to drive suggestion-author tooltips, suggestion colors and\n * version-history author labels.\n *\n * Either a resolver function (called with the ids of users that are not yet\n * cached, returning their information — a user store is built from it\n * internally) or a pre-built user store (see `createUserStore`). Pass the same\n * store you give the comments extension so a single de-duped user cache is\n * shared across comments, suggestions and versioning.\n */\n resolveUsers?: UserStoreOrResolver;\n /**\n * A Yjs provider (used for awareness / cursor information)\n */\n provider?: { awareness?: Awareness };\n /**\n * Optional function to customize how cursors of users are rendered\n */\n renderCursor?: (user: CollaborationUser) => HTMLElement;\n /**\n * Optional flag to set when the user label should be shown with the default\n * collaboration cursor. Setting to \"always\" will always show the label,\n * while \"activity\" will only show the label when the user moves the cursor\n * or types. Defaults to \"activity\".\n */\n showCursorLabels?: \"always\" | \"activity\";\n /**\n * The renderer for the collaboration.\n */\n renderer?: Y.DiffRenderer;\n /**\n * The suggestion doc for the collaboration. If using suggestion mode\n */\n suggestionDoc?: Y.Doc;\n\n /**\n * Optional callback to override suggestion-mark styling by change type instead\n * of by author. Given a mark's content/modification type, return a class name\n * applied to the mark and its hover tooltip; the per-user color is then\n * dropped for that mark. See {@link GetAttributionMarkClassName}.\n */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n\n /**\n * The endpoints for the versioning functionality.\n */\n versioningEndpoints?:\n | VersioningEndpoints<Y.Type, Uint8Array>\n | VersioningEndpointsFactory<Y.Type, Uint8Array>;\n};\n\nexport const CollaborationExtension = createExtension(\n ({ editor, options }: ExtensionOptions<CollaborationOptions>) => {\n // Build a single user store here (from a resolver callback or a store the\n // consumer passed in) and hand that same store down to every sub-extension\n // that needs it — suggestions, suggestion-mark tooltips/colors, versioning —\n // so they share one de-duped cache. Passing the resolved store (rather than\n // the raw resolver) is what guarantees the sharing: each child re-normalizes\n // it to itself instead of building its own.\n const userStore = normalizeToUserStore(options.resolveUsers);\n const optionsWithUserStore = { ...options, resolveUsers: userStore };\n return {\n key: \"collaboration\",\n userStore,\n blockNoteExtensions: [\n options.suggestionDoc\n ? SuggestionsExtension(optionsWithUserStore)\n : null,\n RelativePositionMappingExtension(),\n YSyncExtension(optionsWithUserStore),\n YCursorExtension(options),\n options.versioningEndpoints\n ? VersioningExtension({\n ...createYjsVersioningAdapter(editor, options.fragment),\n endpoints: options.versioningEndpoints,\n resolveUsers: userStore,\n })\n : null,\n AttributionExtension({\n resolveUsers: userStore,\n getAttributionMarkClassName: options.getAttributionMarkClassName,\n }),\n ].filter((a) => a !== null),\n } as const;\n },\n);\n\nexport function withCollaboration<\n Options extends Partial<BlockNoteEditorOptions<any, any, any>>,\n>(\n options: Options & {\n /**\n * Options for configuring the collaboration functionality.\n */\n collaboration: CollaborationOptions;\n },\n): Options {\n if (options.initialContent) {\n // eslint-disable-next-line no-console\n console.warn(\n \"When using Collaboration, initialContent might cause conflicts, because changes should come from the collaboration provider\",\n );\n }\n return {\n ...options,\n extensions: [\n ...(options.extensions ?? []),\n CollaborationExtension(options.collaboration),\n ],\n // We disable the default prosemirror history plugin, since it's not compatible with yjs\n disableExtensions: [\"history\", ...(options.disableExtensions ?? [])],\n // We don't want the default initial content, since it will generate a random id for the initial block on each client,\n // leading to conflicts when syncing happens afterwards.\n initialContent: [{ type: \"paragraph\", id: \"initialBlockId\" }],\n };\n}\n\nexport * from \"./AttributionExtension.js\";\nexport * from \"./DiffVersioningExtension.js\";\nexport * from \"./RelativePositionMapping.js\";\nexport * from \"./Suggestions.js\";\nexport * from \"./Versioning.js\";\nexport * from \"./YAttributionMarks.js\";\nexport * from \"./YCursorPlugin.js\";\nexport * from \"./YSync.js\";\n","import * as Y from \"@y/y\";\nimport type {\n CommentData,\n CommentReactionData,\n ThreadData,\n} from \"../../comments/types.js\";\n\nexport function commentToYType(comment: CommentData) {\n const yType = new Y.Type();\n yType.setAttr(\"id\", comment.id);\n yType.setAttr(\"userId\", comment.userId);\n yType.setAttr(\"createdAt\", comment.createdAt.getTime());\n yType.setAttr(\"updatedAt\", comment.updatedAt.getTime());\n if (comment.deletedAt) {\n yType.setAttr(\"deletedAt\", comment.deletedAt.getTime());\n yType.setAttr(\"body\", undefined);\n } else {\n yType.setAttr(\"body\", comment.body);\n }\n if (comment.reactions.length > 0) {\n throw new Error(\"Reactions should be empty in commentToYType\");\n }\n\n /**\n * Reactions are stored in a map keyed by {userId-emoji},\n * this makes it easy to add / remove reactions and in a way that works local-first.\n * The cost is that \"reading\" the reactions is a bit more complex (see yTypeToReactions).\n */\n yType.setAttr(\"reactionsByUser\", new Y.Type());\n yType.setAttr(\"metadata\", comment.metadata);\n\n return yType;\n}\n\nexport function threadToYType(thread: ThreadData) {\n const yType = new Y.Type();\n yType.setAttr(\"id\", thread.id);\n yType.setAttr(\"createdAt\", thread.createdAt.getTime());\n yType.setAttr(\"updatedAt\", thread.updatedAt.getTime());\n const commentsType = new Y.Type();\n\n commentsType.push(thread.comments.map((comment) => commentToYType(comment)));\n\n yType.setAttr(\"comments\", commentsType);\n yType.setAttr(\"resolved\", thread.resolved);\n yType.setAttr(\"resolvedUpdatedAt\", thread.resolvedUpdatedAt?.getTime());\n yType.setAttr(\"resolvedBy\", thread.resolvedBy);\n yType.setAttr(\"metadata\", thread.metadata);\n return yType;\n}\n\ntype SingleUserCommentReactionData = {\n emoji: string;\n createdAt: Date;\n userId: string;\n};\n\nexport function yTypeToReaction(yType: Y.Type): SingleUserCommentReactionData {\n return {\n emoji: yType.getAttr(\"emoji\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n userId: yType.getAttr(\"userId\"),\n };\n}\n\nfunction yTypeToReactions(yType: Y.Type): CommentReactionData[] {\n const flatReactions = [...yType.attrValues()].map((reaction: Y.Type) =>\n yTypeToReaction(reaction),\n );\n // combine reactions by the same emoji\n return flatReactions.reduce(\n (acc: CommentReactionData[], reaction: SingleUserCommentReactionData) => {\n const existingReaction = acc.find((r) => r.emoji === reaction.emoji);\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(\n existingReaction.createdAt.getTime(),\n reaction.createdAt.getTime(),\n ),\n );\n } else {\n acc.push({\n emoji: reaction.emoji,\n createdAt: reaction.createdAt,\n userIds: [reaction.userId],\n });\n }\n return acc;\n },\n [] as CommentReactionData[],\n );\n}\n\nexport function yTypeToComment(yType: Y.Type): CommentData {\n return {\n type: \"comment\",\n id: yType.getAttr(\"id\"),\n userId: yType.getAttr(\"userId\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n updatedAt: new Date(yType.getAttr(\"updatedAt\")),\n deletedAt: yType.getAttr(\"deletedAt\")\n ? new Date(yType.getAttr(\"deletedAt\"))\n : undefined,\n reactions: yTypeToReactions(yType.getAttr(\"reactionsByUser\")),\n metadata: yType.getAttr(\"metadata\"),\n body: yType.getAttr(\"body\"),\n };\n}\n\nexport function yTypeToThread(yType: Y.Type): ThreadData {\n return {\n type: \"thread\",\n id: yType.getAttr(\"id\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n updatedAt: new Date(yType.getAttr(\"updatedAt\")),\n comments: ((yType.getAttr(\"comments\") as Y.Type)?.toArray() || []).map(\n (comment) => yTypeToComment(comment as Y.Type),\n ),\n resolved: yType.getAttr(\"resolved\"),\n resolvedUpdatedAt: new Date(yType.getAttr(\"resolvedUpdatedAt\")),\n resolvedBy: yType.getAttr(\"resolvedBy\"),\n metadata: yType.getAttr(\"metadata\"),\n };\n}\n","import * as Y from \"@y/y\";\nimport type { ThreadData } from \"../../comments/types.js\";\nimport { ThreadStore } from \"../../comments/threadstore/ThreadStore.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { yTypeToThread } from \"./yjsHelpers.js\";\n\n/**\n * This is an abstract class that only implements the READ methods required by the ThreadStore interface.\n * The data is read from a @y/y Type used as a map (via attributes).\n */\nexport abstract class YjsThreadStoreBase extends ThreadStore {\n constructor(\n protected readonly threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(auth);\n }\n\n // TODO: async / reactive interface?\n public getThread(threadId: string) {\n const yThread = this.threadsYType.getAttr(threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n const thread = yTypeToThread(yThread);\n return thread;\n }\n\n public getThreads(): Map<string, ThreadData> {\n const threadMap = new Map<string, ThreadData>();\n this.threadsYType.forEachAttr((yThread: any, id: string | number) => {\n if (yThread instanceof Y.Type) {\n threadMap.set(String(id), yTypeToThread(yThread));\n }\n });\n return threadMap;\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void) {\n const observer = () => {\n cb(this.getThreads());\n };\n\n this.threadsYType.observeDeep(observer);\n\n return () => {\n this.threadsYType.unobserveDeep(observer);\n };\n }\n}\n","import * as Y from \"@y/y\";\nimport type { CommentBody } from \"../../comments/types.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\n\n/**\n * This is a REST-based implementation of the YjsThreadStoreBase for @y/y (v14).\n * It Reads data directly from the underlying document (same as YjsThreadStore),\n * but for Writes, it sends data to a REST API that should:\n * - check the user has the correct permissions to make the desired changes\n * - apply the updates to the underlying Yjs document\n *\n * (see https://github.com/TypeCellOS/BlockNote-demo-nextjs-hocuspocus)\n *\n * The reason we still use the Yjs document as underlying storage is that it makes it easy to\n * sync updates in real-time to other collaborators.\n * (but technically, you could also implement a different storage altogether\n * and not store the thread related data in the Yjs document)\n */\nexport class RESTYjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly BASE_URL: string,\n private readonly headers: Record<string, string>,\n threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYType, auth);\n }\n\n private doRequest = async (path: string, method: string, body?: any) => {\n const response = await fetch(`${this.BASE_URL}${path}`, {\n method,\n body: JSON.stringify(body),\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.headers,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to ${method} ${path}: ${response.statusText}`);\n }\n\n return response.json();\n };\n\n public addThreadToDocument = async (options: {\n threadId: string;\n selection: {\n head: number;\n anchor: number;\n };\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/addToDocument`, \"POST\", rest);\n };\n\n public createThread = async (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n return this.doRequest(\"\", \"POST\", options);\n };\n\n public addComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments`, \"POST\", rest);\n };\n\n public updateComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments/${commentId}`, \"PUT\", rest);\n };\n\n public deleteComment = (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}?soft=${!!rest.softDelete}`,\n \"DELETE\",\n );\n };\n\n public deleteThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}`, \"DELETE\");\n };\n\n public resolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/resolve`, \"POST\");\n };\n\n public unresolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/unresolve`, \"POST\");\n };\n\n public addReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}/reactions`,\n \"POST\",\n rest,\n );\n };\n\n public deleteReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n return this.doRequest(\n `/${options.threadId}/comments/${options.commentId}/reactions/${options.emoji}`,\n \"DELETE\",\n );\n };\n}\n","import { uuidv4 } from \"lib0/random\";\nimport * as Y from \"@y/y\";\nimport type {\n CommentBody,\n CommentData,\n ThreadData,\n} from \"../../comments/types.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\nimport {\n commentToYType,\n threadToYType,\n yTypeToComment,\n yTypeToThread,\n} from \"./yjsHelpers.js\";\n\n/**\n * This is a @y/y (v14)-based implementation of the ThreadStore interface.\n *\n * It reads and writes thread / comments information directly to the underlying Yjs Document.\n *\n * @important While this is the easiest to add to your app, there are two challenges:\n * - The user needs to be able to write to the Yjs document to store the information.\n * So a user without write access to the Yjs document cannot leave any comments.\n * - Even with write access, the operations are not secure. Unless your Yjs server\n * guards against malicious operations, it's technically possible for one user to make changes to another user's comments, etc.\n * (even though these options are not visible in the UI, a malicious user can make unauthorized changes to the underlying Yjs document)\n */\nexport class YjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly userId: string,\n threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYType, auth);\n }\n\n private transact = <T, R>(\n fn: (options: T) => R,\n ): ((options: T) => Promise<R>) => {\n return async (options: T) => {\n return this.threadsYType.doc!.transact(() => {\n return fn(options);\n });\n };\n };\n\n public createThread = this.transact(\n (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n if (!this.auth.canCreateThread()) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const comment: CommentData = {\n type: \"comment\",\n id: uuidv4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n reactions: [],\n metadata: options.initialComment.metadata,\n body: options.initialComment.body,\n };\n\n const thread: ThreadData = {\n type: \"thread\",\n id: uuidv4(),\n createdAt: date,\n updatedAt: date,\n comments: [comment],\n resolved: false,\n metadata: options.metadata,\n };\n\n this.threadsYType.setAttr(thread.id, threadToYType(thread));\n\n return thread;\n },\n );\n\n // YjsThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n public addComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canAddComment(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n const comment: CommentData = {\n type: \"comment\",\n id: uuidv4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n deletedAt: undefined,\n reactions: [],\n metadata: options.comment.metadata,\n body: options.comment.body,\n };\n\n (yThread.getAttr(\"comments\") as Y.Type).push([commentToYType(comment)]);\n\n yThread.setAttr(\"updatedAt\", new Date().getTime());\n return comment;\n },\n );\n\n public updateComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canUpdateComment(yTypeToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n yComment.setAttr(\"body\", options.comment.body);\n yComment.setAttr(\"updatedAt\", new Date().getTime());\n yComment.setAttr(\"metadata\", options.comment.metadata);\n },\n );\n\n public deleteComment = this.transact(\n (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canDeleteComment(yTypeToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n if (yComment.getAttr(\"deletedAt\")) {\n throw new Error(\"Comment already deleted\");\n }\n\n if (options.softDelete) {\n yComment.setAttr(\"deletedAt\", new Date().getTime());\n yComment.setAttr(\"body\", undefined);\n } else {\n commentsType.delete(yCommentIndex);\n }\n\n if (\n commentsType\n .toArray()\n .every((comment) => (comment as Y.Type).getAttr(\"deletedAt\"))\n ) {\n // all comments deleted\n if (options.softDelete) {\n yThread.setAttr(\"deletedAt\", new Date().getTime());\n } else {\n this.threadsYType.deleteAttr(options.threadId);\n }\n }\n\n yThread.setAttr(\"updatedAt\", new Date().getTime());\n },\n );\n\n public deleteThread = this.transact((options: { threadId: string }) => {\n if (\n !this.auth.canDeleteThread(\n yTypeToThread(this.threadsYType.getAttr(options.threadId) as Y.Type),\n )\n ) {\n throw new Error(\"Not authorized\");\n }\n\n this.threadsYType.deleteAttr(options.threadId);\n });\n\n public resolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canResolveThread(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.setAttr(\"resolved\", true);\n yThread.setAttr(\"resolvedUpdatedAt\", new Date().getTime());\n yThread.setAttr(\"resolvedBy\", this.userId);\n });\n\n public unresolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canUnresolveThread(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.setAttr(\"resolved\", false);\n yThread.setAttr(\"resolvedUpdatedAt\", new Date().getTime());\n });\n\n public addReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canAddReaction(yTypeToComment(yComment), options.emoji)) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.getAttr(\"reactionsByUser\") as Y.Type;\n\n if (reactionsByUser.hasAttr(key)) {\n // already exists\n return;\n } else {\n const reaction = new Y.Type();\n reaction.setAttr(\"emoji\", options.emoji);\n reaction.setAttr(\"createdAt\", date.getTime());\n reaction.setAttr(\"userId\", this.userId);\n reactionsByUser.setAttr(key, reaction);\n }\n },\n );\n\n public deleteReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (\n !this.auth.canDeleteReaction(yTypeToComment(yComment), options.emoji)\n ) {\n throw new Error(\"Not authorized\");\n }\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.getAttr(\"reactionsByUser\") as Y.Type;\n\n reactionsByUser.deleteAttr(key);\n },\n );\n}\n\nfunction yTypeFindIndex(yType: Y.Type, predicate: (item: any) => boolean) {\n for (let i = 0; i < yType.length; i++) {\n if (predicate(yType.get(i))) {\n return i;\n }\n }\n return -1;\n}\n","import * as Y from \"@y/y\";\nimport { decodeAny, encodeAny } from \"lib0/buffer\";\n\nimport {\n CURRENT_VERSION_ID,\n sortSnapshotsNewestFirst,\n VersioningEndpointsFactory,\n type VersioningEndpoints,\n type VersionSnapshot,\n} from \"../../extensions/Versioning/index.js\";\nimport { uint32 } from \"lib0/random\";\nimport { YCursorExtension } from \"../extensions/YCursorPlugin.js\";\nimport { YSyncExtension } from \"../extensions/YSync.js\";\n\n/**\n * Name of the root {@link Y.Type} map on the live collaboration doc that stores\n * a mutable `versionId -> name` mapping. Because YHub attributions are\n * immutable, version names that need to be editable (renamed) live here on the\n * Y.Doc instead of (or in addition to) the immutable `name` attribution.\n */\nconst VERSION_NAMES_MAP = \"__bn_version_names\";\n\n/**\n * Options for creating a YHub versioning endpoints instance.\n */\nexport interface YHubVersioningOptions {\n /**\n * Base URL of the YHub API (e.g. `\"https://yhub.example.com\"`).\n * Must **not** include a trailing slash.\n */\n baseUrl: string;\n\n /** YHub organisation identifier. */\n org: string;\n\n /** Document identifier within the organisation. */\n docId: string;\n\n /**\n * Optional headers to include in every request (e.g. authentication tokens).\n */\n headers?: Record<string, string>;\n\n /**\n * Maximum number of activity entries to fetch when listing versions.\n * @default 50\n */\n activityLimit?: number;\n\n /**\n * When set, forwarded as the `group` query param to the YHub activity API,\n * controlling whether adjacent edits are grouped into single entries.\n */\n group?: boolean;\n\n /**\n * Maximum gap (in ms) between edits for them to be grouped together.\n * Forwarded as the `groupMaxGap` query param.\n * @default 10000\n */\n groupMaxGap?: number;\n\n /**\n * Maximum total duration (in ms) a single group of edits may span.\n * When set, forwarded as the `groupMaxDuration` query param.\n */\n groupMaxDuration?: number;\n\n // TODO mergeUsers is not in standard yhub, but it exists in our fork.\n /**\n * When `true`, adjacent edits are grouped together even when made by\n * *different* users (their ids accumulate in the grouped entry's `by`).\n * When `false` (the default), only same-user adjacent edits are merged.\n * Forwarded as the `mergeUsers` query param.\n * @default false\n */\n mergeUsers?: boolean;\n}\n\n/**\n * Shape of a single activity entry returned by the YHub\n * `GET /activity/{org}/{docId}` endpoint (after `decodeAny`).\n */\ninterface YHubActivityEntry {\n /** Start of the change window (unix-ms timestamp). */\n from: number;\n /** End of the change window (unix-ms timestamp). */\n to: number;\n /** Comma separated list of user-ids that matches the attribution */\n by?: string;\n /** Custom attribution key-value pairs (when `customAttributions=true`). */\n customAttributions?: Array<{ k: string; v: string }>;\n}\n\n/**\n * Shape returned by the YHub `GET /changeset/{org}/{docId}` endpoint (after\n * `decodeAny`).\n */\ninterface YHubChangeset {\n /** Full Y.Doc state **before** the changeset window. */\n prevDoc?: Uint8Array;\n /** Full Y.Doc state **after** the changeset window. */\n nextDoc?: Uint8Array;\n /**\n * Encoded {@link Y.ContentMap} describing who authored each change in the\n * window and when. Present when the changeset is requested with\n * `attributions=true`.\n */\n attributions?: Uint8Array;\n}\n\n/**\n * Whether an activity entry is a version marker (created with a `type:version`\n * custom attribution) as opposed to a plain edit.\n */\nfunction isVersionEntry(entry: YHubActivityEntry): boolean {\n return (\n entry.customAttributions?.some(\n (a) => a.k === \"type\" && a.v === \"version\",\n ) ?? false\n );\n}\n\n/**\n * Convert a YHub activity entry into a {@link VersionSnapshot}.\n *\n * Version markers (entries with a `type:version` custom attribution) map to\n * named snapshots: the `id` attribution becomes the snapshot identifier and the\n * `name` attribution its name. Any other (plain edit) entry maps to a\n * history-only snapshot with a synthetic `history-<to>-<index>` id and no name.\n * In both cases the entry's `by` user-ids are passed through raw on\n * {@link VersionSnapshot.by} — resolving them to user info is the view layer's\n * job.\n *\n * The history id embeds the entry's `index` within the activity response\n * because YHub can emit multiple activity entries sharing the same `to`\n * timestamp (e.g. distinct same-`insertAt` patches that grouping did not merge),\n * and `to` alone would then produce colliding `history-<to>` ids — duplicate\n * React keys in the sidebar. The `index` disambiguates them. The changeset\n * lookups (`getContent`/`getAttributions`/`restore`) key off\n * {@link VersionSnapshot.createdAt} (= `entry.to`), never the id, so embedding\n * the index in the id is safe.\n */\nfunction activityToSnapshot(\n entry: YHubActivityEntry,\n index: number,\n): VersionSnapshot | undefined {\n const by = entry.by\n ?.split(\",\")\n .map((s) => s.trim())\n .filter(Boolean);\n const byField = by && by.length > 0 ? by : undefined;\n\n if (isVersionEntry(entry)) {\n const id = entry.customAttributions?.find((a) => a.k === \"id\")?.v;\n if (id === undefined) {\n return undefined;\n }\n const attributionName = entry.customAttributions?.find(\n (a) => a.k === \"name\",\n )?.v;\n return {\n id,\n name: attributionName,\n createdAt: entry.to,\n updatedAt: entry.to,\n by: byField,\n };\n }\n\n return {\n id: `history-${entry.to}-${index}`,\n createdAt: entry.to,\n updatedAt: entry.to,\n by: byField,\n };\n}\n\nasync function yhubFetch(\n url: string,\n headers: Record<string, string>,\n init?: RequestInit,\n): Promise<ArrayBuffer> {\n const res = await fetch(url, {\n ...init,\n headers: {\n ...headers,\n ...(init?.headers instanceof Headers\n ? Object.fromEntries(init.headers.entries())\n : Array.isArray(init?.headers)\n ? Object.fromEntries(init.headers)\n : init?.headers),\n },\n });\n if (!res.ok) {\n throw new Error(\n `YHub request failed: ${res.status} ${res.statusText} (${url})`,\n );\n }\n return res.arrayBuffer();\n}\n\n/**\n * Create a {@link VersioningEndpoints} implementation backed by the\n * [YHub](https://github.com/yjs/yhub) HTTP API.\n *\n * Versions are created by PATCHing the document with custom attributions\n * (`type:version` + an optional `name`). The `list` endpoint returns the full\n * activity timeline, mapping `type:version` markers to named versions and every\n * other entry to a history-only snapshot, so the sidebar can show both the\n * named versions and the complete edit history.\n *\n * A version's id lives in immutable YHub attributions (`type:version` + `id`),\n * so it is fixed at creation time. Version *names*, however, are stored in a\n * mutable `__bn_version_names` map on the live collaboration doc (see\n * {@link VERSION_NAMES_MAP}), so `rename` is supported and simply updates that\n * store.\n *\n * @example\n * ```ts\n * import { withCollaboration } from \"@blocknote/core/y\";\n * import { createYHubVersioningEndpoints } from \"@blocknote/core/y\";\n *\n * const editor = BlockNoteEditor.create(\n * withCollaboration({\n * collaboration: {\n * fragment,\n * user: { name: \"Alice\", color: \"#ff0\" },\n * provider,\n * versioningEndpoints: createYHubVersioningEndpoints({\n * baseUrl: \"https://yhub.example.com\",\n * org: \"my-org\",\n * docId: \"my-doc\",\n * }),\n * },\n * }),\n * );\n * ```\n */\nexport function createYHubVersioningEndpoints(\n options: YHubVersioningOptions,\n): VersioningEndpointsFactory<Y.Type, Uint8Array, Y.ContentMap> {\n const {\n baseUrl,\n org,\n docId,\n headers = {},\n activityLimit = 50,\n group,\n } = options;\n\n const activityUrl = `${baseUrl}/activity/${org}/${docId}`;\n const changesetUrl = `${baseUrl}/changeset/${org}/${docId}`;\n const rollbackUrl = `${baseUrl}/rollback/${org}/${docId}`;\n\n return (editor) => {\n /**\n * The mutable per-id version-name store on the live collaboration doc.\n *\n * Returns the root {@link VERSION_NAMES_MAP} map-typed {@link Y.Type}, which\n * uses `setAttr`/`getAttr` for keyed access (this Yjs fork has a single\n * unified `Y.Type` rather than a distinct `Y.Map`). `undefined` until the\n * live doc has been captured from a `create` call.\n */\n const getVersionNamesMap = (): Y.Type | undefined => {\n const fragment =\n editor.getExtension<typeof YSyncExtension>(\"ySync\")?.fragment.doc;\n // `fragment` is undefined until the live doc has been captured (e.g. no\n // ySync extension attached yet); return undefined rather than throwing so\n // callers can gracefully fall back to the immutable name attribution.\n return fragment?.get(VERSION_NAMES_MAP);\n };\n\n /**\n * Build the synthetic \"current version\" snapshot, or `undefined` when the\n * live document matches the latest saved version (no edits since).\n *\n * Both lookups are made here, independently of the grouped `list()` request:\n *\n * - the newest activity entry of *any* kind (ungrouped, so its `to` is the\n * true last-edit time), and\n * - the newest **version marker** (via the `withCustomAttributions`\n * server-side filter).\n *\n * Deriving the marker time from `list()`'s grouped entries would be wrong:\n * with grouping (especially `mergeUsers`) the newest marker's group absorbs\n * the later unsaved edit, so the group's `to` equals the edit's `to` and the\n * comparison below can never fire. Fetching the marker unmerged avoids that.\n */\n const getCurrentVersionEntry = async (): Promise<\n VersionSnapshot | undefined\n > => {\n const latestParams = new URLSearchParams({\n order: \"desc\",\n limit: \"1\",\n customAttributions: \"true\",\n });\n const latestVersionParams = new URLSearchParams({\n order: \"desc\",\n limit: \"1\",\n customAttributions: \"true\",\n // Server-side filter to `type:version` markers only, so this ignores the\n // plain edits that would otherwise be the newest entries.\n withCustomAttributions: \"type:version\",\n });\n\n const [latestBuf, latestVersionBuf] = await Promise.all([\n yhubFetch(`${activityUrl}?${latestParams}`, headers),\n yhubFetch(`${activityUrl}?${latestVersionParams}`, headers),\n ]);\n const latestEdit = (\n decodeAny(new Uint8Array(latestBuf)) as YHubActivityEntry[]\n )[0];\n const latestVersion = (\n decodeAny(new Uint8Array(latestVersionBuf)) as YHubActivityEntry[]\n )[0];\n\n if (!latestEdit || latestEdit.to <= (latestVersion?.to ?? 0)) {\n return undefined;\n }\n\n // Build the synthetic entry directly rather than via `activityToSnapshot`,\n // whose `id` comes from a string-typed wire attribution — the current\n // entry's id is the `CURRENT_VERSION_ID` symbol, not a real version id.\n const by =\n latestEdit.by\n ?.split(\",\")\n .map((t) => t.trim())\n .filter(Boolean) ?? [];\n return {\n id: CURRENT_VERSION_ID,\n createdAt: latestEdit.to,\n updatedAt: latestEdit.to,\n by: by.length > 0 ? by : undefined,\n };\n };\n\n /**\n * PATCH the current document state to YHub, optionally with custom\n * attributions. Used both for creating named version markers and for\n * backing up the document before a restore.\n */\n const patchDoc = async (\n fragment: Y.Type,\n customAttributions: Array<{ k: string; v: any }>,\n by?: string,\n ) => {\n const doc = fragment.doc;\n if (!doc) {\n throw new Error(\n \"Cannot patch document: the Y.Type is not attached to a Y.Doc.\",\n );\n }\n\n // YHub only records custom attributions when they attach to NEW content\n // that survives its server-side diff. An update-less PATCH is rejected\n // (400 — \"at least one of update or awareness must be present\"), and even\n // if it weren't, there'd be no content for the attributions to ride on, so\n // no activity entry is created. YHub has no metadata-only marker path.\n //\n // So we introduce a tiny piece of novel content for the marker to attach\n // to: a single insert into a dedicated `__bn_version_markers` fragment that\n // the editor never renders. A fresh Y.Doc guarantees a clientID/content the\n // server has never seen, so the diff is non-empty and the attributions land\n // on it. The reconstructed document at this version's timestamp still\n // contains the full editor content — this marker only ever lives in the\n // throwaway fragment.\n const markerDoc = new Y.Doc();\n markerDoc.get(\"__bn_version_markers\", \"XmlFragment\").insert(0, [\"v\"]);\n const update = Y.encodeStateAsUpdate(markerDoc);\n\n const body: Record<string, unknown> = { update, customAttributions };\n\n await yhubFetch(\n `${baseUrl}/ydoc/${org}/${docId}${by ? `?userid=${by}` : \"\"}`,\n headers,\n {\n method: \"PATCH\",\n body: encodeAny(body) as BufferSource,\n },\n );\n };\n\n /**\n * Create a named version marker for the current document state by PATCHing\n * it with `type:version` custom attributions.\n */\n const create: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"create\"] = async (fragment, options) => {\n const id = String(uint32());\n const now = Date.now();\n\n if (options?.name) {\n getVersionNamesMap()?.setAttr(id, options.name);\n }\n\n const customAttributions: Array<{ k: string; v: string }> = [\n { k: \"type\", v: \"version\" },\n { k: \"id\", v: id },\n ];\n if (options?.name) {\n customAttributions.push({ k: \"name\", v: options.name });\n }\n\n const user = editor\n .getExtension<typeof YCursorExtension>(\"yCursor\")\n ?.getUser();\n await patchDoc(fragment, customAttributions, user?.id);\n\n return {\n id,\n name: options?.name,\n createdAt: now,\n updatedAt: now,\n by: user?.id,\n };\n };\n\n /**\n * Reconstruct the full document state as it was at a given `to` timestamp.\n *\n * The changeset endpoint builds `nextDoc` purely from the `to` timestamp\n * range — it ignores `withCustomAttributions` for doc reconstruction (that\n * filter only scopes the attribution overlay). So historical document state\n * can only be retrieved by timestamp, never by the version's `id`.\n */\n const getContentAt = async (to: number): Promise<Uint8Array> => {\n const params = new URLSearchParams({\n ydoc: \"true\",\n to: String(to),\n });\n\n const buf = await yhubFetch(`${changesetUrl}?${params}`, headers);\n const changeset = decodeAny(new Uint8Array(buf)) as YHubChangeset;\n\n if (!changeset.nextDoc) {\n throw new Error(`YHub returned no document state at timestamp ${to}.`);\n }\n\n return Y.convertUpdateFormatV1ToV2(changeset.nextDoc);\n };\n\n /**\n * Fetch the full document content for a saved version snapshot.\n *\n * The snapshot's `createdAt` is the activity entry's `to` timestamp (see\n * {@link activityToSnapshot}), which is exactly what the changeset API needs.\n */\n const getContent: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"getContent\"] = async (snapshot) => {\n return getContentAt(snapshot.createdAt);\n };\n\n /**\n * Fetch the authorship attributions for the changes between two snapshots\n * (or from the start of the document when `compareTo` is omitted).\n *\n * Snapshots carry their `to` timestamp directly in `createdAt`, so no\n * activity lookup is needed to resolve the changeset window.\n */\n const getAttributions: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"getAttributions\"] = async (snapshot, compareTo) => {\n const to = snapshot.createdAt;\n const from = compareTo !== undefined ? compareTo.createdAt : 0;\n\n const params = new URLSearchParams({\n from: String(from),\n to: String(to),\n attributions: \"true\",\n });\n\n const buf = await yhubFetch(`${changesetUrl}?${params}`, headers);\n const changeset = decodeAny(new Uint8Array(buf)) as YHubChangeset;\n\n if (!changeset.attributions) {\n throw new Error(\n `YHub returned no attributions for snapshot ${String(snapshot.id)}.`,\n );\n }\n\n return Y.decodeContentMap(changeset.attributions);\n };\n\n /**\n * Restore the document to a saved version: fetch the target version's\n * content and roll back everything after it.\n *\n * The snapshot's `createdAt` is the activity entry's `to` timestamp.\n */\n const restore: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"restore\"] = async (_fragment, snapshot) => {\n const to = snapshot.createdAt;\n const snapshotContent = await getContentAt(to);\n\n await yhubFetch(`${rollbackUrl}?from=${to}`, headers, {\n method: \"POST\",\n body: encodeAny({ from: to }) as BufferSource,\n });\n\n return snapshotContent;\n };\n\n /**\n * Rename a saved version by updating its entry in the mutable\n * {@link VERSION_NAMES_MAP} store on the live collaboration doc.\n *\n * The version's `id` remains fixed in its immutable YHub attributions —\n * only the editable name in the map is changed. Passing an empty or\n * `undefined` name clears the entry (falling back to the immutable `name`\n * attribution captured at creation time).\n */\n const rename: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"rename\"] = async (snapshot, name) => {\n if (typeof snapshot.id !== \"string\") {\n // CURRENT_VERSION_ID (symbol) is not renameable.\n return;\n }\n const map = getVersionNamesMap();\n if (!map) {\n throw new Error(\n \"Cannot rename version: no live collaboration document is available.\",\n );\n }\n if (name === undefined || name === \"\") {\n map.deleteAttr(snapshot.id);\n } else {\n map.setAttr(snapshot.id, name);\n }\n };\n\n /**\n * List the full version timeline (newest first), plus a synthetic\n * \"current version\" entry when the live document has unsaved edits.\n *\n * Returns the entire activity timeline: `type:version` markers are mapped\n * to named snapshots and every other entry to a history-only snapshot (see\n * {@link activityToSnapshot}), so the sidebar can offer both a \"named\n * versions\" and a full \"history\" view. Author user-ids are passed through\n * raw on {@link VersionSnapshot.by} — the view layer resolves them to user\n * info via the versioning extension's user store.\n */\n const list: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"list\"] = async () => {\n // Read the grouping knobs fresh from `options` so a caller mutating the\n // object it passed in reconfigures grouping on the next refresh (see the\n // note where these are deliberately left out of the destructure above).\n const groupMaxGap = options.groupMaxGap ?? 10000;\n const groupMaxDuration = options.groupMaxDuration;\n const mergeUsers = options.mergeUsers;\n\n const params = new URLSearchParams({\n order: \"desc\",\n limit: String(activityLimit),\n customAttributions: \"true\",\n });\n // Always send a concrete `groupMaxGap`. Sending\n // `String(undefined)` here would make the server `parseInt(\"undefined\")`\n // to NaN, silently disabling grouping — which surfaces every same-`to`\n // attribution as its own history entry and produces duplicate React keys.\n params.set(\"groupMaxGap\", String(groupMaxGap));\n if (group !== undefined) {\n params.set(\"group\", String(group));\n }\n if (groupMaxDuration !== undefined) {\n params.set(\"groupMaxDuration\", String(groupMaxDuration));\n }\n if (mergeUsers !== undefined) {\n params.set(\"mergeUsers\", String(mergeUsers));\n }\n\n const buf = await yhubFetch(`${activityUrl}?${params}`, headers);\n const entries = decodeAny(new Uint8Array(buf)) as YHubActivityEntry[];\n\n const snapshots = sortSnapshotsNewestFirst(\n entries\n .map((entry, i) => activityToSnapshot(entry, i))\n .filter((s): s is VersionSnapshot => s !== undefined)\n // Prefer the mutable per-id name from the live doc's\n // `__bn_version_names` store over the immutable `name` attribution,\n // so renames (which only mutate that store) are reflected here.\n .map((snapshot) => {\n const attributionName = snapshot.name;\n const mappedName =\n (typeof snapshot.id === \"string\"\n ? (getVersionNamesMap()?.getAttr(snapshot.id) as\n | string\n | undefined)\n : undefined) ?? attributionName;\n return { ...snapshot, name: mappedName };\n }),\n );\n\n // Surface a \"current version\" entry when the live document has edits\n // beyond the most recent saved version marker. `getCurrentVersionEntry`\n // makes its own unmerged lookups (see there), so it is unaffected by the\n // grouping/mergeUsers params used for the list above.\n //\n // This only re-evaluates when `list()` runs (sidebar open / refresh),\n // which matches how YHub versions load today.\n const currentEntry = await getCurrentVersionEntry();\n return currentEntry ? [currentEntry, ...snapshots] : snapshots;\n };\n\n return {\n list,\n create,\n getContent,\n getAttributions,\n restore,\n rename,\n };\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAuDA,IAAa,KACX,GACA,MAEA,MAAW,KAAA,IACP,KAAA,IACA,OAAO,KAAW,WAChB,IACA,EAAO,IAmBT,KAEF,GACA,OAKD,EAAE,SAAM,gBAAgD;CACvD,IAAM,IAAS,GAAS,QAIlB,IACJ,MAAS,WACL,QACA,MAAS,WACP,QACA,IACE,SACA,OACJ,IAAM,SAAS,cAAc,CAAG;CAMtC,AAJA,OAAO,OAAO,EAAI,SAAS;EACzB,SAAS,KAAK,UAAU,EAAK,MAAM,OAAU;EAC7C,QAAQ,OAAO,CAAM;CACvB,CAAC,GACG,MAAS,mBACX,EAAI,QAAQ,OAAU,gBACtB,EAAI,QAAQ,SAAY,KAAK,UAAU,EAAK,MAAM,MAAS;CAS7D,IAAM,IAAmB,EACvB,GAAS,8BAA8B;EACrC,aAAa,IAAS,mBAAmB;EACzC,kBAAkB,MAAS,iBAAiB,WAAW;CACzD,CAAC,GACD,SACF,GAcM,KADW,EAAK,MAAM,WAAkC,CAAC,GACvC,IAClB,IAAW,IACb,EAAuB,CAAO,IAC9B,EAAiB;CACrB,IAAI,GAC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ources":["../src/y/extensions/YAttributionMarks.ts","../src/y/extensions/AttributionExtension.ts","../src/y/extensions/RelativePositionMapping.ts","../src/y/extensions/blockMatchNodes.ts","../src/y/extensions/YSync.ts","../src/y/utils.ts","../src/y/extensions/Suggestions.ts","../src/y/extensions/Versioning.ts","../src/y/extensions/YCursorPlugin.ts","../src/y/extensions/DiffVersioningExtension.ts","../src/y/extensions/index.ts","../src/y/comments/yjsHelpers.ts","../src/y/comments/YjsThreadStoreBase.ts","../src/y/comments/RESTYjsThreadStore.ts","../src/y/comments/YjsThreadStore.ts","../src/y/versioning/yhub.ts"],"sourcesContent":["import { Mark } from \"@tiptap/core\";\nimport { Mark as PMMark, MarkSpec } from \"prosemirror-model\";\nimport {\n createExtension,\n type ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport { BLOCK_LEVEL_SUGGESTION_GROUP } from \"../../pm-nodes/suggestionMarks.js\";\nimport { NON_FORMATTING_MARK_GROUP } from \"../../schema/markGroups.js\";\nimport {\n fallbackColorForUserId,\n userColorPalette,\n userColorVarNames,\n} from \"../../user/index.js\";\n\n/**\n * Describes a suggestion mark to {@link GetAttributionMarkClassName}: whether it\n * wraps inline content or a whole block, and which kind of change it represents.\n * `modificationType: \"format\"` corresponds to the `y-attributed-format`\n * (modification) mark.\n */\nexport type AttributionMarkStyleInfo = {\n contentType: \"inline-content\" | \"block\";\n modificationType: \"insert\" | \"delete\" | \"format\";\n};\n\n/**\n * The class name(s) an app returns to style a suggestion mark. Either:\n * - a single string applied to *both* the mark content and its hover tooltip, or\n * - `{ content, tooltip }` to style the mark content and the tooltip\n * independently.\n */\nexport type AttributionMarkClassNames =\n | string\n | { content: string; tooltip: string };\n\n/**\n * Optional callback to override suggestion-mark styling. Given a mark's\n * {@link AttributionMarkStyleInfo}, it returns the class name(s) to apply (see\n * {@link AttributionMarkClassNames}). When a class is returned, the default\n * per-user color (the `--user-color-*` custom properties and the built-in\n * `.bn-suggestion-mark` / `.bn-suggestion-node` styling) is *not* applied to the\n * mark content, so the class fully controls its appearance — letting an app\n * color suggestions by change type (e.g. green insertions, red deletions)\n * instead of by author.\n */\nexport type GetAttributionMarkClassName = (\n info: AttributionMarkStyleInfo,\n) => AttributionMarkClassNames;\n\n/**\n * Resolve the {@link AttributionMarkClassNames} returned by a\n * {@link GetAttributionMarkClassName} callback to the class name for a single\n * target (the mark `content` or its `tooltip`).\n */\nexport const resolveAttributionMarkClassName = (\n result: AttributionMarkClassNames | undefined,\n target: \"content\" | \"tooltip\",\n): string | undefined =>\n result === undefined\n ? undefined\n : typeof result === \"string\"\n ? result\n : result[target];\n\n/**\n * Shared mark view for the attribution marks (insert / delete / modification).\n * It renders the marked content and tags the wrapper with the author(s) via\n * `data-*` attributes. The attribution tooltip shown on hover is handled\n * separately by the `AttributionExtension`, which reads those attributes\n * straight from the DOM — keeping this mark view purely presentational and the\n * tooltip state off of module scope.\n *\n * Author colors are *not* baked into the (deterministic) mark attrs. Instead the\n * wrapper sets the generic `--user-color-*` custom properties the Block.css rules\n * read, sourcing them from the author's *per-user* CSS variable via\n * `var(--user-color-<id>-*, <fallback>)`. Those per-user variables are populated\n * on the editor root by `AttributionExtension` once the user resolves, so a mark\n * shows the deterministic palette fallback immediately and recolors to the\n * author's own color purely through the CSS cascade — no decoration, no doc\n * transaction. See `userColors.ts`.\n */\nconst createAttributionMarkView =\n (\n type: \"insert\" | \"delete\" | \"modification\",\n options?: {\n editor?: BlockNoteEditor<any, any, any>;\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n },\n ) =>\n ({ mark, inline }: { mark: PMMark; inline: boolean }) => {\n const editor = options?.editor;\n // `<ins>`/`<del>` are semantic elements. The modification mark has no\n // dedicated element, so it renders as a `<span>` inline or a `<div>` over a\n // block, matching its `parseDOM` rules.\n const tag =\n type === \"insert\"\n ? \"ins\"\n : type === \"delete\"\n ? \"del\"\n : inline\n ? \"span\"\n : \"div\";\n const dom = document.createElement(tag);\n\n Object.assign(dom.dataset, {\n userIds: JSON.stringify(mark.attrs[\"userIds\"]),\n inline: String(inline),\n });\n if (type === \"modification\") {\n dom.dataset[\"type\"] = \"modification\";\n dom.dataset[\"format\"] = JSON.stringify(mark.attrs[\"format\"]);\n }\n\n // Optional app-provided override: `y-attributed-format` is exposed as\n // `\"format\"`. When a class is returned it's applied to the content element\n // *instead of* the built-in `.bn-suggestion-mark` / `.bn-suggestion-node`\n // classes, and the per-user `--user-color-*` properties are omitted, so the\n // class fully controls the appearance (background, text color, etc.) with no\n // per-user color leaking through (see the type doc).\n const contentClassName = resolveAttributionMarkClassName(\n options?.getAttributionMarkClassName?.({\n contentType: inline ? \"inline-content\" : \"block\",\n modificationType: type === \"modification\" ? \"format\" : type,\n }),\n \"content\",\n );\n\n // The wrapper is always `display: contents` so it never generates a box of\n // its own — an inline `<ins>`/`<del>` around block/table content (e.g. a\n // suggestion spanning table cells) would otherwise break the normal layout.\n // Because a `display: contents` element paints nothing, the highlight is\n // applied to the inner content span (see `.bn-suggestion-mark` in Block.css)\n // using the `--user-color-*` custom properties, which cascade down from here.\n // They're sourced from the author's per-user variable (populated on the\n // editor root by `AttributionExtension`) with the deterministic palette as a\n // fallback, so a mark is colored before the user resolves and recolors via\n // the cascade afterward. When an override class owns the styling, no per-user\n // color is applied at all.\n const userIds = (mark.attrs[\"userIds\"] as string[] | null) ?? [];\n const firstId = userIds[0];\n const fallback = firstId\n ? fallbackColorForUserId(firstId)\n : userColorPalette[0];\n if (contentClassName) {\n dom.style.cssText = \"display: contents\";\n } else {\n const light = firstId\n ? `var(${userColorVarNames(firstId).light}, ${fallback.light})`\n : fallback.light;\n const dark = firstId\n ? `var(${userColorVarNames(firstId).dark}, ${fallback.dark})`\n : fallback.dark;\n dom.style.cssText =\n \"display: contents\" +\n `; --user-color-light: ${light}; --user-color-dark: ${dark}`;\n }\n\n const contentDOM = document.createElement(\"span\");\n if (inline) {\n // Inline content: the span is a real inline box that carries the highlight\n // (default path) or the app-provided override class.\n contentDOM.className =\n contentClassName ??\n (type === \"delete\"\n ? \"bn-suggestion-mark bn-suggestion-mark--delete\"\n : \"bn-suggestion-mark\");\n } else {\n // Block-level marks wrap block/table structure (e.g. <tr>/<td>/<p>). The\n // span must be `display: contents` so it doesn't inject an inline box into\n // the table layout (which triggers the browser's anonymous-table fixup and\n // breaks the table). Such a span has no box to paint a background on, so\n // the `.bn-suggestion-node` rule highlights its children (the wrapped\n // nodes) instead. An override class is applied to the same span, so it\n // should likewise target its children (e.g. `.my-class > *`).\n contentDOM.style.display = \"contents\";\n contentDOM.className =\n contentClassName ??\n (type === \"delete\"\n ? \"bn-suggestion-node bn-suggestion-node--delete\"\n : \"bn-suggestion-node\");\n if (type === \"delete\") {\n // A deleted block shows a localized \"Deleted\" badge via a `::before`\n // (see Block.css). The badge text is passed down as a CSS string token\n // in `--deleted-label` so the stylesheet stays locale-agnostic; the\n // wrapper is `display: contents` and can't paint a pseudo-element of its\n // own, so the rule renders the badge on the wrapped node instead, which\n // inherits this custom property.\n const label = editor?.dictionary.suggestion_changes.deleted;\n if (label) {\n contentDOM.style.setProperty(\n \"--deleted-label\",\n JSON.stringify(label),\n );\n }\n }\n }\n dom.appendChild(contentDOM);\n\n return {\n dom,\n contentDOM,\n };\n };\n\nexport const YAttributedInsertion = Mark.create<{\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-insert\",\n inclusive: false,\n excludes: \"\",\n // Two groups: `BLOCK_LEVEL_SUGGESTION_GROUP` lets the mark sit on block nodes\n // (see `suggestionMarks`), so a whole block can be marked as inserted in\n // suggestion mode; `NON_FORMATTING_MARK_GROUP` lets it annotate text inside\n // `\"plain\"` blocks (e.g. code blocks) — see `nonFormattingMarks`.\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"insert\", {\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\nexport const YAttributedDeletion = Mark.create<{\n editor?: BlockNoteEditor<any, any, any>;\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-delete\",\n inclusive: false,\n excludes: \"\",\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"delete\", {\n editor: this.options.editor,\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\nexport const YAttributedFormat = Mark.create<{\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n}>({\n name: \"y-attributed-format\",\n inclusive: false,\n excludes: \"\",\n group: `${BLOCK_LEVEL_SUGGESTION_GROUP} ${NON_FORMATTING_MARK_GROUP}`,\n addAttributes() {\n return {\n userIds: { default: null },\n format: { default: null },\n };\n },\n addMarkView() {\n return createAttributionMarkView(\"modification\", {\n getAttributionMarkClassName: this.options.getAttributionMarkClassName,\n });\n },\n extendMarkSchema(extension) {\n if (extension.name !== this.name) {\n return {};\n }\n return {\n blocknoteIgnore: true,\n } satisfies MarkSpec;\n },\n});\n\n/**\n * Bundles the three `y-attributed-*` suggestion marks into a single BlockNote\n * extension, so they can be registered wherever they're actually needed (the\n * Yjs collaboration extension, or a test that exercises suggestions) instead of\n * living in the default schema. The marks opt into being allowed on block nodes\n * via their `blockLevelSuggestion` option — see `suggestionMarks`.\n */\nexport const YAttributionMarksExtension = createExtension(\n ({\n options,\n }: ExtensionOptions<\n { getAttributionMarkClassName?: GetAttributionMarkClassName } | undefined\n >) => ({\n key: \"yAttributionMarks\",\n tiptapExtensions: [\n YAttributedInsertion.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n YAttributedDeletion.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n YAttributedFormat.configure({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n }),\n);\n","import { getChangedRanges } from \"@tiptap/core\";\nimport { Plugin, PluginKey, type Transaction } from \"prosemirror-state\";\nimport {\n createExtension,\n createStore,\n type ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n colorsForUserIds,\n userColorVarNames,\n normalizeToUserStore,\n type UserStoreOrResolver,\n} from \"../../user/index.js\";\nimport {\n resolveAttributionMarkClassName,\n YAttributionMarksExtension,\n type GetAttributionMarkClassName,\n} from \"./YAttributionMarks.js\";\n\n/** The attribution marks, mapped to their `modificationType`. */\nconst ATTRIBUTION_MARK_TYPES = {\n \"y-attributed-insert\": \"insert\",\n \"y-attributed-delete\": \"delete\",\n \"y-attributed-format\": \"format\",\n} as const;\n\nconst ATTRIBUTION_LOAD_PLUGIN_KEY = new PluginKey(\"attributionLoadUsers\");\n\n/** Wrapper of an attribution mark; carries its author(s) in `data-user-ids`. */\nconst ATTRIBUTION_MARK_SELECTOR = \"[data-user-ids]\";\n\n/** Parse the JSON-encoded `data-user-ids` attribute; `[]` if missing/malformed. */\nconst parseUserIds = (userIdsJSON: string | undefined): string[] => {\n if (!userIdsJSON) {\n return [];\n }\n let userIds: unknown;\n try {\n userIds = JSON.parse(userIdsJSON);\n } catch {\n return [];\n }\n return Array.isArray(userIds) ? userIds.map(String) : [];\n};\n\n/**\n * The changed format keys from a modification mark's `data-format` (e.g.\n * `[\"bold\", \"italic\"]`); `[]` if missing/malformed or an empty change. This is\n * the raw change context — turning it into a localized label (e.g.\n * `\"Bold, Italic\"`) is a view concern owned by the React layer's\n * `formatChangeLabel`, so core stays i18n-agnostic here.\n */\nconst parseFormatKeys = (formatJSON: string | undefined): string[] => {\n if (!formatJSON) {\n return [];\n }\n let format: unknown;\n try {\n format = JSON.parse(formatJSON);\n } catch {\n return [];\n }\n if (typeof format !== \"object\" || format === null) {\n return [];\n }\n return Object.keys(format);\n};\n\n/**\n * The element with a real box to anchor the tooltip to. The wrapper is\n * `display: contents` (no box of its own), so use its content span child,\n * falling back further for block marks.\n */\nconst getReferenceElement = (wrapper: Element): Element => {\n const content = wrapper.firstElementChild ?? wrapper;\n const rect = content.getBoundingClientRect();\n if (rect.width || rect.height) {\n return content;\n }\n return content.firstElementChild ?? content;\n};\n\n/**\n * The box the tooltip anchors to. The wrapper is `display: contents` (no box of\n * its own), so use its content span child, falling back further for block marks.\n * Exported for the React controller's floating-ui `getBoundingClientRect`.\n */\nexport const getReferenceRect = (wrapper: Element): DOMRect =>\n getReferenceElement(wrapper).getBoundingClientRect();\n\n/**\n * The per-line client rects of the reference element, for floating-ui's\n * `inline()` middleware — it needs one rect per line to position off a\n * multi-line mark, and virtual elements don't get a default `getClientRects`.\n */\nexport const getReferenceClientRects = (wrapper: Element): DOMRectList =>\n getReferenceElement(wrapper).getClientRects();\n\n/**\n * State for the currently-hovered suggestion mark's tooltip (`undefined` when\n * none). The extension computes it; a React controller renders + positions it\n * (see `AttributionTooltipController`).\n */\nexport type AttributionTooltipState = {\n /** The wrapper element the tooltip anchors to (floating-ui reference). */\n anchor: HTMLElement;\n /** Per-user background color, resolved from the user store (default path). */\n color: string;\n /** The kind of change — `format` is the modification mark. */\n modificationType: \"insert\" | \"delete\" | \"format\";\n /** Whether the mark wraps inline content or a whole block. */\n contentType: \"inline-content\" | \"block\";\n /** Resolved usernames (falls back to raw ids), for custom renderers. */\n users: string[];\n /**\n * The changed format keys (e.g. `[\"bold\", \"italic\"]`), present only for\n * `format` marks. This is the raw change context — the view layer turns it\n * into a localized label via its `formatChangeLabel`.\n */\n format?: string[];\n /**\n * Class name from the `getAttributionMarkClassName` callback (override path).\n * When present, the tooltip applies this and skips the inline `color`.\n */\n className?: string;\n};\n\n/**\n * Resolves the attribution tooltip state for suggestion marks on hover (exposed\n * via a store for React), and loads each mark's author so its color/username\n * resolves. Marks nest, so a single delegated `mouseover` listener picks the\n * `closest` wrapper to the pointer — the innermost mark wins.\n */\nexport const AttributionExtension = createExtension(\n ({\n options,\n }: ExtensionOptions<\n | {\n /** Resolves authors to usernames. Optional; unresolved ids show raw. */\n resolveUsers?: UserStoreOrResolver;\n /** See {@link GetAttributionMarkClassName}. */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n }\n | undefined\n >) => {\n const userStore = normalizeToUserStore(options?.resolveUsers);\n const getAttributionMarkClassName = options?.getAttributionMarkClassName;\n\n const store = createStore<AttributionTooltipState | undefined>(undefined);\n\n // Load the authors of the attribution marks in `tr`'s changed ranges, so\n // their colors/usernames resolve (colors then flow to marks via `syncRootVars`).\n // `getChangedRanges` covers mark-only steps too — which suggestion mode adds\n // over existing text and `tr.changedRange()` would miss.\n const loadChangedUsers = (tr: Transaction) => {\n const ranges = getChangedRanges(tr);\n if (ranges.length === 0) {\n return;\n }\n // Most changes are local (often several steps in one small span), so scan a\n // single range spanning all of them rather than each range individually.\n let from = Infinity;\n let to = -Infinity;\n for (const { newRange } of ranges) {\n from = Math.min(from, newRange.from);\n to = Math.max(to, newRange.to);\n }\n\n const ids = new Set<string>();\n tr.doc.nodesBetween(from, to, (node) => {\n for (const mark of node.marks) {\n if (\n ATTRIBUTION_MARK_TYPES[\n mark.type.name as keyof typeof ATTRIBUTION_MARK_TYPES\n ]\n ) {\n const userIds = mark.attrs[\"userIds\"] as string[] | null;\n userIds?.forEach((id) => ids.add(id));\n }\n }\n return true;\n });\n if (ids.size > 0) {\n void userStore.loadUsers(Array.from(ids));\n }\n };\n\n return {\n key: \"attribution\",\n userStore,\n store,\n prosemirrorPlugins: [\n // Marks arrive in a transaction (suggestion mode, viewing suggestions,\n // version preview), so resolve their authors as the doc changes.\n new Plugin({\n key: ATTRIBUTION_LOAD_PLUGIN_KEY,\n state: {\n init: () => null,\n apply: (tr) => {\n if (tr.docChanged) {\n loadChangedUsers(tr);\n }\n return null;\n },\n },\n }),\n ],\n mount({ dom, root, signal }) {\n // Write each resolved author's color to the editor root as per-user CSS\n // variables (`--user-color-<id>-{light,dark}`) that the mark wrappers read\n // via `var(..., <fallback>)`, so the cascade recolors marks once a color\n // resolves. Color-less users have theirs removed so the fallback applies.\n const syncRootVars = () => {\n for (const [id, user] of userStore.store.state) {\n const { light, dark } = userColorVarNames(id);\n if (user.color && user.colorLight) {\n dom.style.setProperty(light, user.colorLight);\n dom.style.setProperty(dark, user.color);\n } else {\n dom.style.removeProperty(light);\n dom.style.removeProperty(dark);\n }\n }\n };\n\n // The wrapper currently showing a tooltip, so we don't re-emit on every\n // `mouseover` over the same mark.\n let activeAnchor: HTMLElement | undefined;\n\n // The mark's authors as usernames, falling back to the raw id when not\n // cached (`getUser` is cache-only; ids load on hover, see `onPointerOver`).\n const usersLabelArray = (userIdsJSON: string | undefined): string[] =>\n parseUserIds(userIdsJSON).map(\n (id) => userStore.getUser(id)?.username ?? id,\n );\n\n // A stable identity string for a wrapper (empty if unattributed), used to\n // (a) test whether a mark is attributed and (b) group adjacent marks with\n // the *same* attribution under one tooltip. It's an internal grouping key,\n // not the displayed text — that's composed in the view from `users` and\n // the format label — so it's built from raw `data-*` (ids + format keys)\n // and stays free of i18n/username resolution.\n const attributionIdentity = (wrapper: HTMLElement) => {\n const ids = parseUserIds(wrapper.dataset[\"userIds\"]);\n if (ids.length === 0) {\n return \"\";\n }\n const format = parseFormatKeys(wrapper.dataset[\"format\"]);\n return `${format.join(\",\")}:${ids.join(\",\")}`;\n };\n\n // Build the tooltip state from a wrapper's `data-*` attributes.\n const buildState = (anchor: HTMLElement): AttributionTooltipState => {\n const isModification = anchor.dataset[\"format\"] !== undefined;\n const modificationType: AttributionTooltipState[\"modificationType\"] =\n isModification\n ? \"format\"\n : anchor.tagName === \"INS\"\n ? \"insert\"\n : \"delete\";\n const contentType: AttributionTooltipState[\"contentType\"] =\n anchor.dataset[\"inline\"] === \"false\" ? \"block\" : \"inline-content\";\n\n return {\n anchor,\n // The tooltip is portaled outside the editor root, so it can't read\n // the cascaded per-user vars — resolve a concrete color from the store.\n color: colorsForUserIds(\n userStore,\n parseUserIds(anchor.dataset[\"userIds\"]),\n ).dark,\n modificationType,\n contentType,\n users: usersLabelArray(anchor.dataset[\"userIds\"]),\n format: isModification\n ? parseFormatKeys(anchor.dataset[\"format\"])\n : undefined,\n className: resolveAttributionMarkClassName(\n getAttributionMarkClassName?.({ contentType, modificationType }),\n \"tooltip\",\n ),\n };\n };\n\n const hideTooltip = () => {\n if (!activeAnchor) {\n return;\n }\n activeAnchor = undefined;\n store.setState(undefined);\n };\n\n // The innermost attributed mark at or above `el`, skipping unattributed\n // wrappers so an attributed ancestor still wins.\n const innermostAttributed = (\n el: Element | null,\n ): HTMLElement | undefined => {\n while (el) {\n const wrapper = el.closest<HTMLElement>(ATTRIBUTION_MARK_SELECTOR);\n if (!wrapper) {\n return undefined;\n }\n if (attributionIdentity(wrapper)) {\n return wrapper;\n }\n el = wrapper.parentElement;\n }\n return undefined;\n };\n\n const onPointerOver = (event: Event) => {\n const target = event.target instanceof Element ? event.target : null;\n const innermost = innermostAttributed(target);\n if (!innermost) {\n // Not over an attributed mark — drop the current tooltip.\n hideTooltip();\n return;\n }\n\n const identity = attributionIdentity(innermost);\n // Anchor on the outermost ancestor with the *same* attribution so one\n // tooltip covers the whole region; a differently-attributed ancestor\n // breaks the chain, and unattributed ones are climbed past.\n let anchor = innermost;\n let el: Element | null = innermost.parentElement;\n while (el) {\n const ancestor = el.closest<HTMLElement>(ATTRIBUTION_MARK_SELECTOR);\n if (!ancestor) {\n break;\n }\n const ancestorIdentity = attributionIdentity(ancestor);\n if (ancestorIdentity === identity) {\n anchor = ancestor;\n } else if (ancestorIdentity) {\n break;\n }\n el = ancestor.parentElement;\n }\n\n if (activeAnchor === anchor) {\n return;\n }\n\n activeAnchor = anchor;\n store.setState(buildState(anchor));\n\n // First hover renders raw ids (cache-only); load the authors and refresh\n // the resolved usernames once loaded, if this mark is still active.\n const ids = parseUserIds(anchor.dataset[\"userIds\"]);\n if (ids.length > 0) {\n void userStore.loadUsers(ids).then(() => {\n if (activeAnchor !== anchor) {\n return;\n }\n store.setState(buildState(anchor));\n });\n }\n };\n\n root.addEventListener(\"mouseover\", onPointerOver, { signal });\n signal.addEventListener(\"abort\", hideTooltip);\n\n // Seed from the cache, then keep the vars in sync as users resolve.\n syncRootVars();\n const unsubscribe = userStore.store.subscribe(syncRootVars);\n signal.addEventListener(\"abort\", unsubscribe);\n },\n\n // The `y-attributed-*` marks aren't in the default schema — register them\n // here so the block specs can allow them (collaboration-only).\n blockNoteExtensions: [\n YAttributionMarksExtension({\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n };\n },\n);\n","import { relativePositionStore, ySyncPluginKey } from \"@y/prosemirror\";\nimport { createExtension } from \"../../editor/BlockNoteExtension.js\";\n\nexport const RelativePositionMappingExtension = createExtension(\n ({ editor }) => {\n return {\n key: \"yPositionMapping\",\n mapPosition: (position: number, side: \"left\" | \"right\" = \"left\") => {\n const ySyncPluginState = ySyncPluginKey.getState(\n editor.prosemirrorState,\n );\n if (!ySyncPluginState?.ytype) {\n throw new Error(\"YSync plugin state not found\");\n }\n\n // 0 is a special case & always should map to itself\n if (position === 0) {\n return () => 0;\n }\n\n const posStore = relativePositionStore(\n editor.prosemirrorState.doc.resolve(\n position + (side === \"right\" ? 1 : -1),\n ),\n ySyncPluginState.ytype,\n ySyncPluginState.renderer,\n );\n\n return () => {\n const curYSyncPluginState = ySyncPluginKey.getState(\n editor.prosemirrorState,\n ) as typeof ySyncPluginState;\n const pos = posStore(\n editor.prosemirrorState.doc,\n curYSyncPluginState.ytype,\n curYSyncPluginState.renderer,\n );\n\n // This can happen if the element is garbage collected\n if (pos === null) {\n throw new Error(\"Position not found, cannot track positions\");\n }\n\n return pos + (side === \"right\" ? -1 : 1);\n };\n },\n } as const;\n },\n);\n","import { $prosemirrorDelta } from \"@y/prosemirror\";\nimport * as delta from \"lib0/delta\";\nimport * as schema from \"lib0/schema\";\n\n/**\n * Canonical name of a content delta's first block child (the child carried by an\n * insert op), or `null`. For a BlockNote `blockContainer` (content\n * `blockContent blockGroup?`) this is its block-content type (paragraph,\n * heading, image, ...).\n */\nconst firstChild = (\n d: schema.Unwrap<typeof $prosemirrorDelta>,\n): schema.Unwrap<typeof $prosemirrorDelta> | null => {\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const it of op.insert) {\n if (delta.$deltaAny.check(it)) {\n return it;\n }\n }\n }\n }\n return null;\n};\n\n/**\n * Whether a `blockContainer` delta carries a child `blockGroup` — i.e. the block\n * has nested children. A container's content is `blockContent blockGroup?`, so\n * this is what tells a leaf block apart from a parent.\n */\nconst hasBlockGroup = (d: schema.Unwrap<typeof $prosemirrorDelta>): boolean => {\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const it of op.insert) {\n if (delta.$deltaAny.check(it) && it.name === \"blockGroup\") {\n return true;\n }\n }\n }\n }\n return false;\n};\n\nfunction getTableDimensions(\n d: schema.Unwrap<typeof $prosemirrorDelta>,\n): { rows: number; cols: number } | null {\n if (d.name !== \"table\") {\n return null;\n }\n\n // Collect all rows with their cells' colspan/rowspan values.\n const rows: Array<Array<{ colspan: number; rowspan: number }>> = [];\n for (const op of (d as any).children) {\n if (delta.$insertOp.check(op)) {\n for (const tr of op.insert as Array<\n schema.Unwrap<typeof $prosemirrorDelta>\n >) {\n if (tr.name !== \"tableRow\") {\n return null;\n }\n const cells: Array<{ colspan: number; rowspan: number }> = [];\n for (const trOp of (tr as any).children) {\n if (delta.$insertOp.check(trOp)) {\n for (const td of trOp.insert as Array<\n schema.Unwrap<typeof $prosemirrorDelta>\n >) {\n if (td.name !== \"tableCell\" && td.name !== \"tableHeader\") {\n return null;\n }\n cells.push({\n colspan: Number(td.attrs.colspan) || 1,\n rowspan: Number(td.attrs.rowspan) || 1,\n });\n }\n }\n }\n rows.push(cells);\n }\n }\n }\n\n if (rows.length === 0) {\n return null;\n }\n\n // Build an occupancy grid to determine the true column count.\n // Each entry in `grid[r]` tracks which columns are already occupied\n // (by a cell from a previous row with rowspan > 1).\n const grid: boolean[][] = [];\n for (let r = 0; r < rows.length; r++) {\n if (!grid[r]) {\n grid[r] = [];\n }\n let col = 0;\n for (const cell of rows[r]) {\n // Skip columns already occupied by a rowspan from above.\n while (grid[r][col]) {\n col++;\n }\n // Mark all slots this cell occupies.\n for (let dr = 0; dr < cell.rowspan; dr++) {\n if (!grid[r + dr]) {\n grid[r + dr] = [];\n }\n for (let dc = 0; dc < cell.colspan; dc++) {\n grid[r + dr][col + dc] = true;\n }\n }\n col += cell.colspan;\n }\n }\n\n const numCols = Math.max(...grid.map((row) => row.length));\n return { rows: rows.length, cols: numCols };\n}\n\n/**\n * BlockNote's node-pairing policy for y-prosemirror's `matchNodes` option\n * (forwarded to `lib0/delta.diff`). This is the schema-specific bit that lives\n * in userland - the binding itself stays schema-agnostic.\n *\n * A `blockContainer` holds exactly one block content (`blockContent\n * blockGroup?`). Diffing a *type change* of that content as an in-place child\n * delete+insert would, under a suggestion, tombstone the old content next to the\n * new one => two block-contents in one container => schema-invalid. So we\n * declare a container's identity to be its first block-content child's type:\n * when that changes, the two containers are reported as *different*, the PM->Y\n * diff replaces the whole container, and the deleted + inserted containers sit\n * as siblings in the blockGroup (`blockGroupChild+` allows that). Each carries\n * the `y-attributed-*` node mark - which `blockContainer` already whitelists -\n * so no schema change and no storage transform are needed. A plain text edit\n * keeps the same first-child type => same identity => the diff descends and\n * merges as usual.\n *\n * @param a removed (old) node\n * @param b inserted (new) node\n * @returns whether `a` and `b` are the same node (diff in place) vs different (replace)\n */\nexport const blockMatchNodes = (\n a: schema.Unwrap<typeof $prosemirrorDelta>,\n b: schema.Unwrap<typeof $prosemirrorDelta>,\n): boolean => {\n if (a.name !== b.name) {\n return false;\n }\n\n if (a.name !== \"blockContainer\") {\n return true;\n }\n\n const childA = firstChild(a);\n const childB = firstChild(b);\n\n if (childA?.name !== childB?.name) {\n return false;\n }\n\n // A change in nesting is structural too: if one container gains or loses a\n // child `blockGroup`, diffing it in place would insert/delete the blockGroup as\n // a sibling of the block content inside a single container — schema-invalid.\n // Treat it as different so the whole container is replaced instead, same as a\n // content-type change. Keeps concurrent nesting merges (e.g. two users nesting\n // a block under the same parent) from producing a lopsided in-place result.\n if (hasBlockGroup(a) !== hasBlockGroup(b)) {\n return false;\n }\n\n if (childA?.name === \"table\" && childB?.name === \"table\") {\n const dimA = getTableDimensions(childA);\n const dimB = getTableDimensions(childB);\n if (\n dimA !== null &&\n dimB !== null &&\n dimA.rows !== dimB.rows &&\n dimA.cols !== dimB.cols\n ) {\n return false;\n }\n }\n\n return true;\n};\n","import { configureYProsemirror, syncPlugin } from \"@y/prosemirror\";\nimport {\n type ExtensionOptions,\n createExtension,\n} from \"../../editor/BlockNoteExtension.js\";\nimport { blockMatchNodes } from \"./blockMatchNodes.js\";\nimport { CollaborationOptions } from \"./index.js\";\n\n/**\n * Maps a Y attribution to BlockNote's `y-attributed-*` mark attrs.\n *\n * The mapper must be deterministic in `(format, attribution)` and emit attrs\n * that exactly match the declared mark schema in YAttributionMarks.ts. Any\n * mismatch causes the sync plugin to fire phantom reconcile dispatches in a\n * loop. See ATTRIBUTION.md in @y/prosemirror.\n *\n * Crucially the marks carry only stable identity (`userIds`, plus `format` for\n * the modification mark) — *not* user colors. Colors resolve asynchronously\n * from the {@link UserStore}, so baking them in here would make the mapper's\n * output change under a fixed `(format, attribution)` once a user loads, which\n * is exactly the non-determinism that triggers the reconcile loop. Instead the\n * `AttributionExtension` applies colors as a decoration layer that can\n * update independently of the mark representation.\n */\nexport const mapAttributionToMark = (\n format: Record<string, unknown> | null,\n attribution: {\n insert?: readonly string[];\n delete?: readonly string[];\n format?: Record<string, readonly string[]>;\n insertAt?: number;\n deleteAt?: number;\n formatAt?: number;\n },\n): Record<string, unknown> => {\n const out: Record<string, unknown> = { ...format };\n\n if (attribution.insert) {\n out[\"y-attributed-insert\"] = { userIds: attribution.insert };\n }\n\n if (attribution.delete) {\n out[\"y-attributed-delete\"] = { userIds: attribution.delete };\n }\n\n if (attribution.format) {\n const userIds = [...new Set(Object.values(attribution.format).flat())];\n out[\"y-attributed-format\"] = { userIds, format: attribution.format };\n }\n\n return out;\n};\n\nexport const YSyncExtension = createExtension(\n ({\n options,\n editor,\n }: ExtensionOptions<\n Pick<\n CollaborationOptions,\n | \"fragment\"\n | \"renderer\"\n | \"suggestionDoc\"\n | \"provider\"\n | \"getAttributionMarkClassName\"\n >\n >) => {\n return {\n key: \"ySync\",\n fragment: options.fragment,\n mount: () => {\n const configure = () => {\n editor.exec(\n configureYProsemirror({\n ytype: options.fragment,\n // purposefully not passing `renderer` here, this is only for syncing the main doc, not switching to suggestion mode\n // In the future, we may want view suggestion mode to be the default, and then we can decide how to indicate that through the options.\n // For now though, we are leaving suggestion mode as experimental and must be explicitly enabled through the SuggestionsExtension.\n }),\n );\n };\n\n if (\n options.provider &&\n \"synced\" in options.provider &&\n typeof options.provider.synced === \"boolean\"\n ) {\n if (options.provider[\"synced\"]) {\n configure();\n } else if (\n \"on\" in options.provider &&\n typeof options.provider.on === \"function\"\n ) {\n options.provider.on(\"synced\", (synced: boolean) => {\n if (synced) {\n configure();\n }\n });\n } else {\n throw new Error(\n \"YSyncExtension: provider must have a 'synced' boolean or an 'on' method to listen for 'sync'\",\n );\n }\n } else {\n configure();\n }\n },\n prosemirrorPlugins: [\n syncPlugin({\n suggestionDoc: options.suggestionDoc,\n mapAttributionToMark,\n // Node-pairing policy for the PM->Y diff: a `blockContainer` whose\n // block-content type changes is treated as a *different* node, so the\n // diff replaces the whole container (deleted + inserted siblings in\n // the blockGroup) instead of producing two block-contents in one\n // container => schema-invalid. No schema change / storage transform\n // needed; `blockContainer` already whitelists the `y-attributed-*`\n // marks. See blockMatchNodes.ts.\n customCompare: blockMatchNodes,\n }),\n ],\n runsBefore: [\"default\"],\n } as const;\n },\n);\n","import {\n deltaAttributionToFormat,\n deltaToPNode,\n deltaToPSteps,\n docToDelta,\n nodeToDelta,\n pmToFragment,\n} from \"@y/prosemirror\";\nimport * as d from \"lib0/delta\";\nimport { Node } from \"prosemirror-model\";\nimport { Transaction } from \"prosemirror-state\";\nimport {\n type Block,\n type BlockNoteEditor,\n type BlockSchema,\n type InlineContentSchema,\n type PartialBlock,\n type StyleSchema,\n blockToNode,\n docToBlocks,\n} from \"../index.js\";\nimport { blockMatchNodes } from \"./extensions/blockMatchNodes.js\";\nimport { mapAttributionToMark } from \"./extensions/YSync.js\";\n\nimport * as Y from \"@y/y\";\n\n/**\n * Find the equivalent of a Y.Type in another Y.Doc.\n *\n * For root types this looks up the matching shared key; for sub-types it\n * locates the item by its client/clock ID in the target doc's store.\n */\nexport function findTypeInOtherYdoc<T extends Y.Type<any>>(\n ytype: T,\n otherYdoc: Y.Doc,\n): T {\n const ydoc = ytype.doc;\n if (!ydoc) {\n throw new Error(\"type does not have a ydoc\");\n }\n if (ytype._item === null) {\n /**\n * If is a root type, we need to find the root key in the original ydoc\n * and use it to get the type in the other ydoc.\n */\n const rootKey = Array.from(ydoc.share.keys()).find(\n (key) => ydoc.share.get(key) === ytype,\n );\n if (rootKey == null) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n return otherYdoc.get(rootKey as string, ytype.constructor as any) as T;\n } else {\n /**\n * If it is a sub type, we use the item id to find the history type.\n */\n const ytypeItem = ytype._item;\n const otherStructs = otherYdoc.store.clients.get(ytypeItem.id.client) ?? [];\n const itemIndex = Y.findIndexSS(otherStructs, ytypeItem.id.clock);\n const otherItem = otherStructs[itemIndex] as Y.Item | undefined;\n if (!otherItem) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n const otherContent = otherItem.content as Y.ContentType | undefined;\n if (!otherContent) {\n throw new Error(\"type does not exist in other ydoc\");\n }\n return otherContent.type as T;\n }\n}\n\n/**\n * Turn Prosemirror JSON to BlockNote style JSON\n * @param editor BlockNote editor\n * @param json Prosemirror JSON\n * @returns BlockNote style JSON\n */\nexport function _prosemirrorJSONToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(editor: BlockNoteEditor<BSchema, ISchema, SSchema>, json: any) {\n // note: theoretically this should also be possible without creating prosemirror nodes,\n // but this is definitely the easiest way\n const doc = editor.pmSchema.nodeFromJSON(json);\n return docToBlocks<BSchema, ISchema, SSchema>(doc);\n}\n\n/**\n * Turn BlockNote JSON to Prosemirror node / state\n * @param editor BlockNote editor\n * @param blocks BlockNote blocks\n * @returns Prosemirror root node\n */\nexport function _blocksToProsemirrorNode<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: PartialBlock<BSchema, ISchema, SSchema>[],\n) {\n const pmNodes = blocks.map((b) => blockToNode(b, editor.pmSchema));\n\n const doc = editor.pmSchema.topNodeType.create(\n null,\n editor.pmSchema.nodes[\"blockGroup\"].create(null, pmNodes),\n );\n return doc;\n}\n\n/** YJS / BLOCKNOTE conversions */\n\n/**\n * Turn a Y.Type collaborative doc into a BlockNote document (BlockNote style JSON of all blocks)\n * @param editor BlockNote editor\n * @param fragment Y.Type\n * @returns BlockNote document (BlockNote style JSON of all blocks)\n */\nexport function yfragmentToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(editor: BlockNoteEditor<BSchema, ISchema, SSchema>, fragment: Y.Type) {\n const pmNode = deltaToPNode(fragment.toDeltaDeep(), editor.pmSchema, null);\n if (pmNode === null) {\n return [];\n }\n return docToBlocks<BSchema, ISchema, SSchema>(pmNode);\n}\n\n/**\n * Convert blocks to a Y.Type\n *\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param editor BlockNote editor\n * @param blocks the blocks to convert\n * @param fragment XML fragment name\n * @returns Y.Type\n */\nexport function blocksToYType<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: Block<BSchema, ISchema, SSchema>[],\n fragment?: Y.Type,\n) {\n if (!fragment) {\n fragment = new Y.Doc().get(\"prosemirror\");\n }\n return pmToFragment(_blocksToProsemirrorNode(editor, blocks), fragment);\n}\n\n/**\n * Turn a Y.Doc collaborative doc into a BlockNote document (BlockNote style JSON of all blocks)\n * @param editor BlockNote editor\n * @param ydoc Y.Doc\n * @param fragment XML fragment name\n * @returns BlockNote document (BlockNote style JSON of all blocks)\n */\nexport function yDocToBlocks<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n ydoc: Y.Doc,\n fragment = \"prosemirror\",\n) {\n return yfragmentToBlocks(editor, ydoc.get(fragment));\n}\n\n/**\n * This can be used when importing existing content to Y.Doc for the first time,\n * note that this should not be used to rehydrate a Y.Doc from a database once\n * collaboration has begun as all history will be lost\n *\n * @param editor BlockNote editor\n * @param blocks the blocks to convert\n * @param fragment XML fragment name\n */\nexport function blocksToYDoc<\n BSchema extends BlockSchema,\n ISchema extends InlineContentSchema,\n SSchema extends StyleSchema,\n>(\n editor: BlockNoteEditor<BSchema, ISchema, SSchema>,\n blocks: PartialBlock<BSchema, ISchema, SSchema>[],\n fragment = \"prosemirror\",\n) {\n const delta = docToDelta(_blocksToProsemirrorNode(editor, blocks));\n const doc = new Y.Doc();\n doc.get(fragment).applyDelta(delta);\n return doc;\n}\n\n/**\n * Diff two ProseMirror documents into a delta, using BlockNote's node-pairing\n * policy ({@link blockMatchNodes}) so a block's content-type change is reported\n * as a replace rather than a schema-invalid in-place edit.\n */\nexport function docDiffToDelta(previousDoc: Node, newDoc: Node) {\n const initialDelta = nodeToDelta(previousDoc);\n const finalDelta = nodeToDelta(newDoc);\n return d.diff(initialDelta.done(), finalDelta.done(), {\n compare: blockMatchNodes,\n });\n}\n\n/**\n * Build a ProseMirror transaction that turns `tr.doc` into the content of a\n * Y.Type `fragment`, applying the `renderer`'s authorship as\n * `y-attributed-*` marks. Used to render a (read-only) diff of a snapshot / a\n * version comparison into the editor.\n */\nexport function getProseMirrorTrFromYFragment({\n tr,\n fragment,\n renderer,\n}: {\n tr: Transaction;\n fragment: Y.Type;\n renderer?: Y.AbstractRenderer | null;\n}): Transaction {\n const ycontent = deltaAttributionToFormat(\n fragment.toDeltaDeep({ renderer }),\n mapAttributionToMark,\n );\n // @todo it is preferred to apply the minimal diff - at least for debugging purposes. the\n // document replacal is more reliable though\n\n const pcontent = nodeToDelta(tr.doc, undefined, true);\n const diff = d.diff(pcontent.done(), ycontent.done(), {\n compare: blockMatchNodes,\n });\n return deltaToPSteps(tr, diff, undefined, undefined);\n}\n","import { getMarkRange, posToDOMRect } from \"@tiptap/core\";\n\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n acceptChanges,\n rejectAllChanges,\n rejectChanges,\n configureYProsemirror,\n acceptAllChanges,\n} from \"@y/prosemirror\";\nimport { CollaborationOptions } from \"./index.js\";\nimport { findTypeInOtherYdoc } from \"../utils.js\";\nimport { normalizeToUserStore } from \"../../user/index.js\";\n\nexport const SuggestionsExtension = createExtension(\n ({ editor, options }: ExtensionOptions<CollaborationOptions>) => {\n const suggestionDoc = options.suggestionDoc;\n if (!suggestionDoc) {\n throw new Error(\"Suggestion doc not found\");\n }\n // Shared with the rest of collaboration; the collaboration extension passes\n // an already-built store here.\n const userStore = normalizeToUserStore(options.resolveUsers);\n\n function getSuggestionElementAtPos(pos: number) {\n let currentNode = editor.prosemirrorView.nodeDOM(pos);\n while (currentNode && currentNode.parentElement) {\n if (currentNode.nodeName === \"INS\" || currentNode.nodeName === \"DEL\") {\n return currentNode as HTMLElement;\n }\n currentNode = currentNode.parentElement;\n }\n return null;\n }\n\n function getMarkAtPos(pos: number, markType: string) {\n return editor.transact((tr) => {\n const resolvedPos = tr.doc.resolve(pos);\n const mark = resolvedPos\n .marks()\n .find((mark) => mark.type.name === markType);\n\n if (!mark) {\n return;\n }\n\n const markRange = getMarkRange(resolvedPos, mark.type);\n if (!markRange) {\n return;\n }\n\n return {\n range: markRange,\n mark,\n get text() {\n return tr.doc.textBetween(markRange.from, markRange.to);\n },\n get position() {\n // to minimize re-renders, we convert to JSON, which is the same shape anyway\n return posToDOMRect(\n editor.prosemirrorView,\n markRange.from,\n markRange.to,\n ).toJSON() as DOMRect;\n },\n };\n });\n }\n\n function getSuggestionAtSelection() {\n return editor.transact((tr) => {\n const selection = tr.selection;\n if (!selection.empty) {\n return undefined;\n }\n return (\n getMarkAtPos(selection.anchor, \"insertion\") ||\n getMarkAtPos(selection.anchor, \"deletion\") ||\n getMarkAtPos(selection.anchor, \"modification\")\n );\n });\n }\n\n return {\n key: \"suggestions\",\n userStore,\n runsBefore: [\"ySync\"],\n viewSuggestions: () => {\n if (options.renderer) {\n options.renderer.suggestionMode = false;\n }\n editor.exec(\n configureYProsemirror({\n ytype: findTypeInOtherYdoc(options.fragment, suggestionDoc),\n renderer: options.renderer,\n }),\n );\n },\n enableSuggestions: () => {\n if (options.renderer) {\n options.renderer.suggestionMode = true;\n }\n editor.exec(\n configureYProsemirror({\n ytype: findTypeInOtherYdoc(options.fragment, suggestionDoc),\n renderer: options.renderer,\n }),\n );\n },\n disableSuggestions: () => {\n editor.exec(\n configureYProsemirror({\n ytype: options.fragment,\n renderer: null,\n }),\n );\n },\n applyAllSuggestions: () => {\n return editor.exec(acceptAllChanges());\n },\n applySuggestion: (start: number, end?: number) => {\n return editor.exec(acceptChanges(start, end));\n },\n revertSuggestion: (start: number, end?: number) => {\n return editor.exec(rejectChanges(start, end));\n },\n revertAllSuggestions: () => {\n return editor.exec(rejectAllChanges());\n },\n\n getSuggestionElementAtPos,\n getMarkAtPos,\n getSuggestionAtSelection,\n getSuggestionAtCoords: (coords: { left: number; top: number }) => {\n return editor.transact(() => {\n const posAtCoords = editor.prosemirrorView.posAtCoords(coords);\n if (posAtCoords === null || posAtCoords?.inside === -1) {\n return undefined;\n }\n\n return (\n getMarkAtPos(posAtCoords.pos, \"y-attributed-insert\") ||\n getMarkAtPos(posAtCoords.pos, \"y-attributed-delete\") ||\n getMarkAtPos(posAtCoords.pos, \"y-attributed-format\")\n );\n });\n },\n checkUnresolvedSuggestions: () => {\n let hasUnresolvedSuggestions = false;\n\n editor.prosemirrorState.doc.descendants((node) => {\n if (hasUnresolvedSuggestions) {\n return false;\n }\n\n hasUnresolvedSuggestions =\n node.marks.findIndex(\n (mark) =>\n mark.type.name === \"y-attributed-insert\" ||\n mark.type.name === \"y-attributed-delete\" ||\n mark.type.name === \"y-attributed-format\",\n ) !== -1;\n\n return true;\n });\n\n return hasUnresolvedSuggestions;\n },\n } as const;\n },\n);\n","import { configureYProsemirror, pauseSync } from \"@y/prosemirror\";\nimport * as Y from \"@y/y\";\n\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport type { PreviewController } from \"../../extensions/Versioning/index.js\";\nimport {\n findTypeInOtherYdoc,\n getProseMirrorTrFromYFragment,\n} from \"../utils.js\";\n\n/**\n * Empties the document before a {@link configureYProsemirror} refill so\n * ProseMirror rebuilds every node view instead of reusing a stale-positioned one\n * (BlockNote node views resolve their block eagerly via `getPos()` and throw on\n * a moved node). Sync is paused first so the clear never reaches the Y.Doc.\n *\n * TODO: remove once `configureYProsemirror` applies a minimal diff.\n */\nfunction clearDocumentForConfigure(editor: BlockNoteEditor<any, any, any>) {\n // Pause sync (ytype -> null) so the deletion below stays local.\n editor.exec(pauseSync);\n editor.removeBlocks(editor.document);\n}\n\n/**\n * Creates a Yjs-specific adapter that provides the {@link PreviewController}\n * and `getCurrentDocument` callback required by the base\n * {@link VersioningExtension}.\n *\n * This is wired automatically by the {@link CollaborationExtension} when\n * `versioningEndpoints` is provided. You only need to call this directly if\n * you're using the `VersioningExtension` outside of the collaboration wrapper.\n */\nexport function createYjsVersioningAdapter(\n editor: BlockNoteEditor<any, any, any>,\n fragment: Y.Type,\n): {\n preview: PreviewController<Uint8Array, Y.ContentMap>;\n getCurrentDocument: () => Y.Type;\n serializeCurrentContent: () => Uint8Array;\n} {\n return {\n getCurrentDocument: () => fragment,\n // Serialise the live document as a V2 update — the same format that\n // `getContent` returns (via `convertUpdateFormatV1ToV2`) and that\n // `enterPreview` consumes (`applyUpdateV2`). Used to render a read-only\n // diff of the live document against a snapshot.\n serializeCurrentContent: () => Y.encodeStateAsUpdateV2(fragment.doc!),\n preview: {\n enterPreview: (\n snapshotContent: Uint8Array,\n compareToContent?: Uint8Array,\n attributions?: Y.ContentMap,\n ) => {\n let prevSnapshot: { fragment: Y.Type } | undefined;\n if (compareToContent) {\n const compareToDoc = new Y.Doc({ isSuggestionDoc: true });\n Y.applyUpdateV2(compareToDoc, compareToContent);\n prevSnapshot = {\n fragment: findTypeInOtherYdoc(fragment, compareToDoc),\n };\n }\n\n const doc = new Y.Doc();\n Y.applyUpdateV2(doc, snapshotContent);\n // Empty the document before reconfiguring so ProseMirror rebuilds node\n // views from scratch instead of reusing stale-positioned ones. See\n // clearDocumentForConfigure.\n clearDocumentForConfigure(editor);\n\n editor.exec((state, dispatch) => {\n const tr = getProseMirrorTrFromYFragment({\n tr: state.tr,\n fragment: findTypeInOtherYdoc(fragment, doc),\n // Pass the optional content map as `attrs` so the diff renderer\n // knows who/when authored each change. Without it, the renderer\n // only produces \"what changed\" (empty userIds, null timestamps) and\n // downstream mark tooltips show \"unknown / unknown time\".\n renderer: prevSnapshot\n ? Y.createDiffRenderer(\n prevSnapshot.fragment.doc!,\n doc,\n attributions ? { attrs: attributions } : undefined,\n )\n : undefined,\n });\n if (dispatch) {\n dispatch(tr);\n }\n return true;\n });\n },\n exitPreview: () => {\n // Empty the document before reconfiguring so ProseMirror rebuilds node\n // views from scratch instead of reusing stale-positioned ones. See\n // clearDocumentForConfigure.\n clearDocumentForConfigure(editor);\n editor.exec(configureYProsemirror({ ytype: fragment }));\n },\n applyRestore: (_snapshotContent: Uint8Array) => {\n // For Yjs-backed versioning, restoration happens on the server (e.g.\n // YHub's `/rollback` endpoint) which publishes a reverting update to\n // the document's room. That update propagates back to this client over\n // the live sync connection and updates `fragment` automatically, so\n // there is nothing to apply locally — we only need to leave preview\n // mode. `exitPreview` is already called by the base extension before\n // this runs, so this is a no-op.\n //\n // Note: this assumes `endpoints.restore` performs the server-side\n // restore. The default in-memory adapter has no server, which is why\n // this is specific to the Yjs collaboration setup.\n },\n },\n };\n}\n","import { defaultSelectionBuilder, yCursorPlugin } from \"@y/prosemirror\";\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport { CollaborationOptions } from \"./index.js\";\n\nexport type CollaborationUser = {\n id?: string;\n name: string;\n color: string;\n [key: string]: unknown;\n};\n\n/**\n * Determine whether the foreground color should be white or black based on a provided background color\n * Inspired by: https://stackoverflow.com/a/3943023\n */\nfunction isDarkColor(bgColor: string): boolean {\n const color = bgColor.charAt(0) === \"#\" ? bgColor.substring(1, 7) : bgColor;\n const r = parseInt(color.substring(0, 2), 16); // hexToR\n const g = parseInt(color.substring(2, 4), 16); // hexToG\n const b = parseInt(color.substring(4, 6), 16); // hexToB\n const uicolors = [r / 255, g / 255, b / 255];\n const c = uicolors.map((col) => {\n if (col <= 0.03928) {\n return col / 12.92;\n }\n return Math.pow((col + 0.055) / 1.055, 2.4);\n });\n const L = 0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2];\n return L <= 0.179;\n}\n\nfunction defaultCursorRender(user: CollaborationUser) {\n const cursorElement = document.createElement(\"span\");\n\n cursorElement.classList.add(\"bn-collaboration-cursor__base\");\n\n const caretElement = document.createElement(\"span\");\n caretElement.setAttribute(\"contentedEditable\", \"false\");\n caretElement.classList.add(\"bn-collaboration-cursor__caret\");\n caretElement.setAttribute(\n \"style\",\n `background-color: ${user.color}; color: ${\n isDarkColor(user.color) ? \"white\" : \"black\"\n }`,\n );\n\n const labelElement = document.createElement(\"span\");\n\n labelElement.classList.add(\"bn-collaboration-cursor__label\");\n labelElement.setAttribute(\n \"style\",\n `background-color: ${user.color}; color: ${\n isDarkColor(user.color) ? \"white\" : \"black\"\n }`,\n );\n labelElement.insertBefore(document.createTextNode(user.name), null);\n\n caretElement.insertBefore(labelElement, null);\n\n cursorElement.insertBefore(document.createTextNode(\"\\u2060\"), null); // Non-breaking space\n cursorElement.insertBefore(caretElement, null);\n cursorElement.insertBefore(document.createTextNode(\"\\u2060\"), null); // Non-breaking space\n\n return cursorElement;\n}\n\nexport const YCursorExtension = createExtension(\n ({ options }: ExtensionOptions<CollaborationOptions>) => {\n const recentlyUpdatedCursors = new Map();\n const awareness =\n options.provider &&\n \"awareness\" in options.provider &&\n typeof options.provider.awareness === \"object\"\n ? options.provider.awareness\n : undefined;\n if (awareness) {\n if (\n \"setLocalStateField\" in awareness &&\n typeof awareness.setLocalStateField === \"function\"\n ) {\n awareness.setLocalStateField(\"user\", options.user);\n }\n if (\"on\" in awareness && typeof awareness.on === \"function\") {\n if (options.showCursorLabels !== \"always\") {\n awareness.on(\n \"change\",\n ({\n updated,\n }: {\n added: Array<number>;\n updated: Array<number>;\n removed: Array<number>;\n }) => {\n for (const clientID of updated) {\n const cursor = recentlyUpdatedCursors.get(clientID);\n\n if (cursor) {\n setTimeout(() => {\n cursor.element.setAttribute(\"data-active\", \"\");\n }, 10);\n\n if (cursor.hideTimeout) {\n clearTimeout(cursor.hideTimeout);\n }\n\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: setTimeout(() => {\n cursor.element.removeAttribute(\"data-active\");\n }, 2000),\n });\n }\n }\n },\n );\n }\n }\n }\n\n return {\n key: \"yCursor\",\n prosemirrorPlugins: [\n awareness\n ? yCursorPlugin(awareness, {\n selectionBuilder: defaultSelectionBuilder,\n cursorBuilder(user, clientID) {\n let cursorData = recentlyUpdatedCursors.get(clientID);\n\n if (!cursorData) {\n const cursorElement = (\n options.renderCursor ?? defaultCursorRender\n )(user as CollaborationUser);\n\n if (options.showCursorLabels !== \"always\") {\n cursorElement.addEventListener(\"mouseenter\", () => {\n const cursor = recentlyUpdatedCursors.get(clientID)!;\n cursor.element.setAttribute(\"data-active\", \"\");\n\n if (cursor.hideTimeout) {\n clearTimeout(cursor.hideTimeout);\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: undefined,\n });\n }\n });\n\n cursorElement.addEventListener(\"mouseleave\", () => {\n const cursor = recentlyUpdatedCursors.get(clientID)!;\n\n recentlyUpdatedCursors.set(clientID, {\n element: cursor.element,\n hideTimeout: setTimeout(() => {\n cursor.element.removeAttribute(\"data-active\");\n }, 2000),\n });\n });\n }\n\n cursorData = {\n element: cursorElement,\n hideTimeout: undefined,\n };\n\n recentlyUpdatedCursors.set(clientID, cursorData);\n }\n\n return cursorData.element;\n },\n })\n : undefined,\n ].filter((a) => a !== undefined),\n dependsOn: [\"ySync\"],\n updateUser(user: CollaborationUser) {\n awareness?.setLocalStateField(\"user\", user);\n },\n getUser(): CollaborationUser | undefined {\n const state = awareness?.getLocalState();\n if (!state) {\n return undefined;\n }\n return state[\"user\"];\n },\n } as const;\n },\n);\n","import { docToDelta } from \"@y/prosemirror\";\nimport * as Y from \"@y/y\";\n\nimport type { Block } from \"../../blocks/defaultBlocks.js\";\nimport type { BlockNoteEditor } from \"../../editor/BlockNoteEditor.js\";\nimport { createExtension } from \"../../editor/BlockNoteExtension.js\";\nimport type { User } from \"../../user/index.js\";\nimport {\n _blocksToProsemirrorNode,\n docDiffToDelta,\n findTypeInOtherYdoc,\n getProseMirrorTrFromYFragment,\n} from \"../utils.js\";\nimport { AttributionExtension } from \"./AttributionExtension.js\";\nimport type { GetAttributionMarkClassName } from \"./YAttributionMarks.js\";\n\n/**\n * A version diff has a single \"author\" — the version that introduced the changes\n * — not a real user, so all `y-attributed-*` marks carry one synthetic id. That\n * id is derived from the version's label (see {@link diffAuthorId}) so it's stable\n * per version: the user store caches resolved users by id, so a per-label id keeps\n * each version's tooltip showing its own name instead of a stale cached one.\n */\nconst DIFF_AUTHOR_ID_PREFIX = \"version:\";\n\n/** The synthetic author id for a given version label. */\nconst diffAuthorId = (label: string) => DIFF_AUTHOR_ID_PREFIX + label;\n\n/** Fallback label used when a diff is rendered without a version name. */\nconst DEFAULT_DIFF_LABEL = \"This version\";\n\n/** Color used for the version diff marks. */\nconst DIFF_AUTHOR_COLOR = \"#4363d8\";\n\nexport type DiffVersioningExtensionOptions = {\n /**\n * The color used for the diff's attribution marks. Defaults to a blue.\n */\n color?: string;\n /**\n * See {@link GetAttributionMarkClassName}. Forwarded to the underlying\n * {@link AttributionExtension} to override mark styling by change type.\n */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n};\n\n/**\n * Records the author of each transaction on `doc` into a mutable\n * {@link Y.Attributions}, so the resulting attribution marks carry a non-empty\n * `userIds` (and therefore resolve to a color/name). The listener must be\n * attached *before* the attributed transaction runs. Mirrors the store used by\n * the suggestion gallery example (`createAttributionStore`).\n */\nfunction attributeTransactionsTo(doc: Y.Doc, userId: string): Y.Attributions {\n const attrs = new Y.Attributions();\n doc.on(\"beforeObserverCalls\", (tr) => {\n if (!tr.insertSet.isEmpty()) {\n Y.insertIntoIdMap(\n attrs.inserts,\n Y.createIdMapFromIdSet(tr.insertSet, [\n Y.createContentAttribute(\"insert\", userId),\n ]),\n );\n }\n if (!tr.deleteSet.isEmpty()) {\n Y.insertIntoIdMap(\n attrs.deletes,\n Y.createIdMapFromIdSet(tr.deleteSet, [\n Y.createContentAttribute(\"delete\", userId),\n ]),\n );\n }\n });\n return attrs;\n}\n\n/**\n * An opt-in extension that renders a read-only diff between two BlockNote\n * documents (`Block[]`) directly in the editor, marking insertions/deletions\n * with the `y-attributed-*` suggestion marks — the same visual result the Yjs\n * collaboration adapter produces, but driven from two plain block arrays with\n * no server and no live Yjs sync.\n *\n * It composes {@link AttributionExtension} (which registers the attribution\n * marks and drives their colors + hover tooltips from a user store), and adds\n * the {@link renderDiff} / {@link clearDiff} capability.\n *\n * Registering this extension is what makes non-collaborative versioning\n * (`inMemoryVersioning`) capable of showing diffs: the in-memory preview\n * controller looks this up by key (`\"diffVersioning\"`) and delegates to\n * {@link renderDiff}, falling back to a static document swap when it's absent.\n *\n * `renderDiff` is also a standalone, directly-callable API — you can register\n * just this extension and call\n * `editor.getExtension(DiffVersioningExtension).renderDiff(target, baseline)`\n * to render a known diff (e.g. in tests).\n *\n * @example\n * ```ts\n * const editor = BlockNoteEditor.create({\n * extensions: [DiffVersioningExtension()],\n * });\n * editor.getExtension(DiffVersioningExtension)!.renderDiff(target, baseline);\n * ```\n */\nexport const DiffVersioningExtension = createExtension(\n ({\n options,\n editor,\n }: {\n options: DiffVersioningExtensionOptions | undefined;\n editor: BlockNoteEditor<any, any, any>;\n }) => {\n const color = options?.color ?? DIFF_AUTHOR_COLOR;\n\n // Resolve a synthetic author id back to its version label. The id encodes\n // the label (`version:<label>`), so this is a pure decode — no shared mutable\n // state, and the user store caches each version's \"user\" separately (so\n // switching between version comparisons never shows a stale name).\n const resolveUsers = async (ids: string[]): Promise<User[]> =>\n ids\n .filter((id) => id.startsWith(DIFF_AUTHOR_ID_PREFIX))\n .map((id) => ({\n id,\n username: id.slice(DIFF_AUTHOR_ID_PREFIX.length),\n avatarUrl: \"\",\n color,\n }));\n\n /**\n * Render a read-only diff of `baselineBlocks` → `snapshotBlocks` into the\n * editor. The changes are attributed to the version that introduced them:\n * pass `versionLabel` to label the diff marks (shown in their hover tooltip,\n * e.g. \"Edited by: {versionLabel}\"). Uses the \"two-doc fork\" recipe so the\n * two Y.Docs share history — a hard requirement for\n * `createDiffRenderer`, which diffs by Yjs client/clock ids.\n */\n const renderDiff = (\n snapshotBlocks: Block<any, any, any>[],\n baselineBlocks: Block<any, any, any>[],\n versionLabel: string = DEFAULT_DIFF_LABEL,\n ) => {\n const authorId = diffAuthorId(versionLabel);\n\n if (!editor.pmSchema.marks[\"y-attributed-insert\"]) {\n throw new Error(\n \"DiffVersioningExtension: the y-attributed-* marks are missing from \" +\n \"the schema. This should not happen — the extension registers them \" +\n \"via AttributionExtension.\",\n );\n }\n\n const baselineNode = _blocksToProsemirrorNode(editor, baselineBlocks);\n const snapshotNode = _blocksToProsemirrorNode(editor, snapshotBlocks);\n\n // gc must stay off so the attribution manager can read the full struct\n // store (including deleted items) when diffing.\n const prevDoc = new Y.Doc({ gc: false });\n const prevType = prevDoc.get(\"prosemirror\");\n prevDoc.transact(() => {\n prevType.applyDelta(docToDelta(baselineNode) as any);\n });\n\n // Fork prevDoc into nextDoc so they share client/clock ids, then apply the\n // baseline → snapshot delta as a new transaction. New content gets ids\n // that prevDoc lacks (→ inserts); items retained-away become deletes.\n const nextDoc = new Y.Doc({ gc: false });\n Y.applyUpdateV2(nextDoc, Y.encodeStateAsUpdateV2(prevDoc));\n const nextType = findTypeInOtherYdoc(prevType, nextDoc);\n\n // Attach the author store BEFORE applying the delta so the diff\n // transaction's inserts/deletes are attributed.\n const attrs = attributeTransactionsTo(nextDoc, authorId);\n\n const delta = docDiffToDelta(baselineNode, snapshotNode);\n nextDoc.transact(() => {\n nextType.applyDelta(delta as any);\n }, authorId);\n\n const renderer = Y.createDiffRenderer(prevDoc, nextDoc, { attrs });\n\n // Clear the live doc first so ProseMirror rebuilds node views from\n // scratch (BlockNote node views resolve their block eagerly via getPos()\n // and throw on a moved node). The diff then inserts the attributed content\n // against an empty doc.\n editor.replaceBlocks(editor.document, []);\n\n editor.exec((state, dispatch) => {\n const tr = getProseMirrorTrFromYFragment({\n tr: state.tr,\n fragment: nextType,\n renderer,\n });\n if (dispatch) {\n dispatch(tr);\n }\n return true;\n });\n\n prevDoc.destroy();\n nextDoc.destroy();\n };\n\n /**\n * Leave diff view: clear the (mark-carrying) document and restore the given\n * blocks. Clears first so stale node views for block-level marks are torn\n * down instead of reused.\n */\n const clearDiff = (restore: Block<any, any, any>[]) => {\n editor.replaceBlocks(editor.document, []);\n editor.replaceBlocks(editor.document, restore);\n };\n\n return {\n key: \"diffVersioning\",\n // Compose AttributionExtension: registers the y-attributed-* marks and\n // drives their colors + hover-tooltip names from the (static) user store.\n blockNoteExtensions: [\n AttributionExtension({\n resolveUsers,\n getAttributionMarkClassName: options?.getAttributionMarkClassName,\n }),\n ],\n renderDiff,\n clearDiff,\n };\n },\n);\n","import type { Awareness } from \"@y/protocols/awareness\";\nimport type * as Y from \"@y/y\";\nimport { BlockNoteEditorOptions } from \"../../editor/BlockNoteEditor.js\";\nimport {\n createExtension,\n ExtensionOptions,\n} from \"../../editor/BlockNoteExtension.js\";\nimport {\n VersioningEndpoints,\n VersioningEndpointsFactory,\n VersioningExtension,\n} from \"../../extensions/Versioning/index.js\";\nimport { normalizeToUserStore, UserStoreOrResolver } from \"../../user/index.js\";\nimport { AttributionExtension } from \"./AttributionExtension.js\";\nimport { RelativePositionMappingExtension } from \"./RelativePositionMapping.js\";\nimport { SuggestionsExtension } from \"./Suggestions.js\";\nimport { createYjsVersioningAdapter } from \"./Versioning.js\";\nimport { CollaborationUser, YCursorExtension } from \"./YCursorPlugin.js\";\nimport type { GetAttributionMarkClassName } from \"./YAttributionMarks.js\";\nimport { YSyncExtension } from \"./YSync.js\";\n\nexport type CollaborationOptions = {\n /**\n * The Yjs Type that's used for collaboration.\n */\n fragment: Y.Type;\n /**\n * The user info for the current user that's shown to other collaborators.\n */\n user: CollaborationUser;\n /**\n * Resolve user information (usernames, colors) for suggestions and versions,\n * used to drive suggestion-author tooltips, suggestion colors and\n * version-history author labels.\n *\n * Either a resolver function (called with the ids of users that are not yet\n * cached, returning their information — a user store is built from it\n * internally) or a pre-built user store (see `createUserStore`). Pass the same\n * store you give the comments extension so a single de-duped user cache is\n * shared across comments, suggestions and versioning.\n */\n resolveUsers?: UserStoreOrResolver;\n /**\n * A Yjs provider (used for awareness / cursor information)\n */\n provider?: { awareness?: Awareness };\n /**\n * Optional function to customize how cursors of users are rendered\n */\n renderCursor?: (user: CollaborationUser) => HTMLElement;\n /**\n * Optional flag to set when the user label should be shown with the default\n * collaboration cursor. Setting to \"always\" will always show the label,\n * while \"activity\" will only show the label when the user moves the cursor\n * or types. Defaults to \"activity\".\n */\n showCursorLabels?: \"always\" | \"activity\";\n /**\n * The renderer for the collaboration.\n */\n renderer?: Y.DiffRenderer;\n /**\n * The suggestion doc for the collaboration. If using suggestion mode\n */\n suggestionDoc?: Y.Doc;\n\n /**\n * Optional callback to override suggestion-mark styling by change type instead\n * of by author. Given a mark's content/modification type, return a class name\n * applied to the mark and its hover tooltip; the per-user color is then\n * dropped for that mark. See {@link GetAttributionMarkClassName}.\n */\n getAttributionMarkClassName?: GetAttributionMarkClassName;\n\n /**\n * The endpoints for the versioning functionality.\n */\n versioningEndpoints?:\n | VersioningEndpoints<Y.Type, Uint8Array>\n | VersioningEndpointsFactory<Y.Type, Uint8Array>;\n};\n\nexport const CollaborationExtension = createExtension(\n ({ editor, options }: ExtensionOptions<CollaborationOptions>) => {\n // Build a single user store here (from a resolver callback or a store the\n // consumer passed in) and hand that same store down to every sub-extension\n // that needs it — suggestions, suggestion-mark tooltips/colors, versioning —\n // so they share one de-duped cache. Passing the resolved store (rather than\n // the raw resolver) is what guarantees the sharing: each child re-normalizes\n // it to itself instead of building its own.\n const userStore = normalizeToUserStore(options.resolveUsers);\n const optionsWithUserStore = { ...options, resolveUsers: userStore };\n return {\n key: \"collaboration\",\n userStore,\n blockNoteExtensions: [\n options.suggestionDoc\n ? SuggestionsExtension(optionsWithUserStore)\n : null,\n RelativePositionMappingExtension(),\n YSyncExtension(optionsWithUserStore),\n YCursorExtension(options),\n options.versioningEndpoints\n ? VersioningExtension({\n ...createYjsVersioningAdapter(editor, options.fragment),\n endpoints: options.versioningEndpoints,\n resolveUsers: userStore,\n })\n : null,\n AttributionExtension({\n resolveUsers: userStore,\n getAttributionMarkClassName: options.getAttributionMarkClassName,\n }),\n ].filter((a) => a !== null),\n } as const;\n },\n);\n\nexport function withCollaboration<\n Options extends Partial<BlockNoteEditorOptions<any, any, any>>,\n>(\n options: Options & {\n /**\n * Options for configuring the collaboration functionality.\n */\n collaboration: CollaborationOptions;\n },\n): Options {\n if (options.initialContent) {\n // eslint-disable-next-line no-console\n console.warn(\n \"When using Collaboration, initialContent might cause conflicts, because changes should come from the collaboration provider\",\n );\n }\n return {\n ...options,\n extensions: [\n ...(options.extensions ?? []),\n CollaborationExtension(options.collaboration),\n ],\n // We disable the default prosemirror history plugin, since it's not compatible with yjs\n disableExtensions: [\"history\", ...(options.disableExtensions ?? [])],\n // We don't want the default initial content, since it will generate a random id for the initial block on each client,\n // leading to conflicts when syncing happens afterwards.\n initialContent: [{ type: \"paragraph\", id: \"initialBlockId\" }],\n };\n}\n\nexport * from \"./AttributionExtension.js\";\nexport * from \"./DiffVersioningExtension.js\";\nexport * from \"./RelativePositionMapping.js\";\nexport * from \"./Suggestions.js\";\nexport * from \"./Versioning.js\";\nexport * from \"./YAttributionMarks.js\";\nexport * from \"./YCursorPlugin.js\";\nexport * from \"./YSync.js\";\n","import * as Y from \"@y/y\";\nimport type {\n CommentData,\n CommentReactionData,\n ThreadData,\n} from \"../../comments/types.js\";\n\nexport function commentToYType(comment: CommentData) {\n const yType = new Y.Type();\n yType.setAttr(\"id\", comment.id);\n yType.setAttr(\"userId\", comment.userId);\n yType.setAttr(\"createdAt\", comment.createdAt.getTime());\n yType.setAttr(\"updatedAt\", comment.updatedAt.getTime());\n if (comment.deletedAt) {\n yType.setAttr(\"deletedAt\", comment.deletedAt.getTime());\n yType.setAttr(\"body\", undefined);\n } else {\n yType.setAttr(\"body\", comment.body);\n }\n if (comment.reactions.length > 0) {\n throw new Error(\"Reactions should be empty in commentToYType\");\n }\n\n /**\n * Reactions are stored in a map keyed by {userId-emoji},\n * this makes it easy to add / remove reactions and in a way that works local-first.\n * The cost is that \"reading\" the reactions is a bit more complex (see yTypeToReactions).\n */\n yType.setAttr(\"reactionsByUser\", new Y.Type());\n yType.setAttr(\"metadata\", comment.metadata);\n\n return yType;\n}\n\nexport function threadToYType(thread: ThreadData) {\n const yType = new Y.Type();\n yType.setAttr(\"id\", thread.id);\n yType.setAttr(\"createdAt\", thread.createdAt.getTime());\n yType.setAttr(\"updatedAt\", thread.updatedAt.getTime());\n const commentsType = new Y.Type();\n\n commentsType.push(thread.comments.map((comment) => commentToYType(comment)));\n\n yType.setAttr(\"comments\", commentsType);\n yType.setAttr(\"resolved\", thread.resolved);\n yType.setAttr(\"resolvedUpdatedAt\", thread.resolvedUpdatedAt?.getTime());\n yType.setAttr(\"resolvedBy\", thread.resolvedBy);\n yType.setAttr(\"metadata\", thread.metadata);\n return yType;\n}\n\ntype SingleUserCommentReactionData = {\n emoji: string;\n createdAt: Date;\n userId: string;\n};\n\nexport function yTypeToReaction(yType: Y.Type): SingleUserCommentReactionData {\n return {\n emoji: yType.getAttr(\"emoji\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n userId: yType.getAttr(\"userId\"),\n };\n}\n\nfunction yTypeToReactions(yType: Y.Type): CommentReactionData[] {\n const flatReactions = [...yType.attrValues()].map((reaction: Y.Type) =>\n yTypeToReaction(reaction),\n );\n // combine reactions by the same emoji\n return flatReactions.reduce(\n (acc: CommentReactionData[], reaction: SingleUserCommentReactionData) => {\n const existingReaction = acc.find((r) => r.emoji === reaction.emoji);\n if (existingReaction) {\n existingReaction.userIds.push(reaction.userId);\n existingReaction.createdAt = new Date(\n Math.min(\n existingReaction.createdAt.getTime(),\n reaction.createdAt.getTime(),\n ),\n );\n } else {\n acc.push({\n emoji: reaction.emoji,\n createdAt: reaction.createdAt,\n userIds: [reaction.userId],\n });\n }\n return acc;\n },\n [] as CommentReactionData[],\n );\n}\n\nexport function yTypeToComment(yType: Y.Type): CommentData {\n return {\n type: \"comment\",\n id: yType.getAttr(\"id\"),\n userId: yType.getAttr(\"userId\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n updatedAt: new Date(yType.getAttr(\"updatedAt\")),\n deletedAt: yType.getAttr(\"deletedAt\")\n ? new Date(yType.getAttr(\"deletedAt\"))\n : undefined,\n reactions: yTypeToReactions(yType.getAttr(\"reactionsByUser\")),\n metadata: yType.getAttr(\"metadata\"),\n body: yType.getAttr(\"body\"),\n };\n}\n\nexport function yTypeToThread(yType: Y.Type): ThreadData {\n return {\n type: \"thread\",\n id: yType.getAttr(\"id\"),\n createdAt: new Date(yType.getAttr(\"createdAt\")),\n updatedAt: new Date(yType.getAttr(\"updatedAt\")),\n comments: ((yType.getAttr(\"comments\") as Y.Type)?.toArray() || []).map(\n (comment) => yTypeToComment(comment as Y.Type),\n ),\n resolved: yType.getAttr(\"resolved\"),\n resolvedUpdatedAt: new Date(yType.getAttr(\"resolvedUpdatedAt\")),\n resolvedBy: yType.getAttr(\"resolvedBy\"),\n metadata: yType.getAttr(\"metadata\"),\n };\n}\n","import * as Y from \"@y/y\";\nimport type { ThreadData } from \"../../comments/types.js\";\nimport { ThreadStore } from \"../../comments/threadstore/ThreadStore.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { yTypeToThread } from \"./yjsHelpers.js\";\n\n/**\n * This is an abstract class that only implements the READ methods required by the ThreadStore interface.\n * The data is read from a @y/y Type used as a map (via attributes).\n */\nexport abstract class YjsThreadStoreBase extends ThreadStore {\n constructor(\n protected readonly threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(auth);\n }\n\n // TODO: async / reactive interface?\n public getThread(threadId: string) {\n const yThread = this.threadsYType.getAttr(threadId);\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n const thread = yTypeToThread(yThread);\n return thread;\n }\n\n public getThreads(): Map<string, ThreadData> {\n const threadMap = new Map<string, ThreadData>();\n this.threadsYType.forEachAttr((yThread: any, id: string | number) => {\n if (yThread instanceof Y.Type) {\n threadMap.set(String(id), yTypeToThread(yThread));\n }\n });\n return threadMap;\n }\n\n public subscribe(cb: (threads: Map<string, ThreadData>) => void) {\n const observer = () => {\n cb(this.getThreads());\n };\n\n this.threadsYType.observeDeep(observer);\n\n return () => {\n this.threadsYType.unobserveDeep(observer);\n };\n }\n}\n","import * as Y from \"@y/y\";\nimport type { CommentBody } from \"../../comments/types.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\n\n/**\n * This is a REST-based implementation of the YjsThreadStoreBase for @y/y (v14).\n * It Reads data directly from the underlying document (same as YjsThreadStore),\n * but for Writes, it sends data to a REST API that should:\n * - check the user has the correct permissions to make the desired changes\n * - apply the updates to the underlying Yjs document\n *\n * (see https://github.com/TypeCellOS/BlockNote-demo-nextjs-hocuspocus)\n *\n * The reason we still use the Yjs document as underlying storage is that it makes it easy to\n * sync updates in real-time to other collaborators.\n * (but technically, you could also implement a different storage altogether\n * and not store the thread related data in the Yjs document)\n */\nexport class RESTYjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly BASE_URL: string,\n private readonly headers: Record<string, string>,\n threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYType, auth);\n }\n\n private doRequest = async (path: string, method: string, body?: any) => {\n const response = await fetch(`${this.BASE_URL}${path}`, {\n method,\n body: JSON.stringify(body),\n headers: {\n \"Content-Type\": \"application/json\",\n ...this.headers,\n },\n });\n\n if (!response.ok) {\n throw new Error(`Failed to ${method} ${path}: ${response.statusText}`);\n }\n\n return response.json();\n };\n\n public addThreadToDocument = async (options: {\n threadId: string;\n selection: {\n head: number;\n anchor: number;\n };\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/addToDocument`, \"POST\", rest);\n };\n\n public createThread = async (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n return this.doRequest(\"\", \"POST\", options);\n };\n\n public addComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const { threadId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments`, \"POST\", rest);\n };\n\n public updateComment = (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(`/${threadId}/comments/${commentId}`, \"PUT\", rest);\n };\n\n public deleteComment = (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}?soft=${!!rest.softDelete}`,\n \"DELETE\",\n );\n };\n\n public deleteThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}`, \"DELETE\");\n };\n\n public resolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/resolve`, \"POST\");\n };\n\n public unresolveThread = (options: { threadId: string }) => {\n return this.doRequest(`/${options.threadId}/unresolve`, \"POST\");\n };\n\n public addReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n const { threadId, commentId, ...rest } = options;\n return this.doRequest(\n `/${threadId}/comments/${commentId}/reactions`,\n \"POST\",\n rest,\n );\n };\n\n public deleteReaction = (options: {\n threadId: string;\n commentId: string;\n emoji: string;\n }) => {\n return this.doRequest(\n `/${options.threadId}/comments/${options.commentId}/reactions/${options.emoji}`,\n \"DELETE\",\n );\n };\n}\n","import { uuidv4 } from \"lib0/random\";\nimport * as Y from \"@y/y\";\nimport type {\n CommentBody,\n CommentData,\n ThreadData,\n} from \"../../comments/types.js\";\nimport type { ThreadStoreAuth } from \"../../comments/threadstore/ThreadStoreAuth.js\";\nimport { YjsThreadStoreBase } from \"./YjsThreadStoreBase.js\";\nimport {\n commentToYType,\n threadToYType,\n yTypeToComment,\n yTypeToThread,\n} from \"./yjsHelpers.js\";\n\n/**\n * This is a @y/y (v14)-based implementation of the ThreadStore interface.\n *\n * It reads and writes thread / comments information directly to the underlying Yjs Document.\n *\n * @important While this is the easiest to add to your app, there are two challenges:\n * - The user needs to be able to write to the Yjs document to store the information.\n * So a user without write access to the Yjs document cannot leave any comments.\n * - Even with write access, the operations are not secure. Unless your Yjs server\n * guards against malicious operations, it's technically possible for one user to make changes to another user's comments, etc.\n * (even though these options are not visible in the UI, a malicious user can make unauthorized changes to the underlying Yjs document)\n */\nexport class YjsThreadStore extends YjsThreadStoreBase {\n constructor(\n private readonly userId: string,\n threadsYType: Y.Type,\n auth: ThreadStoreAuth,\n ) {\n super(threadsYType, auth);\n }\n\n private transact = <T, R>(\n fn: (options: T) => R,\n ): ((options: T) => Promise<R>) => {\n return async (options: T) => {\n return this.threadsYType.doc!.transact(() => {\n return fn(options);\n });\n };\n };\n\n public createThread = this.transact(\n (options: {\n initialComment: {\n body: CommentBody;\n metadata?: any;\n };\n metadata?: any;\n }) => {\n if (!this.auth.canCreateThread()) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const comment: CommentData = {\n type: \"comment\",\n id: uuidv4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n reactions: [],\n metadata: options.initialComment.metadata,\n body: options.initialComment.body,\n };\n\n const thread: ThreadData = {\n type: \"thread\",\n id: uuidv4(),\n createdAt: date,\n updatedAt: date,\n comments: [comment],\n resolved: false,\n metadata: options.metadata,\n };\n\n this.threadsYType.setAttr(thread.id, threadToYType(thread));\n\n return thread;\n },\n );\n\n // YjsThreadStore does not support addThreadToDocument\n public addThreadToDocument = undefined;\n\n public addComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canAddComment(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n const comment: CommentData = {\n type: \"comment\",\n id: uuidv4(),\n userId: this.userId,\n createdAt: date,\n updatedAt: date,\n deletedAt: undefined,\n reactions: [],\n metadata: options.comment.metadata,\n body: options.comment.body,\n };\n\n (yThread.getAttr(\"comments\") as Y.Type).push([commentToYType(comment)]);\n\n yThread.setAttr(\"updatedAt\", new Date().getTime());\n return comment;\n },\n );\n\n public updateComment = this.transact(\n (options: {\n comment: {\n body: CommentBody;\n metadata?: any;\n };\n threadId: string;\n commentId: string;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canUpdateComment(yTypeToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n yComment.setAttr(\"body\", options.comment.body);\n yComment.setAttr(\"updatedAt\", new Date().getTime());\n yComment.setAttr(\"metadata\", options.comment.metadata);\n },\n );\n\n public deleteComment = this.transact(\n (options: {\n threadId: string;\n commentId: string;\n softDelete?: boolean;\n }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canDeleteComment(yTypeToComment(yComment))) {\n throw new Error(\"Not authorized\");\n }\n\n if (yComment.getAttr(\"deletedAt\")) {\n throw new Error(\"Comment already deleted\");\n }\n\n if (options.softDelete) {\n yComment.setAttr(\"deletedAt\", new Date().getTime());\n yComment.setAttr(\"body\", undefined);\n } else {\n commentsType.delete(yCommentIndex);\n }\n\n if (\n commentsType\n .toArray()\n .every((comment) => (comment as Y.Type).getAttr(\"deletedAt\"))\n ) {\n // all comments deleted\n if (options.softDelete) {\n yThread.setAttr(\"deletedAt\", new Date().getTime());\n } else {\n this.threadsYType.deleteAttr(options.threadId);\n }\n }\n\n yThread.setAttr(\"updatedAt\", new Date().getTime());\n },\n );\n\n public deleteThread = this.transact((options: { threadId: string }) => {\n if (\n !this.auth.canDeleteThread(\n yTypeToThread(this.threadsYType.getAttr(options.threadId) as Y.Type),\n )\n ) {\n throw new Error(\"Not authorized\");\n }\n\n this.threadsYType.deleteAttr(options.threadId);\n });\n\n public resolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canResolveThread(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.setAttr(\"resolved\", true);\n yThread.setAttr(\"resolvedUpdatedAt\", new Date().getTime());\n yThread.setAttr(\"resolvedBy\", this.userId);\n });\n\n public unresolveThread = this.transact((options: { threadId: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n if (!this.auth.canUnresolveThread(yTypeToThread(yThread))) {\n throw new Error(\"Not authorized\");\n }\n\n yThread.setAttr(\"resolved\", false);\n yThread.setAttr(\"resolvedUpdatedAt\", new Date().getTime());\n });\n\n public addReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (!this.auth.canAddReaction(yTypeToComment(yComment), options.emoji)) {\n throw new Error(\"Not authorized\");\n }\n\n const date = new Date();\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.getAttr(\"reactionsByUser\") as Y.Type;\n\n if (reactionsByUser.hasAttr(key)) {\n // already exists\n return;\n } else {\n const reaction = new Y.Type();\n reaction.setAttr(\"emoji\", options.emoji);\n reaction.setAttr(\"createdAt\", date.getTime());\n reaction.setAttr(\"userId\", this.userId);\n reactionsByUser.setAttr(key, reaction);\n }\n },\n );\n\n public deleteReaction = this.transact(\n (options: { threadId: string; commentId: string; emoji: string }) => {\n const yThread = this.threadsYType.getAttr(options.threadId) as\n | Y.Type\n | undefined;\n if (!yThread) {\n throw new Error(\"Thread not found\");\n }\n\n const commentsType = yThread.getAttr(\"comments\") as Y.Type;\n const yCommentIndex = yTypeFindIndex(\n commentsType,\n (comment) => (comment as Y.Type).getAttr(\"id\") === options.commentId,\n );\n\n if (yCommentIndex === -1) {\n throw new Error(\"Comment not found\");\n }\n\n const yComment = commentsType.get(yCommentIndex) as Y.Type;\n\n if (\n !this.auth.canDeleteReaction(yTypeToComment(yComment), options.emoji)\n ) {\n throw new Error(\"Not authorized\");\n }\n\n const key = `${this.userId}-${options.emoji}`;\n\n const reactionsByUser = yComment.getAttr(\"reactionsByUser\") as Y.Type;\n\n reactionsByUser.deleteAttr(key);\n },\n );\n}\n\nfunction yTypeFindIndex(yType: Y.Type, predicate: (item: any) => boolean) {\n for (let i = 0; i < yType.length; i++) {\n if (predicate(yType.get(i))) {\n return i;\n }\n }\n return -1;\n}\n","import * as Y from \"@y/y\";\nimport { decodeAny, encodeAny } from \"lib0/buffer\";\n\nimport {\n CURRENT_VERSION_ID,\n sortSnapshotsNewestFirst,\n VersioningEndpointsFactory,\n type VersioningEndpoints,\n type VersionSnapshot,\n} from \"../../extensions/Versioning/index.js\";\nimport { uint32 } from \"lib0/random\";\nimport { YCursorExtension } from \"../extensions/YCursorPlugin.js\";\nimport { YSyncExtension } from \"../extensions/YSync.js\";\n\n/**\n * Name of the root {@link Y.Type} map on the live collaboration doc that stores\n * a mutable `versionId -> name` mapping. Because YHub attributions are\n * immutable, version names that need to be editable (renamed) live here on the\n * Y.Doc instead of (or in addition to) the immutable `name` attribution.\n */\nconst VERSION_NAMES_MAP = \"__bn_version_names\";\n\n/**\n * Options for creating a YHub versioning endpoints instance.\n */\nexport interface YHubVersioningOptions {\n /**\n * Base URL of the YHub API (e.g. `\"https://yhub.example.com\"`).\n * Must **not** include a trailing slash.\n */\n baseUrl: string;\n\n /** YHub organisation identifier. */\n org: string;\n\n /** Document identifier within the organisation. */\n docId: string;\n\n /**\n * Optional headers to include in every request (e.g. authentication tokens).\n */\n headers?: Record<string, string>;\n\n /**\n * Maximum number of activity entries to fetch when listing versions.\n * @default 50\n */\n activityLimit?: number;\n\n /**\n * When set, forwarded as the `group` query param to the YHub activity API,\n * controlling whether adjacent edits are grouped into single entries.\n */\n group?: boolean;\n\n /**\n * Maximum gap (in ms) between edits for them to be grouped together.\n * Forwarded as the `groupMaxGap` query param.\n * @default 10000\n */\n groupMaxGap?: number;\n\n /**\n * Maximum total duration (in ms) a single group of edits may span.\n * When set, forwarded as the `groupMaxDuration` query param.\n */\n groupMaxDuration?: number;\n\n // TODO mergeUsers is not in standard yhub, but it exists in our fork.\n /**\n * When `true`, adjacent edits are grouped together even when made by\n * *different* users (their ids accumulate in the grouped entry's `by`).\n * When `false` (the default), only same-user adjacent edits are merged.\n * Forwarded as the `mergeUsers` query param.\n * @default false\n */\n mergeUsers?: boolean;\n}\n\n/**\n * Shape of a single activity entry returned by the YHub\n * `GET /activity/{org}/{docId}` endpoint (after `decodeAny`).\n */\ninterface YHubActivityEntry {\n /** Start of the change window (unix-ms timestamp). */\n from: number;\n /** End of the change window (unix-ms timestamp). */\n to: number;\n /** Comma separated list of user-ids that matches the attribution */\n by?: string;\n /** Custom attribution key-value pairs (when `customAttributions=true`). */\n customAttributions?: Array<{ k: string; v: string }>;\n}\n\n/**\n * Shape returned by the YHub `GET /changeset/{org}/{docId}` endpoint (after\n * `decodeAny`).\n */\ninterface YHubChangeset {\n /** Full Y.Doc state **before** the changeset window. */\n prevDoc?: Uint8Array;\n /** Full Y.Doc state **after** the changeset window. */\n nextDoc?: Uint8Array;\n /**\n * Encoded {@link Y.ContentMap} describing who authored each change in the\n * window and when. Present when the changeset is requested with\n * `attributions=true`.\n */\n attributions?: Uint8Array;\n}\n\n/**\n * Whether an activity entry is a version marker (created with a `type:version`\n * custom attribution) as opposed to a plain edit.\n */\nfunction isVersionEntry(entry: YHubActivityEntry): boolean {\n return (\n entry.customAttributions?.some(\n (a) => a.k === \"type\" && a.v === \"version\",\n ) ?? false\n );\n}\n\n/**\n * Convert a YHub activity entry into a {@link VersionSnapshot}.\n *\n * Version markers (entries with a `type:version` custom attribution) map to\n * named snapshots: the `id` attribution becomes the snapshot identifier and the\n * `name` attribution its name. Any other (plain edit) entry maps to a\n * history-only snapshot with a synthetic `history-<to>-<index>` id and no name.\n * In both cases the entry's `by` user-ids are passed through raw on\n * {@link VersionSnapshot.by} — resolving them to user info is the view layer's\n * job.\n *\n * The history id embeds the entry's `index` within the activity response\n * because YHub can emit multiple activity entries sharing the same `to`\n * timestamp (e.g. distinct same-`insertAt` patches that grouping did not merge),\n * and `to` alone would then produce colliding `history-<to>` ids — duplicate\n * React keys in the sidebar. The `index` disambiguates them. The changeset\n * lookups (`getContent`/`getAttributions`/`restore`) key off\n * {@link VersionSnapshot.createdAt} (= `entry.to`), never the id, so embedding\n * the index in the id is safe.\n */\nfunction activityToSnapshot(\n entry: YHubActivityEntry,\n index: number,\n): VersionSnapshot | undefined {\n const by = entry.by\n ?.split(\",\")\n .map((s) => s.trim())\n .filter(Boolean);\n const byField = by && by.length > 0 ? by : undefined;\n\n if (isVersionEntry(entry)) {\n const id = entry.customAttributions?.find((a) => a.k === \"id\")?.v;\n if (id === undefined) {\n return undefined;\n }\n const attributionName = entry.customAttributions?.find(\n (a) => a.k === \"name\",\n )?.v;\n return {\n id,\n name: attributionName,\n createdAt: entry.to,\n updatedAt: entry.to,\n by: byField,\n };\n }\n\n return {\n id: `history-${entry.to}-${index}`,\n createdAt: entry.to,\n updatedAt: entry.to,\n by: byField,\n };\n}\n\nasync function yhubFetch(\n url: string,\n headers: Record<string, string>,\n init?: RequestInit,\n): Promise<ArrayBuffer> {\n const res = await fetch(url, {\n ...init,\n headers: {\n ...headers,\n ...(init?.headers instanceof Headers\n ? Object.fromEntries(init.headers.entries())\n : Array.isArray(init?.headers)\n ? Object.fromEntries(init.headers)\n : init?.headers),\n },\n });\n if (!res.ok) {\n throw new Error(\n `YHub request failed: ${res.status} ${res.statusText} (${url})`,\n );\n }\n return res.arrayBuffer();\n}\n\n/**\n * Create a {@link VersioningEndpoints} implementation backed by the\n * [YHub](https://github.com/yjs/yhub) HTTP API.\n *\n * Versions are created by PATCHing the document with custom attributions\n * (`type:version` + an optional `name`). The `list` endpoint returns the full\n * activity timeline, mapping `type:version` markers to named versions and every\n * other entry to a history-only snapshot, so the sidebar can show both the\n * named versions and the complete edit history.\n *\n * A version's id lives in immutable YHub attributions (`type:version` + `id`),\n * so it is fixed at creation time. Version *names*, however, are stored in a\n * mutable `__bn_version_names` map on the live collaboration doc (see\n * {@link VERSION_NAMES_MAP}), so `rename` is supported and simply updates that\n * store.\n *\n * @example\n * ```ts\n * import { withCollaboration } from \"@blocknote/core/y\";\n * import { createYHubVersioningEndpoints } from \"@blocknote/core/y\";\n *\n * const editor = BlockNoteEditor.create(\n * withCollaboration({\n * collaboration: {\n * fragment,\n * user: { name: \"Alice\", color: \"#ff0\" },\n * provider,\n * versioningEndpoints: createYHubVersioningEndpoints({\n * baseUrl: \"https://yhub.example.com\",\n * org: \"my-org\",\n * docId: \"my-doc\",\n * }),\n * },\n * }),\n * );\n * ```\n */\nexport function createYHubVersioningEndpoints(\n options: YHubVersioningOptions,\n): VersioningEndpointsFactory<Y.Type, Uint8Array, Y.ContentMap> {\n const {\n baseUrl,\n org,\n docId,\n headers = {},\n activityLimit = 50,\n group,\n } = options;\n\n const activityUrl = `${baseUrl}/activity/${org}/${docId}`;\n const changesetUrl = `${baseUrl}/changeset/${org}/${docId}`;\n const rollbackUrl = `${baseUrl}/rollback/${org}/${docId}`;\n\n return (editor) => {\n /**\n * The mutable per-id version-name store on the live collaboration doc.\n *\n * Returns the root {@link VERSION_NAMES_MAP} map-typed {@link Y.Type}, which\n * uses `setAttr`/`getAttr` for keyed access (this Yjs fork has a single\n * unified `Y.Type` rather than a distinct `Y.Map`). `undefined` until the\n * live doc has been captured from a `create` call.\n */\n const getVersionNamesMap = (): Y.Type | undefined => {\n const fragment =\n editor.getExtension<typeof YSyncExtension>(\"ySync\")?.fragment.doc;\n // `fragment` is undefined until the live doc has been captured (e.g. no\n // ySync extension attached yet); return undefined rather than throwing so\n // callers can gracefully fall back to the immutable name attribution.\n return fragment?.get(VERSION_NAMES_MAP);\n };\n\n /**\n * Build the synthetic \"current version\" snapshot, or `undefined` when the\n * live document matches the latest saved version (no edits since).\n *\n * Both lookups are made here, independently of the grouped `list()` request:\n *\n * - the newest activity entry of *any* kind (ungrouped, so its `to` is the\n * true last-edit time), and\n * - the newest **version marker** (via the `withCustomAttributions`\n * server-side filter).\n *\n * Deriving the marker time from `list()`'s grouped entries would be wrong:\n * with grouping (especially `mergeUsers`) the newest marker's group absorbs\n * the later unsaved edit, so the group's `to` equals the edit's `to` and the\n * comparison below can never fire. Fetching the marker unmerged avoids that.\n */\n const getCurrentVersionEntry = async (): Promise<\n VersionSnapshot | undefined\n > => {\n const latestParams = new URLSearchParams({\n order: \"desc\",\n limit: \"1\",\n customAttributions: \"true\",\n });\n const latestVersionParams = new URLSearchParams({\n order: \"desc\",\n limit: \"1\",\n customAttributions: \"true\",\n // Server-side filter to `type:version` markers only, so this ignores the\n // plain edits that would otherwise be the newest entries.\n withCustomAttributions: \"type:version\",\n });\n\n const [latestBuf, latestVersionBuf] = await Promise.all([\n yhubFetch(`${activityUrl}?${latestParams}`, headers),\n yhubFetch(`${activityUrl}?${latestVersionParams}`, headers),\n ]);\n const latestEdit = (\n decodeAny(new Uint8Array(latestBuf)) as YHubActivityEntry[]\n )[0];\n const latestVersion = (\n decodeAny(new Uint8Array(latestVersionBuf)) as YHubActivityEntry[]\n )[0];\n\n if (!latestEdit || latestEdit.to <= (latestVersion?.to ?? 0)) {\n return undefined;\n }\n\n // Build the synthetic entry directly rather than via `activityToSnapshot`,\n // whose `id` comes from a string-typed wire attribution — the current\n // entry's id is the `CURRENT_VERSION_ID` symbol, not a real version id.\n const by =\n latestEdit.by\n ?.split(\",\")\n .map((t) => t.trim())\n .filter(Boolean) ?? [];\n return {\n id: CURRENT_VERSION_ID,\n createdAt: latestEdit.to,\n updatedAt: latestEdit.to,\n by: by.length > 0 ? by : undefined,\n };\n };\n\n /**\n * PATCH the current document state to YHub, optionally with custom\n * attributions. Used both for creating named version markers and for\n * backing up the document before a restore.\n */\n const patchDoc = async (\n fragment: Y.Type,\n customAttributions: Array<{ k: string; v: any }>,\n by?: string,\n ) => {\n const doc = fragment.doc;\n if (!doc) {\n throw new Error(\n \"Cannot patch document: the Y.Type is not attached to a Y.Doc.\",\n );\n }\n\n // YHub only records custom attributions when they attach to NEW content\n // that survives its server-side diff. An update-less PATCH is rejected\n // (400 — \"at least one of update or awareness must be present\"), and even\n // if it weren't, there'd be no content for the attributions to ride on, so\n // no activity entry is created. YHub has no metadata-only marker path.\n //\n // So we introduce a tiny piece of novel content for the marker to attach\n // to: a single insert into a dedicated `__bn_version_markers` fragment that\n // the editor never renders. A fresh Y.Doc guarantees a clientID/content the\n // server has never seen, so the diff is non-empty and the attributions land\n // on it. The reconstructed document at this version's timestamp still\n // contains the full editor content — this marker only ever lives in the\n // throwaway fragment.\n const markerDoc = new Y.Doc();\n markerDoc.get(\"__bn_version_markers\", \"XmlFragment\").insert(0, [\"v\"]);\n const update = Y.encodeStateAsUpdate(markerDoc);\n\n const body: Record<string, unknown> = { update, customAttributions };\n\n await yhubFetch(\n `${baseUrl}/ydoc/${org}/${docId}${by ? `?userid=${by}` : \"\"}`,\n headers,\n {\n method: \"PATCH\",\n body: encodeAny(body) as BufferSource,\n },\n );\n };\n\n /**\n * Create a named version marker for the current document state by PATCHing\n * it with `type:version` custom attributions.\n */\n const create: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"create\"] = async (fragment, options) => {\n const id = String(uint32());\n const now = Date.now();\n\n if (options?.name) {\n getVersionNamesMap()?.setAttr(id, options.name);\n }\n\n const customAttributions: Array<{ k: string; v: string }> = [\n { k: \"type\", v: \"version\" },\n { k: \"id\", v: id },\n ];\n if (options?.name) {\n customAttributions.push({ k: \"name\", v: options.name });\n }\n\n const user = editor\n .getExtension<typeof YCursorExtension>(\"yCursor\")\n ?.getUser();\n await patchDoc(fragment, customAttributions, user?.id);\n\n return {\n id,\n name: options?.name,\n createdAt: now,\n updatedAt: now,\n by: user?.id,\n };\n };\n\n /**\n * Reconstruct the full document state as it was at a given `to` timestamp.\n *\n * The changeset endpoint builds `nextDoc` purely from the `to` timestamp\n * range — it ignores `withCustomAttributions` for doc reconstruction (that\n * filter only scopes the attribution overlay). So historical document state\n * can only be retrieved by timestamp, never by the version's `id`.\n */\n const getContentAt = async (to: number): Promise<Uint8Array> => {\n const params = new URLSearchParams({\n ydoc: \"true\",\n to: String(to),\n });\n\n const buf = await yhubFetch(`${changesetUrl}?${params}`, headers);\n const changeset = decodeAny(new Uint8Array(buf)) as YHubChangeset;\n\n if (!changeset.nextDoc) {\n throw new Error(`YHub returned no document state at timestamp ${to}.`);\n }\n\n return Y.convertUpdateFormatV1ToV2(changeset.nextDoc);\n };\n\n /**\n * Fetch the full document content for a saved version snapshot.\n *\n * The snapshot's `createdAt` is the activity entry's `to` timestamp (see\n * {@link activityToSnapshot}), which is exactly what the changeset API needs.\n */\n const getContent: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"getContent\"] = async (snapshot) => {\n return getContentAt(snapshot.createdAt);\n };\n\n /**\n * Fetch the authorship attributions for the changes between two snapshots\n * (or from the start of the document when `compareTo` is omitted).\n *\n * Snapshots carry their `to` timestamp directly in `createdAt`, so no\n * activity lookup is needed to resolve the changeset window.\n */\n const getAttributions: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"getAttributions\"] = async (snapshot, compareTo) => {\n const to = snapshot.createdAt;\n const from = compareTo !== undefined ? compareTo.createdAt : 0;\n\n const params = new URLSearchParams({\n from: String(from),\n to: String(to),\n attributions: \"true\",\n });\n\n const buf = await yhubFetch(`${changesetUrl}?${params}`, headers);\n const changeset = decodeAny(new Uint8Array(buf)) as YHubChangeset;\n\n if (!changeset.attributions) {\n throw new Error(\n `YHub returned no attributions for snapshot ${String(snapshot.id)}.`,\n );\n }\n\n return Y.decodeContentMap(changeset.attributions);\n };\n\n /**\n * Restore the document to a saved version: fetch the target version's\n * content and roll back everything after it.\n *\n * The snapshot's `createdAt` is the activity entry's `to` timestamp.\n */\n const restore: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"restore\"] = async (_fragment, snapshot) => {\n const to = snapshot.createdAt;\n const snapshotContent = await getContentAt(to);\n\n await yhubFetch(`${rollbackUrl}?from=${to}`, headers, {\n method: \"POST\",\n body: encodeAny({ from: to }) as BufferSource,\n });\n\n return snapshotContent;\n };\n\n /**\n * Rename a saved version by updating its entry in the mutable\n * {@link VERSION_NAMES_MAP} store on the live collaboration doc.\n *\n * The version's `id` remains fixed in its immutable YHub attributions —\n * only the editable name in the map is changed. Passing an empty or\n * `undefined` name clears the entry (falling back to the immutable `name`\n * attribution captured at creation time).\n */\n const rename: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"rename\"] = async (snapshot, name) => {\n if (typeof snapshot.id !== \"string\") {\n // CURRENT_VERSION_ID (symbol) is not renameable.\n return;\n }\n const map = getVersionNamesMap();\n if (!map) {\n throw new Error(\n \"Cannot rename version: no live collaboration document is available.\",\n );\n }\n if (name === undefined || name === \"\") {\n map.deleteAttr(snapshot.id);\n } else {\n map.setAttr(snapshot.id, name);\n }\n };\n\n /**\n * List the full version timeline (newest first), plus a synthetic\n * \"current version\" entry when the live document has unsaved edits.\n *\n * Returns the entire activity timeline: `type:version` markers are mapped\n * to named snapshots and every other entry to a history-only snapshot (see\n * {@link activityToSnapshot}), so the sidebar can offer both a \"named\n * versions\" and a full \"history\" view. Author user-ids are passed through\n * raw on {@link VersionSnapshot.by} — the view layer resolves them to user\n * info via the versioning extension's user store.\n */\n const list: VersioningEndpoints<\n Y.Type,\n Uint8Array,\n Y.ContentMap\n >[\"list\"] = async () => {\n // Read the grouping knobs fresh from `options` so a caller mutating the\n // object it passed in reconfigures grouping on the next refresh (see the\n // note where these are deliberately left out of the destructure above).\n const groupMaxGap = options.groupMaxGap ?? 10000;\n const groupMaxDuration = options.groupMaxDuration;\n const mergeUsers = options.mergeUsers;\n\n const params = new URLSearchParams({\n order: \"desc\",\n limit: String(activityLimit),\n customAttributions: \"true\",\n });\n // Always send a concrete `groupMaxGap`. Sending\n // `String(undefined)` here would make the server `parseInt(\"undefined\")`\n // to NaN, silently disabling grouping — which surfaces every same-`to`\n // attribution as its own history entry and produces duplicate React keys.\n params.set(\"groupMaxGap\", String(groupMaxGap));\n if (group !== undefined) {\n params.set(\"group\", String(group));\n }\n if (groupMaxDuration !== undefined) {\n params.set(\"groupMaxDuration\", String(groupMaxDuration));\n }\n if (mergeUsers !== undefined) {\n params.set(\"mergeUsers\", String(mergeUsers));\n }\n\n const buf = await yhubFetch(`${activityUrl}?${params}`, headers);\n const entries = decodeAny(new Uint8Array(buf)) as YHubActivityEntry[];\n\n const snapshots = sortSnapshotsNewestFirst(\n entries\n .map((entry, i) => activityToSnapshot(entry, i))\n .filter((s): s is VersionSnapshot => s !== undefined)\n // Prefer the mutable per-id name from the live doc's\n // `__bn_version_names` store over the immutable `name` attribution,\n // so renames (which only mutate that store) are reflected here.\n .map((snapshot) => {\n const attributionName = snapshot.name;\n const mappedName =\n (typeof snapshot.id === \"string\"\n ? (getVersionNamesMap()?.getAttr(snapshot.id) as\n | string\n | undefined)\n : undefined) ?? attributionName;\n return { ...snapshot, name: mappedName };\n }),\n );\n\n // Surface a \"current version\" entry when the live document has edits\n // beyond the most recent saved version marker. `getCurrentVersionEntry`\n // makes its own unmerged lookups (see there), so it is unaffected by the\n // grouping/mergeUsers params used for the list above.\n //\n // This only re-evaluates when `list()` runs (sidebar open / refresh),\n // which matches how YHub versions load today.\n const currentEntry = await getCurrentVersionEntry();\n return currentEntry ? [currentEntry, ...snapshots] : snapshots;\n };\n\n return {\n list,\n create,\n getContent,\n getAttributions,\n restore,\n rename,\n };\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAuDA,IAAa,KACX,GACA,MAEA,MAAW,KAAA,IACP,KAAA,IACA,OAAO,KAAW,WAChB,IACA,EAAO,IAmBT,KAEF,GACA,OAKD,EAAE,SAAM,gBAAgD;CACvD,IAAM,IAAS,GAAS,QAIlB,IACJ,MAAS,WACL,QACA,MAAS,WACP,QACA,IACE,SACA,OACJ,IAAM,SAAS,cAAc,CAAG;CAMtC,AAJA,OAAO,OAAO,EAAI,SAAS;EACzB,SAAS,KAAK,UAAU,EAAK,MAAM,OAAU;EAC7C,QAAQ,OAAO,CAAM;CACvB,CAAC,GACG,MAAS,mBACX,EAAI,QAAQ,OAAU,gBACtB,EAAI,QAAQ,SAAY,KAAK,UAAU,EAAK,MAAM,MAAS;CAS7D,IAAM,IAAmB,EACvB,GAAS,8BAA8B;EACrC,aAAa,IAAS,mBAAmB;EACzC,kBAAkB,MAAS,iBAAiB,WAAW;CACzD,CAAC,GACD,SACF,GAcM,KADW,EAAK,MAAM,WAAkC,CAAC,EAAA,CACvC,IAClB,IAAW,IACb,EAAuB,CAAO,IAC9B,EA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