@blocknote/core 0.53.0 → 0.54.1

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 (219) 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-CTB_5Vyj.cjs +12 -0
  12. package/dist/blocks-CTB_5Vyj.cjs.map +1 -0
  13. package/dist/{blocks-BEJW9OM_.js → blocks-Dol85hpH.js} +680 -670
  14. package/dist/blocks-Dol85hpH.js.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-B7Rsp_Ft.cjs +58 -0
  26. package/dist/extensions-B7Rsp_Ft.cjs.map +1 -0
  27. package/dist/{extensions-BR5hkAio.js → extensions-D6JcJCwd.js} +735 -457
  28. package/dist/extensions-D6JcJCwd.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-Buuo5l7X.js} +924 -851
  36. package/dist/src-Buuo5l7X.js.map +1 -0
  37. package/dist/src-C54Ok9E0.cjs +23 -0
  38. package/dist/src-C54Ok9E0.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 +245 -242
  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 +79 -67
  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/getBlocksChangedByTransaction.test.ts +211 -0
  55. package/src/api/getBlocksChangedByTransaction.ts +48 -12
  56. package/src/api/getChangedRange.ts +54 -0
  57. package/src/api/nodeConversions/blockToNode.ts +5 -1
  58. package/src/api/nodeConversions/nodeToBlock.ts +3 -0
  59. package/src/api/pmUtil.ts +16 -8
  60. package/src/blocks/Code/CodeBlockOptions.ts +83 -0
  61. package/src/blocks/Code/block.test.ts +1 -1
  62. package/src/blocks/Code/block.ts +29 -281
  63. package/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.ts +120 -0
  64. package/src/blocks/Code/helpers/parse/parsePreCode.ts +45 -0
  65. package/src/blocks/Code/helpers/render/createCodeBlock.ts +98 -0
  66. package/src/blocks/Code/helpers/toExternalHTML/createPreCode.ts +14 -0
  67. package/src/blocks/index.ts +3 -1
  68. package/src/comments/extension.ts +2 -4
  69. package/src/editor/Block.css +273 -2
  70. package/src/editor/BlockNoteEditor.ts +25 -4
  71. package/src/editor/BlockNoteExtension.ts +1 -1
  72. package/src/editor/managers/ExtensionManager/extensions.ts +6 -0
  73. package/src/editor/performance.test.ts +45 -0
  74. package/src/exporter/ExportImage.ts +50 -0
  75. package/src/exporter/Exporter.test.ts +186 -0
  76. package/src/exporter/Exporter.ts +66 -14
  77. package/src/exporter/index.ts +1 -0
  78. package/src/exporter/mapping.ts +4 -1
  79. package/src/extensions/FormattingToolbar/FormattingToolbar.ts +10 -8
  80. package/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.ts +109 -0
  81. package/src/extensions/PreviousBlockType/PreviousBlockType.test.ts +21 -0
  82. package/src/extensions/PreviousBlockType/PreviousBlockType.ts +5 -3
  83. package/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.ts +192 -0
  84. package/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.ts +142 -0
  85. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.ts +120 -0
  86. package/src/extensions/SyntaxHighlighting/SyntaxHighlighting.ts +89 -0
  87. package/src/extensions/SyntaxHighlighting/inlineHighlight.test.ts +135 -0
  88. package/src/extensions/SyntaxHighlighting/shiki.ts +116 -0
  89. package/src/extensions/TableHandles/TableHandles.browser.test.ts +305 -0
  90. package/src/extensions/TableHandles/TableHandles.ts +82 -41
  91. package/src/extensions/Versioning/inMemoryVersioning.ts +13 -3
  92. package/src/extensions/index.ts +4 -0
  93. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.test.ts +51 -2
  94. package/src/extensions/tiptap-extensions/KeyboardShortcuts/KeyboardShortcutsExtension.ts +22 -2
  95. package/src/fonts/inter.css +9 -9
  96. package/src/i18n/locales/ar.ts +9 -0
  97. package/src/i18n/locales/de.ts +9 -0
  98. package/src/i18n/locales/en.ts +9 -0
  99. package/src/i18n/locales/es.ts +9 -0
  100. package/src/i18n/locales/fa.ts +9 -0
  101. package/src/i18n/locales/fr.ts +9 -0
  102. package/src/i18n/locales/he.ts +9 -0
  103. package/src/i18n/locales/hr.ts +9 -0
  104. package/src/i18n/locales/is.ts +9 -0
  105. package/src/i18n/locales/it.ts +9 -0
  106. package/src/i18n/locales/ja.ts +9 -0
  107. package/src/i18n/locales/ko.ts +9 -0
  108. package/src/i18n/locales/nl.ts +9 -0
  109. package/src/i18n/locales/no.ts +9 -0
  110. package/src/i18n/locales/pl.ts +9 -0
  111. package/src/i18n/locales/pt.ts +9 -0
  112. package/src/i18n/locales/ru.ts +9 -0
  113. package/src/i18n/locales/sk.ts +9 -0
  114. package/src/i18n/locales/uk.ts +9 -0
  115. package/src/i18n/locales/uz.ts +9 -0
  116. package/src/i18n/locales/vi.ts +9 -0
  117. package/src/i18n/locales/zh-tw.ts +9 -0
  118. package/src/i18n/locales/zh.ts +9 -0
  119. package/src/index.ts +5 -1
  120. package/src/schema/blocks/createSpec.ts +5 -7
  121. package/src/schema/blocks/types.ts +53 -3
  122. package/src/schema/contentTypePropagation.test.ts +1 -1
  123. package/src/schema/inlineContent/createSpec.test.ts +161 -0
  124. package/src/schema/inlineContent/createSpec.ts +175 -17
  125. package/src/schema/inlineContent/internal.ts +21 -2
  126. package/src/schema/inlineContent/types.ts +35 -5
  127. package/src/schema/nodeViewMutations.test.ts +81 -56
  128. package/src/schema/nodeViewMutations.ts +30 -27
  129. package/src/schema/schema.test.ts +110 -0
  130. package/src/schema/schema.ts +13 -4
  131. package/src/user/UserStore.ts +1 -1
  132. package/src/util/Store.test.ts +132 -0
  133. package/src/util/Store.ts +135 -0
  134. package/src/y/extensions/AttributionExtension.test.ts +18 -9
  135. package/src/y/extensions/Versioning.test.ts +20 -7
  136. package/src/y/versioning/__test__/yhub.test.ts +92 -59
  137. package/src/y/versioning/yhub.ts +35 -29
  138. package/src/yjs/comments/RESTYjsThreadStore.test.ts +85 -0
  139. package/src/yjs/comments/RESTYjsThreadStore.ts +7 -1
  140. package/src/yjs/extensions/ForkYDoc.test.ts +37 -0
  141. package/src/yjs/extensions/ForkYDoc.ts +26 -1
  142. package/src/yjs/extensions/RelativePositionMapping.ts +8 -2
  143. package/src/yjs/extensions/Versioning.test.ts +17 -5
  144. package/types/src/api/getChangedRange.d.ts +14 -0
  145. package/types/src/api/pmUtil.d.ts +7 -7
  146. package/types/src/blocks/Code/CodeBlockOptions.d.ts +61 -0
  147. package/types/src/blocks/Code/block.d.ts +3 -53
  148. package/types/src/blocks/Code/helpers/extensions/CodeKeyboardShortcutsExtension.d.ts +35 -0
  149. package/types/src/blocks/Code/helpers/parse/parsePreCode.d.ts +8 -0
  150. package/types/src/blocks/Code/helpers/render/createCodeBlock.d.ts +18 -0
  151. package/types/src/blocks/Code/helpers/toExternalHTML/createPreCode.d.ts +5 -0
  152. package/types/src/blocks/index.d.ts +3 -1
  153. package/types/src/comments/extension.d.ts +1 -15
  154. package/types/src/editor/BlockNoteEditor.d.ts +3 -3
  155. package/types/src/editor/BlockNoteExtension.d.ts +1 -1
  156. package/types/src/exporter/ExportImage.d.ts +36 -0
  157. package/types/src/exporter/Exporter.d.ts +30 -0
  158. package/types/src/exporter/Exporter.test.d.ts +1 -0
  159. package/types/src/exporter/index.d.ts +1 -0
  160. package/types/src/exporter/mapping.d.ts +1 -1
  161. package/types/src/extensions/FilePanel/FilePanel.d.ts +1 -1
  162. package/types/src/extensions/FormattingToolbar/FormattingToolbar.d.ts +1 -1
  163. package/types/src/extensions/InlineContentBoundaryEdit/InlineContentBoundaryEdit.d.ts +5 -0
  164. package/types/src/extensions/ShowSelection/ShowSelection.d.ts +1 -5
  165. package/types/src/extensions/SideMenu/SideMenu.d.ts +1 -1
  166. package/types/src/extensions/SourceBlockWithPreview/SourceBlockWithPreview.d.ts +45 -0
  167. package/types/src/extensions/SourceInlineContentWithPreview/SourceInlineContentWithPreview.d.ts +49 -0
  168. package/types/src/extensions/SuggestionMenu/SuggestionMenu.d.ts +1 -11
  169. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.d.ts +44 -0
  170. package/types/src/extensions/SyntaxHighlighting/SyntaxHighlighting.test.d.ts +1 -0
  171. package/types/src/extensions/SyntaxHighlighting/inlineHighlight.test.d.ts +1 -0
  172. package/types/src/extensions/SyntaxHighlighting/shiki.d.ts +13 -0
  173. package/types/src/extensions/TableHandles/TableHandles.browser.test.d.ts +1 -0
  174. package/types/src/extensions/TableHandles/TableHandles.d.ts +4 -1
  175. package/types/src/extensions/Versioning/Versioning.d.ts +1 -35
  176. package/types/src/extensions/index.d.ts +4 -0
  177. package/types/src/i18n/locales/en.d.ts +9 -0
  178. package/types/src/i18n/locales/fa.d.ts +9 -0
  179. package/types/src/i18n/locales/sk.d.ts +9 -0
  180. package/types/src/index.d.ts +2 -1
  181. package/types/src/schema/blocks/types.d.ts +36 -3
  182. package/types/src/schema/inlineContent/createSpec.d.ts +35 -3
  183. package/types/src/schema/inlineContent/createSpec.test.d.ts +1 -0
  184. package/types/src/schema/inlineContent/internal.d.ts +3 -2
  185. package/types/src/schema/inlineContent/types.d.ts +22 -3
  186. package/types/src/schema/nodeViewMutations.d.ts +2 -2
  187. package/types/src/schema/schema.test.d.ts +1 -0
  188. package/types/src/user/UserStore.d.ts +1 -1
  189. package/types/src/util/Store.d.ts +45 -0
  190. package/types/src/util/Store.test.d.ts +1 -0
  191. package/types/src/y/extensions/AttributionExtension.d.ts +1 -1
  192. package/types/src/y/extensions/DiffVersioningExtension.d.ts +1 -1
  193. package/types/src/y/extensions/ForkYDoc.d.ts +1 -5
  194. package/types/src/y/extensions/index.d.ts +2 -10
  195. package/types/src/y/versioning/yhub.d.ts +3 -2
  196. package/types/src/yjs/comments/RESTYjsThreadStore.test.d.ts +1 -0
  197. package/types/src/yjs/extensions/ForkYDoc.d.ts +1 -5
  198. package/types/src/yjs/extensions/index.d.ts +1 -5
  199. package/dist/BlockNoteExtension-B_IEJHS8.js +0 -38
  200. package/dist/BlockNoteExtension-B_IEJHS8.js.map +0 -1
  201. package/dist/BlockNoteExtension-M0znTzxP.cjs +0 -2
  202. package/dist/BlockNoteExtension-M0znTzxP.cjs.map +0 -1
  203. package/dist/UserStore-QbbPFj0z.js.map +0 -1
  204. package/dist/blocks-BEJW9OM_.js.map +0 -1
  205. package/dist/blocks-BPmJglFo.cjs +0 -12
  206. package/dist/blocks-BPmJglFo.cjs.map +0 -1
  207. package/dist/en-C5Z4Ag9_.cjs +0 -2
  208. package/dist/en-C5Z4Ag9_.cjs.map +0 -1
  209. package/dist/en-CGKXfWma.js.map +0 -1
  210. package/dist/extensions-AitWbYdJ.cjs +0 -52
  211. package/dist/extensions-AitWbYdJ.cjs.map +0 -1
  212. package/dist/extensions-BR5hkAio.js.map +0 -1
  213. package/dist/src-BHPuOo7W.cjs +0 -21
  214. package/dist/src-BHPuOo7W.cjs.map +0 -1
  215. package/dist/src-BUXut68d.js.map +0 -1
  216. package/src/blocks/Code/shiki.ts +0 -73
  217. package/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.ts +0 -20
  218. package/types/src/blocks/Code/shiki.d.ts +0 -4
  219. package/types/src/blocks/File/helpers/uploadToTmpFilesDotOrg_DEV_ONLY.d.ts +0 -6
@@ -0,0 +1,135 @@
1
+ // Vendored from https://github.com/TanStack/store/blob/main/packages/store/src/store.ts (MIT)
2
+ //
3
+ // BlockNote only ever used `Store` — never the `Derived`/`Effect` reactive graph that
4
+ // makes up the rest of `@tanstack/store`, and that graph isn't tree-shakeable because
5
+ // `setState` reaches into the scheduler. Since the `Store` type is part of BlockNote's
6
+ // public API (every extension exposes one), owning these ~40 lines lets us keep that
7
+ // surface stable instead of tracking a 0.x dependency's breaking changes.
8
+ //
9
+ // Behaviour matches `@tanstack/store@0.7.7`, minus the dependency graph and the
10
+ // `updateFn`/`onSubscribe` options, which nothing used. `onUpdate` additionally receives
11
+ // the new and previous state rather than requiring callers to close over the store.
12
+
13
+ /**
14
+ * The value a {@link Listener} is called with when a {@link Store} updates.
15
+ */
16
+ export interface ListenerValue<T> {
17
+ readonly prevVal: T;
18
+ readonly currentVal: T;
19
+ }
20
+
21
+ /**
22
+ * A callback invoked when a {@link Store}'s state changes.
23
+ */
24
+ export type Listener<T> = (value: ListenerValue<T>) => void;
25
+
26
+ /**
27
+ * A new state, or a function deriving it from the previous state.
28
+ */
29
+ export type Updater<T> = T | ((prev: T) => T);
30
+
31
+ export interface StoreOptions<TState> {
32
+ /**
33
+ * Called after the state has been updated, before listeners are notified.
34
+ */
35
+ onUpdate?: (state: TState, prevState: TState) => void;
36
+ }
37
+
38
+ /**
39
+ * A minimal observable state container.
40
+ *
41
+ * Extensions expose one of these as their `store` so that both React (via
42
+ * `useExtensionState`) and vanilla consumers can read and subscribe to their state.
43
+ */
44
+ export class Store<TState> {
45
+ listeners = new Set<Listener<TState>>();
46
+ state: TState;
47
+ prevState: TState;
48
+ options?: StoreOptions<TState>;
49
+
50
+ constructor(initialState: TState, options?: StoreOptions<TState>) {
51
+ this.prevState = initialState;
52
+ this.state = initialState;
53
+ this.options = options;
54
+ }
55
+
56
+ subscribe = (listener: Listener<TState>) => {
57
+ this.listeners.add(listener);
58
+
59
+ return () => {
60
+ this.listeners.delete(listener);
61
+ };
62
+ };
63
+
64
+ /**
65
+ * Update the store state, either with a new state or a function deriving it from the
66
+ * previous one.
67
+ */
68
+ setState(updater: (prevState: TState) => TState): void;
69
+ setState(updater: TState): void;
70
+ setState(updater: Updater<TState>): void {
71
+ this.prevState = this.state;
72
+ this.state = isUpdaterFunction(updater) ? updater(this.prevState) : updater;
73
+
74
+ flush(this);
75
+ }
76
+
77
+ /**
78
+ * @internal Only to be called by {@link flush}.
79
+ */
80
+ _notify() {
81
+ const value: ListenerValue<TState> = {
82
+ prevVal: this.prevState,
83
+ currentVal: this.state,
84
+ };
85
+ for (const listener of this.listeners) {
86
+ listener(value);
87
+ }
88
+ }
89
+ }
90
+
91
+ function isUpdaterFunction<T>(updater: Updater<T>): updater is (prev: T) => T {
92
+ return typeof updater === "function";
93
+ }
94
+
95
+ // Both `onUpdate` and listener notification can synchronously trigger further `setState`
96
+ // calls — a callback that dispatches a ProseMirror transaction, for example. Rather than
97
+ // recursing, re-entrant writes are queued and drained by the outermost flush, so a write
98
+ // made during a callback still reaches every listener but the stack stays flat and
99
+ // subscribers see the settled state once.
100
+ //
101
+ // `onUpdate` therefore runs *inside* the flush transaction: the store is queued and the
102
+ // flush is marked in progress before the callback fires, so a nested write it makes is
103
+ // coalesced into this drain rather than starting its own.
104
+ let isFlushing = false;
105
+ const pendingUpdates = new Set<Store<any>>();
106
+
107
+ function flush(store: Store<any>) {
108
+ pendingUpdates.add(store);
109
+
110
+ const isOutermost = !isFlushing;
111
+ isFlushing = true;
112
+
113
+ try {
114
+ store.options?.onUpdate?.(store.state, store.prevState);
115
+
116
+ if (!isOutermost) {
117
+ return;
118
+ }
119
+
120
+ while (pendingUpdates.size > 0) {
121
+ const stores = Array.from(pendingUpdates);
122
+ pendingUpdates.clear();
123
+ for (const pendingStore of stores) {
124
+ pendingStore._notify();
125
+ }
126
+ }
127
+ } finally {
128
+ if (isOutermost) {
129
+ isFlushing = false;
130
+ // A throwing callback would otherwise strand queued stores, letting them notify
131
+ // during an unrelated store's next flush.
132
+ pendingUpdates.clear();
133
+ }
134
+ }
135
+ }
@@ -7,25 +7,31 @@ import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js";
7
7
  import type { User } from "../../user/index.js";
8
8
  import { AttributionExtension } from "./AttributionExtension.js";
9
9
 
10
+ // Editors created during a test, destroyed in afterEach: an undestroyed
11
+ // EditorView leaves ProseMirror DOMObserver timers behind, which fire after
12
+ // the jsdom environment is torn down ("document is not defined" as an
13
+ // unhandled error - flaky, timing-dependent, mostly on slow CI).
14
+ const editors: BlockNoteEditor[] = [];
15
+
10
16
  // A `resolveUsers` spy plus an editor with the AttributionExtension registered.
11
17
  // No Yjs/collaboration needed — the extension's load plugin only cares that a
12
18
  // transaction adds a `y-attributed-*` mark, which we do directly below.
13
19
  function createEditor() {
14
- const resolveUsers = vi.fn(
15
- async (ids: string[]): Promise<User[]> =>
16
- ids.map((id) => ({
17
- id,
18
- username: `name-${id}`,
19
- avatarUrl: "",
20
- color: "#123456",
21
- colorLight: "#abcdef",
22
- })),
20
+ const resolveUsers = vi.fn(async (ids: string[]): Promise<User[]> =>
21
+ ids.map((id) => ({
22
+ id,
23
+ username: `name-${id}`,
24
+ avatarUrl: "",
25
+ color: "#123456",
26
+ colorLight: "#abcdef",
27
+ })),
23
28
  );
24
29
 
25
30
  const editor = BlockNoteEditor.create({
26
31
  extensions: [AttributionExtension({ resolveUsers })],
27
32
  });
28
33
  editor.mount(document.createElement("div"));
34
+ editors.push(editor);
29
35
 
30
36
  return { editor, resolveUsers };
31
37
  }
@@ -47,6 +53,9 @@ function addInsertMark(editor: BlockNoteEditor, userIds: string[]) {
47
53
 
48
54
  describe("AttributionExtension user loading", () => {
49
55
  afterEach(() => {
56
+ for (const editor of editors.splice(0)) {
57
+ editor._tiptapEditor.destroy();
58
+ }
50
59
  vi.restoreAllMocks();
51
60
  });
52
61
 
@@ -31,15 +31,26 @@ function createInMemoryYjsEndpoints(): VersioningEndpoints<Y.Type, Uint8Array> {
31
31
  >();
32
32
  const contents = new Map<string, Uint8Array>();
33
33
 
34
+ // `Date.now()` only has millisecond resolution, so snapshots created in quick
35
+ // succession (as they are in these tests) can share a timestamp, which leaves
36
+ // the newest-first ordering of `list()` ambiguous. Hand out strictly
37
+ // increasing timestamps so creation order is always recoverable.
38
+ let lastTimestamp = 0;
39
+ function nextTimestamp() {
40
+ lastTimestamp = Math.max(Date.now(), lastTimestamp + 1);
41
+ return lastTimestamp;
42
+ }
43
+
34
44
  return {
35
45
  list: async () =>
36
46
  [...snapshots.values()].sort((a, b) => b.createdAt - a.createdAt),
37
47
  create: async (fragment, options) => {
48
+ const now = nextTimestamp();
38
49
  const snapshot = {
39
50
  id: crypto.randomUUID(),
40
51
  name: options?.name,
41
- createdAt: Date.now(),
42
- updatedAt: Date.now(),
52
+ createdAt: now,
53
+ updatedAt: now,
43
54
  restoredFromSnapshotId: options?.restoredFromSnapshot?.id
44
55
  ? String(options.restoredFromSnapshot.id)
45
56
  : undefined,
@@ -57,11 +68,12 @@ function createInMemoryYjsEndpoints(): VersioningEndpoints<Y.Type, Uint8Array> {
57
68
  },
58
69
  restore: async (fragment, snapshot) => {
59
70
  // Create backup
71
+ const backupTimestamp = nextTimestamp();
60
72
  const backup = {
61
73
  id: crypto.randomUUID(),
62
74
  name: "Backup",
63
- createdAt: Date.now(),
64
- updatedAt: Date.now(),
75
+ createdAt: backupTimestamp,
76
+ updatedAt: backupTimestamp,
65
77
  };
66
78
  contents.set(backup.id, Y.encodeStateAsUpdateV2(fragment.doc!));
67
79
  snapshots.set(backup.id, backup);
@@ -70,11 +82,12 @@ function createInMemoryYjsEndpoints(): VersioningEndpoints<Y.Type, Uint8Array> {
70
82
  const tempDoc = new Y.Doc();
71
83
  Y.applyUpdateV2(tempDoc, snapshotContent);
72
84
 
85
+ const restoredTimestamp = nextTimestamp();
73
86
  const restored = {
74
87
  id: crypto.randomUUID(),
75
88
  name: "Restored Snapshot",
76
- createdAt: Date.now(),
77
- updatedAt: Date.now(),
89
+ createdAt: restoredTimestamp,
90
+ updatedAt: restoredTimestamp,
78
91
  restoredFromSnapshotId: String(snapshot.id),
79
92
  };
80
93
  contents.set(restored.id, Y.encodeStateAsUpdateV2(tempDoc));
@@ -89,7 +102,7 @@ function createInMemoryYjsEndpoints(): VersioningEndpoints<Y.Type, Uint8Array> {
89
102
  throw new Error(`Snapshot ${String(snapshot.id)} not found`);
90
103
  }
91
104
  s.name = name;
92
- s.updatedAt = Date.now();
105
+ s.updatedAt = nextTimestamp();
93
106
  },
94
107
  };
95
108
  }
@@ -66,15 +66,12 @@ const SNAPSHOT_2: VersionSnapshot = {
66
66
 
67
67
  const PATCH_RESPONSE = { success: true, message: "Document updated" };
68
68
 
69
- function makeChangeset(
70
- opts: { nextDoc?: boolean; attributions?: boolean } = {},
71
- ) {
69
+ function makeChangeset(opts: { ydoc?: boolean; attributions?: boolean } = {}) {
72
70
  const doc = new Y.Doc();
73
71
  const frag = doc.get("default", "XmlFragment");
74
72
  frag.insert(0, ["hello"]);
75
73
  return {
76
- prevDoc: Y.encodeStateAsUpdate(new Y.Doc()),
77
- ...(opts.nextDoc !== false ? { nextDoc: Y.encodeStateAsUpdate(doc) } : {}),
74
+ ...(opts.ydoc !== false ? { ydoc: Y.encodeStateAsUpdate(doc) } : {}),
78
75
  ...(opts.attributions ? { attributions: new Uint8Array([0]) } : {}),
79
76
  };
80
77
  }
@@ -83,7 +80,7 @@ function makeChangeset(
83
80
  // Helpers
84
81
  // ---------------------------------------------------------------------------
85
82
 
86
- const BASE_URL = "https://yhub.test";
83
+ const BASE_URL = "https://yhub.test/api";
87
84
  const ORG = "test-org";
88
85
  const DOC_ID = "test-doc";
89
86
 
@@ -162,13 +159,17 @@ describe("createYHubVersioningEndpoints", () => {
162
159
  describe("list", () => {
163
160
  it("returns version-tagged entries using the id attribution as snapshot id", async () => {
164
161
  fetchSpy.mockResolvedValueOnce(
165
- mockFetchResponse([VERSION_ENTRY_2, VERSION_ENTRY_1]),
162
+ mockFetchResponse({ activity: [VERSION_ENTRY_2, VERSION_ENTRY_1] }),
166
163
  );
167
164
  // Current-version probe: latest edit of any kind, then latest version
168
165
  // marker. Both are VERSION_ENTRY_2, so latest edit == latest marker → no
169
166
  // synthetic "current version" entry.
170
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
171
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
167
+ fetchSpy.mockResolvedValueOnce(
168
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
169
+ );
170
+ fetchSpy.mockResolvedValueOnce(
171
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
172
+ );
172
173
 
173
174
  const endpoints = makeEndpoints();
174
175
  const snapshots = await endpoints.list();
@@ -188,16 +189,16 @@ describe("createYHubVersioningEndpoints", () => {
188
189
  it("fetches the full activity timeline (no type:version filter) with grouping defaults", async () => {
189
190
  // 1: full activity timeline. 2: latest edit of any kind. 3: latest
190
191
  // version marker (the current-version probe makes both 2 & 3).
191
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
192
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
193
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
192
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
193
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
194
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
194
195
 
195
196
  const endpoints = makeEndpoints();
196
197
  await endpoints.list();
197
198
 
198
199
  expect(fetchSpy).toHaveBeenCalledTimes(3);
199
200
  const versionUrl = new URL(fetchSpy.mock.calls[0][0] as string);
200
- expect(versionUrl.pathname).toBe(`/activity/${ORG}/${DOC_ID}`);
201
+ expect(versionUrl.pathname).toBe(`/api/activity/v1/${ORG}/${DOC_ID}`);
201
202
  // The `type:version` overlay filter is dropped so history entries are
202
203
  // returned too.
203
204
  expect(versionUrl.searchParams.get("withCustomAttributions")).toBe(null);
@@ -207,14 +208,14 @@ describe("createYHubVersioningEndpoints", () => {
207
208
 
208
209
  // Probe A: the latest entry of *any* type (no marker filter).
209
210
  const latestUrl = new URL(fetchSpy.mock.calls[1][0] as string);
210
- expect(latestUrl.pathname).toBe(`/activity/${ORG}/${DOC_ID}`);
211
+ expect(latestUrl.pathname).toBe(`/api/activity/v1/${ORG}/${DOC_ID}`);
211
212
  expect(latestUrl.searchParams.get("limit")).toBe("1");
212
213
  expect(latestUrl.searchParams.has("withCustomAttributions")).toBe(false);
213
214
 
214
215
  // Probe B: the latest *version marker*, server-filtered to `type:version`
215
216
  // so grouping/mergeUsers can't conflate it with a later edit.
216
217
  const markerUrl = new URL(fetchSpy.mock.calls[2][0] as string);
217
- expect(markerUrl.pathname).toBe(`/activity/${ORG}/${DOC_ID}`);
218
+ expect(markerUrl.pathname).toBe(`/api/activity/v1/${ORG}/${DOC_ID}`);
218
219
  expect(markerUrl.searchParams.get("limit")).toBe("1");
219
220
  expect(markerUrl.searchParams.get("withCustomAttributions")).toBe(
220
221
  "type:version",
@@ -222,9 +223,9 @@ describe("createYHubVersioningEndpoints", () => {
222
223
  });
223
224
 
224
225
  it("forwards group + groupMaxDuration params when configured", async () => {
225
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
226
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
227
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
226
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
227
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
228
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
228
229
 
229
230
  const endpoints = createYHubVersioningEndpoints({
230
231
  baseUrl: BASE_URL,
@@ -242,9 +243,9 @@ describe("createYHubVersioningEndpoints", () => {
242
243
  });
243
244
 
244
245
  it("omits group params by default while keeping the groupMaxGap default", async () => {
245
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
246
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
247
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
246
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
247
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
248
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
248
249
 
249
250
  // `makeEndpoints` builds the factory with no group/groupMaxDuration opts.
250
251
  const endpoints = makeEndpoints();
@@ -275,12 +276,16 @@ describe("createYHubVersioningEndpoints", () => {
275
276
  by: "user-2",
276
277
  };
277
278
  fetchSpy.mockResolvedValueOnce(
278
- mockFetchResponse([namedEntry, historyEntry]),
279
+ mockFetchResponse({ activity: [namedEntry, historyEntry] }),
279
280
  );
280
281
  // Current-version probe: latest edit == latest marker == `namedEntry`, so
281
282
  // no synthetic current row.
282
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([namedEntry]));
283
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([namedEntry]));
283
+ fetchSpy.mockResolvedValueOnce(
284
+ mockFetchResponse({ activity: [namedEntry] }),
285
+ );
286
+ fetchSpy.mockResolvedValueOnce(
287
+ mockFetchResponse({ activity: [namedEntry] }),
288
+ );
284
289
 
285
290
  const endpoints = makeEndpoints();
286
291
  const snapshots = await endpoints.list();
@@ -297,9 +302,9 @@ describe("createYHubVersioningEndpoints", () => {
297
302
  });
298
303
 
299
304
  it("returns empty array when no versions exist", async () => {
300
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
301
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
302
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
305
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
306
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
307
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
303
308
 
304
309
  const endpoints = makeEndpoints();
305
310
  const snapshots = await endpoints.list();
@@ -309,10 +314,14 @@ describe("createYHubVersioningEndpoints", () => {
309
314
 
310
315
  it("sorts snapshots newest-first", async () => {
311
316
  fetchSpy.mockResolvedValueOnce(
312
- mockFetchResponse([VERSION_ENTRY_1, VERSION_ENTRY_2]),
317
+ mockFetchResponse({ activity: [VERSION_ENTRY_1, VERSION_ENTRY_2] }),
318
+ );
319
+ fetchSpy.mockResolvedValueOnce(
320
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
321
+ );
322
+ fetchSpy.mockResolvedValueOnce(
323
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
313
324
  );
314
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
315
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
316
325
 
317
326
  const endpoints = makeEndpoints();
318
327
  const snapshots = await endpoints.list();
@@ -328,10 +337,14 @@ describe("createYHubVersioningEndpoints", () => {
328
337
  customAttributions: [{ k: "type", v: "version" }],
329
338
  };
330
339
  fetchSpy.mockResolvedValueOnce(
331
- mockFetchResponse([VERSION_ENTRY_1, noIdEntry]),
340
+ mockFetchResponse({ activity: [VERSION_ENTRY_1, noIdEntry] }),
341
+ );
342
+ fetchSpy.mockResolvedValueOnce(
343
+ mockFetchResponse({ activity: [VERSION_ENTRY_1] }),
344
+ );
345
+ fetchSpy.mockResolvedValueOnce(
346
+ mockFetchResponse({ activity: [VERSION_ENTRY_1] }),
332
347
  );
333
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_1]));
334
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_1]));
335
348
 
336
349
  const endpoints = makeEndpoints();
337
350
  const snapshots = await endpoints.list();
@@ -361,9 +374,15 @@ describe("createYHubVersioningEndpoints", () => {
361
374
  };
362
375
  // 1: activity fetch. 2 & 3: current-version probe (latest edit, latest
363
376
  // marker) — same entry both times, so no newer edit and no current row.
364
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([versionEntry]));
365
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([versionEntry]));
366
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([versionEntry]));
377
+ fetchSpy.mockResolvedValueOnce(
378
+ mockFetchResponse({ activity: [versionEntry] }),
379
+ );
380
+ fetchSpy.mockResolvedValueOnce(
381
+ mockFetchResponse({ activity: [versionEntry] }),
382
+ );
383
+ fetchSpy.mockResolvedValueOnce(
384
+ mockFetchResponse({ activity: [versionEntry] }),
385
+ );
367
386
 
368
387
  const snapshots = await endpoints.list();
369
388
 
@@ -373,9 +392,15 @@ describe("createYHubVersioningEndpoints", () => {
373
392
  });
374
393
 
375
394
  it("falls back to the attribution name when the store has no entry", async () => {
376
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_1]));
377
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_1]));
378
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_1]));
395
+ fetchSpy.mockResolvedValueOnce(
396
+ mockFetchResponse({ activity: [VERSION_ENTRY_1] }),
397
+ );
398
+ fetchSpy.mockResolvedValueOnce(
399
+ mockFetchResponse({ activity: [VERSION_ENTRY_1] }),
400
+ );
401
+ fetchSpy.mockResolvedValueOnce(
402
+ mockFetchResponse({ activity: [VERSION_ENTRY_1] }),
403
+ );
379
404
 
380
405
  const endpoints = makeEndpoints();
381
406
  const snapshots = await endpoints.list();
@@ -395,10 +420,14 @@ describe("createYHubVersioningEndpoints", () => {
395
420
  // version marker (VERSION_ENTRY_2). Since latestEdit.to > VERSION_ENTRY_2.to
396
421
  // a synthetic current-version row is prepended.
397
422
  fetchSpy.mockResolvedValueOnce(
398
- mockFetchResponse([VERSION_ENTRY_2, VERSION_ENTRY_1]),
423
+ mockFetchResponse({ activity: [VERSION_ENTRY_2, VERSION_ENTRY_1] }),
424
+ );
425
+ fetchSpy.mockResolvedValueOnce(
426
+ mockFetchResponse({ activity: [latestEdit] }),
427
+ );
428
+ fetchSpy.mockResolvedValueOnce(
429
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
399
430
  );
400
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([latestEdit]));
401
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
402
431
 
403
432
  const endpoints = makeEndpoints();
404
433
  const snapshots = await endpoints.list();
@@ -427,12 +456,18 @@ describe("createYHubVersioningEndpoints", () => {
427
456
  };
428
457
  // 1: activity list — newest entry is the history edit, not a marker.
429
458
  fetchSpy.mockResolvedValueOnce(
430
- mockFetchResponse([historyEdit, VERSION_ENTRY_2, VERSION_ENTRY_1]),
459
+ mockFetchResponse({
460
+ activity: [historyEdit, VERSION_ENTRY_2, VERSION_ENTRY_1],
461
+ }),
431
462
  );
432
463
  // 2: latest edit of any kind → the history edit.
433
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([historyEdit]));
464
+ fetchSpy.mockResolvedValueOnce(
465
+ mockFetchResponse({ activity: [historyEdit] }),
466
+ );
434
467
  // 3: latest version marker → VERSION_ENTRY_2 (older than the edit).
435
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([VERSION_ENTRY_2]));
468
+ fetchSpy.mockResolvedValueOnce(
469
+ mockFetchResponse({ activity: [VERSION_ENTRY_2] }),
470
+ );
436
471
 
437
472
  const endpoints = makeEndpoints();
438
473
  const snapshots = await endpoints.list();
@@ -443,9 +478,9 @@ describe("createYHubVersioningEndpoints", () => {
443
478
  });
444
479
 
445
480
  it("forwards the mergeUsers param when configured", async () => {
446
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
447
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
448
- fetchSpy.mockResolvedValueOnce(mockFetchResponse([]));
481
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
482
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
483
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({ activity: [] }));
449
484
 
450
485
  const endpoints = createYHubVersioningEndpoints({
451
486
  baseUrl: BASE_URL,
@@ -475,7 +510,7 @@ describe("createYHubVersioningEndpoints", () => {
475
510
  // Only one fetch call (PATCH) — no activity fetch
476
511
  expect(fetchSpy).toHaveBeenCalledOnce();
477
512
  const [patchUrl, patchInit] = fetchSpy.mock.calls[0];
478
- expect(patchUrl).toBe(`${BASE_URL}/ydoc/${ORG}/${DOC_ID}`);
513
+ expect(patchUrl).toBe(`${BASE_URL}/ydoc/v1/${ORG}/${DOC_ID}`);
479
514
  expect(patchInit.method).toBe("PATCH");
480
515
  expect(patchInit.body).toBeInstanceOf(Uint8Array);
481
516
 
@@ -510,9 +545,9 @@ describe("createYHubVersioningEndpoints", () => {
510
545
  });
511
546
 
512
547
  expect(snapshot.by).toBe("user-1");
513
- // The PATCH is attributed to the same user.
548
+ // The PATCH is attributed to the same user via the body, not query params.
514
549
  const patchUrl = new URL(fetchSpy.mock.calls[0][0] as string);
515
- expect(patchUrl.searchParams.get("userid")).toBe("user-1");
550
+ expect(patchUrl.searchParams.has("userid")).toBe(false);
516
551
  });
517
552
 
518
553
  it("creates a version without a name", async () => {
@@ -587,17 +622,15 @@ describe("createYHubVersioningEndpoints", () => {
587
622
 
588
623
  // changeset reconstructed by timestamp, NOT by custom attribution
589
624
  const url = new URL(fetchSpy.mock.calls[0][0] as string);
590
- expect(url.pathname).toBe(`/changeset/${ORG}/${DOC_ID}`);
625
+ expect(url.pathname).toBe(`/api/changeset/v1/${ORG}/${DOC_ID}`);
591
626
  expect(url.searchParams.get("ydoc")).toBe("true");
592
627
  expect(url.searchParams.get("to")).toBe(String(SNAPSHOT_1.createdAt));
593
628
  expect(url.searchParams.has("from")).toBe(false);
594
629
  expect(url.searchParams.has("withCustomAttributions")).toBe(false);
595
630
  });
596
631
 
597
- it("throws when changeset has no nextDoc", async () => {
598
- fetchSpy.mockResolvedValueOnce(
599
- mockFetchResponse({ prevDoc: new Uint8Array() }),
600
- );
632
+ it("throws when changeset has no ydoc", async () => {
633
+ fetchSpy.mockResolvedValueOnce(mockFetchResponse({}));
601
634
 
602
635
  const endpoints = makeEndpoints();
603
636
  await expect(endpoints.getContent(SNAPSHOT_1)).rejects.toThrow(
@@ -653,7 +686,7 @@ describe("createYHubVersioningEndpoints", () => {
653
686
 
654
687
  // changeset without attributions
655
688
  fetchSpy.mockResolvedValueOnce(
656
- mockFetchResponse({ nextDoc: new Uint8Array() }),
689
+ mockFetchResponse({ ydoc: new Uint8Array() }),
657
690
  );
658
691
 
659
692
  await expect(endpoints.getAttributions!(SNAPSHOT_1)).rejects.toThrow(
@@ -682,12 +715,12 @@ describe("createYHubVersioningEndpoints", () => {
682
715
 
683
716
  // 1st call: GET changeset by timestamp
684
717
  const csUrl = new URL(fetchSpy.mock.calls[0][0] as string);
685
- expect(csUrl.pathname).toBe(`/changeset/${ORG}/${DOC_ID}`);
718
+ expect(csUrl.pathname).toBe(`/api/changeset/v1/${ORG}/${DOC_ID}`);
686
719
  expect(csUrl.searchParams.get("to")).toBe(String(SNAPSHOT_1.createdAt));
687
720
 
688
721
  // 2nd call: POST rollback
689
722
  const [rollbackUrl, rollbackInit] = fetchSpy.mock.calls[1];
690
- expect(rollbackUrl).toContain(`/rollback/${ORG}/${DOC_ID}`);
723
+ expect(rollbackUrl).toBe(`${BASE_URL}/rollback/v1/${ORG}/${DOC_ID}`);
691
724
  expect(rollbackInit.method).toBe("POST");
692
725
 
693
726
  expect(content).toBeInstanceOf(Uint8Array);