@tristan2828/ui-foundation 4.125.0 → 4.127.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 (94) hide show
  1. package/README.md +1 -1
  2. package/conventions/docs/add-an-entity.md +140 -35
  3. package/conventions/docs/cell-patterns.md +144 -56
  4. package/conventions/docs/design-language.md +45 -13
  5. package/dist/api/contracts.d.ts +8 -1
  6. package/dist/api/gateway/errors.js +4 -0
  7. package/dist/api/schema.d.ts +9 -0
  8. package/dist/api/transport/index.js +10 -1
  9. package/dist/components/app/data-table-tools.d.ts +12 -0
  10. package/dist/components/app/data-table-tools.js +2 -2
  11. package/dist/components/app/data-table.d.ts +2 -1
  12. package/dist/components/app/data-table.js +1 -1
  13. package/dist/components/app/edit-caret.d.ts +25 -0
  14. package/dist/components/app/edit-caret.js +118 -0
  15. package/dist/components/app/edit-in-place-presets.d.ts +51 -1
  16. package/dist/components/app/edit-in-place-presets.js +30 -0
  17. package/dist/components/app/edit-in-place-store.d.ts +70 -4
  18. package/dist/components/app/edit-in-place-store.js +219 -21
  19. package/dist/components/app/edit-in-place.d.ts +11 -0
  20. package/dist/components/app/editable-value.js +202 -25
  21. package/dist/components/app/entity-form.js +4 -0
  22. package/dist/components/app/entity-view.js +5 -1
  23. package/dist/components/app/equal-items.d.ts +32 -6
  24. package/dist/components/app/equal-items.js +59 -8
  25. package/dist/components/app/error-state.js +1 -0
  26. package/dist/components/app/form-leave-guard.js +7 -1
  27. package/dist/components/app/milkdown-editor.d.ts +1 -0
  28. package/dist/components/app/milkdown-editor.js +226 -64
  29. package/dist/components/app/pill-filter.d.ts +16 -0
  30. package/dist/components/app/pill-filter.js +22 -1
  31. package/dist/components/app/prose-look.d.ts +1 -1
  32. package/dist/components/app/prose-look.js +3 -1
  33. package/dist/components/app/rating-input.d.ts +22 -0
  34. package/dist/components/app/rating-input.js +65 -0
  35. package/dist/components/app/rating-score.d.ts +2 -1
  36. package/dist/components/app/rating-score.js +2 -6
  37. package/dist/components/app/rich-text-autolink.d.ts +6 -0
  38. package/dist/components/app/rich-text-autolink.js +79 -0
  39. package/dist/components/app/rich-text-edges.d.ts +8 -0
  40. package/dist/components/app/rich-text-edges.js +144 -0
  41. package/dist/components/app/rich-text-editor.d.ts +19 -4
  42. package/dist/components/app/rich-text-editor.js +19 -6
  43. package/dist/components/app/rich-text-empty-paragraph.d.ts +14 -6
  44. package/dist/components/app/rich-text-empty-paragraph.js +19 -8
  45. package/dist/components/app/rich-text-formats.js +88 -15
  46. package/dist/components/app/rich-text-full-screen.d.ts +2 -0
  47. package/dist/components/app/rich-text-full-screen.js +78 -0
  48. package/dist/components/app/rich-text-highlight-menu.d.ts +3 -1
  49. package/dist/components/app/rich-text-highlight-menu.js +3 -2
  50. package/dist/components/app/rich-text-input-rules.d.ts +7 -0
  51. package/dist/components/app/rich-text-input-rules.js +83 -0
  52. package/dist/components/app/rich-text-keyboard-bar.d.ts +24 -0
  53. package/dist/components/app/rich-text-keyboard-bar.js +215 -0
  54. package/dist/components/app/rich-text-node-views.js +2 -1
  55. package/dist/components/app/rich-text-paste.d.ts +43 -0
  56. package/dist/components/app/rich-text-paste.js +355 -0
  57. package/dist/components/app/rich-text-shortcuts.d.ts +5 -1
  58. package/dist/components/app/rich-text-shortcuts.js +132 -2
  59. package/dist/components/app/rich-text-slash-menu.js +47 -9
  60. package/dist/components/app/rich-text-slash.d.ts +7 -0
  61. package/dist/components/app/rich-text-slash.js +27 -14
  62. package/dist/components/app/rich-text-table-grips.d.ts +8 -6
  63. package/dist/components/app/rich-text-table-grips.js +11 -12
  64. package/dist/components/app/rich-text-tasks.d.ts +8 -0
  65. package/dist/components/app/rich-text-tasks.js +20 -0
  66. package/dist/components/app/rich-text-toolbar.d.ts +5 -1
  67. package/dist/components/app/rich-text-toolbar.js +14 -6
  68. package/dist/components/app/rich-text-tools-bar.d.ts +3 -1
  69. package/dist/components/app/rich-text-tools-bar.js +2 -2
  70. package/dist/components/app/rich-text-touch.d.ts +2 -0
  71. package/dist/components/app/rich-text-touch.js +18 -0
  72. package/dist/components/app/rich-text-widgets.js +1 -1
  73. package/dist/hooks/table-url-changes.d.ts +4 -0
  74. package/dist/hooks/table-url-changes.js +13 -0
  75. package/dist/hooks/use-record-update.d.ts +13 -1
  76. package/dist/hooks/use-record-update.js +17 -3
  77. package/dist/hooks/use-table-url-state.js +14 -8
  78. package/dist/index.d.ts +5 -2
  79. package/dist/index.js +8 -0
  80. package/dist/lib/markdown-merge.d.ts +38 -0
  81. package/dist/lib/markdown-merge.js +202 -17
  82. package/dist/lib/page-leaving.d.ts +8 -0
  83. package/dist/lib/page-leaving.js +30 -0
  84. package/dist/lib/pending-edits.d.ts +14 -0
  85. package/dist/lib/pending-edits.js +51 -0
  86. package/dist/lib/rating-glyphs.d.ts +3 -0
  87. package/dist/lib/rating-glyphs.js +7 -0
  88. package/dist/mocks/e2e-hooks.js +10 -0
  89. package/dist/mocks/override.d.ts +8 -0
  90. package/dist/testing/index.d.ts +40 -2
  91. package/dist/testing/index.js +101 -7
  92. package/openapi/foundation.yaml +12 -0
  93. package/package.json +1 -1
  94. package/styles/index.css +7 -0
@@ -1,7 +1,33 @@
1
1
  import { refusalReason } from '../../hooks/use-record-update.js';
2
+ import { flushPendingEdits } from '../../lib/pending-edits.js';
2
3
  /** How long typing has to stop before an autosaving field saves. */
3
4
  export const AUTOSAVE_DELAY_MS = 1000;
4
5
  const CLOSED = { status: 'closed' };
6
+ /** What a field shows when the saved value changed under unsaved typing. */
7
+ export const CONFLICT_MESSAGE = 'This changed elsewhere (another tab, device or chat) since you started typing. Saving now replaces that.';
8
+ /** The conflict's message for a field that can keep both (a `rebase`): Keep Mine adds what's typed to it. */
9
+ export const CONFLICT_REBASE_MESSAGE = 'This changed elsewhere (another tab, device or chat) since you started typing. Keep Mine adds your changes to it.';
10
+ /** "Keep mine" found both sides changed the same part. */
11
+ export const OVERLAP_MESSAGE = "Your changes and the ones saved elsewhere are in the same place, so both can't be kept. Keep Mine again replaces theirs; Show Saved drops yours.";
12
+ function conflictMessage(config) {
13
+ return config?.rebase ? CONFLICT_REBASE_MESSAGE : CONFLICT_MESSAGE;
14
+ }
15
+ /** Whether a save's rejection is the server's "changed since" (a 409, AppError kind `conflict`). */
16
+ export function isConflict(error) {
17
+ return Boolean(error && typeof error === 'object' && error.kind === 'conflict');
18
+ }
19
+ // Line endings, trailing spaces on a line and blank lines at either end:
20
+ // what a backend commonly normalises in text it's sent.
21
+ function normalisedText(value) {
22
+ return value
23
+ .replace(/\r\n?/g, '\n')
24
+ .split('\n')
25
+ .map((line) => line.trimEnd())
26
+ .join('\n')
27
+ .trim();
28
+ }
29
+ // How many of a field's last sent values still count as its own echo.
30
+ const ECHOES = 4;
5
31
  const sameAsJson = (a, b) => JSON.stringify(a) === JSON.stringify(b);
6
32
  /** Why a save failed, in words to show beside what was being saved. */
7
33
  export function messageOf(error) {
@@ -31,6 +57,14 @@ export class EditInPlaceStore {
31
57
  always = new Set();
32
58
  // Leaving the page, waiting for its saves: the leave prompt stays.
33
59
  holds = 0;
60
+ // The saved value each field last saw (`refresh` tells a change from it),
61
+ // and the values it last sent (their echo isn't a change elsewhere).
62
+ seen = new Map();
63
+ sent = new Map();
64
+ // Saves of fields that left the view mid-save, still to send or land.
65
+ detached = new Set();
66
+ // The page is hidden: an autosave doesn't wait for the pause.
67
+ hurried = false;
34
68
  constructor(options = {}) {
35
69
  this.autosaveDelay = options.autosaveDelay ?? AUTOSAVE_DELAY_MS;
36
70
  }
@@ -54,23 +88,59 @@ export class EditInPlaceStore {
54
88
  register(key, config) {
55
89
  this.configs.set(key, config);
56
90
  const { alwaysEditable, value } = config();
91
+ this.seen.set(key, value);
57
92
  if (alwaysEditable) {
58
93
  this.always.add(key);
59
94
  this.set(key, { status: 'open', draft: value, initial: value, error: null, problem: null });
60
95
  }
61
96
  return () => {
97
+ // A rich-text editor reports its typing a pause after it stops
98
+ // (src/lib/pending-edits.ts, F6): asked for now, before this reads it.
99
+ flushPendingEdits();
100
+ const inFlight = this.autosaving.get(key);
101
+ const state = this.state(key);
102
+ const last = this.configs.get(key)?.();
103
+ // With a save in flight, what was typed since goes once it lands,
104
+ // from this draft and config: the field is gone by then (F3).
105
+ if (inFlight) {
106
+ this.clearTimer(key);
107
+ if (state.status === 'open' && last)
108
+ this.sendDetached(inFlight, state, last);
109
+ }
62
110
  // A save still waiting for the pause goes now: the field is leaving,
63
111
  // not giving up what was typed.
64
- if (this.timers.has(key))
112
+ else if (this.timers.has(key))
65
113
  void this.flush(key);
66
114
  this.configs.delete(key);
67
115
  this.states.delete(key);
116
+ this.seen.delete(key);
117
+ this.sent.delete(key);
68
118
  if (this.openKey === key)
69
119
  this.openKey = null;
70
120
  if (this.always.delete(key))
71
121
  this.emit();
72
122
  };
73
123
  }
124
+ // A field that left mid-save: once its save in flight settles, its last
125
+ // draft is sent if it's newer than what that save held.
126
+ sendDetached(inFlight, state, config) {
127
+ const isEqual = config.isEqual ?? sameAsJson;
128
+ const run = inFlight.promise
129
+ .then((saved) => {
130
+ if (state.problem || state.conflict || isEqual(state.draft, inFlight.draft))
131
+ return saved;
132
+ const parsed = config.schema ? config.schema.safeParse(state.draft) : { success: true, data: state.draft };
133
+ if (!parsed.success)
134
+ return false;
135
+ return config.save(parsed.data).then(() => true, () => false);
136
+ })
137
+ .finally(() => {
138
+ this.detached.delete(run);
139
+ this.emit();
140
+ });
141
+ this.detached.add(run);
142
+ this.emit();
143
+ }
74
144
  state(key) {
75
145
  return this.states.get(key) ?? CLOSED;
76
146
  }
@@ -83,7 +153,7 @@ export class EditInPlaceStore {
83
153
  * a move off the page waiting for its saves (`hold`).
84
154
  */
85
155
  get editing() {
86
- return this.openKey !== null || this.always.size > 0 || this.holds > 0;
156
+ return this.openKey !== null || this.always.size > 0 || this.holds > 0 || this.detached.size > 0;
87
157
  }
88
158
  /**
89
159
  * Keeps `editing` true until released: the leave prompt, saving on the
@@ -103,11 +173,17 @@ export class EditInPlaceStore {
103
173
  }
104
174
  /** Whether leaving the page now would lose something: a draft that isn't saved. */
105
175
  get dirty() {
106
- return this.unsaved().length > 0;
176
+ return this.unsaved().length > 0 || this.detached.size > 0;
107
177
  }
108
- /** Whether everything unsaved saves on its own, so leaving can save it first rather than ask. */
178
+ /**
179
+ * Whether everything unsaved saves on its own, so leaving can save it
180
+ * first rather than ask. A problem or a conflict can't: leaving asks at once.
181
+ */
109
182
  get savesOnLeave() {
110
- return this.unsaved().every((key) => this.autosaves(key));
183
+ return this.unsaved().every((key) => {
184
+ const state = this.state(key);
185
+ return this.autosaves(key) && !(state.status !== 'closed' && (state.problem || state.conflict));
186
+ });
111
187
  }
112
188
  unsaved() {
113
189
  const keys = new Set(this.always);
@@ -119,6 +195,9 @@ export class EditInPlaceStore {
119
195
  return false;
120
196
  if (state.status === 'saving' || this.autosaving.has(key) || state.error !== null)
121
197
  return true;
198
+ // A reported problem is unsaved at once, not only after the pause (F5).
199
+ if (state.problem !== null || state.conflict)
200
+ return true;
122
201
  return !this.isEqual(key)(state.draft, state.initial);
123
202
  });
124
203
  }
@@ -149,6 +228,7 @@ export class EditInPlaceStore {
149
228
  if (!config)
150
229
  return false;
151
230
  this.openKey = key;
231
+ this.seen.set(key, config.value);
152
232
  this.set(key, { status: 'open', draft: config.value, initial: config.value, error: null, problem: null });
153
233
  return true;
154
234
  }
@@ -188,6 +268,7 @@ export class EditInPlaceStore {
188
268
  * and resolves true once what's on screen is saved.
189
269
  */
190
270
  commit(key, draft) {
271
+ flushPendingEdits();
191
272
  if (this.autosaves(key))
192
273
  return this.commitAutosaved(key, draft);
193
274
  const inFlight = this.saving.get(key);
@@ -204,8 +285,8 @@ export class EditInPlaceStore {
204
285
  const config = this.configs.get(key)?.();
205
286
  if (!config)
206
287
  return Promise.resolve(false);
207
- if (open.problem) {
208
- this.set(key, { ...open, error: open.problem });
288
+ if (open.problem || open.conflict) {
289
+ this.set(key, { ...open, error: open.problem ?? open.error ?? conflictMessage(config) });
209
290
  return Promise.resolve(false);
210
291
  }
211
292
  const isEqual = config.isEqual ?? sameAsJson;
@@ -220,13 +301,21 @@ export class EditInPlaceStore {
220
301
  return Promise.resolve(false);
221
302
  }
222
303
  this.set(key, { ...open, status: 'saving', error: null, fieldErrors: undefined });
304
+ this.remember(key, open.draft);
305
+ if (parsed.data !== open.draft)
306
+ this.remember(key, parsed.data);
223
307
  const save = config
224
308
  .save(parsed.data)
225
309
  .then(() => {
226
310
  this.set(key, CLOSED);
227
311
  return true;
228
312
  }, (error) => {
229
- this.set(key, { ...open, status: 'open', error: messageOf(error), fieldErrors: fieldErrorsOf(config, error) });
313
+ // Another writer saved since (a 409): nothing was written, and
314
+ // nothing saves until the person picks.
315
+ if (isConflict(error))
316
+ this.set(key, { ...open, status: 'open', conflict: true, error: conflictMessage(config) });
317
+ else
318
+ this.set(key, { ...open, status: 'open', error: messageOf(error), fieldErrors: fieldErrorsOf(config, error) });
230
319
  return false;
231
320
  })
232
321
  .finally(() => this.saving.delete(key));
@@ -254,7 +343,25 @@ export class EditInPlaceStore {
254
343
  this.timers.set(key, setTimeout(() => {
255
344
  this.timers.delete(key);
256
345
  void this.flush(key);
257
- }, this.autosaveDelay));
346
+ }, this.hurried ? 0 : this.autosaveDelay));
347
+ }
348
+ /**
349
+ * The page is hidden (another tab, a phone's home screen, F13): while it
350
+ * is, an autosave goes without the pause, and one waiting goes now.
351
+ */
352
+ hurry(hidden) {
353
+ this.hurried = hidden;
354
+ if (hidden)
355
+ for (const key of [...this.timers.keys()])
356
+ void this.flush(key);
357
+ }
358
+ // A value this field sent: its echo from the server isn't a change elsewhere.
359
+ remember(key, value) {
360
+ this.sent.set(key, [...(this.sent.get(key) ?? []), value].slice(-ECHOES));
361
+ }
362
+ isEcho(key, value, isEqual) {
363
+ return (this.sent.get(key) ?? []).some((sent) => isEqual(value, sent) ||
364
+ (typeof value === 'string' && typeof sent === 'string' && normalisedText(value) === normalisedText(sent)));
258
365
  }
259
366
  clearTimer(key) {
260
367
  clearTimeout(this.timers.get(key));
@@ -267,6 +374,8 @@ export class EditInPlaceStore {
267
374
  * sent is saved (or there was nothing to send).
268
375
  */
269
376
  flush(key) {
377
+ // Typing an editor holds back is reported first (F6).
378
+ flushPendingEdits();
270
379
  this.clearTimer(key);
271
380
  const inFlight = this.autosaving.get(key);
272
381
  if (inFlight) {
@@ -290,8 +399,8 @@ export class EditInPlaceStore {
290
399
  const config = this.configs.get(key)?.();
291
400
  if (!config)
292
401
  return Promise.resolve(false);
293
- if (state.problem) {
294
- this.set(key, { ...state, error: state.problem });
402
+ if (state.problem || state.conflict) {
403
+ this.set(key, { ...state, error: state.problem ?? state.error ?? conflictMessage(config) });
295
404
  return Promise.resolve(false);
296
405
  }
297
406
  const isEqual = config.isEqual ?? sameAsJson;
@@ -307,6 +416,9 @@ export class EditInPlaceStore {
307
416
  return Promise.resolve(false);
308
417
  }
309
418
  const sent = state.draft;
419
+ this.remember(key, sent);
420
+ if (parsed.data !== sent)
421
+ this.remember(key, parsed.data);
310
422
  this.set(key, { ...state, error: null, fieldErrors: undefined, autosave: 'saving' });
311
423
  const promise = config
312
424
  .save(parsed.data)
@@ -319,7 +431,15 @@ export class EditInPlaceStore {
319
431
  return true;
320
432
  }, (error) => {
321
433
  const now = this.state(key);
322
- if (now.status === 'open')
434
+ if (now.status !== 'open')
435
+ return false;
436
+ // Another writer saved since (a 409): nothing was written, and
437
+ // nothing saves until the person picks.
438
+ if (isConflict(error)) {
439
+ this.clearTimer(key);
440
+ this.set(key, { ...now, conflict: true, overlap: false, error: conflictMessage(config), fieldErrors: undefined, autosave: undefined });
441
+ }
442
+ else
323
443
  this.set(key, { ...now, error: messageOf(error), fieldErrors: fieldErrorsOf(config, error), autosave: undefined });
324
444
  return false;
325
445
  })
@@ -332,34 +452,110 @@ export class EditInPlaceStore {
332
452
  * (leaving the page). Resolves true once nothing unsaved is left.
333
453
  */
334
454
  async saveAll() {
455
+ flushPendingEdits();
335
456
  const keys = this.unsaved().filter((key) => this.autosaves(key));
336
- const results = await Promise.all(keys.map((key) => this.flush(key)));
457
+ const results = await Promise.all([...keys.map((key) => this.flush(key)), ...this.detached]);
337
458
  return results.every(Boolean) && !this.dirty;
338
459
  }
339
460
  /**
340
- * An always-editable field shows the saved value again when it changed
341
- * elsewhere (another tab, the form), but only while nothing of its own
342
- * is unsaved or saving. True when it did: the control starts over.
461
+ * The field's saved value (its config's `value`) may have changed
462
+ * elsewhere: another tab, a phone, an AI chat, the form. Call it whenever
463
+ * the value changes. An open autosaving field with nothing of its own unsaved or
464
+ * saving shows it, starting its control over (`revision`): true when it
465
+ * did, so the caret can be put back (F4). One with unsaved typing gets a
466
+ * conflict (`conflict`, its message), and nothing saves until the person
467
+ * picks. The field's own save coming back, as sent or as the server
468
+ * normalised it (trimmed), is no change: the control keeps its text and
469
+ * its undo (F25).
343
470
  */
344
471
  refresh(key) {
472
+ // Typing an editor holds back counts as unsaved (a conflict, not a reload).
473
+ flushPendingEdits();
345
474
  const state = this.state(key);
346
475
  const config = this.configs.get(key)?.();
347
- if (!this.always.has(key) || state.status !== 'open' || !config)
348
- return false;
349
- if (this.timers.has(key) || this.autosaving.has(key) || state.error !== null)
476
+ // Only a field that saves itself (notes): a field opened by a click
477
+ // holds its draft until it's saved, and a control that saves each
478
+ // pick itself (MultiPick) changes the value it shows while open.
479
+ if (state.status === 'closed' || !config || !this.autosaves(key))
350
480
  return false;
351
481
  const isEqual = config.isEqual ?? sameAsJson;
352
- if (!isEqual(state.draft, state.initial) || isEqual(config.value, state.initial))
482
+ const seen = this.seen.has(key) ? this.seen.get(key) : state.initial;
483
+ if (isEqual(config.value, seen))
484
+ return false;
485
+ this.seen.set(key, config.value);
486
+ if (this.isEcho(key, config.value, isEqual) || isEqual(config.value, state.initial))
353
487
  return false;
488
+ const busy = this.timers.has(key) || this.autosaving.has(key) || this.queued.has(key) || state.status === 'saving';
489
+ const clean = !busy && state.error === null && state.problem === null && isEqual(state.draft, state.initial);
490
+ if (!clean) {
491
+ // Already a conflict ("keep mine" found an overlap): the new value is
492
+ // what it's now against, and the person picks again.
493
+ this.set(key, { ...state, conflict: true, overlap: false, error: conflictMessage(config) });
494
+ return false;
495
+ }
354
496
  const revision = (state.revision ?? 0) + 1;
355
497
  this.set(key, { status: 'open', draft: config.value, initial: config.value, error: null, problem: null, revision });
356
498
  return true;
357
499
  }
500
+ /**
501
+ * The saved value changed under this field's unsaved typing, as a
502
+ * server's conflict answer says. A save refused with a 409 (AppError
503
+ * kind `conflict`) does this itself. Nothing saves until the person picks.
504
+ */
505
+ markConflict(key) {
506
+ const state = this.state(key);
507
+ if (state.status === 'closed')
508
+ return;
509
+ this.clearTimer(key);
510
+ this.set(key, { ...state, status: 'open', conflict: true, error: conflictMessage(this.configs.get(key)?.()), autosave: undefined });
511
+ }
512
+ /**
513
+ * A conflict, answered "keep mine". With a `rebase`, what's typed is laid
514
+ * onto the value saved elsewhere and the control starts over showing
515
+ * both (`revision`) as it saves; if both changed the same part it stays
516
+ * a conflict (`overlap`), and keeping mine again saves what's typed over
517
+ * theirs. Without a `rebase`, what's typed saves over it. The save
518
+ * carries the record's latest version, so a writer that saved again
519
+ * meanwhile is another conflict, not an overwrite.
520
+ */
521
+ keepMine(key) {
522
+ const state = this.state(key);
523
+ const config = this.configs.get(key)?.();
524
+ if (state.status !== 'open' || !state.conflict || !config)
525
+ return Promise.resolve(false);
526
+ let draft = state.draft;
527
+ let revision = state.revision;
528
+ if (config.rebase && !state.overlap) {
529
+ const merged = config.rebase(state.initial, state.draft, config.value);
530
+ if (merged === null || merged === undefined) {
531
+ this.set(key, { ...state, overlap: true, error: OVERLAP_MESSAGE });
532
+ return Promise.resolve(false);
533
+ }
534
+ if (!(config.isEqual ?? sameAsJson)(merged, state.draft)) {
535
+ draft = merged;
536
+ revision = (state.revision ?? 0) + 1;
537
+ }
538
+ }
539
+ this.seen.set(key, config.value);
540
+ this.set(key, { ...state, draft, revision, conflict: false, overlap: false, error: null, initial: config.value });
541
+ return this.autosaves(key) ? this.flush(key) : this.commit(key);
542
+ }
543
+ /** A conflict, answered "show the saved one": what's typed goes, and the control starts over from the saved value. */
544
+ takeSaved(key) {
545
+ const state = this.state(key);
546
+ const config = this.configs.get(key)?.();
547
+ if (state.status !== 'open' || !state.conflict || !config)
548
+ return;
549
+ this.clearTimer(key);
550
+ const revision = (state.revision ?? 0) + 1;
551
+ this.set(key, { status: 'open', draft: config.value, initial: config.value, error: null, problem: null, revision });
552
+ }
358
553
  /**
359
554
  * Esc: closes and shows the saved value again. Not while a save is in
360
555
  * flight, and never for an always-editable field.
361
556
  */
362
557
  cancel(key) {
558
+ flushPendingEdits();
363
559
  const state = this.state(key);
364
560
  if (state.status !== 'open' || this.always.has(key) || this.autosaving.has(key))
365
561
  return;
@@ -368,6 +564,8 @@ export class EditInPlaceStore {
368
564
  }
369
565
  /** Leaving the page anyway: whatever is open is dropped, and nothing waiting to autosave is sent. */
370
566
  discard() {
567
+ // Reported now, so nothing held back lands after it's dropped.
568
+ flushPendingEdits();
371
569
  for (const key of [...this.timers.keys()])
372
570
  this.clearTimer(key);
373
571
  if (this.openKey !== null) {
@@ -379,7 +577,7 @@ export class EditInPlaceStore {
379
577
  for (const key of this.always) {
380
578
  const state = this.state(key);
381
579
  if (state.status === 'open')
382
- this.set(key, { ...state, draft: state.initial, error: null, autosave: undefined });
580
+ this.set(key, { ...state, draft: state.initial, error: null, problem: null, conflict: false, overlap: false, autosave: undefined });
383
581
  }
384
582
  }
385
583
  }
@@ -76,6 +76,7 @@ export type EditInPlace = {
76
76
  shown?: ReactNode;
77
77
  fieldErrors?: (cause: unknown) => Record<string, string> | null;
78
78
  errorsOnFields?: boolean;
79
+ rebase?: ((base: unknown, mine: unknown, theirs: unknown) => unknown) | false;
79
80
  };
80
81
  export type EditInPlaceOptions<TDraft, TValue> = {
81
82
  kind: EditKind;
@@ -138,6 +139,16 @@ export type EditInPlaceOptions<TDraft, TValue> = {
138
139
  * its path inside the value. The control gets them as `errors`.
139
140
  */
140
141
  fieldErrors?: (cause: unknown) => Record<string, string> | null;
142
+ /**
143
+ * "Keep Mine" after the value changed elsewhere (another tab, a phone,
144
+ * an AI chat): what's typed (`mine`, started from `base`) laid onto the
145
+ * value saved since (`theirs`), or null when both changed the same part
146
+ * (the person is told, and a second Keep Mine replaces theirs). Long
147
+ * text (`kind: 'long-text'`, a string) defaults to `rebaseMarkdown`,
148
+ * block by block; `false` turns that off, so Keep Mine saves what's
149
+ * typed over theirs, as every other kind does.
150
+ */
151
+ rebase?: ((base: TDraft, mine: TDraft, theirs: TDraft) => TDraft | null) | false;
141
152
  /**
142
153
  * The control shows `errors` on its own fields, so a refusal about one
143
154
  * of them isn't repeated under the control; one about none still shows there.