@itrocks/sync 0.0.0-stage → 0.1.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 (156) hide show
  1. package/LICENSE +165 -0
  2. package/README.md +249 -2
  3. package/config.yaml +2 -0
  4. package/demo/src/demo.css +458 -0
  5. package/dist/client/author-background.d.ts +2 -0
  6. package/dist/client/author-background.js +28 -0
  7. package/dist/client/clock.d.ts +10 -0
  8. package/dist/client/clock.js +30 -0
  9. package/dist/client/collections/order.d.ts +10 -0
  10. package/dist/client/collections/order.js +77 -0
  11. package/dist/client/collections/reorder-nodes.d.ts +2 -0
  12. package/dist/client/collections/reorder-nodes.js +67 -0
  13. package/dist/client/collections/row-changes.d.ts +12 -0
  14. package/dist/client/collections/row-changes.js +26 -0
  15. package/dist/client/collections/row-history.d.ts +16 -0
  16. package/dist/client/collections/row-history.js +61 -0
  17. package/dist/client/collections/table-columns.d.ts +19 -0
  18. package/dist/client/collections/table-columns.js +200 -0
  19. package/dist/client/collections.d.ts +54 -0
  20. package/dist/client/collections.js +262 -0
  21. package/dist/client/discovery.d.ts +1 -0
  22. package/dist/client/discovery.js +57 -0
  23. package/dist/client/field-address.d.ts +38 -0
  24. package/dist/client/field-address.js +225 -0
  25. package/dist/client/field-value.d.ts +7 -0
  26. package/dist/client/field-value.js +105 -0
  27. package/dist/client/form-identity.d.ts +1 -0
  28. package/dist/client/form-identity.js +43 -0
  29. package/dist/client/form-session.d.ts +53 -0
  30. package/dist/client/form-session.js +218 -0
  31. package/dist/client/identity.d.ts +3 -0
  32. package/dist/client/identity.js +27 -0
  33. package/dist/client/index.d.ts +43 -0
  34. package/dist/client/index.js +109 -0
  35. package/dist/client/journal.d.ts +13 -0
  36. package/dist/client/journal.js +59 -0
  37. package/dist/client/lifecycle.d.ts +26 -0
  38. package/dist/client/lifecycle.js +112 -0
  39. package/dist/client/observation.d.ts +11 -0
  40. package/dist/client/observation.js +46 -0
  41. package/dist/client/outbox.d.ts +15 -0
  42. package/dist/client/outbox.js +71 -0
  43. package/dist/client/plugin.d.ts +42 -0
  44. package/dist/client/plugin.js +1 -0
  45. package/dist/client/plugins/column-order/apply.d.ts +2 -0
  46. package/dist/client/plugins/column-order/apply.js +30 -0
  47. package/dist/client/plugins/column-order/colgroups.d.ts +2 -0
  48. package/dist/client/plugins/column-order/colgroups.js +78 -0
  49. package/dist/client/plugins/column-order/index.d.ts +2 -0
  50. package/dist/client/plugins/column-order/index.js +98 -0
  51. package/dist/client/plugins/cursor/editable.d.ts +16 -0
  52. package/dist/client/plugins/cursor/editable.js +67 -0
  53. package/dist/client/plugins/cursor/geometry.d.ts +16 -0
  54. package/dist/client/plugins/cursor/geometry.js +122 -0
  55. package/dist/client/plugins/cursor/index.d.ts +4 -0
  56. package/dist/client/plugins/cursor/index.js +13 -0
  57. package/dist/client/plugins/cursor/indicators.d.ts +31 -0
  58. package/dist/client/plugins/cursor/indicators.js +144 -0
  59. package/dist/client/plugins/cursor/local-selection.d.ts +19 -0
  60. package/dist/client/plugins/cursor/local-selection.js +234 -0
  61. package/dist/client/plugins/cursor/selection.d.ts +9 -0
  62. package/dist/client/plugins/cursor/selection.js +49 -0
  63. package/dist/client/plugins/cursor/text-presence.d.ts +15 -0
  64. package/dist/client/plugins/cursor/text-presence.js +95 -0
  65. package/dist/client/plugins/fields/author.d.ts +7 -0
  66. package/dist/client/plugins/fields/author.js +22 -0
  67. package/dist/client/plugins/fields/index.d.ts +4 -0
  68. package/dist/client/plugins/fields/index.js +192 -0
  69. package/dist/client/plugins/fields/writes.d.ts +19 -0
  70. package/dist/client/plugins/fields/writes.js +51 -0
  71. package/dist/client/plugins/focus/index.d.ts +9 -0
  72. package/dist/client/plugins/focus/index.js +92 -0
  73. package/dist/client/plugins/focus/indicators.d.ts +19 -0
  74. package/dist/client/plugins/focus/indicators.js +163 -0
  75. package/dist/client/plugins/naming/algorithm.d.ts +7 -0
  76. package/dist/client/plugins/naming/algorithm.js +65 -0
  77. package/dist/client/plugins/naming/attributes.d.ts +16 -0
  78. package/dist/client/plugins/naming/attributes.js +195 -0
  79. package/dist/client/plugins/naming/index.d.ts +5 -0
  80. package/dist/client/plugins/naming/index.js +185 -0
  81. package/dist/client/plugins/range/index.d.ts +4 -0
  82. package/dist/client/plugins/range/index.js +15 -0
  83. package/dist/client/plugins/reconciliation/index.d.ts +6 -0
  84. package/dist/client/plugins/reconciliation/index.js +282 -0
  85. package/dist/client/plugins/reconciliation/matching.d.ts +12 -0
  86. package/dist/client/plugins/reconciliation/matching.js +37 -0
  87. package/dist/client/plugins/reconciliation/proposals.d.ts +11 -0
  88. package/dist/client/plugins/reconciliation/proposals.js +32 -0
  89. package/dist/client/plugins/row-order/index.d.ts +2 -0
  90. package/dist/client/plugins/row-order/index.js +99 -0
  91. package/dist/client/plugins/rows/clone.d.ts +2 -0
  92. package/dist/client/plugins/rows/clone.js +123 -0
  93. package/dist/client/plugins/rows/index.d.ts +2 -0
  94. package/dist/client/plugins/rows/index.js +191 -0
  95. package/dist/client/plugins/rows/materialize.d.ts +4 -0
  96. package/dist/client/plugins/rows/materialize.js +43 -0
  97. package/dist/client/plugins/rows/remove.d.ts +1 -0
  98. package/dist/client/plugins/rows/remove.js +13 -0
  99. package/dist/client/plugins/rows/template.d.ts +2 -0
  100. package/dist/client/plugins/rows/template.js +21 -0
  101. package/dist/client/transport.d.ts +23 -0
  102. package/dist/client/transport.js +128 -0
  103. package/dist/client/user-colour.d.ts +3 -0
  104. package/dist/client/user-colour.js +11 -0
  105. package/dist/server/acknowledgments.d.ts +3 -0
  106. package/dist/server/acknowledgments.js +8 -0
  107. package/dist/server/broadcast.d.ts +3 -0
  108. package/dist/server/broadcast.js +7 -0
  109. package/dist/server/cli.d.ts +2 -0
  110. package/dist/server/cli.js +44 -0
  111. package/dist/server/clock.d.ts +2 -0
  112. package/dist/server/clock.js +3 -0
  113. package/dist/server/configuration.d.ts +3 -0
  114. package/dist/server/configuration.js +7 -0
  115. package/dist/server/connection.d.ts +13 -0
  116. package/dist/server/connection.js +1 -0
  117. package/dist/server/express.d.ts +2 -0
  118. package/dist/server/express.js +1 -0
  119. package/dist/server/fastify.d.ts +10 -0
  120. package/dist/server/fastify.js +15 -0
  121. package/dist/server/index.d.ts +7 -0
  122. package/dist/server/index.js +1 -0
  123. package/dist/server/journal.d.ts +7 -0
  124. package/dist/server/journal.js +16 -0
  125. package/dist/server/messages.d.ts +4 -0
  126. package/dist/server/messages.js +106 -0
  127. package/dist/server/participants.d.ts +9 -0
  128. package/dist/server/participants.js +7 -0
  129. package/dist/server/receipts.d.ts +13 -0
  130. package/dist/server/receipts.js +28 -0
  131. package/dist/server/recovery.d.ts +3 -0
  132. package/dist/server/recovery.js +9 -0
  133. package/dist/server/server.d.ts +34 -0
  134. package/dist/server/server.js +189 -0
  135. package/dist/server/sessions.d.ts +16 -0
  136. package/dist/server/sessions.js +37 -0
  137. package/dist/server/standalone.d.ts +9 -0
  138. package/dist/server/standalone.js +12 -0
  139. package/dist/server/transactions.d.ts +15 -0
  140. package/dist/server/transactions.js +50 -0
  141. package/dist/server/websocket-connection.d.ts +3 -0
  142. package/dist/server/websocket-connection.js +38 -0
  143. package/dist/server/websocket.d.ts +11 -0
  144. package/dist/server/websocket.js +26 -0
  145. package/dist/shared/field-update.d.ts +12 -0
  146. package/dist/shared/field-update.js +18 -0
  147. package/dist/shared/journal-entries.d.ts +9 -0
  148. package/dist/shared/journal-entries.js +33 -0
  149. package/dist/shared/protocol.d.ts +105 -0
  150. package/dist/shared/protocol.js +15 -0
  151. package/docs/dependencies.md +130 -0
  152. package/docs/spec-addendum-dependencies.md +10 -0
  153. package/docs/spec-addendum.md +717 -0
  154. package/docs/spec.md +1165 -0
  155. package/docs/testing.md +39 -0
  156. package/package.json +96 -5
@@ -0,0 +1,458 @@
1
+ :root {
2
+ --accent: #166553;
3
+ --border: #d5e2dc;
4
+ --ink: #15362e;
5
+ --muted: #50655d;
6
+ --soft: #eaf2ed;
7
+ background: #eff4f0;
8
+ color: var(--ink);
9
+ font: 100% / 1.5 system-ui, sans-serif;
10
+ }
11
+
12
+ body, h1, h2, h3, p, pre {
13
+ margin: 0;
14
+ }
15
+
16
+ main {
17
+ box-sizing: border-box;
18
+ padding: 10px;
19
+ }
20
+
21
+ h1, h2, h3 {
22
+ line-height: 1.15;
23
+ }
24
+
25
+ h2 {
26
+ font-size: 1.25rem;
27
+ }
28
+
29
+ button {
30
+ background: white;
31
+ border: 1px solid #bdd0c7;
32
+ border-radius: .45rem;
33
+ color: var(--ink);
34
+ cursor: pointer;
35
+ font: 600 .875rem / 1.25 system-ui, sans-serif;
36
+ padding: .5rem .65rem;
37
+
38
+ &:hover:enabled { background: var(--soft); }
39
+ &:disabled { cursor: default; opacity: .55; }
40
+ }
41
+
42
+ :focus-visible {
43
+ outline: 2px solid var(--accent);
44
+ outline-offset: 2px;
45
+ }
46
+
47
+ [data-demo-focus] {
48
+ outline: 2px solid var(--accent);
49
+ outline-offset: 1px;
50
+ }
51
+
52
+ .demo-text-selection {
53
+ overflow: hidden;
54
+ pointer-events: none;
55
+ position: fixed;
56
+ z-index: 2;
57
+
58
+ > span { position: absolute; }
59
+ .caret { background: var(--accent); }
60
+ .range { background: rgb(22 101 83 / 22%); }
61
+ }
62
+
63
+ .panel, dialog, .code-example[open] {
64
+ background: white;
65
+ border: 1px solid var(--border);
66
+ border-radius: 1rem;
67
+ box-shadow: 0 3px 12px #19363008;
68
+ min-inline-size: 0;
69
+ padding: 10px;
70
+ }
71
+
72
+ .source > header, #order-collections header, .source-actions {
73
+ align-items: center;
74
+ display: flex;
75
+ flex-wrap: wrap;
76
+ gap: .65rem;
77
+ }
78
+
79
+ .source > header, #order-collections header {
80
+ justify-content: space-between;
81
+ }
82
+
83
+ .order-fields {
84
+ display: grid;
85
+ font-size: .875rem;
86
+ gap: .65rem .8rem;
87
+ grid-template-columns: minmax(0, 2fr) repeat(2, minmax(0, 1fr));
88
+ max-inline-size: 52rem;
89
+
90
+ > div {
91
+ display: grid;
92
+ gap: .25rem;
93
+ min-inline-size: 0;
94
+ }
95
+
96
+ label, #delivery-details-label { font-weight: 650; }
97
+
98
+ .full-width { grid-column: 1 / -1; }
99
+
100
+ input, select, textarea, #delivery-details {
101
+ box-sizing: border-box;
102
+ inline-size: 100%;
103
+ min-inline-size: 0;
104
+ }
105
+
106
+ textarea { resize: vertical; }
107
+ }
108
+
109
+ #delivery-details {
110
+ min-block-size: 2.5rem;
111
+ overflow-wrap: anywhere;
112
+ white-space: pre-wrap;
113
+ }
114
+
115
+ #selection-hint {
116
+ color: var(--muted);
117
+ font-size: .8rem;
118
+ }
119
+
120
+ #order-collections:not([hidden]) {
121
+ display: grid;
122
+ gap: 1rem;
123
+ margin-block-start: 1rem;
124
+ max-inline-size: 52rem;
125
+
126
+ > section {
127
+ border-block-start: 1px solid var(--border);
128
+ min-inline-size: 0;
129
+ padding-block-start: .7rem;
130
+ }
131
+
132
+ header { margin-block-end: .5rem; }
133
+ h2 { font-size: 1.05rem; }
134
+ }
135
+
136
+ #delivery-notes {
137
+ display: grid;
138
+ gap: .45rem;
139
+ grid-template-columns: minmax(0, 1fr);
140
+ list-style: none;
141
+ margin: 0;
142
+ max-inline-size: 36rem;
143
+ padding: 0;
144
+
145
+ > li {
146
+ align-items: center;
147
+ background: #f7faf8;
148
+ border: 1px solid var(--border);
149
+ border-radius: .45rem;
150
+ display: flex;
151
+ flex-wrap: wrap;
152
+ gap: .35rem;
153
+ padding: .35rem;
154
+
155
+ > input[type='text'] {
156
+ box-sizing: border-box;
157
+ flex: 1 1 11rem;
158
+ max-inline-size: 100%;
159
+ min-inline-size: 0;
160
+ }
161
+
162
+ > div { display: flex; gap: .2rem; }
163
+ }
164
+
165
+ button { padding: .25rem .45rem; }
166
+ }
167
+
168
+ .table-frame {
169
+ inline-size: min(100%, 48rem);
170
+ max-block-size: 18rem;
171
+ overflow: auto;
172
+ scrollbar-color: #91afa0 var(--soft);
173
+ }
174
+
175
+ .inventory.component-table {
176
+ --row-separator: var(--border);
177
+ background: white;
178
+ font-size: .875rem;
179
+ inline-size: 100%;
180
+ min-inline-size: 35rem;
181
+
182
+ > thead > tr > th {
183
+ background: #eaf2ee;
184
+ padding: .5rem .6rem;
185
+ vertical-align: middle;
186
+ }
187
+
188
+ > thead > tr > th:has([data-move-column]) { cursor: grab; }
189
+
190
+ > tbody > tr > td[data-component-control] {
191
+ &:focus-within, &:has([data-demo-focus]) {
192
+ outline: 2px solid var(--accent);
193
+ outline-offset: -2px;
194
+ }
195
+
196
+ > :is(input:not([type='hidden'], [type='checkbox']), select) {
197
+ color: inherit;
198
+ font: inherit;
199
+ padding: .55rem .6rem;
200
+ }
201
+ }
202
+
203
+ [data-demo-focus] { outline: none; }
204
+
205
+ [data-field='item'] { inline-size: 11rem; }
206
+ [data-field='quantity'] { inline-size: 4.25rem; }
207
+ [data-sync-column='ready'] { text-align: center; }
208
+ td[data-sync-column='actions'] { padding: .25rem .4rem; white-space: nowrap; }
209
+
210
+ :is(th, td).column-reorder {
211
+ background: #dcece4;
212
+ cursor: grabbing;
213
+ }
214
+
215
+ :is(th, td).moving { background: #cce6d8; }
216
+
217
+ input[type='checkbox'] { accent-color: var(--accent); }
218
+ button { padding: .2rem .35rem; }
219
+
220
+ [data-move-column] {
221
+ background: transparent;
222
+ border-color: transparent;
223
+ color: var(--muted);
224
+ margin-inline-start: .14rem;
225
+
226
+ &:is(:hover, :focus-visible) { background: white; border-color: var(--border); }
227
+ }
228
+ }
229
+
230
+ .source {
231
+ pre {
232
+ background: #0e2521;
233
+ border-radius: .55rem;
234
+ color: #c9efe0;
235
+ font: .81rem / 1.65 ui-monospace, monospace;
236
+ margin: 10px 0 0;
237
+ overflow-x: auto;
238
+ padding: 10px;
239
+ tab-size: 2;
240
+ white-space: pre;
241
+
242
+ .token.class-name, .token.function {
243
+ color: #91eaff;
244
+ }
245
+
246
+ .token.keyword {
247
+ color: #e4baff;
248
+ font-weight: 700;
249
+ }
250
+
251
+ .token.boolean, .token.number {
252
+ color: #ffb59d;
253
+ }
254
+
255
+ .token.operator {
256
+ color: #e4baff;
257
+ }
258
+
259
+ .token.string, .token.template-string {
260
+ color: #ffd58a;
261
+ }
262
+
263
+ .token.tag {
264
+ color: #91eaff;
265
+
266
+ .token.attr-name { color: #e4baff; }
267
+ .token.attr-value { color: #ffd58a; }
268
+ .token.punctuation { color: #9fb7b0; }
269
+ }
270
+
271
+ .token.doctype {
272
+ color: #ffb59d;
273
+ }
274
+ }
275
+
276
+ #source-language, .source-adapter {
277
+ background: var(--soft);
278
+ display: inline-flex;
279
+ gap: .12rem;
280
+ padding: .19rem;
281
+
282
+ span { border-radius: .3rem; display: block; padding: .25rem .46rem; }
283
+ }
284
+
285
+ #source-language[aria-checked='false'] span:first-child,
286
+ #source-language[aria-checked='true'] span:last-child,
287
+ :is(fieldset#source-language, .source-adapter) input:checked + span {
288
+ background: var(--accent);
289
+ color: white;
290
+ }
291
+
292
+ .source-file {
293
+ color: var(--muted);
294
+ font: .85rem ui-monospace, monospace;
295
+ }
296
+
297
+ fieldset#source-language, .source-adapter {
298
+ border: 1px solid #bdd0c7;
299
+ border-radius: .45rem;
300
+ flex-wrap: wrap;
301
+ font: 600 .875rem / 1.25 system-ui, sans-serif;
302
+ margin: 0;
303
+ min-inline-size: 0;
304
+
305
+ label { cursor: pointer; }
306
+ input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
307
+ }
308
+ }
309
+
310
+ .visually-hidden {
311
+ clip-path: inset(50%);
312
+ height: 1px;
313
+ overflow: hidden;
314
+ position: absolute;
315
+ white-space: nowrap;
316
+ width: 1px;
317
+ }
318
+
319
+ .button {
320
+ background: white;
321
+ border: 1px solid #bdd0c7;
322
+ border-radius: .45rem;
323
+ color: var(--ink);
324
+ font: 600 .875rem / 1.25 system-ui, sans-serif;
325
+ padding: .5rem .65rem;
326
+ text-decoration: none;
327
+
328
+ &:hover { background: var(--soft); }
329
+ }
330
+
331
+ select, textarea, .order-fields input, #delivery-details, #delivery-notes input[type='text'] {
332
+ background: white;
333
+ border: 1px solid var(--border);
334
+ border-radius: .28rem;
335
+ color: var(--ink);
336
+ font: inherit;
337
+ padding: .28rem .4rem;
338
+ }
339
+
340
+ .configuration {
341
+ margin-block-start: .7rem;
342
+
343
+ > summary {
344
+ color: var(--accent);
345
+ cursor: pointer;
346
+ font-size: .875rem;
347
+ font-weight: 650;
348
+ inline-size: fit-content;
349
+ padding: .3rem .55rem;
350
+ }
351
+
352
+ > :not(summary) { margin-block-start: .5rem; }
353
+ }
354
+
355
+ .code-example {
356
+ > summary { margin: 0; }
357
+ h2 { display: inline; font-size: 1rem; }
358
+
359
+ &[open] {
360
+ --narrow-source-template: 'title title' auto 'file actions' auto 'code code' auto / max-content minmax(0, 1fr);
361
+ align-items: center;
362
+ column-gap: .65rem;
363
+ display: grid;
364
+ grid-template: 'title file actions' auto 'code code code' auto / max-content minmax(0, 1fr) max-content;
365
+
366
+ &::details-content { display: contents; }
367
+ > .source { display: contents; }
368
+ > summary { grid-area: title; padding-inline-start: 0; }
369
+ .source-file { grid-area: file; justify-self: var(--source-file-align, center); }
370
+ .source-actions { grid-area: actions; justify-content: flex-end; min-inline-size: 0; }
371
+ pre { grid-area: code; min-inline-size: 0; }
372
+ }
373
+ }
374
+
375
+ @media (max-width: 39rem) {
376
+ .server-view .code-example[open] {
377
+ --source-file-align: start;
378
+ grid-template: var(--narrow-source-template);
379
+ }
380
+ }
381
+
382
+ @media (max-width: 29rem) {
383
+ .client .code-example[open] {
384
+ --source-file-align: start;
385
+ grid-template: var(--narrow-source-template);
386
+ }
387
+ }
388
+
389
+ #connection:not([hidden]) {
390
+ color: #863513;
391
+ font-size: .875rem;
392
+ margin-block-end: .45rem;
393
+ }
394
+
395
+ .server-view {
396
+ h1 { font-size: 1.4rem; }
397
+ .panel p { margin-block-start: .45rem; }
398
+ #server-connections { font-size: .9rem; font-weight: 600; }
399
+ #server-start {
400
+ background: var(--soft);
401
+ border-radius: .45rem;
402
+ font: .85rem / 1.5 ui-monospace, monospace;
403
+ margin-block-start: .4rem;
404
+ overflow-x: auto;
405
+ padding: .45rem .65rem;
406
+ }
407
+ #server-clients {
408
+ font-size: .875rem;
409
+ margin-block: .4rem 0;
410
+ padding-inline-start: 1.25rem;
411
+ }
412
+ }
413
+
414
+ @media (max-width: 34rem) {
415
+ .order-fields {
416
+ grid-template-columns: repeat(2, minmax(0, 1fr));
417
+
418
+ > :first-child { grid-column: 1 / -1; }
419
+ }
420
+ }
421
+
422
+ @media (max-width: 22rem) {
423
+ .client {
424
+ main { padding: .35rem; }
425
+ .preview { border-radius: .65rem; padding: .45rem; }
426
+ }
427
+
428
+ .order-fields { grid-template-columns: minmax(0, 1fr); }
429
+ }
430
+
431
+ #server-runtime {
432
+ border-block-start: 1px solid var(--border);
433
+ margin-block-start: .9rem;
434
+ padding-block-start: .7rem;
435
+
436
+ header {
437
+ align-items: center;
438
+ display: flex;
439
+ flex-wrap: wrap;
440
+ gap: .35rem .65rem;
441
+ }
442
+
443
+ h2 { font-size: 1.05rem; }
444
+
445
+ #server-state {
446
+ background: var(--soft);
447
+ border-radius: .3rem;
448
+ font-size: .78rem;
449
+ font-weight: 700;
450
+ padding: .12rem .4rem;
451
+
452
+ &[data-state='active'] { color: var(--accent); }
453
+ &[data-state='standby'] { background: #f7ecd3; color: #74500e; }
454
+ &:is([data-state='stopped'], [data-state='unavailable']) { background: #f8e8e0; color: #863513; }
455
+ }
456
+
457
+ #server-reason { color: var(--muted); font-size: .875rem; }
458
+ }
@@ -0,0 +1,2 @@
1
+ export declare function paintAuthorBackground(element: HTMLElement, colour: string): void;
2
+ export declare function restoreAuthorBackground(model: Element, clone: Element): void;
@@ -0,0 +1,28 @@
1
+ const backgrounds = new WeakMap();
2
+ export function paintAuthorBackground(element, colour) {
3
+ let saved = backgrounds.get(element);
4
+ const value = 'color-mix(in srgb, ' + colour + ' 16%, white)';
5
+ if (!saved || (element.style.getPropertyValue('background-color') !== saved.applied)) {
6
+ saved = {
7
+ value: element.style.getPropertyValue('background-color'),
8
+ priority: element.style.getPropertyPriority('background-color'),
9
+ applied: ''
10
+ };
11
+ backgrounds.set(element, saved);
12
+ }
13
+ if (element.style.getPropertyValue('background-color') !== value) {
14
+ element.style.setProperty('background-color', value);
15
+ saved.applied = element.style.getPropertyValue('background-color');
16
+ }
17
+ }
18
+ export function restoreAuthorBackground(model, clone) {
19
+ if (!(model instanceof HTMLElement) || !(clone instanceof HTMLElement))
20
+ return;
21
+ const saved = backgrounds.get(model);
22
+ if (saved && (clone.style.getPropertyValue('background-color') === saved.applied)) {
23
+ clone.style.setProperty('background-color', saved.value, saved.priority);
24
+ }
25
+ if (model === clone) {
26
+ backgrounds.delete(model);
27
+ }
28
+ }
@@ -0,0 +1,10 @@
1
+ export declare class Clock {
2
+ private calibrated;
3
+ private last;
4
+ private offset;
5
+ private samples;
6
+ measure(sent: number, server: number, received?: number): void;
7
+ local(): number;
8
+ seal(): number;
9
+ now(): number;
10
+ }
@@ -0,0 +1,30 @@
1
+ export class Clock {
2
+ calibrated = false;
3
+ last = 0;
4
+ offset = 0;
5
+ samples = [];
6
+ measure(sent, server, received = this.local()) {
7
+ if (!Number.isFinite(sent) || !Number.isFinite(server) || (received < sent))
8
+ return;
9
+ const delay = received - sent;
10
+ this.samples.push({ delay, offset: server - (sent + delay / 2) });
11
+ if (this.samples.length > 5) {
12
+ this.samples.shift();
13
+ }
14
+ this.offset = this.samples.reduce((best, current) => (current.delay < best.delay) ? current : best).offset;
15
+ }
16
+ local() {
17
+ return performance.timeOrigin + performance.now();
18
+ }
19
+ seal() {
20
+ if (this.calibrated)
21
+ return 0;
22
+ this.calibrated = true;
23
+ this.last += this.offset;
24
+ return this.offset;
25
+ }
26
+ now() {
27
+ this.last = Math.max(this.last + 0.001, this.local() + (this.calibrated ? this.offset : 0));
28
+ return this.last;
29
+ }
30
+ }
@@ -0,0 +1,10 @@
1
+ import type { Transaction } from '../../shared/protocol.js';
2
+ export interface Insertion {
3
+ after: string | null;
4
+ row: string;
5
+ transaction: Transaction;
6
+ }
7
+ export declare function equalOrder(before: readonly string[], after: readonly string[]): boolean;
8
+ export declare function mergeOrder(present: readonly string[], requested: readonly string[]): string[];
9
+ export declare function orderChanged(before: readonly string[], after: readonly string[]): boolean;
10
+ export declare function rowSequence(initial: readonly string[], insertions: readonly Insertion[], removed: ReadonlySet<string>, requested?: readonly string[]): string[];
@@ -0,0 +1,77 @@
1
+ import { compareTransactions } from '../../shared/protocol.js';
2
+ export function equalOrder(before, after) {
3
+ return (before.length === after.length) && before.every((id, index) => id === after[index]);
4
+ }
5
+ export function mergeOrder(present, requested) {
6
+ const available = new Set(present);
7
+ const ordered = [...new Set(requested)].filter(id => available.has(id));
8
+ const mentioned = new Set(ordered);
9
+ let index = 0;
10
+ return present.map(id => mentioned.has(id)
11
+ ? ordered[index++]
12
+ : id);
13
+ }
14
+ export function orderChanged(before, after) {
15
+ const previous = new Set(before);
16
+ const current = new Set(after);
17
+ return !equalOrder(before.filter(id => current.has(id)), after.filter(id => previous.has(id)));
18
+ }
19
+ export function rowSequence(initial, insertions, removed, requested) {
20
+ const baseline = new Set(initial);
21
+ const additions = new Map();
22
+ const children = new Map();
23
+ const visited = new Set();
24
+ const ordered = [];
25
+ // Keep the first placement for an identity. Newer siblings precede the existing continuation.
26
+ const chronological = insertions.slice().sort((left, right) => compareTransactions(left.transaction, right.transaction));
27
+ for (const insertion of chronological) {
28
+ if (!additions.has(insertion.row)) {
29
+ additions.set(insertion.row, insertion);
30
+ }
31
+ }
32
+ const latestFirst = [...additions.values()].reverse();
33
+ const roots = [...new Set(requested ?? initial.filter(row => !additions.has(row)))];
34
+ const explicit = new Set(roots);
35
+ const addChild = (parent, child) => {
36
+ if (explicit.has(child))
37
+ return;
38
+ const siblings = children.get(parent);
39
+ if (siblings) {
40
+ siblings.push(child);
41
+ }
42
+ else {
43
+ children.set(parent, [child]);
44
+ }
45
+ };
46
+ for (const { after, row } of latestFirst) {
47
+ addChild(after, row);
48
+ }
49
+ initial.forEach((row, index) => {
50
+ if (!additions.has(row))
51
+ addChild(initial[index - 1] ?? null, row);
52
+ });
53
+ const walk = (start) => {
54
+ const pending = [...start].reverse();
55
+ while (pending.length) {
56
+ const row = pending.pop();
57
+ if (visited.has(row))
58
+ continue;
59
+ visited.add(row);
60
+ // Removed and temporarily unknown rows still anchor insertions.
61
+ if (!removed.has(row) && (explicit.has(row) || baseline.has(row) || additions.has(row))) {
62
+ ordered.push(row);
63
+ }
64
+ const descendants = children.get(row);
65
+ for (let index = (descendants?.length ?? 0) - 1; index >= 0; index--) {
66
+ pending.push(descendants[index]);
67
+ }
68
+ }
69
+ };
70
+ walk(children.get(null) ?? []);
71
+ walk(roots);
72
+ // Unknown anchors or cyclic aliases can leave an entire branch outside the known roots.
73
+ walk([...children.keys()].filter((id) => !!id && !baseline.has(id) && !additions.has(id)).sort());
74
+ walk(latestFirst.map(insertion => insertion.row));
75
+ walk(initial);
76
+ return ordered;
77
+ }
@@ -0,0 +1,2 @@
1
+ export declare function reorderByParent(rows: readonly (Element | undefined)[]): void;
2
+ export declare function reorderNodes(parent: Element, target: readonly Element[]): void;
@@ -0,0 +1,67 @@
1
+ export function reorderByParent(rows) {
2
+ const parents = new Map();
3
+ for (const row of rows) {
4
+ const parent = row?.parentElement;
5
+ if (!row || !parent)
6
+ continue;
7
+ const siblings = parents.get(parent);
8
+ if (siblings) {
9
+ siblings.push(row);
10
+ }
11
+ else {
12
+ parents.set(parent, [row]);
13
+ }
14
+ }
15
+ for (const [parent, children] of parents)
16
+ reorderNodes(parent, children);
17
+ }
18
+ export function reorderNodes(parent, target) {
19
+ const nodes = new Set(target);
20
+ const current = [...parent.children].filter(node => nodes.has(node));
21
+ if ((current.length === target.length) && current.every((node, index) => node === target[index]))
22
+ return;
23
+ const stationary = stationaryNodes(current, target);
24
+ const movable = parent;
25
+ let reference = current.at(-1)?.nextSibling ?? null;
26
+ for (let index = target.length - 1; index >= 0; index--) {
27
+ const node = target[index];
28
+ if (!stationary.has(node)) {
29
+ if (movable.moveBefore) {
30
+ movable.moveBefore(node, reference);
31
+ }
32
+ else {
33
+ parent.insertBefore(node, reference);
34
+ }
35
+ }
36
+ reference = node;
37
+ }
38
+ }
39
+ function stationaryNodes(current, target) {
40
+ const indices = new Map(current.map((node, index) => [node, index]));
41
+ const ends = [];
42
+ const previous = [];
43
+ // The longest increasing subsequence is the largest set of elements already in order.
44
+ target.forEach((node, index) => {
45
+ const position = indices.get(node);
46
+ let left = 0;
47
+ let right = ends.length;
48
+ while (left < right) {
49
+ const middle = Math.floor((left + right) / 2);
50
+ if (indices.get(target[ends[middle]]) < position) {
51
+ left = middle + 1;
52
+ }
53
+ else {
54
+ right = middle;
55
+ }
56
+ }
57
+ previous[index] = ends[left - 1] ?? -1;
58
+ ends[left] = index;
59
+ });
60
+ const stationary = new Set();
61
+ let index = ends.at(-1) ?? -1;
62
+ while (index >= 0) {
63
+ stationary.add(target[index]);
64
+ index = previous[index];
65
+ }
66
+ return stationary;
67
+ }
@@ -0,0 +1,12 @@
1
+ import type { Transaction } from '../../shared/protocol.js';
2
+ import type { RowCreation } from '../collections.js';
3
+ interface RowReference {
4
+ collection: string;
5
+ row: string;
6
+ }
7
+ interface RowRemoval extends RowReference {
8
+ after?: string | null;
9
+ }
10
+ export declare function creationOf(transaction: Transaction): RowCreation | undefined;
11
+ export declare function removalOf(transaction: Transaction): RowRemoval | undefined;
12
+ export {};