@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,57 @@
1
+ import { identifyForms } from './form-identity.js';
2
+ export function discoverForms(selector, attach, detach, session) {
3
+ let active = new Map();
4
+ const window = document.defaultView;
5
+ const abort = new AbortController();
6
+ function scan() {
7
+ const next = session ? configuredForms(selector, session) : identifyForms(document, selector);
8
+ for (const [form, key] of active) {
9
+ if (next.get(form) !== key) {
10
+ detach(key);
11
+ }
12
+ }
13
+ for (const [form, key] of next) {
14
+ if (active.get(form) !== key) {
15
+ attach(form, key);
16
+ }
17
+ }
18
+ active = next;
19
+ }
20
+ scan();
21
+ const observer = new MutationObserver(records => {
22
+ if ((selector !== 'form') || records.some(changesForms)) {
23
+ scan();
24
+ }
25
+ });
26
+ observer.observe(document, { attributes: true, childList: true, subtree: true });
27
+ window?.addEventListener('popstate', scan, { signal: abort.signal });
28
+ window?.navigation?.addEventListener('currententrychange', scan, { signal: abort.signal });
29
+ return () => {
30
+ abort.abort();
31
+ observer.disconnect();
32
+ active.clear();
33
+ };
34
+ }
35
+ function changesForms(record) {
36
+ const target = record.target;
37
+ if (record.type === 'attributes') {
38
+ return (target instanceof HTMLFormElement) && ['action', 'id', 'name'].includes(record.attributeName ?? '');
39
+ }
40
+ if ([...record.addedNodes, ...record.removedNodes].some(node => (node instanceof Element) && ((node instanceof HTMLFormElement) || node.querySelector('form'))))
41
+ return true;
42
+ return (target instanceof Element) && !!target.querySelector('form') && ([...record.addedNodes, ...record.removedNodes].some(node => node instanceof Element));
43
+ }
44
+ function configuredForms(selector, session) {
45
+ const forms = new Map();
46
+ const keys = new Set();
47
+ for (const form of document.querySelectorAll(selector)) {
48
+ if (!(form instanceof HTMLFormElement))
49
+ continue;
50
+ const key = session(form);
51
+ if (!key || keys.has(key))
52
+ continue;
53
+ keys.add(key);
54
+ forms.set(form, key);
55
+ }
56
+ return forms;
57
+ }
@@ -0,0 +1,38 @@
1
+ import type { FieldAddress } from '../shared/field-update.js';
2
+ import type { Collections } from './collections.js';
3
+ import type { Control } from './field-value.js';
4
+ import type { FieldValue } from './field-value.js';
5
+ export { addressKey, fieldUpdate } from '../shared/field-update.js';
6
+ export type { FieldAddress, FieldUpdate } from '../shared/field-update.js';
7
+ export interface InputPairOptions {
8
+ selector: string;
9
+ applied?: (input: HTMLInputElement) => void;
10
+ }
11
+ export declare function parseAddress(key: string): FieldAddress | undefined;
12
+ export declare function controlsIn(scope: Element): Control[];
13
+ export declare function targetControl(target: EventTarget | null): Control | undefined;
14
+ export declare function valuesEqual(first: FieldValue | undefined, second: FieldValue): boolean;
15
+ export declare class FieldAddresses {
16
+ private form;
17
+ private collections;
18
+ private ignore?;
19
+ private flat?;
20
+ private pairSelector?;
21
+ private pairApplied?;
22
+ constructor(form: HTMLFormElement, collections: Collections, ignore?: string | undefined, inputPairs?: string | InputPairOptions);
23
+ address(control: Control): FieldAddress | undefined;
24
+ controls(address: FieldAddress): Control[];
25
+ control(target: EventTarget | null): Control | undefined;
26
+ read(control: Control): FieldValue;
27
+ write(control: Control, value: FieldValue): void;
28
+ formControls(): Control[];
29
+ invalidate(): void;
30
+ key(address: FieldAddress): string;
31
+ owns(control: Control): boolean;
32
+ snapshot(row: Element): Record<string, FieldValue>;
33
+ private relative;
34
+ private name;
35
+ private partner;
36
+ private pairInput;
37
+ private standalone;
38
+ }
@@ -0,0 +1,225 @@
1
+ import { addressKey } from '../shared/field-update.js';
2
+ import { columnIdentity } from './collections/table-columns.js';
3
+ import { controlSelector } from './field-value.js';
4
+ import { isControl } from './field-value.js';
5
+ import { readValue } from './field-value.js';
6
+ import { writeValue } from './field-value.js';
7
+ import { domPath } from './identity.js';
8
+ export { addressKey, fieldUpdate } from '../shared/field-update.js';
9
+ export function parseAddress(key) {
10
+ try {
11
+ const parts = JSON.parse(key);
12
+ if (!Array.isArray(parts) || (parts.length !== 3) || parts.some(part => typeof part !== 'string')) {
13
+ return;
14
+ }
15
+ const [collection, row, field] = parts;
16
+ return collection ? { collection, row, field } : { field };
17
+ }
18
+ catch {
19
+ return;
20
+ }
21
+ }
22
+ export function controlsIn(scope) {
23
+ return Array.from(scope.querySelectorAll(controlSelector)).filter(isControl);
24
+ }
25
+ export function targetControl(target) {
26
+ const node = (target instanceof Element) ? target.closest(controlSelector) : null;
27
+ if (node && isControl(node))
28
+ return node;
29
+ }
30
+ export function valuesEqual(first, second) {
31
+ return (first === second) || (Array.isArray(first) && Array.isArray(second)
32
+ && (first.length === second.length) && first.every((value, index) => value === second[index]));
33
+ }
34
+ export class FieldAddresses {
35
+ form;
36
+ collections;
37
+ ignore;
38
+ flat;
39
+ pairSelector;
40
+ pairApplied;
41
+ constructor(form, collections, ignore, inputPairs) {
42
+ this.form = form;
43
+ this.collections = collections;
44
+ this.ignore = ignore;
45
+ this.pairSelector = (typeof inputPairs === 'string') ? inputPairs : inputPairs?.selector;
46
+ this.pairApplied = (typeof inputPairs === 'object') ? inputPairs.applied : undefined;
47
+ }
48
+ address(control) {
49
+ if (!this.owns(control))
50
+ return;
51
+ const location = this.collections.locate(control);
52
+ if (location) {
53
+ const key = this.collections.get(location.collection)?.options.key;
54
+ const node = key ? location.element.querySelector(key) : null;
55
+ if (node === control)
56
+ return;
57
+ return {
58
+ collection: location.collection,
59
+ row: location.raw,
60
+ field: this.relative(control, location.element, node)
61
+ };
62
+ }
63
+ const fields = this.standalone();
64
+ let field = fields.addresses.get(control);
65
+ if (!field) {
66
+ this.invalidate();
67
+ field = this.standalone().addresses.get(control);
68
+ }
69
+ return field ? { field } : undefined;
70
+ }
71
+ controls(address) {
72
+ const row = (address.collection && address.row)
73
+ ? this.collections.findRow(address.collection, address.row)
74
+ : undefined;
75
+ if (address.collection) {
76
+ return row ? controlsIn(row).filter(control => {
77
+ const candidate = this.address(control);
78
+ return (candidate?.field === address.field) && (candidate.collection === address.collection);
79
+ }) : [];
80
+ }
81
+ const controls = this.standalone().controls.get(address.field) ?? [];
82
+ if (controls.some(control => !this.owns(control))) {
83
+ this.invalidate();
84
+ return this.standalone().controls.get(address.field) ?? [];
85
+ }
86
+ return controls;
87
+ }
88
+ control(target) {
89
+ if ((target instanceof HTMLInputElement) && (target.type === 'hidden') && this.pairSelector) {
90
+ return [target.previousElementSibling, target.nextElementSibling].find((control) => (control instanceof HTMLInputElement)
91
+ && (this.partner(control) === target) && this.owns(control));
92
+ }
93
+ return targetControl(target);
94
+ }
95
+ read(control) {
96
+ if (control instanceof HTMLInputElement) {
97
+ const partner = this.partner(control);
98
+ if (partner)
99
+ return [control.value, partner.value];
100
+ }
101
+ return readValue(control);
102
+ }
103
+ write(control, value) {
104
+ const partner = this.partner(control);
105
+ if (!partner) {
106
+ writeValue(control, value);
107
+ }
108
+ else if ((control instanceof HTMLInputElement) && Array.isArray(value) && (value.length === 2)) {
109
+ if (partner.value !== value[1]) {
110
+ partner.value = value[1];
111
+ }
112
+ writeValue(control, value[0]);
113
+ this.pairApplied?.(control);
114
+ }
115
+ }
116
+ formControls() {
117
+ const controls = controlsIn(this.form);
118
+ return controls.concat(Array.from(this.form.elements).filter(isControl).filter(node => !this.form.contains(node)))
119
+ .filter(control => this.owns(control));
120
+ }
121
+ invalidate() {
122
+ this.flat = undefined;
123
+ }
124
+ key(address) {
125
+ const { collection, row } = address;
126
+ return addressKey((collection && row)
127
+ ? { ...address, row: this.collections.canonical(collection, row) }
128
+ : address);
129
+ }
130
+ owns(control) {
131
+ if (this.ignore && control.matches(this.ignore))
132
+ return false;
133
+ if ('form' in control)
134
+ return control.form === this.form;
135
+ return control.closest('form') === this.form;
136
+ }
137
+ snapshot(row) {
138
+ const result = Object.create(null);
139
+ for (const control of controlsIn(row)) {
140
+ const address = this.address(control);
141
+ if (!address || (this.collections.locate(control)?.element !== row))
142
+ continue;
143
+ const value = this.read(control);
144
+ if (!(address.field in result) || (value !== null)) {
145
+ result[address.field] = value;
146
+ }
147
+ }
148
+ return result;
149
+ }
150
+ relative(control, row, key) {
151
+ const first = (control instanceof HTMLInputElement) && (control.type === 'radio') && control.name
152
+ ? controlsIn(row).find(other => sameRadioGroup(control, other)) ?? control
153
+ : control;
154
+ const name = this.name(first);
155
+ const prefix = key?.getAttribute('name')?.match(/^(.+?)(?:\.id|\[id\])$/)?.[1];
156
+ if (prefix && name && (name.startsWith(prefix + '.') || name.startsWith(prefix + '['))) {
157
+ return 'name:' + name.slice(prefix.length);
158
+ }
159
+ const col = columnIdentity(first);
160
+ const cell = first.closest('td,th');
161
+ return (col && cell && row.contains(cell))
162
+ ? JSON.stringify([col, domPath(first, cell)])
163
+ : domPath(first, row);
164
+ }
165
+ name(control) {
166
+ return control.getAttribute('name') || this.partner(control)?.name || '';
167
+ }
168
+ partner(control) {
169
+ if (!this.pairInput(control))
170
+ return;
171
+ const candidates = [control.previousElementSibling, control.nextElementSibling].filter((candidate) => (candidate instanceof HTMLInputElement)
172
+ && (candidate.type === 'hidden') && !!candidate.name && this.owns(candidate));
173
+ if (candidates.length !== 1)
174
+ return;
175
+ const partner = candidates[0];
176
+ const other = (partner.previousElementSibling === control)
177
+ ? partner.nextElementSibling
178
+ : partner.previousElementSibling;
179
+ if (!this.pairInput(other) || !this.owns(other))
180
+ return partner;
181
+ }
182
+ pairInput(element) {
183
+ return !!this.pairSelector && (element instanceof HTMLInputElement)
184
+ && ((element.type === 'text') || (element.type === 'search')) && element.matches(this.pairSelector);
185
+ }
186
+ standalone() {
187
+ const elements = this.form.elements.length;
188
+ const revision = this.collections.revision;
189
+ if (this.flat && (this.flat.elements === elements) && (this.flat.revision === revision))
190
+ return this.flat;
191
+ const addresses = new WeakMap();
192
+ const controls = new Map();
193
+ const counts = new Map();
194
+ for (const control of this.formControls()) {
195
+ if (this.collections.locate(control))
196
+ continue;
197
+ const name = this.name(control);
198
+ const radio = (control instanceof HTMLInputElement) && (control.type === 'radio') && name;
199
+ const base = radio ? 'radio:' + name : standaloneKey(control, name, this.form);
200
+ const count = counts.get(base) ?? 0;
201
+ const key = (!count || radio) ? base : JSON.stringify([base, count]);
202
+ addresses.set(control, key);
203
+ const group = controls.get(key) ?? [];
204
+ group.push(control);
205
+ controls.set(key, group);
206
+ counts.set(base, count + 1);
207
+ }
208
+ return this.flat = { addresses, controls, elements, revision };
209
+ }
210
+ }
211
+ function sameRadioGroup(first, second) {
212
+ return (second instanceof HTMLInputElement) && (second.type === 'radio') && (first.name === second.name);
213
+ }
214
+ function standaloneKey(control, name, form) {
215
+ if (control.id) {
216
+ return 'id:' + control.id;
217
+ }
218
+ if ((control instanceof HTMLInputElement) && (control.type === 'checkbox') && name) {
219
+ return JSON.stringify(['checkbox', name, control.value]);
220
+ }
221
+ if (name) {
222
+ return 'name:' + name;
223
+ }
224
+ return domPath(control, form);
225
+ }
@@ -0,0 +1,7 @@
1
+ import type { FieldValue } from '../shared/field-update.js';
2
+ export type { FieldValue } from '../shared/field-update.js';
3
+ export type Control = HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement | HTMLElement;
4
+ export declare const controlSelector = "input, select, textarea, [contenteditable]:not([contenteditable=\"false\"])";
5
+ export declare function isControl(node: Element): node is Control;
6
+ export declare function readValue(control: Control): FieldValue;
7
+ export declare function writeValue(control: Control, value: FieldValue): void;
@@ -0,0 +1,105 @@
1
+ export const controlSelector = 'input, select, textarea, [contenteditable]:not([contenteditable="false"])';
2
+ export function isControl(node) {
3
+ if (!(node instanceof HTMLElement) || !node.matches(controlSelector))
4
+ return false;
5
+ return !(node instanceof HTMLInputElement)
6
+ || !['button', 'file', 'hidden', 'image', 'password', 'reset', 'submit'].includes(node.type);
7
+ }
8
+ export function readValue(control) {
9
+ if (control instanceof HTMLSelectElement) {
10
+ if (control.multiple) {
11
+ return Array.from(control.selectedOptions, option => option.value);
12
+ }
13
+ return (control.selectedIndex < 0) ? null : control.value;
14
+ }
15
+ if (control instanceof HTMLInputElement) {
16
+ if (control.type === 'checkbox')
17
+ return control.checked;
18
+ if (control.type === 'radio') {
19
+ return control.checked ? control.value : null;
20
+ }
21
+ return control.value;
22
+ }
23
+ if (control instanceof HTMLTextAreaElement)
24
+ return control.value;
25
+ return control.innerText;
26
+ }
27
+ export function writeValue(control, value) {
28
+ if (control instanceof HTMLSelectElement) {
29
+ if (control.multiple && Array.isArray(value)) {
30
+ const selected = new Set(value);
31
+ for (const option of control.options) {
32
+ option.selected = selected.has(option.value);
33
+ }
34
+ }
35
+ else if ((typeof value === 'string') || (value === null)) {
36
+ if (value === null) {
37
+ control.selectedIndex = -1;
38
+ }
39
+ else {
40
+ control.value = value;
41
+ }
42
+ }
43
+ return;
44
+ }
45
+ if (control instanceof HTMLInputElement) {
46
+ if (control.type === 'checkbox') {
47
+ if (typeof value === 'boolean') {
48
+ control.checked = value;
49
+ }
50
+ return;
51
+ }
52
+ if (control.type === 'radio') {
53
+ control.checked = value === control.value;
54
+ return;
55
+ }
56
+ }
57
+ if (typeof value !== 'string')
58
+ return;
59
+ if ((control instanceof HTMLInputElement) || (control instanceof HTMLTextAreaElement)) {
60
+ if (control.value === value)
61
+ return;
62
+ const start = control.selectionStart;
63
+ const end = control.selectionEnd;
64
+ const direction = control.selectionDirection;
65
+ control.value = value;
66
+ if ((start !== null) && (end !== null)) {
67
+ control.setSelectionRange(start, end, direction ?? undefined);
68
+ }
69
+ return;
70
+ }
71
+ if (control.innerText === value)
72
+ return;
73
+ writeEditable(control, value);
74
+ }
75
+ function writeEditable(control, value) {
76
+ const selection = control.ownerDocument.getSelection();
77
+ const anchor = offsetIn(control, selection?.anchorNode ?? null, selection?.anchorOffset ?? 0);
78
+ const focus = offsetIn(control, selection?.focusNode ?? null, selection?.focusOffset ?? 0);
79
+ control.innerText = value;
80
+ if (!selection || (anchor === null) || (focus === null))
81
+ return;
82
+ const position = (offset) => {
83
+ const walker = control.ownerDocument.createTreeWalker(control, NodeFilter.SHOW_TEXT);
84
+ let node = walker.nextNode();
85
+ while (node) {
86
+ const length = node.textContent?.length ?? 0;
87
+ if (offset <= length)
88
+ return [node, offset];
89
+ offset -= length + Number(node.nextSibling?.nodeName === 'BR');
90
+ node = walker.nextNode();
91
+ }
92
+ return [control, control.childNodes.length];
93
+ };
94
+ const [anchorNode, anchorOffset] = position(anchor);
95
+ const [focusNode, focusOffset] = position(focus);
96
+ selection.setBaseAndExtent(anchorNode, anchorOffset, focusNode, focusOffset);
97
+ }
98
+ function offsetIn(control, node, offset) {
99
+ if (!node || !control.contains(node))
100
+ return null;
101
+ const range = control.ownerDocument.createRange();
102
+ range.selectNodeContents(control);
103
+ range.setEnd(node, offset);
104
+ return range.toString().length;
105
+ }
@@ -0,0 +1 @@
1
+ export declare function identifyForms(document: Document, selector: string): Map<HTMLFormElement, string>;
@@ -0,0 +1,43 @@
1
+ import { domPath } from './identity.js';
2
+ import { pageIdentity } from './identity.js';
3
+ export function identifyForms(document, selector) {
4
+ const page = pageIdentity(document.URL);
5
+ const groups = new Map();
6
+ const forms = new Map();
7
+ for (const form of document.querySelectorAll(selector)) {
8
+ if (!(form instanceof HTMLFormElement))
9
+ continue;
10
+ const name = form.getAttribute('name') ?? '';
11
+ const action = actionIdentity(form, document);
12
+ const details = [name, form.id, action];
13
+ const index = details.findIndex(Boolean);
14
+ const primary = (index < 0) ? domPath(form, document.documentElement) : details[index];
15
+ const key = JSON.stringify([page, index, primary]);
16
+ const group = groups.get(key) ?? [];
17
+ group.push({ form, details });
18
+ groups.set(key, group);
19
+ }
20
+ for (const [key, group] of groups) {
21
+ for (const { form, details } of group) {
22
+ if (group.length === 1) {
23
+ forms.set(form, key);
24
+ continue;
25
+ }
26
+ const repeated = group.filter(entry => entry.details.every((part, index) => part === details[index]));
27
+ const path = (repeated.length > 1) ? domPath(form, document.documentElement) : '';
28
+ forms.set(form, JSON.stringify([page, ...details, path]));
29
+ }
30
+ }
31
+ return forms;
32
+ }
33
+ function actionIdentity(form, document) {
34
+ const action = form.getAttribute('action');
35
+ if (!action)
36
+ return '';
37
+ try {
38
+ return pageIdentity(new URL(action, document.baseURI).href);
39
+ }
40
+ catch {
41
+ return '';
42
+ }
43
+ }
@@ -0,0 +1,53 @@
1
+ import type { ClientMessage } from '../shared/protocol.js';
2
+ import type { ServerMessage } from '../shared/protocol.js';
3
+ import type { User } from '../shared/protocol.js';
4
+ import type { Clock } from './clock.js';
5
+ import type { CollectionOptions } from './collections.js';
6
+ import type { InputPairOptions } from './field-address.js';
7
+ import type { Plugin } from './plugin.js';
8
+ interface Services {
9
+ client: string;
10
+ clock: Clock;
11
+ collections: readonly CollectionOptions[];
12
+ debounce: number;
13
+ ignore?: string;
14
+ inputPairs?: string | InputPairOptions;
15
+ plugins: readonly Plugin[];
16
+ retired(): void;
17
+ send(message: ClientMessage): boolean;
18
+ user(): User | undefined;
19
+ }
20
+ type Message = Extract<ServerMessage, {
21
+ session: string;
22
+ }>;
23
+ export declare class FormSession {
24
+ private session;
25
+ private services;
26
+ private connection?;
27
+ private field;
28
+ private journal?;
29
+ private lifecycle?;
30
+ private outbox;
31
+ private position;
32
+ private ready;
33
+ private retired;
34
+ private selection;
35
+ private timer?;
36
+ constructor(session: string, services: Services);
37
+ attach(form: HTMLFormElement, connection?: string): void;
38
+ correct(offset: number): void;
39
+ detach(): void;
40
+ disconnect(): void;
41
+ dispose(): void;
42
+ join(connection: string): void;
43
+ receive(message: Message): void;
44
+ private cursor;
45
+ private sendCursor;
46
+ private range;
47
+ private sendRange;
48
+ private focus;
49
+ private publish;
50
+ private pump;
51
+ private receiveTransactions;
52
+ }
53
+ export {};