@estiva-app/ui 0.100.0 → 0.102.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 (60) hide show
  1. package/dist/AskedBlocks.d.ts +30 -0
  2. package/dist/AskedBlocks.d.ts.map +1 -0
  3. package/dist/DeletedNotice.d.ts +17 -0
  4. package/dist/DeletedNotice.d.ts.map +1 -0
  5. package/dist/DocumentEditor.d.ts +39 -6
  6. package/dist/DocumentEditor.d.ts.map +1 -1
  7. package/dist/IconButton.d.ts +7 -1
  8. package/dist/IconButton.d.ts.map +1 -1
  9. package/dist/LinkRow.d.ts +7 -10
  10. package/dist/LinkRow.d.ts.map +1 -1
  11. package/dist/NoAccessNotice.d.ts +13 -0
  12. package/dist/NoAccessNotice.d.ts.map +1 -0
  13. package/dist/PartCard.d.ts +3 -3
  14. package/dist/PartCard.d.ts.map +1 -1
  15. package/dist/editor.d.ts +1 -1
  16. package/dist/editor.d.ts.map +1 -1
  17. package/dist/editor.js +311 -8
  18. package/dist/editor.js.map +4 -4
  19. package/dist/index.d.ts +2 -0
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +433 -389
  22. package/dist/index.js.map +4 -4
  23. package/dist/linked.d.ts +2 -2
  24. package/dist/linked.d.ts.map +1 -1
  25. package/dist/linkedFrom.d.ts.map +1 -1
  26. package/package.json +1 -1
  27. package/registry.json +95 -7
  28. package/src/AskedBlocks.ts +158 -0
  29. package/src/DeletedNotice.mdx +43 -0
  30. package/src/DeletedNotice.stories.tsx +29 -0
  31. package/src/DeletedNotice.test.tsx +46 -0
  32. package/src/DeletedNotice.tsx +47 -0
  33. package/src/DocumentEditor.mdx +5 -1
  34. package/src/DocumentEditor.stories.tsx +84 -0
  35. package/src/DocumentEditor.test.tsx +188 -0
  36. package/src/DocumentEditor.tsx +294 -14
  37. package/src/IconButton.mdx +4 -0
  38. package/src/IconButton.stories.tsx +28 -1
  39. package/src/IconButton.test.tsx +30 -0
  40. package/src/IconButton.tsx +22 -1
  41. package/src/LinkCard.test.tsx +4 -6
  42. package/src/LinkCard.tsx +1 -1
  43. package/src/LinkRow.mdx +1 -1
  44. package/src/LinkRow.stories.tsx +3 -3
  45. package/src/LinkRow.test.tsx +2 -2
  46. package/src/LinkRow.tsx +11 -20
  47. package/src/NoAccessNotice.mdx +40 -0
  48. package/src/NoAccessNotice.stories.tsx +17 -0
  49. package/src/NoAccessNotice.tsx +18 -0
  50. package/src/PartCard.test.tsx +2 -2
  51. package/src/PartCard.tsx +5 -4
  52. package/src/Toolbar.test.tsx +15 -0
  53. package/src/UnreadDot.mdx +3 -0
  54. package/src/editor.ts +1 -0
  55. package/src/index.ts +2 -0
  56. package/src/linked.ts +2 -2
  57. package/src/linkedFrom.test.tsx +4 -7
  58. package/src/linkedFrom.ts +0 -2
  59. package/src/registry/registry.test.ts +5 -4
  60. package/stories/Choosing.mdx +2 -0
@@ -389,3 +389,87 @@ export const ReadOnly: Story = {
389
389
  export const ReadOnlyWithoutComments: Story = {
390
390
  render: () => <Workbench readOnly />,
391
391
  }
392
+
393
+ // ─── A block both people changed ───
394
+
395
+ const block = (id: string, text: string) => ({ type: 'paragraph', id, content: [{ type: 'text', text }] })
396
+ const blocks = (...content: object[]) => JSON.stringify({ type: 'doc', content })
397
+ const ASK_START = blocks(
398
+ block('k1', 'The first block. Nobody changed it, so it reads as it did.'),
399
+ block('k2', 'The second block, as it was when you both started.'),
400
+ block('k3', 'The third block, as it was when you both started.'),
401
+ )
402
+ const ASK_THEIRS = blocks(
403
+ block('k1', 'The first block. Nobody changed it, so it reads as it did.'),
404
+ block('k2', 'The second block, as the other person saved it a moment before you: their words stand in the text.'),
405
+ block('k3', 'The third block. They changed it too, but you did not, so their words simply appear.'),
406
+ )
407
+ const ASK_MINE = blocks(
408
+ block('k1', 'The first block. Nobody changed it, so it reads as it did.'),
409
+ block('k2', 'The second block, as you had it: your words, kept beside theirs until you choose.'),
410
+ block('k3', 'The third block, as it was when you both started.'),
411
+ )
412
+
413
+ /**
414
+ * The app's side: the first save is refused because the other person saved
415
+ * the second block first, and the merge asks about it; the next save goes
416
+ * through. The story saves your text as it opens, as leaving the editor would.
417
+ */
418
+ function Asking({ sinceOpened = false }: { sinceOpened?: boolean }) {
419
+ const answered = useRef(false)
420
+ const started = useRef(false)
421
+ const [said, setSaid] = useState<string | null>(null)
422
+ const onEditor = (editor: Editor | null) => {
423
+ if (!editor || started.current) return
424
+ started.current = true
425
+ setTimeout(() => {
426
+ editor.commands.setContent(codec.read(ASK_MINE, 'blocks'))
427
+ editor.view.focus()
428
+ editor.view.dom.blur()
429
+ })
430
+ }
431
+ return (
432
+ <div className="flex max-w-3xl flex-col gap-4 px-6 py-5">
433
+ <DocumentEditor
434
+ value={ASK_START}
435
+ format="blocks"
436
+ codec={codec}
437
+ label="Document"
438
+ merges
439
+ onEditor={onEditor}
440
+ onCommit={async (value) => {
441
+ if (!answered.current) {
442
+ answered.current = true
443
+ return { value: ASK_THEIRS, theirs: ASK_THEIRS, mine: ASK_MINE, blocks: ['k2'], sinceOpened }
444
+ }
445
+ setSaid(`Saved: ${savedLines(value)[1]}`)
446
+ return true
447
+ }}
448
+ />
449
+ {said && <p className="text-caption text-text-secondary">{said}</p>}
450
+ </div>
451
+ )
452
+ }
453
+
454
+ /**
455
+ * A block you both changed: their words in the text, marked at its edge;
456
+ * yours beside it with Use mine, Copy and Dismiss. Nothing is saved until you
457
+ * choose. A block only one of you changed shows nothing: their words appear.
458
+ */
459
+ export const BlockBothChanged: Story = {
460
+ name: 'A block both changed',
461
+ render: () => <Asking />,
462
+ }
463
+
464
+ /** A page opened before the text changed, and refused: the same shape, under one line. */
465
+ export const OpenedBefore: Story = {
466
+ name: 'Opened before it changed',
467
+ render: () => <Asking sinceOpened />,
468
+ }
469
+
470
+ /** At phone width your version sits under the block instead of beside it. */
471
+ export const BlockBothChangedNarrow: Story = {
472
+ name: 'A block both changed, phone width',
473
+ globals: { viewport: { value: 'mobile1', isRotated: false } },
474
+ render: () => <Asking />,
475
+ }
@@ -212,6 +212,194 @@ describe("someone else's save", () => {
212
212
  await settle()
213
213
  expect(screen.queryByText('Person two saved a newer version.')).toBeNull()
214
214
  })
215
+
216
+ it('with merges, waits with no line: leaving saves yours to be merged onto it', async () => {
217
+ const onCommit = vi.fn(async () => true)
218
+ const { rerender } = render(<Doc merges onCommit={onCommit} />)
219
+ await type(' mine')
220
+ rerender(<Doc merges onCommit={onCommit} value={doc(para('a1', 'Their line'))} changedBy={{ name: 'Person two' }} />)
221
+ await settle()
222
+ expect(screen.queryByText(/saved a newer version/)).toBeNull()
223
+ expect(screen.queryByRole('button', { name: 'Load it' })).toBeNull()
224
+ expect(editorOf().getText()).toBe('First line mine')
225
+ await leave()
226
+ expect(onCommit).toHaveBeenCalledTimes(1)
227
+ })
228
+ })
229
+
230
+ describe('a block you both changed', () => {
231
+ // You and they started from the same text; they saved first.
232
+ const START = doc(para('b1', 'One as written.'), para('b2', 'Two as written.'), para('b3', 'Three as written.'))
233
+ const THEIRS = doc(para('b1', 'One as written.'), para('b2', 'Two: their words.'), para('b3', 'Three: theirs too.'))
234
+ const MINE = doc(para('b1', 'One: my words.'), para('b2', 'Two: my words.'), para('b3', 'Three as written.'))
235
+ // The app's merge as far as it goes: your first block in, their third, and their words in the second, which asks.
236
+ const HELD = doc(para('b1', 'One: my words.'), para('b2', 'Two: their words.'), para('b3', 'Three: theirs too.'))
237
+ const asks = (extra: object = {}) => ({ value: HELD, theirs: THEIRS, mine: MINE, blocks: ['b2'], ...extra })
238
+
239
+ /** You change the first two blocks and leave; the save asks. */
240
+ async function saveThatAsks(onCommit: DocumentEditorProps['onCommit'], props: Partial<DocumentEditorProps> = {}) {
241
+ const view = render(<Doc value={START} onCommit={onCommit} {...props} />)
242
+ await act(async () => {
243
+ const editor = editorOf()
244
+ editor.commands.setContent(codec.read(MINE, 'blocks'))
245
+ })
246
+ await leave()
247
+ await settle()
248
+ return view
249
+ }
250
+ const textNow = () => editorOf().getText({ blockSeparator: '|' })
251
+ const yourVersion = () => document.querySelector('[data-your-version]') as HTMLElement | null
252
+
253
+ it('holds their words in that block and yours beside it, keeps your other changes, and saves nothing until you choose', async () => {
254
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(async () => asks())
255
+ await saveThatAsks(onCommit)
256
+ expect(textNow()).toBe('One: my words.|Two: their words.|Three: theirs too.')
257
+ expect(within(yourVersion()!).getByText('Your version')).toBeTruthy()
258
+ expect(within(yourVersion()!).getByText('Two: my words.')).toBeTruthy()
259
+ expect(document.querySelector('[data-asked-block="b2"]')?.textContent).toContain('Two: their words.')
260
+ // Typing more and leaving again sends nothing while the block asks.
261
+ await type(' More.')
262
+ await leave()
263
+ expect(onCommit).toHaveBeenCalledTimes(1)
264
+ })
265
+
266
+ it('Use mine puts yours in its place and saves after their text', async () => {
267
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>().mockResolvedValueOnce(asks()).mockResolvedValue(true)
268
+ await saveThatAsks(onCommit)
269
+ await act(async () => {
270
+ fireEvent.click(within(yourVersion()!).getByRole('button', { name: 'Use mine' }))
271
+ })
272
+ await settle()
273
+ expect(onCommit).toHaveBeenCalledTimes(2)
274
+ const [value, , editedFrom] = onCommit.mock.calls[1]
275
+ expect(saved(value).map((b) => `${b.id}:${b.runs[0].text}`)).toEqual(['b1:One: my words.', 'b2:Two: my words.', 'b3:Three: theirs too.'])
276
+ expect(editedFrom).toBe(THEIRS)
277
+ expect(yourVersion()).toBeNull()
278
+ })
279
+
280
+ it('Dismiss keeps theirs in that block and saves the rest of yours', async () => {
281
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>().mockResolvedValueOnce(asks()).mockResolvedValue(true)
282
+ await saveThatAsks(onCommit)
283
+ await act(async () => {
284
+ fireEvent.click(within(yourVersion()!).getByRole('button', { name: 'Dismiss' }))
285
+ })
286
+ await settle()
287
+ expect(onCommit).toHaveBeenCalledTimes(2)
288
+ expect(saved(onCommit.mock.calls[1][0]).map((b) => `${b.id}:${b.runs[0].text}`)).toEqual(['b1:One: my words.', 'b2:Two: their words.', 'b3:Three: theirs too.'])
289
+ expect(document.querySelector('[data-asked-block]')).toBeNull()
290
+ })
291
+
292
+ /** You leave with MINE; while that save is out you type `typedSince`; then it asks. */
293
+ async function typedWhileItIsOut(typedSince: ReturnType<typeof doc>) {
294
+ let answer: (asked: ReturnType<typeof asks>) => void = () => {}
295
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(() => new Promise((resolve) => (answer = resolve)))
296
+ render(<Doc value={START} onCommit={onCommit} />)
297
+ await act(async () => {
298
+ editorOf().commands.setContent(codec.read(MINE, 'blocks'))
299
+ })
300
+ await leave()
301
+ await act(async () => {
302
+ editorOf().commands.setContent(codec.read(typedSince, 'blocks'))
303
+ })
304
+ await act(async () => answer(asks()))
305
+ await settle()
306
+ return onCommit
307
+ }
308
+ const yourVersions = () => [...document.querySelectorAll('[data-your-version]')].map((card) => card.textContent)
309
+
310
+ it('what you typed while the save was out stays: in the merge, and as your version of the block that asks', async () => {
311
+ const onCommit = await typedWhileItIsOut(
312
+ doc(para('b1', 'One: my words, and more.'), para('b2', 'Two: my words, and more.'), para('b3', 'Three as written.'), para('b4', 'Four, new.')),
313
+ )
314
+ expect(textNow()).toBe('One: my words, and more.|Two: their words.|Three: theirs too.|Four, new.')
315
+ expect(yourVersions().length).toBeGreaterThan(0)
316
+ expect(yourVersions().every((words) => words?.includes('Two: my words, and more.'))).toBe(true)
317
+ expect(onCommit).toHaveBeenCalledTimes(1)
318
+ })
319
+
320
+ it('a block they changed that you typed in while the save was out asks too', async () => {
321
+ await typedWhileItIsOut(doc(para('b1', 'One: my words.'), para('b2', 'Two: my words.'), para('b3', 'Three: typed while out.')))
322
+ expect(textNow()).toBe('One: my words.|Two: their words.|Three: theirs too.')
323
+ expect(document.querySelector('[data-asked-block="b3"]')).not.toBeNull()
324
+ expect(yourVersions().some((words) => words?.includes('Three: typed while out.'))).toBe(true)
325
+ })
326
+
327
+ it('Copy puts your version on the clipboard and keeps it there to choose', async () => {
328
+ const writeText = vi.fn(async () => {})
329
+ Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true })
330
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(async () => asks())
331
+ await saveThatAsks(onCommit)
332
+ await act(async () => {
333
+ fireEvent.click(within(yourVersion()!).getByRole('button', { name: 'Copy' }))
334
+ })
335
+ expect(writeText).toHaveBeenCalledWith('Two: my words.')
336
+ expect(within(yourVersion()!).getByRole('button', { name: 'Copied' })).toBeTruthy()
337
+ expect(onCommit).toHaveBeenCalledTimes(1)
338
+ Reflect.deleteProperty(navigator, 'clipboard')
339
+ })
340
+
341
+ it('Load it while a block asks takes theirs whole, and nothing asks after it', async () => {
342
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(async () => asks())
343
+ const { rerender } = await saveThatAsks(onCommit)
344
+ rerender(<Doc value={doc(para('b1', 'Newest.'))} onCommit={onCommit} changedBy={{ name: 'Person two' }} />)
345
+ await settle()
346
+ await act(async () => {
347
+ fireEvent.click(screen.getByRole('button', { name: 'Load it' }))
348
+ })
349
+ await settle()
350
+ expect(textNow()).toBe('Newest.')
351
+ expect(yourVersion()).toBeNull()
352
+ })
353
+
354
+ it('Use mine on a block the text no longer holds brings it back', async () => {
355
+ const theirs = doc(para('b1', 'One as written.'), para('b3', 'Three as written.'))
356
+ const held = doc(para('b1', 'One: my words.'), para('b3', 'Three as written.'))
357
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>().mockResolvedValueOnce({ value: held, theirs, mine: MINE, blocks: ['b2'] }).mockResolvedValue(true)
358
+ await saveThatAsks(onCommit)
359
+ await act(async () => {
360
+ fireEvent.click(within(yourVersion()!).getByRole('button', { name: 'Use mine' }))
361
+ })
362
+ await settle()
363
+ expect(saved(onCommit.mock.calls[1][0]).map((b) => `${b.id}:${b.runs[0].text}`)).toContain('b2:Two: my words.')
364
+ })
365
+
366
+ it('a block they deleted that you changed: Dismiss takes it out', async () => {
367
+ const theirs = doc(para('b1', 'One as written.'), para('b3', 'Three as written.'))
368
+ const held = doc(para('b1', 'One: my words.'), para('b2', 'Two: my words.'), para('b3', 'Three as written.'))
369
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>().mockResolvedValueOnce({ value: held, theirs, mine: MINE, blocks: ['b2'] }).mockResolvedValue(true)
370
+ await saveThatAsks(onCommit)
371
+ await act(async () => {
372
+ fireEvent.click(within(yourVersion()!).getByRole('button', { name: 'Dismiss' }))
373
+ })
374
+ await settle()
375
+ expect(saved(onCommit.mock.calls[1][0]).map((b) => b.id)).toEqual(['b1', 'b3'])
376
+ })
377
+
378
+ it('leaving the page before choosing saves nothing, and hands the app your text with your version of the block', async () => {
379
+ const onLeftUnsaved = vi.fn<(unsaved?: string) => void>()
380
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(async () => asks())
381
+ const { unmount } = await saveThatAsks(onCommit, { onLeftUnsaved })
382
+ unmount()
383
+ await settle()
384
+ expect(onCommit).toHaveBeenCalledTimes(1)
385
+ expect(onLeftUnsaved).toHaveBeenCalledTimes(1)
386
+ expect(saved(onLeftUnsaved.mock.calls[0][0]!).map((b) => `${b.id}:${b.runs[0].text}`)).toEqual(['b1:One: my words.', 'b2:Two: my words.', 'b3:Three: theirs too.'])
387
+ })
388
+
389
+ it('their save showing up afterwards does not replace what you are choosing in', async () => {
390
+ const onCommit = vi.fn<DocumentEditorProps['onCommit']>(async () => asks())
391
+ const { rerender } = await saveThatAsks(onCommit)
392
+ rerender(<Doc value={THEIRS} onCommit={onCommit} />)
393
+ await settle()
394
+ expect(textNow()).toBe('One: my words.|Two: their words.|Three: theirs too.')
395
+ expect(yourVersion()).toBeTruthy()
396
+ })
397
+
398
+ it('a page opened before the text changed says so above it, in the same shape', async () => {
399
+ await saveThatAsks(async () => asks({ sinceOpened: true }))
400
+ expect(screen.getByText('This page changed since you opened it.')).toBeTruthy()
401
+ expect(yourVersion()).toBeTruthy()
402
+ })
215
403
  })
216
404
 
217
405
  describe('the sign-in ending', () => {