@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.
- package/LICENSE +165 -0
- package/README.md +249 -2
- package/config.yaml +2 -0
- package/demo/src/demo.css +458 -0
- package/dist/client/author-background.d.ts +2 -0
- package/dist/client/author-background.js +28 -0
- package/dist/client/clock.d.ts +10 -0
- package/dist/client/clock.js +30 -0
- package/dist/client/collections/order.d.ts +10 -0
- package/dist/client/collections/order.js +77 -0
- package/dist/client/collections/reorder-nodes.d.ts +2 -0
- package/dist/client/collections/reorder-nodes.js +67 -0
- package/dist/client/collections/row-changes.d.ts +12 -0
- package/dist/client/collections/row-changes.js +26 -0
- package/dist/client/collections/row-history.d.ts +16 -0
- package/dist/client/collections/row-history.js +61 -0
- package/dist/client/collections/table-columns.d.ts +19 -0
- package/dist/client/collections/table-columns.js +200 -0
- package/dist/client/collections.d.ts +54 -0
- package/dist/client/collections.js +262 -0
- package/dist/client/discovery.d.ts +1 -0
- package/dist/client/discovery.js +57 -0
- package/dist/client/field-address.d.ts +38 -0
- package/dist/client/field-address.js +225 -0
- package/dist/client/field-value.d.ts +7 -0
- package/dist/client/field-value.js +105 -0
- package/dist/client/form-identity.d.ts +1 -0
- package/dist/client/form-identity.js +43 -0
- package/dist/client/form-session.d.ts +53 -0
- package/dist/client/form-session.js +218 -0
- package/dist/client/identity.d.ts +3 -0
- package/dist/client/identity.js +27 -0
- package/dist/client/index.d.ts +43 -0
- package/dist/client/index.js +109 -0
- package/dist/client/journal.d.ts +13 -0
- package/dist/client/journal.js +59 -0
- package/dist/client/lifecycle.d.ts +26 -0
- package/dist/client/lifecycle.js +112 -0
- package/dist/client/observation.d.ts +11 -0
- package/dist/client/observation.js +46 -0
- package/dist/client/outbox.d.ts +15 -0
- package/dist/client/outbox.js +71 -0
- package/dist/client/plugin.d.ts +42 -0
- package/dist/client/plugin.js +1 -0
- package/dist/client/plugins/column-order/apply.d.ts +2 -0
- package/dist/client/plugins/column-order/apply.js +30 -0
- package/dist/client/plugins/column-order/colgroups.d.ts +2 -0
- package/dist/client/plugins/column-order/colgroups.js +78 -0
- package/dist/client/plugins/column-order/index.d.ts +2 -0
- package/dist/client/plugins/column-order/index.js +98 -0
- package/dist/client/plugins/cursor/editable.d.ts +16 -0
- package/dist/client/plugins/cursor/editable.js +67 -0
- package/dist/client/plugins/cursor/geometry.d.ts +16 -0
- package/dist/client/plugins/cursor/geometry.js +122 -0
- package/dist/client/plugins/cursor/index.d.ts +4 -0
- package/dist/client/plugins/cursor/index.js +13 -0
- package/dist/client/plugins/cursor/indicators.d.ts +31 -0
- package/dist/client/plugins/cursor/indicators.js +144 -0
- package/dist/client/plugins/cursor/local-selection.d.ts +19 -0
- package/dist/client/plugins/cursor/local-selection.js +234 -0
- package/dist/client/plugins/cursor/selection.d.ts +9 -0
- package/dist/client/plugins/cursor/selection.js +49 -0
- package/dist/client/plugins/cursor/text-presence.d.ts +15 -0
- package/dist/client/plugins/cursor/text-presence.js +95 -0
- package/dist/client/plugins/fields/author.d.ts +7 -0
- package/dist/client/plugins/fields/author.js +22 -0
- package/dist/client/plugins/fields/index.d.ts +4 -0
- package/dist/client/plugins/fields/index.js +192 -0
- package/dist/client/plugins/fields/writes.d.ts +19 -0
- package/dist/client/plugins/fields/writes.js +51 -0
- package/dist/client/plugins/focus/index.d.ts +9 -0
- package/dist/client/plugins/focus/index.js +92 -0
- package/dist/client/plugins/focus/indicators.d.ts +19 -0
- package/dist/client/plugins/focus/indicators.js +163 -0
- package/dist/client/plugins/naming/algorithm.d.ts +7 -0
- package/dist/client/plugins/naming/algorithm.js +65 -0
- package/dist/client/plugins/naming/attributes.d.ts +16 -0
- package/dist/client/plugins/naming/attributes.js +195 -0
- package/dist/client/plugins/naming/index.d.ts +5 -0
- package/dist/client/plugins/naming/index.js +185 -0
- package/dist/client/plugins/range/index.d.ts +4 -0
- package/dist/client/plugins/range/index.js +15 -0
- package/dist/client/plugins/reconciliation/index.d.ts +6 -0
- package/dist/client/plugins/reconciliation/index.js +282 -0
- package/dist/client/plugins/reconciliation/matching.d.ts +12 -0
- package/dist/client/plugins/reconciliation/matching.js +37 -0
- package/dist/client/plugins/reconciliation/proposals.d.ts +11 -0
- package/dist/client/plugins/reconciliation/proposals.js +32 -0
- package/dist/client/plugins/row-order/index.d.ts +2 -0
- package/dist/client/plugins/row-order/index.js +99 -0
- package/dist/client/plugins/rows/clone.d.ts +2 -0
- package/dist/client/plugins/rows/clone.js +123 -0
- package/dist/client/plugins/rows/index.d.ts +2 -0
- package/dist/client/plugins/rows/index.js +191 -0
- package/dist/client/plugins/rows/materialize.d.ts +4 -0
- package/dist/client/plugins/rows/materialize.js +43 -0
- package/dist/client/plugins/rows/remove.d.ts +1 -0
- package/dist/client/plugins/rows/remove.js +13 -0
- package/dist/client/plugins/rows/template.d.ts +2 -0
- package/dist/client/plugins/rows/template.js +21 -0
- package/dist/client/transport.d.ts +23 -0
- package/dist/client/transport.js +128 -0
- package/dist/client/user-colour.d.ts +3 -0
- package/dist/client/user-colour.js +11 -0
- package/dist/server/acknowledgments.d.ts +3 -0
- package/dist/server/acknowledgments.js +8 -0
- package/dist/server/broadcast.d.ts +3 -0
- package/dist/server/broadcast.js +7 -0
- package/dist/server/cli.d.ts +2 -0
- package/dist/server/cli.js +44 -0
- package/dist/server/clock.d.ts +2 -0
- package/dist/server/clock.js +3 -0
- package/dist/server/configuration.d.ts +3 -0
- package/dist/server/configuration.js +7 -0
- package/dist/server/connection.d.ts +13 -0
- package/dist/server/connection.js +1 -0
- package/dist/server/express.d.ts +2 -0
- package/dist/server/express.js +1 -0
- package/dist/server/fastify.d.ts +10 -0
- package/dist/server/fastify.js +15 -0
- package/dist/server/index.d.ts +7 -0
- package/dist/server/index.js +1 -0
- package/dist/server/journal.d.ts +7 -0
- package/dist/server/journal.js +16 -0
- package/dist/server/messages.d.ts +4 -0
- package/dist/server/messages.js +106 -0
- package/dist/server/participants.d.ts +9 -0
- package/dist/server/participants.js +7 -0
- package/dist/server/receipts.d.ts +13 -0
- package/dist/server/receipts.js +28 -0
- package/dist/server/recovery.d.ts +3 -0
- package/dist/server/recovery.js +9 -0
- package/dist/server/server.d.ts +34 -0
- package/dist/server/server.js +189 -0
- package/dist/server/sessions.d.ts +16 -0
- package/dist/server/sessions.js +37 -0
- package/dist/server/standalone.d.ts +9 -0
- package/dist/server/standalone.js +12 -0
- package/dist/server/transactions.d.ts +15 -0
- package/dist/server/transactions.js +50 -0
- package/dist/server/websocket-connection.d.ts +3 -0
- package/dist/server/websocket-connection.js +38 -0
- package/dist/server/websocket.d.ts +11 -0
- package/dist/server/websocket.js +26 -0
- package/dist/shared/field-update.d.ts +12 -0
- package/dist/shared/field-update.js +18 -0
- package/dist/shared/journal-entries.d.ts +9 -0
- package/dist/shared/journal-entries.js +33 -0
- package/dist/shared/protocol.d.ts +105 -0
- package/dist/shared/protocol.js +15 -0
- package/docs/dependencies.md +130 -0
- package/docs/spec-addendum-dependencies.md +10 -0
- package/docs/spec-addendum.md +717 -0
- package/docs/spec.md +1165 -0
- package/docs/testing.md +39 -0
- package/package.json +96 -5
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export function creationOf(transaction) {
|
|
2
|
+
if (transaction.type !== 'add-row')
|
|
3
|
+
return undefined;
|
|
4
|
+
const data = rowReference(transaction.data);
|
|
5
|
+
if (!data || ((data.after !== null) && (typeof data.after !== 'string')))
|
|
6
|
+
return undefined;
|
|
7
|
+
if (!data.values || (typeof data.values !== 'object') || Array.isArray(data.values))
|
|
8
|
+
return undefined;
|
|
9
|
+
return data;
|
|
10
|
+
}
|
|
11
|
+
export function removalOf(transaction) {
|
|
12
|
+
if (transaction.type !== 'remove-row')
|
|
13
|
+
return undefined;
|
|
14
|
+
const data = rowReference(transaction.data);
|
|
15
|
+
if (!data || ((data.after != null) && (typeof data.after !== 'string')))
|
|
16
|
+
return undefined;
|
|
17
|
+
return data;
|
|
18
|
+
}
|
|
19
|
+
function rowReference(data) {
|
|
20
|
+
if (!data || (typeof data !== 'object'))
|
|
21
|
+
return undefined;
|
|
22
|
+
const row = data;
|
|
23
|
+
return ((typeof row.collection === 'string') && (typeof row.row === 'string'))
|
|
24
|
+
? row
|
|
25
|
+
: undefined;
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Transaction } from '../../shared/protocol.js';
|
|
2
|
+
import type { RowCreation } from '../collections.js';
|
|
3
|
+
import type { Insertion } from './order.js';
|
|
4
|
+
interface Creation extends Insertion {
|
|
5
|
+
change: RowCreation;
|
|
6
|
+
}
|
|
7
|
+
interface RowHistory {
|
|
8
|
+
additions: Map<string, Creation>;
|
|
9
|
+
removed: Set<string>;
|
|
10
|
+
rawRemoved: Set<string>;
|
|
11
|
+
retired: Map<string, Insertion>;
|
|
12
|
+
}
|
|
13
|
+
export declare function rowHistory(history: readonly Transaction[], canonical: (row: string) => string): RowHistory;
|
|
14
|
+
export declare function rowPlacements(history: RowHistory, initial: readonly string[]): Insertion[];
|
|
15
|
+
export declare function rowTransactions(history: readonly Transaction[]): ReadonlyMap<string, readonly Transaction[]>;
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { compareTransactions } from '../../shared/protocol.js';
|
|
2
|
+
import { creationOf } from './row-changes.js';
|
|
3
|
+
import { removalOf } from './row-changes.js';
|
|
4
|
+
const byHistory = new WeakMap();
|
|
5
|
+
const rowOperations = new Set(['add-row', 'order-rows', 'remove-row']);
|
|
6
|
+
export function rowHistory(history, canonical) {
|
|
7
|
+
const additions = new Map();
|
|
8
|
+
const removed = new Set();
|
|
9
|
+
const rawRemoved = new Set();
|
|
10
|
+
const retired = new Map();
|
|
11
|
+
for (const transaction of history) {
|
|
12
|
+
const creation = creationOf(transaction);
|
|
13
|
+
if (creation) {
|
|
14
|
+
const row = canonical(creation.row);
|
|
15
|
+
const previous = additions.get(row);
|
|
16
|
+
if (!previous || (compareTransactions(transaction, previous.transaction) < 0)) {
|
|
17
|
+
const after = (creation.after === null) ? null : canonical(creation.after);
|
|
18
|
+
additions.set(row, { after, change: creation, row, transaction });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
const removal = removalOf(transaction);
|
|
22
|
+
if (removal) {
|
|
23
|
+
const row = canonical(removal.row);
|
|
24
|
+
removed.add(row);
|
|
25
|
+
rawRemoved.add(removal.row);
|
|
26
|
+
const previous = retired.get(row);
|
|
27
|
+
if ((removal.after !== undefined) && (!previous || (compareTransactions(transaction, previous.transaction) < 0))) {
|
|
28
|
+
const after = (removal.after === null) ? null : canonical(removal.after);
|
|
29
|
+
retired.set(row, { after, row, transaction });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { additions, removed, rawRemoved, retired };
|
|
34
|
+
}
|
|
35
|
+
export function rowPlacements(history, initial) {
|
|
36
|
+
const baseline = new Set(initial);
|
|
37
|
+
const retired = [...history.retired.values()].filter(({ row }) => !baseline.has(row) && !history.additions.has(row));
|
|
38
|
+
return [...history.additions.values(), ...retired];
|
|
39
|
+
}
|
|
40
|
+
export function rowTransactions(history) {
|
|
41
|
+
const cached = byHistory.get(history);
|
|
42
|
+
if (cached)
|
|
43
|
+
return cached;
|
|
44
|
+
const indexed = new Map();
|
|
45
|
+
for (const transaction of history) {
|
|
46
|
+
if (!rowOperations.has(transaction.type))
|
|
47
|
+
continue;
|
|
48
|
+
const { collection } = (transaction.data ?? {});
|
|
49
|
+
if (typeof collection !== 'string')
|
|
50
|
+
continue;
|
|
51
|
+
const operations = indexed.get(collection);
|
|
52
|
+
if (operations) {
|
|
53
|
+
operations.push(transaction);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
indexed.set(collection, [transaction]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
byHistory.set(history, indexed);
|
|
60
|
+
return indexed;
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface GridRow {
|
|
2
|
+
cells: HTMLTableCellElement[];
|
|
3
|
+
row: HTMLTableRowElement;
|
|
4
|
+
}
|
|
5
|
+
interface Group {
|
|
6
|
+
columns: HTMLTableColElement[];
|
|
7
|
+
group: HTMLTableColElement;
|
|
8
|
+
}
|
|
9
|
+
export interface TableColumns {
|
|
10
|
+
groups: Group[];
|
|
11
|
+
ids: string[];
|
|
12
|
+
rows: GridRow[];
|
|
13
|
+
slots: ReadonlyMap<Element, readonly string[]>;
|
|
14
|
+
}
|
|
15
|
+
export declare function bindRowColumns(table: HTMLTableElement, row: HTMLTableRowElement, model?: HTMLTableRowElement): void;
|
|
16
|
+
export declare function columnIdentity(element: Element): string | undefined;
|
|
17
|
+
export declare function columnsOf(table: HTMLTableElement): TableColumns | undefined;
|
|
18
|
+
export declare function rememberColumnSpan(node: HTMLTableColElement, ids: readonly string[]): void;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
const columnCells = new WeakMap();
|
|
2
|
+
const firstOrders = new WeakMap();
|
|
3
|
+
const headerSlots = new WeakMap();
|
|
4
|
+
const metadata = new WeakMap();
|
|
5
|
+
export function bindRowColumns(table, row, model) {
|
|
6
|
+
const baseline = firstOrders.get(table) ?? columnsOf(table)?.ids;
|
|
7
|
+
if (!baseline)
|
|
8
|
+
return;
|
|
9
|
+
let index = 0;
|
|
10
|
+
for (const [offset, cell] of [...row.cells].entries()) {
|
|
11
|
+
const source = model?.cells[offset];
|
|
12
|
+
const known = source ? columnCells.get(source)?.ids : undefined;
|
|
13
|
+
const ids = known ?? baseline.slice(index, index + cell.colSpan);
|
|
14
|
+
if (ids.length)
|
|
15
|
+
columnCells.set(cell, { ids, table });
|
|
16
|
+
index += cell.colSpan;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function columnIdentity(element) {
|
|
20
|
+
const cell = element.closest('td, th');
|
|
21
|
+
const table = cell?.closest('table');
|
|
22
|
+
if (!cell || !table)
|
|
23
|
+
return undefined;
|
|
24
|
+
const known = columnCells.get(cell);
|
|
25
|
+
const ids = (known?.table === table)
|
|
26
|
+
? known.ids
|
|
27
|
+
: columnsOf(table)?.slots.get(cell);
|
|
28
|
+
if (!ids?.length)
|
|
29
|
+
return undefined;
|
|
30
|
+
return (ids.length === 1)
|
|
31
|
+
? ids[0]
|
|
32
|
+
: JSON.stringify(['span', ...ids.slice().sort()]);
|
|
33
|
+
}
|
|
34
|
+
export function columnsOf(table) {
|
|
35
|
+
const grids = rowGrids(table);
|
|
36
|
+
const header = headerGrid(table, grids);
|
|
37
|
+
if (!header)
|
|
38
|
+
return undefined;
|
|
39
|
+
const ids = headerIdentities(header);
|
|
40
|
+
const active = new Set(ids);
|
|
41
|
+
const keyed = keyedHeaders(header, ids);
|
|
42
|
+
const slots = new Map();
|
|
43
|
+
const groups = [];
|
|
44
|
+
let index = 0;
|
|
45
|
+
if (!firstOrders.has(table))
|
|
46
|
+
firstOrders.set(table, ids);
|
|
47
|
+
for (const [row, cells] of grids) {
|
|
48
|
+
const positions = new Map();
|
|
49
|
+
cells.forEach((cell, index) => {
|
|
50
|
+
if (cell && (cell.parentElement === row) && ids[index]) {
|
|
51
|
+
const covered = positions.get(cell);
|
|
52
|
+
if (covered) {
|
|
53
|
+
covered.push(ids[index]);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
positions.set(cell, [ids[index]]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
for (const [cell, covered] of positions) {
|
|
61
|
+
const previous = columnCells.get(cell);
|
|
62
|
+
const existing = (previous?.table === table) ? previous.ids.filter(id => active.has(id)) : undefined;
|
|
63
|
+
const explicit = boundColumns(cell, covered.length, keyed);
|
|
64
|
+
const bound = explicit ?? ((existing?.length === covered.length) ? existing : covered);
|
|
65
|
+
columnCells.set(cell, { ids: bound, table });
|
|
66
|
+
slots.set(cell, bound);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
for (const group of table.querySelectorAll(':scope > colgroup')) {
|
|
70
|
+
const columns = [...group.querySelectorAll(':scope > col')];
|
|
71
|
+
for (const node of columns.length ? columns : [group]) {
|
|
72
|
+
const covered = ids.slice(index, index + node.span);
|
|
73
|
+
const previous = metadata.get(node);
|
|
74
|
+
const existing = previous?.filter(id => active.has(id));
|
|
75
|
+
const explicit = boundColumns(node, covered.length, keyed);
|
|
76
|
+
const bound = (existing?.length === covered.length) ? existing : explicit ?? covered;
|
|
77
|
+
metadata.set(node, bound);
|
|
78
|
+
slots.set(node, bound);
|
|
79
|
+
index += node.span;
|
|
80
|
+
}
|
|
81
|
+
groups.push({ columns, group });
|
|
82
|
+
}
|
|
83
|
+
const rows = [...grids.keys()].map(row => ({ cells: [...row.cells], row }));
|
|
84
|
+
return { ids, groups, rows, slots };
|
|
85
|
+
}
|
|
86
|
+
function boundColumns(element, width, headers) {
|
|
87
|
+
const key = columnKey(element) ?? element.getAttribute('headers');
|
|
88
|
+
const ids = key ? headers.get(key) : undefined;
|
|
89
|
+
return (ids?.length === width) ? ids : undefined;
|
|
90
|
+
}
|
|
91
|
+
function columnKey(element) {
|
|
92
|
+
return element.getAttribute('data-sync-column')
|
|
93
|
+
|| element.getAttribute('data-column-id')
|
|
94
|
+
|| element.getAttribute('data-key');
|
|
95
|
+
}
|
|
96
|
+
function headerGrid(table, grids) {
|
|
97
|
+
if (table.tHead?.rows.length) {
|
|
98
|
+
const rows = [...table.tHead.rows];
|
|
99
|
+
const size = Math.max(...rows.map(row => grids.get(row)?.length ?? 0));
|
|
100
|
+
const grid = Array.from({ length: size }, (_, index) => {
|
|
101
|
+
for (let row = rows.length - 1; row >= 0; row--) {
|
|
102
|
+
const cell = grids.get(rows[row])?.[index];
|
|
103
|
+
if (cell)
|
|
104
|
+
return cell;
|
|
105
|
+
}
|
|
106
|
+
return undefined;
|
|
107
|
+
});
|
|
108
|
+
if (grid.every(cell => !!cell))
|
|
109
|
+
return grid;
|
|
110
|
+
}
|
|
111
|
+
for (const cells of grids.values()) {
|
|
112
|
+
if ([...cells].every(cell => !!cell) && cells.some(cell => (cell.localName === 'th') && (cell.scope !== 'row'))) {
|
|
113
|
+
return cells;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
function headerIdentities(header) {
|
|
119
|
+
const used = new Set();
|
|
120
|
+
for (const cell of new Set(header)) {
|
|
121
|
+
for (const id of headerSlots.get(cell) ?? [])
|
|
122
|
+
used.add(id);
|
|
123
|
+
}
|
|
124
|
+
const offsets = new Map();
|
|
125
|
+
return header.map(cell => {
|
|
126
|
+
const offset = offsets.get(cell) ?? 0;
|
|
127
|
+
const known = headerSlots.get(cell) ?? [];
|
|
128
|
+
offsets.set(cell, offset + 1);
|
|
129
|
+
if (known[offset])
|
|
130
|
+
return known[offset];
|
|
131
|
+
const key = columnKey(cell) || cell.id || cell.abbr;
|
|
132
|
+
const label = cell.textContent?.trim().replace(/\s+/g, ' ') ?? '';
|
|
133
|
+
const basis = key ? ['key', key] : ['text', label];
|
|
134
|
+
let count = 0;
|
|
135
|
+
let id = JSON.stringify([...basis, offset, count]);
|
|
136
|
+
while (used.has(id))
|
|
137
|
+
id = JSON.stringify([...basis, offset, ++count]);
|
|
138
|
+
used.add(id);
|
|
139
|
+
known[offset] = id;
|
|
140
|
+
headerSlots.set(cell, known);
|
|
141
|
+
return id;
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
function keyedHeaders(header, ids) {
|
|
145
|
+
const keyed = new Map();
|
|
146
|
+
header.forEach((cell, index) => {
|
|
147
|
+
const key = columnKey(cell) || cell.id;
|
|
148
|
+
if (!key)
|
|
149
|
+
return;
|
|
150
|
+
const columns = keyed.get(key);
|
|
151
|
+
if (columns) {
|
|
152
|
+
columns.push(ids[index]);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
keyed.set(key, [ids[index]]);
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
return keyed;
|
|
159
|
+
}
|
|
160
|
+
export function rememberColumnSpan(node, ids) {
|
|
161
|
+
metadata.set(node, ids);
|
|
162
|
+
}
|
|
163
|
+
function rowGrids(table) {
|
|
164
|
+
const grids = new Map();
|
|
165
|
+
const active = new Map();
|
|
166
|
+
let group = null;
|
|
167
|
+
let index = 0;
|
|
168
|
+
for (const row of table.rows) {
|
|
169
|
+
if (row.parentElement !== group) {
|
|
170
|
+
active.clear();
|
|
171
|
+
group = row.parentElement;
|
|
172
|
+
index = 0;
|
|
173
|
+
}
|
|
174
|
+
const cells = [];
|
|
175
|
+
for (const [slot, item] of active) {
|
|
176
|
+
if (item.end <= index) {
|
|
177
|
+
active.delete(slot);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
cells[slot] = item.cell;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
let start = 0;
|
|
184
|
+
for (const cell of row.cells) {
|
|
185
|
+
while (cells.slice(start, start + cell.colSpan).some(occupied => !!occupied)) {
|
|
186
|
+
start++;
|
|
187
|
+
}
|
|
188
|
+
const end = (cell.rowSpan === 0) ? Infinity : index + cell.rowSpan;
|
|
189
|
+
for (let count = 0; count < cell.colSpan; count++) {
|
|
190
|
+
cells[start] = cell;
|
|
191
|
+
if (end > index + 1)
|
|
192
|
+
active.set(start, { cell, end });
|
|
193
|
+
start++;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
grids.set(row, cells);
|
|
197
|
+
index++;
|
|
198
|
+
}
|
|
199
|
+
return grids;
|
|
200
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { FieldValue } from './field-value.js';
|
|
2
|
+
export interface Collection {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly element: Element;
|
|
5
|
+
readonly initial: readonly string[];
|
|
6
|
+
readonly options: CollectionOptions;
|
|
7
|
+
affected(records: readonly MutationRecord[]): boolean;
|
|
8
|
+
contains(row: Element): boolean;
|
|
9
|
+
items(): Element[];
|
|
10
|
+
}
|
|
11
|
+
export interface CollectionOptions {
|
|
12
|
+
create?: string | ((collection: Element, row: RowCreation) => unknown);
|
|
13
|
+
items?: string;
|
|
14
|
+
key?: string;
|
|
15
|
+
naming?: (row: Element, collection: Element) => void;
|
|
16
|
+
selector: string;
|
|
17
|
+
}
|
|
18
|
+
export interface RowCreation {
|
|
19
|
+
after: string | null;
|
|
20
|
+
collection: string;
|
|
21
|
+
row: string;
|
|
22
|
+
values: Record<string, FieldValue>;
|
|
23
|
+
}
|
|
24
|
+
export declare const autoCollections: readonly Readonly<CollectionOptions>[];
|
|
25
|
+
export declare function keyAttribute(selector: string): string | undefined;
|
|
26
|
+
export declare function rowKeyInput(row: Element): HTMLInputElement | null;
|
|
27
|
+
export declare class Collections {
|
|
28
|
+
#private;
|
|
29
|
+
readonly form: HTMLFormElement;
|
|
30
|
+
readonly options: readonly CollectionOptions[];
|
|
31
|
+
readonly client: string;
|
|
32
|
+
managedNaming: boolean;
|
|
33
|
+
managedRowOrder: boolean;
|
|
34
|
+
constructor(form: HTMLFormElement, options: readonly CollectionOptions[], client: string);
|
|
35
|
+
adopt(collection: Collection, row: Element, id: string, creator: string): void;
|
|
36
|
+
businessKey(collection: Collection, row: Element): string | undefined;
|
|
37
|
+
canonical(collection: string, id: string): string;
|
|
38
|
+
creator(collection: string, id: string): string | undefined;
|
|
39
|
+
findRow(collection: string, id: string): Element | undefined;
|
|
40
|
+
get(collection: string): Collection | undefined;
|
|
41
|
+
identify(collection: Collection, row: Element): string;
|
|
42
|
+
locate(control: Element): {
|
|
43
|
+
collection: string;
|
|
44
|
+
row: string;
|
|
45
|
+
raw: string;
|
|
46
|
+
element: Element;
|
|
47
|
+
} | undefined;
|
|
48
|
+
rawIdentity(collection: Collection, row: Element): string;
|
|
49
|
+
rebind(collection: string, aliases: ReadonlyMap<string, string>, historical: ReadonlySet<string>): boolean;
|
|
50
|
+
refresh(): void;
|
|
51
|
+
get revision(): number;
|
|
52
|
+
states(): readonly Collection[];
|
|
53
|
+
structureChanged(): void;
|
|
54
|
+
}
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { domPath } from './identity.js';
|
|
2
|
+
import { newId } from './identity.js';
|
|
3
|
+
const automaticOption = Object.freeze({ selector: 'tbody, ol, ul' });
|
|
4
|
+
const itemTags = new Map([['ol', 'li'], ['tbody', 'tr'], ['tfoot', 'tr'], ['thead', 'tr'], ['ul', 'li']]);
|
|
5
|
+
const rowKeyNames = ':is([name$=".id"], [name$="[id]"])';
|
|
6
|
+
export const autoCollections = Object.freeze([automaticOption]);
|
|
7
|
+
function aliasesEqual(first, second) {
|
|
8
|
+
if (first.size !== second.size)
|
|
9
|
+
return false;
|
|
10
|
+
for (const [from, to] of first) {
|
|
11
|
+
if (second.get(from) !== to)
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
function automaticCollection(element) {
|
|
17
|
+
return (element.localName === 'tbody')
|
|
18
|
+
|| !!element.querySelector('input, select, textarea, [contenteditable]')
|
|
19
|
+
|| (element.nextElementSibling?.localName === 'template')
|
|
20
|
+
|| (element.previousElementSibling?.localName === 'template');
|
|
21
|
+
}
|
|
22
|
+
function collectionPath(element, form, identifiers) {
|
|
23
|
+
for (let root = element; root && (root !== form); root = root.parentElement) {
|
|
24
|
+
if (root.id && (identifiers.get(root.id) === 1)) {
|
|
25
|
+
return JSON.stringify([root.id, domPath(element, root)]);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return domPath(element, form);
|
|
29
|
+
}
|
|
30
|
+
export function keyAttribute(selector) {
|
|
31
|
+
return selector.match(/\[(data-[\w-]+|id)(?=[\s~=|^$*\]])/)?.[1];
|
|
32
|
+
}
|
|
33
|
+
function isDirectItem(element, tag) {
|
|
34
|
+
return (tag === '*') || (element.localName === tag);
|
|
35
|
+
}
|
|
36
|
+
function keyValue(element, selector) {
|
|
37
|
+
const attribute = keyAttribute(selector);
|
|
38
|
+
const value = (attribute ? element.getAttribute(attribute) : undefined)
|
|
39
|
+
?? element.value
|
|
40
|
+
?? element.getAttribute('data-key')
|
|
41
|
+
?? element.getAttribute('data-id')
|
|
42
|
+
?? element.textContent;
|
|
43
|
+
return value?.trim() || undefined;
|
|
44
|
+
}
|
|
45
|
+
export function rowKeyInput(row) {
|
|
46
|
+
return row.querySelector('input[type=hidden]' + rowKeyNames)
|
|
47
|
+
?? row.querySelector('input' + rowKeyNames);
|
|
48
|
+
}
|
|
49
|
+
export class Collections {
|
|
50
|
+
form;
|
|
51
|
+
options;
|
|
52
|
+
client;
|
|
53
|
+
managedNaming = false;
|
|
54
|
+
managedRowOrder = false;
|
|
55
|
+
#byElement = new Map();
|
|
56
|
+
#byId = new Map();
|
|
57
|
+
#rows = new WeakMap();
|
|
58
|
+
#revision = 0;
|
|
59
|
+
constructor(form, options, client) {
|
|
60
|
+
this.form = form;
|
|
61
|
+
this.options = options;
|
|
62
|
+
this.client = client;
|
|
63
|
+
this.#refresh(true);
|
|
64
|
+
}
|
|
65
|
+
adopt(collection, row, id, creator) {
|
|
66
|
+
const state = this.#byId.get(collection.id);
|
|
67
|
+
const canonical = this.canonical(collection.id, id);
|
|
68
|
+
this.#rows.set(row, { collection: collection.id, id });
|
|
69
|
+
if (state && !state.creators.has(id))
|
|
70
|
+
state.creators.set(id, creator);
|
|
71
|
+
state?.rows.set(canonical, new WeakRef(row));
|
|
72
|
+
}
|
|
73
|
+
businessKey(collection, row) {
|
|
74
|
+
const { key } = collection.options;
|
|
75
|
+
if (key) {
|
|
76
|
+
const target = row.matches(key) ? row : row.querySelector(key);
|
|
77
|
+
return target ? keyValue(target, key) : undefined;
|
|
78
|
+
}
|
|
79
|
+
const attribute = row.getAttribute('data-key') ?? row.getAttribute('data-id');
|
|
80
|
+
if (attribute?.trim()) {
|
|
81
|
+
return attribute.trim();
|
|
82
|
+
}
|
|
83
|
+
return rowKeyInput(row)?.value.trim() || undefined;
|
|
84
|
+
}
|
|
85
|
+
canonical(collection, id) {
|
|
86
|
+
return this.#byId.get(collection)?.aliases.get(id) ?? id;
|
|
87
|
+
}
|
|
88
|
+
creator(collection, id) {
|
|
89
|
+
const state = this.#byId.get(collection);
|
|
90
|
+
if (!state)
|
|
91
|
+
return undefined;
|
|
92
|
+
const owner = state.creators.get(id);
|
|
93
|
+
if (owner)
|
|
94
|
+
return owner;
|
|
95
|
+
const row = state.rows.get(this.canonical(collection, id))?.deref();
|
|
96
|
+
return row ? state.creators.get(this.rawIdentity(state, row)) : undefined;
|
|
97
|
+
}
|
|
98
|
+
findRow(collection, id) {
|
|
99
|
+
const state = this.#byId.get(collection);
|
|
100
|
+
if (!state)
|
|
101
|
+
return undefined;
|
|
102
|
+
id = this.canonical(collection, id);
|
|
103
|
+
const known = state.rows.get(id)?.deref();
|
|
104
|
+
if (known && state.contains(known))
|
|
105
|
+
return known;
|
|
106
|
+
const match = state.items().find(row => this.identify(state, row) === id);
|
|
107
|
+
if (match)
|
|
108
|
+
state.rows.set(id, new WeakRef(match));
|
|
109
|
+
return match;
|
|
110
|
+
}
|
|
111
|
+
get(collection) {
|
|
112
|
+
return this.#byId.get(collection);
|
|
113
|
+
}
|
|
114
|
+
identify(collection, row) {
|
|
115
|
+
return this.canonical(collection.id, this.rawIdentity(collection, row));
|
|
116
|
+
}
|
|
117
|
+
locate(control) {
|
|
118
|
+
let child;
|
|
119
|
+
for (let node = control; node && (node !== this.form); node = node.parentElement) {
|
|
120
|
+
const state = this.#byElement.get(node);
|
|
121
|
+
if (state) {
|
|
122
|
+
const element = state.direct
|
|
123
|
+
? (child && isDirectItem(child, state.direct) ? child : undefined)
|
|
124
|
+
: state.items().find(row => row.contains(control));
|
|
125
|
+
if (element) {
|
|
126
|
+
const raw = this.rawIdentity(state, element);
|
|
127
|
+
return { collection: state.id, row: this.canonical(state.id, raw), raw, element };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
child = node;
|
|
131
|
+
}
|
|
132
|
+
return undefined;
|
|
133
|
+
}
|
|
134
|
+
rawIdentity(collection, row) {
|
|
135
|
+
const identity = this.#rows.get(row);
|
|
136
|
+
if (identity?.collection === collection.id)
|
|
137
|
+
return identity.id;
|
|
138
|
+
const key = this.businessKey(collection, row);
|
|
139
|
+
let id = key ? `key:${key}` : newId();
|
|
140
|
+
const owner = this.#byId.get(collection.id)?.rows.get(id)?.deref();
|
|
141
|
+
if (owner && (owner !== row) && collection.contains(owner) && (this.#rows.get(owner)?.id === id)) {
|
|
142
|
+
id = newId();
|
|
143
|
+
}
|
|
144
|
+
this.adopt(collection, row, id, this.client);
|
|
145
|
+
return id;
|
|
146
|
+
}
|
|
147
|
+
rebind(collection, aliases, historical) {
|
|
148
|
+
const state = this.#byId.get(collection);
|
|
149
|
+
if (!state)
|
|
150
|
+
return false;
|
|
151
|
+
if (aliasesEqual(state.aliases, aliases))
|
|
152
|
+
return true;
|
|
153
|
+
for (const target of aliases.values()) {
|
|
154
|
+
if (aliases.has(target))
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
const baseline = new Set(state.initial);
|
|
158
|
+
const mounted = new Map();
|
|
159
|
+
const redundant = [];
|
|
160
|
+
for (const element of state.items()) {
|
|
161
|
+
const raw = this.rawIdentity(state, element);
|
|
162
|
+
const preferred = aliases.get(raw) ?? raw;
|
|
163
|
+
const current = mounted.get(preferred);
|
|
164
|
+
if (current) {
|
|
165
|
+
const temporary = historical.has(raw) && !baseline.has(raw);
|
|
166
|
+
const currentTemporary = historical.has(current.raw) && !baseline.has(current.raw);
|
|
167
|
+
if (!temporary && !currentTemporary)
|
|
168
|
+
return false;
|
|
169
|
+
const currentWins = !currentTemporary || (temporary && (current.raw < raw));
|
|
170
|
+
if (currentWins) {
|
|
171
|
+
redundant.push(element);
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
redundant.push(current.element);
|
|
175
|
+
}
|
|
176
|
+
mounted.set(preferred, { element, raw });
|
|
177
|
+
}
|
|
178
|
+
state.aliases = new Map(aliases);
|
|
179
|
+
state.rows = new Map([...mounted].map(([id, { element }]) => [id, new WeakRef(element)]));
|
|
180
|
+
this.#revision++;
|
|
181
|
+
for (const element of redundant)
|
|
182
|
+
element.remove();
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
refresh() {
|
|
186
|
+
this.#refresh(false);
|
|
187
|
+
}
|
|
188
|
+
get revision() {
|
|
189
|
+
return this.#revision;
|
|
190
|
+
}
|
|
191
|
+
states() {
|
|
192
|
+
return [...this.#byId.values()];
|
|
193
|
+
}
|
|
194
|
+
structureChanged() {
|
|
195
|
+
this.#revision++;
|
|
196
|
+
}
|
|
197
|
+
#refresh(initial) {
|
|
198
|
+
const elements = new Map();
|
|
199
|
+
const options = this.options.length ? this.options : autoCollections;
|
|
200
|
+
for (const option of options) {
|
|
201
|
+
for (const element of this.form.querySelectorAll(option.selector)) {
|
|
202
|
+
if (((option !== automaticOption) || this.#byElement.has(element) || automaticCollection(element))
|
|
203
|
+
&& !elements.has(element)) {
|
|
204
|
+
elements.set(element, option);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
for (const [element, state] of this.#byElement) {
|
|
209
|
+
if (!elements.has(element)) {
|
|
210
|
+
this.#byElement.delete(element);
|
|
211
|
+
this.#byId.delete(state.id);
|
|
212
|
+
this.#revision++;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const identifiers = new Map();
|
|
216
|
+
if (elements.size !== this.#byElement.size) {
|
|
217
|
+
for (const { id } of this.form.querySelectorAll('[id]')) {
|
|
218
|
+
identifiers.set(id, (identifiers.get(id) ?? 0) + 1);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
for (const [element, option] of elements) {
|
|
222
|
+
if (this.#byElement.has(element))
|
|
223
|
+
continue;
|
|
224
|
+
const selector = option.items ?? (':scope > ' + (itemTags.get(element.localName) ?? '*'));
|
|
225
|
+
const direct = selector.match(/^:scope\s*>\s*([a-z][a-z0-9-]*|\*)$/)?.[1];
|
|
226
|
+
const items = direct
|
|
227
|
+
? () => [...element.children].filter(child => isDirectItem(child, direct))
|
|
228
|
+
: () => [...element.querySelectorAll(selector)];
|
|
229
|
+
const base = collectionPath(element, this.form, identifiers);
|
|
230
|
+
let id = base;
|
|
231
|
+
for (let suffix = 1; this.#byId.has(id); suffix++) {
|
|
232
|
+
id = base + '#' + suffix;
|
|
233
|
+
}
|
|
234
|
+
const baseline = [];
|
|
235
|
+
const state = {
|
|
236
|
+
direct, id, element, items, initial: baseline, options: option,
|
|
237
|
+
aliases: new Map(), creators: new Map(), rows: new Map(),
|
|
238
|
+
affected: records => records.some(record => {
|
|
239
|
+
if (direct)
|
|
240
|
+
return (record.type === 'childList') && (record.target === element);
|
|
241
|
+
return element.contains(record.target);
|
|
242
|
+
}),
|
|
243
|
+
contains: row => direct
|
|
244
|
+
? (row.parentElement === element) && isDirectItem(row, direct)
|
|
245
|
+
: items().includes(row)
|
|
246
|
+
};
|
|
247
|
+
if (initial) {
|
|
248
|
+
items().forEach((row, index) => {
|
|
249
|
+
const key = this.businessKey(state, row);
|
|
250
|
+
const keyed = key ? `key:${key}` : undefined;
|
|
251
|
+
const rowId = (keyed && !state.rows.has(keyed)) ? keyed : `initial:${index}`;
|
|
252
|
+
this.#rows.set(row, { collection: id, id: rowId });
|
|
253
|
+
state.rows.set(rowId, new WeakRef(row));
|
|
254
|
+
baseline.push(rowId);
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
this.#byElement.set(element, state);
|
|
258
|
+
this.#byId.set(id, state);
|
|
259
|
+
this.#revision++;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function discoverForms(selector: string, attach: (form: HTMLFormElement, key: string) => void, detach: (key: string) => void, session?: (form: HTMLFormElement) => string): () => void;
|