@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,15 @@
|
|
|
1
|
+
import type { Transaction } from '../shared/protocol.js';
|
|
2
|
+
export declare class Outbox {
|
|
3
|
+
private delay;
|
|
4
|
+
private deferred;
|
|
5
|
+
private outstanding;
|
|
6
|
+
constructor(delay: number);
|
|
7
|
+
acknowledge(ids: readonly string[]): void;
|
|
8
|
+
get(id: string): Transaction | undefined;
|
|
9
|
+
get empty(): boolean;
|
|
10
|
+
ids(): string[];
|
|
11
|
+
plan(transaction: Transaction, now: number, key?: string): Transaction | undefined;
|
|
12
|
+
pump(now: number, send: (transactions: Transaction[]) => boolean, prepare?: (tx: Transaction) => void): number;
|
|
13
|
+
resume(now: number): void;
|
|
14
|
+
transactions(): Transaction[];
|
|
15
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const RETRY = 3000;
|
|
2
|
+
export class Outbox {
|
|
3
|
+
delay;
|
|
4
|
+
deferred = new Map();
|
|
5
|
+
outstanding = new Map();
|
|
6
|
+
constructor(delay) {
|
|
7
|
+
this.delay = delay;
|
|
8
|
+
}
|
|
9
|
+
acknowledge(ids) {
|
|
10
|
+
for (const id of ids) {
|
|
11
|
+
this.outstanding.delete(id);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
get(id) {
|
|
15
|
+
return this.outstanding.get(id)?.tx;
|
|
16
|
+
}
|
|
17
|
+
get empty() {
|
|
18
|
+
return !this.deferred.size && !this.outstanding.size;
|
|
19
|
+
}
|
|
20
|
+
ids() {
|
|
21
|
+
return [...this.outstanding.keys()];
|
|
22
|
+
}
|
|
23
|
+
plan(transaction, now, key) {
|
|
24
|
+
if (key !== undefined) {
|
|
25
|
+
const previous = this.deferred.get(key);
|
|
26
|
+
this.deferred.set(key, { due: now + this.delay, tx: transaction });
|
|
27
|
+
return previous?.tx;
|
|
28
|
+
}
|
|
29
|
+
this.outstanding.set(transaction.id, { retry: now, tx: transaction });
|
|
30
|
+
}
|
|
31
|
+
pump(now, send, prepare) {
|
|
32
|
+
const active = new Set([...this.outstanding.values()].map(entry => entry.key));
|
|
33
|
+
for (const [key, waiting] of this.deferred) {
|
|
34
|
+
if (waiting.due > now)
|
|
35
|
+
continue;
|
|
36
|
+
if (active.has(key)) {
|
|
37
|
+
waiting.due = now + Math.max(this.delay, 10);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
prepare?.(waiting.tx);
|
|
41
|
+
this.outstanding.set(waiting.tx.id, { key, retry: now, tx: waiting.tx });
|
|
42
|
+
this.deferred.delete(key);
|
|
43
|
+
}
|
|
44
|
+
const ready = [...this.outstanding.values()].filter(entry => entry.retry <= now);
|
|
45
|
+
for (let index = 0; index < ready.length; index += 100) {
|
|
46
|
+
const chunk = ready.slice(index, index + 100);
|
|
47
|
+
if (!send(chunk.map(entry => entry.tx)))
|
|
48
|
+
break;
|
|
49
|
+
for (const entry of chunk) {
|
|
50
|
+
entry.retry = now + RETRY;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
let next = Infinity;
|
|
54
|
+
for (const waiting of this.deferred.values()) {
|
|
55
|
+
next = Math.min(next, waiting.due);
|
|
56
|
+
}
|
|
57
|
+
for (const entry of this.outstanding.values()) {
|
|
58
|
+
next = Math.min(next, entry.retry);
|
|
59
|
+
}
|
|
60
|
+
return next;
|
|
61
|
+
}
|
|
62
|
+
resume(now) {
|
|
63
|
+
for (const entry of this.outstanding.values()) {
|
|
64
|
+
entry.retry = now;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
transactions() {
|
|
68
|
+
return [...this.outstanding.values()].map(entry => entry.tx)
|
|
69
|
+
.concat([...this.deferred.values()].map(waiting => waiting.tx));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CursorPosition } from '../shared/protocol.js';
|
|
2
|
+
import type { Participant } from '../shared/protocol.js';
|
|
3
|
+
import type { RangeSelection } from '../shared/protocol.js';
|
|
4
|
+
import type { Transaction } from '../shared/protocol.js';
|
|
5
|
+
import type { User } from '../shared/protocol.js';
|
|
6
|
+
import type { Collections } from './collections.js';
|
|
7
|
+
import type { FieldAddresses } from './field-address.js';
|
|
8
|
+
import type { FieldValue } from './field-value.js';
|
|
9
|
+
export interface Lifecycle {
|
|
10
|
+
cursor?(client: string, position: CursorPosition | null): void;
|
|
11
|
+
destroy?(): void;
|
|
12
|
+
flush?(source: 'local' | 'remote'): void;
|
|
13
|
+
observe?(records: MutationRecord[]): void;
|
|
14
|
+
prepare?(transaction: Transaction): void;
|
|
15
|
+
presence?(participants: readonly Participant[], self: string | undefined): void;
|
|
16
|
+
range?(client: string, selection: RangeSelection | null): void;
|
|
17
|
+
receive?(transaction: Transaction): void;
|
|
18
|
+
}
|
|
19
|
+
export interface Plugin {
|
|
20
|
+
name: string;
|
|
21
|
+
init(context: PluginContext): Lifecycle;
|
|
22
|
+
}
|
|
23
|
+
export interface PluginContext {
|
|
24
|
+
addresses: FieldAddresses;
|
|
25
|
+
client: string;
|
|
26
|
+
collections: Collections;
|
|
27
|
+
display?: {
|
|
28
|
+
caret: boolean;
|
|
29
|
+
};
|
|
30
|
+
form: HTMLFormElement;
|
|
31
|
+
changed(): void;
|
|
32
|
+
cursor(position: CursorPosition | null): void;
|
|
33
|
+
focus(field: string | null): void;
|
|
34
|
+
history(): readonly Transaction[];
|
|
35
|
+
mute(change: () => void): void;
|
|
36
|
+
muted(): boolean;
|
|
37
|
+
publish(type: string, data: unknown, key?: string): Transaction;
|
|
38
|
+
range(selection: RangeSelection | null): void;
|
|
39
|
+
replaying(): boolean;
|
|
40
|
+
snapshot(row: Element): Record<string, FieldValue>;
|
|
41
|
+
user(): User | undefined;
|
|
42
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { mergeOrder } from '../../collections/order.js';
|
|
2
|
+
import { reorderNodes } from '../../collections/reorder-nodes.js';
|
|
3
|
+
import { applyColgroups } from './colgroups.js';
|
|
4
|
+
export function applyColumns(table, model, requested) {
|
|
5
|
+
const { ids, rows, slots } = model;
|
|
6
|
+
const order = mergeOrder(ids, requested);
|
|
7
|
+
const rank = new Map(order.map((id, index) => [id, index]));
|
|
8
|
+
const moves = [];
|
|
9
|
+
const positions = (node) => (slots.get(node) ?? [])
|
|
10
|
+
.map(id => rank.get(id))
|
|
11
|
+
.sort((left, right) => left - right);
|
|
12
|
+
const plan = (parent, elements) => {
|
|
13
|
+
const ranked = elements
|
|
14
|
+
.map(element => ({ element, indexes: positions(element) }))
|
|
15
|
+
.filter(entry => entry.indexes.length);
|
|
16
|
+
if (ranked.some(({ indexes }) => indexes.at(-1) - indexes[0] + 1 !== indexes.length))
|
|
17
|
+
return false;
|
|
18
|
+
ranked.sort((left, right) => left.indexes[0] - right.indexes[0]);
|
|
19
|
+
moves.push({ elements: ranked.map(entry => entry.element), parent });
|
|
20
|
+
return true;
|
|
21
|
+
};
|
|
22
|
+
for (const row of rows) {
|
|
23
|
+
if (!plan(row.row, row.cells))
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
for (const { elements, parent } of moves)
|
|
27
|
+
reorderNodes(parent, elements);
|
|
28
|
+
applyColgroups(table, model, rank);
|
|
29
|
+
return order;
|
|
30
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { reorderNodes } from '../../collections/reorder-nodes.js';
|
|
2
|
+
import { rememberColumnSpan } from '../../collections/table-columns.js';
|
|
3
|
+
export function applyColgroups(table, model, order) {
|
|
4
|
+
if (!model.groups.length)
|
|
5
|
+
return;
|
|
6
|
+
const slots = columnSlots(model, order);
|
|
7
|
+
const groups = groupSlots(slots);
|
|
8
|
+
const seen = new Set();
|
|
9
|
+
const anchor = model.groups.at(-1).group.nextSibling;
|
|
10
|
+
const nodes = groups.map(({ source, spans }) => {
|
|
11
|
+
const group = useNode(source, seen);
|
|
12
|
+
if (group !== source)
|
|
13
|
+
table.insertBefore(group, anchor);
|
|
14
|
+
if (spans[0]?.source === source) {
|
|
15
|
+
setSpan(group, spans[0]);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
const columns = spans.map(span => {
|
|
19
|
+
const col = useNode(span.source, seen);
|
|
20
|
+
setSpan(col, span);
|
|
21
|
+
if (col.parentElement !== group)
|
|
22
|
+
group.append(col);
|
|
23
|
+
return col;
|
|
24
|
+
});
|
|
25
|
+
reorderNodes(group, columns);
|
|
26
|
+
}
|
|
27
|
+
return group;
|
|
28
|
+
});
|
|
29
|
+
reorderNodes(table, nodes);
|
|
30
|
+
}
|
|
31
|
+
function columnSlots(model, order) {
|
|
32
|
+
const slots = [];
|
|
33
|
+
for (const { columns, group } of model.groups) {
|
|
34
|
+
for (const source of columns.length ? columns : [group]) {
|
|
35
|
+
const ids = model.slots.get(source) ?? [];
|
|
36
|
+
for (let index = 0; index < source.span; index++) {
|
|
37
|
+
const id = ids[index];
|
|
38
|
+
const position = (id ? order.get(id) : undefined) ?? order.size + slots.length;
|
|
39
|
+
slots.push({ group, id, position, source });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return slots.sort((left, right) => left.position - right.position);
|
|
44
|
+
}
|
|
45
|
+
function groupSlots(slots) {
|
|
46
|
+
const groups = [];
|
|
47
|
+
for (const { group, id, source } of slots) {
|
|
48
|
+
let current = groups.at(-1);
|
|
49
|
+
if (current?.source !== group) {
|
|
50
|
+
current = { source: group, spans: [] };
|
|
51
|
+
groups.push(current);
|
|
52
|
+
}
|
|
53
|
+
let span = current.spans.at(-1);
|
|
54
|
+
if (span?.source !== source) {
|
|
55
|
+
span = { ids: [], size: 0, source };
|
|
56
|
+
current.spans.push(span);
|
|
57
|
+
}
|
|
58
|
+
span.size++;
|
|
59
|
+
if (id)
|
|
60
|
+
span.ids.push(id);
|
|
61
|
+
}
|
|
62
|
+
return groups;
|
|
63
|
+
}
|
|
64
|
+
function setSpan(node, span) {
|
|
65
|
+
if (node.span !== span.size)
|
|
66
|
+
node.span = span.size;
|
|
67
|
+
rememberColumnSpan(node, span.ids);
|
|
68
|
+
}
|
|
69
|
+
function useNode(node, seen) {
|
|
70
|
+
if (!seen.has(node)) {
|
|
71
|
+
seen.add(node);
|
|
72
|
+
return node;
|
|
73
|
+
}
|
|
74
|
+
// A group or span can become disjoint. Keep its styling on each resulting part.
|
|
75
|
+
const copy = node.cloneNode(false);
|
|
76
|
+
copy.removeAttribute('id');
|
|
77
|
+
return copy;
|
|
78
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { compareTransactions } from '../../../shared/protocol.js';
|
|
2
|
+
import { orderChanged } from '../../collections/order.js';
|
|
3
|
+
import { applyColumns } from './apply.js';
|
|
4
|
+
import { columnsOf } from '../../collections/table-columns.js';
|
|
5
|
+
function orderOf(transaction) {
|
|
6
|
+
if ((transaction.type !== 'order-columns') || !transaction.data || (typeof transaction.data !== 'object')) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const data = transaction.data;
|
|
10
|
+
if ((typeof data.collection === 'string') && Array.isArray(data.columns)
|
|
11
|
+
&& data.columns.every(id => typeof id === 'string')) {
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
return undefined;
|
|
15
|
+
}
|
|
16
|
+
function tables(context) {
|
|
17
|
+
const tables = new Map();
|
|
18
|
+
for (const collection of context.collections.states()) {
|
|
19
|
+
const { element, id } = collection;
|
|
20
|
+
if (!element.matches('table, thead, tbody, tfoot'))
|
|
21
|
+
continue;
|
|
22
|
+
const table = element.closest('table');
|
|
23
|
+
if (table && !tables.has(table))
|
|
24
|
+
tables.set(table, id);
|
|
25
|
+
}
|
|
26
|
+
return tables;
|
|
27
|
+
}
|
|
28
|
+
export function columnOrder() {
|
|
29
|
+
return {
|
|
30
|
+
name: 'column-order',
|
|
31
|
+
init(context) {
|
|
32
|
+
const { collections } = context;
|
|
33
|
+
const observed = new WeakMap();
|
|
34
|
+
const orders = new Map();
|
|
35
|
+
let dirty = false;
|
|
36
|
+
let revision = collections.revision;
|
|
37
|
+
const receive = (transaction) => {
|
|
38
|
+
const order = orderOf(transaction);
|
|
39
|
+
if (order) {
|
|
40
|
+
const previous = orders.get(order.collection);
|
|
41
|
+
if (!previous || (compareTransactions(previous.transaction, transaction) < 0)) {
|
|
42
|
+
orders.set(order.collection, { order, transaction });
|
|
43
|
+
dirty = true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
dirty ||= (transaction.type === 'add-row') || (transaction.type === 'remove-row');
|
|
47
|
+
};
|
|
48
|
+
for (const table of tables(context).keys()) {
|
|
49
|
+
const model = columnsOf(table);
|
|
50
|
+
if (model)
|
|
51
|
+
observed.set(table, model.ids);
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
observe(records) {
|
|
55
|
+
const affected = new Set();
|
|
56
|
+
for (const record of records) {
|
|
57
|
+
const node = record.target;
|
|
58
|
+
if ((record.type === 'childList') && (node instanceof Element)
|
|
59
|
+
&& node.matches('table, thead, tbody, tfoot, tr, colgroup')) {
|
|
60
|
+
const table = node.closest('table');
|
|
61
|
+
if (table)
|
|
62
|
+
affected.add(table);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (!affected.size)
|
|
66
|
+
return;
|
|
67
|
+
dirty = true;
|
|
68
|
+
for (const [table, collection] of tables(context)) {
|
|
69
|
+
if (!affected.has(table))
|
|
70
|
+
continue;
|
|
71
|
+
const model = columnsOf(table);
|
|
72
|
+
const before = observed.get(table);
|
|
73
|
+
if (model && before && orderChanged(before, model.ids)) {
|
|
74
|
+
receive(context.publish('order-columns', { collection, columns: model.ids }));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
flush() {
|
|
79
|
+
if (!dirty && (revision === collections.revision))
|
|
80
|
+
return;
|
|
81
|
+
dirty = false;
|
|
82
|
+
revision = collections.revision;
|
|
83
|
+
context.mute(() => {
|
|
84
|
+
for (const [table, id] of tables(context)) {
|
|
85
|
+
const model = columnsOf(table);
|
|
86
|
+
if (!model)
|
|
87
|
+
continue;
|
|
88
|
+
const requested = orders.get(id)?.order.columns;
|
|
89
|
+
const order = requested ? applyColumns(table, model, requested) : undefined;
|
|
90
|
+
observed.set(table, order ?? model.ids);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
},
|
|
94
|
+
receive
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
type Point = [Node, number];
|
|
2
|
+
export declare class EditableText {
|
|
3
|
+
private readonly offsets;
|
|
4
|
+
private readonly lines;
|
|
5
|
+
private readonly text;
|
|
6
|
+
private length;
|
|
7
|
+
private lineStart;
|
|
8
|
+
private end;
|
|
9
|
+
constructor(root: HTMLElement);
|
|
10
|
+
offset(node: Node, offset: number): number | undefined;
|
|
11
|
+
point(offset: number): Point;
|
|
12
|
+
startsEmptyLine(offset: number): boolean;
|
|
13
|
+
private visit;
|
|
14
|
+
private newline;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const blocks = 'address, article, blockquote, div, h1, h2, h3, h4, h5, h6, li, p, pre, section';
|
|
2
|
+
export class EditableText {
|
|
3
|
+
offsets = new Map();
|
|
4
|
+
lines = new Map();
|
|
5
|
+
text = [];
|
|
6
|
+
length = 0;
|
|
7
|
+
lineStart = true;
|
|
8
|
+
end;
|
|
9
|
+
constructor(root) {
|
|
10
|
+
this.end = [root, 0];
|
|
11
|
+
this.lines.set(0, this.end);
|
|
12
|
+
this.visit(root);
|
|
13
|
+
}
|
|
14
|
+
offset(node, offset) {
|
|
15
|
+
const offsets = this.offsets.get(node);
|
|
16
|
+
if (!offsets)
|
|
17
|
+
return;
|
|
18
|
+
return (node instanceof Text) ? offsets[0] + offset : offsets[offset];
|
|
19
|
+
}
|
|
20
|
+
point(offset) {
|
|
21
|
+
for (const part of this.text) {
|
|
22
|
+
if (offset <= part.end) {
|
|
23
|
+
return (offset < part.start)
|
|
24
|
+
? this.lines.get(offset) ?? [part.node, 0]
|
|
25
|
+
: [part.node, offset - part.start];
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return this.lines.get(offset) ?? this.end;
|
|
29
|
+
}
|
|
30
|
+
startsEmptyLine(offset) {
|
|
31
|
+
return (offset > 0) && this.lines.has(offset) && !(this.point(offset)[0] instanceof Text);
|
|
32
|
+
}
|
|
33
|
+
visit(parent) {
|
|
34
|
+
const offsets = [this.length];
|
|
35
|
+
let previousBlock = false;
|
|
36
|
+
for (const [index, child] of Array.from(parent.childNodes).entries()) {
|
|
37
|
+
const block = (child instanceof HTMLElement) && child.matches(blocks);
|
|
38
|
+
const br = (child instanceof HTMLElement) && (child.localName === 'br');
|
|
39
|
+
if ((block || previousBlock) && !this.lineStart && !br) {
|
|
40
|
+
this.newline(parent, index);
|
|
41
|
+
}
|
|
42
|
+
if ((child instanceof Text) && child.length) {
|
|
43
|
+
const start = this.length;
|
|
44
|
+
this.offsets.set(child, [start]);
|
|
45
|
+
this.length += child.length;
|
|
46
|
+
this.text.push({ node: child, start, end: this.length });
|
|
47
|
+
this.end = [child, child.length];
|
|
48
|
+
this.lineStart = child.data.endsWith('\n');
|
|
49
|
+
}
|
|
50
|
+
else if (br) {
|
|
51
|
+
this.newline(parent, index + 1);
|
|
52
|
+
}
|
|
53
|
+
else if (child instanceof HTMLElement) {
|
|
54
|
+
this.visit(child);
|
|
55
|
+
}
|
|
56
|
+
offsets.push(this.length);
|
|
57
|
+
previousBlock = block;
|
|
58
|
+
}
|
|
59
|
+
this.offsets.set(parent, offsets);
|
|
60
|
+
}
|
|
61
|
+
newline(parent, offset) {
|
|
62
|
+
this.length++;
|
|
63
|
+
this.lineStart = true;
|
|
64
|
+
this.end = [parent, offset];
|
|
65
|
+
this.lines.set(this.length, this.end);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Offsets } from './selection.js';
|
|
2
|
+
export interface Geometry {
|
|
3
|
+
caret: DOMRect;
|
|
4
|
+
selection: DOMRect[];
|
|
5
|
+
}
|
|
6
|
+
export declare function clipOf(control: HTMLElement, window: Window): DOMRect;
|
|
7
|
+
export declare function intersect(first: DOMRect, second: DOMRect): DOMRect;
|
|
8
|
+
export declare function editableGeometry(control: HTMLElement, offsets: Offsets): Geometry;
|
|
9
|
+
export declare class TextControlMirror {
|
|
10
|
+
private document;
|
|
11
|
+
private readonly mirror;
|
|
12
|
+
private readonly text;
|
|
13
|
+
constructor(document: Document);
|
|
14
|
+
destroy(): void;
|
|
15
|
+
measure(control: HTMLInputElement | HTMLTextAreaElement, offsets: Offsets): Geometry;
|
|
16
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { EditableText } from './editable.js';
|
|
2
|
+
import { rangesOf } from './selection.js';
|
|
3
|
+
export function clipOf(control, window) {
|
|
4
|
+
let clip = new DOMRect(0, 0, window.innerWidth, window.innerHeight);
|
|
5
|
+
for (let element = control; element && (element !== control.ownerDocument.body); element = element.parentElement) {
|
|
6
|
+
const style = window.getComputedStyle(element);
|
|
7
|
+
const textField = (element instanceof HTMLInputElement) || (element instanceof HTMLTextAreaElement);
|
|
8
|
+
const clipX = textField || (style.overflowX !== 'visible');
|
|
9
|
+
const clipY = textField || (style.overflowY !== 'visible');
|
|
10
|
+
if (!clipX && !clipY)
|
|
11
|
+
continue;
|
|
12
|
+
const rect = element.getBoundingClientRect();
|
|
13
|
+
clip = intersect(clip, new DOMRect(clipX ? rect.left + element.clientLeft : clip.left, clipY ? rect.top + element.clientTop : clip.top, clipX ? element.clientWidth : clip.width, clipY ? element.clientHeight : clip.height));
|
|
14
|
+
}
|
|
15
|
+
return clip;
|
|
16
|
+
}
|
|
17
|
+
export function intersect(first, second) {
|
|
18
|
+
const left = Math.max(first.left, second.left);
|
|
19
|
+
const top = Math.max(first.top, second.top);
|
|
20
|
+
return new DOMRect(left, top, Math.max(0, Math.min(first.right, second.right) - left), Math.max(0, Math.min(first.bottom, second.bottom) - top));
|
|
21
|
+
}
|
|
22
|
+
function pixels(value) {
|
|
23
|
+
return parseFloat(value) || 0;
|
|
24
|
+
}
|
|
25
|
+
function caretHeight(style) {
|
|
26
|
+
return pixels(style.lineHeight) || (pixels(style.fontSize) * 1.2);
|
|
27
|
+
}
|
|
28
|
+
function measureRanges(document, offsets, point) {
|
|
29
|
+
const range = document.createRange();
|
|
30
|
+
const selection = [];
|
|
31
|
+
for (const { start, end } of rangesOf(offsets)) {
|
|
32
|
+
range.setStart(...point(start));
|
|
33
|
+
range.setEnd(...point(end));
|
|
34
|
+
for (const rect of range.getClientRects()) {
|
|
35
|
+
if (rect.width && rect.height) {
|
|
36
|
+
selection.push(rect);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
range.setStart(...point(offsets.focus));
|
|
41
|
+
range.collapse(true);
|
|
42
|
+
return { caret: range.getBoundingClientRect(), selection };
|
|
43
|
+
}
|
|
44
|
+
export function editableGeometry(control, offsets) {
|
|
45
|
+
const document = control.ownerDocument;
|
|
46
|
+
const text = new EditableText(control);
|
|
47
|
+
const geometry = measureRanges(document, offsets, offset => text.point(offset));
|
|
48
|
+
const emptyLine = text.startsEmptyLine(offsets.focus);
|
|
49
|
+
if (geometry.caret.height && !emptyLine)
|
|
50
|
+
return geometry;
|
|
51
|
+
const bounds = control.getBoundingClientRect();
|
|
52
|
+
const style = document.defaultView.getComputedStyle(control);
|
|
53
|
+
const height = caretHeight(style);
|
|
54
|
+
const x = (style.direction === 'rtl')
|
|
55
|
+
? bounds.right - pixels(style.borderRightWidth) - pixels(style.paddingRight)
|
|
56
|
+
: bounds.left + pixels(style.borderLeftWidth) + pixels(style.paddingLeft);
|
|
57
|
+
const top = bounds.top + pixels(style.borderTopWidth) + pixels(style.paddingTop);
|
|
58
|
+
let y = geometry.selection.at(-1)?.top ?? top;
|
|
59
|
+
if (emptyLine) {
|
|
60
|
+
const breakRange = document.createRange();
|
|
61
|
+
breakRange.setStart(...text.point(offsets.focus - 1));
|
|
62
|
+
breakRange.setEnd(...text.point(offsets.focus));
|
|
63
|
+
const previous = Array.from(breakRange.getClientRects()).filter(rect => rect.height > 0).at(-1);
|
|
64
|
+
if (previous) {
|
|
65
|
+
y = previous.top + height;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
geometry.caret = new DOMRect(x, y, 0, height);
|
|
69
|
+
return geometry;
|
|
70
|
+
}
|
|
71
|
+
const typography = [
|
|
72
|
+
'direction', 'font-family', 'font-feature-settings', 'font-kerning', 'font-optical-sizing', 'font-size',
|
|
73
|
+
'font-size-adjust', 'font-stretch', 'font-style', 'font-variant', 'font-variation-settings', 'font-weight',
|
|
74
|
+
'letter-spacing', 'tab-size', 'text-align', 'text-indent', 'text-transform', 'word-spacing'
|
|
75
|
+
];
|
|
76
|
+
export class TextControlMirror {
|
|
77
|
+
document;
|
|
78
|
+
mirror;
|
|
79
|
+
text;
|
|
80
|
+
constructor(document) {
|
|
81
|
+
this.document = document;
|
|
82
|
+
this.mirror = document.createElement('div');
|
|
83
|
+
this.text = document.createTextNode('');
|
|
84
|
+
Object.assign(this.mirror.style, {
|
|
85
|
+
border: '0', boxSizing: 'content-box', margin: '0', padding: '0', overflow: 'visible',
|
|
86
|
+
pointerEvents: 'none', position: 'fixed', visibility: 'hidden'
|
|
87
|
+
});
|
|
88
|
+
this.mirror.setAttribute('aria-hidden', 'true');
|
|
89
|
+
this.mirror.append(this.text);
|
|
90
|
+
document.body.append(this.mirror);
|
|
91
|
+
}
|
|
92
|
+
destroy() {
|
|
93
|
+
this.mirror.remove();
|
|
94
|
+
}
|
|
95
|
+
measure(control, offsets) {
|
|
96
|
+
const rect = control.getBoundingClientRect();
|
|
97
|
+
const style = this.document.defaultView.getComputedStyle(control);
|
|
98
|
+
const height = caretHeight(style);
|
|
99
|
+
const left = pixels(style.paddingLeft);
|
|
100
|
+
const right = pixels(style.paddingRight);
|
|
101
|
+
const top = pixels(style.paddingTop);
|
|
102
|
+
const bottom = pixels(style.paddingBottom);
|
|
103
|
+
const multiline = control instanceof HTMLTextAreaElement;
|
|
104
|
+
const startY = multiline ? top : (control.clientHeight - height + top - bottom) / 2;
|
|
105
|
+
for (const property of typography) {
|
|
106
|
+
this.mirror.style.setProperty(property, style.getPropertyValue(property));
|
|
107
|
+
}
|
|
108
|
+
Object.assign(this.mirror.style, {
|
|
109
|
+
left: (rect.left + pixels(style.borderLeftWidth) + left - control.scrollLeft) + 'px',
|
|
110
|
+
lineHeight: multiline ? style.lineHeight : (height + 'px'),
|
|
111
|
+
overflowWrap: multiline ? style.overflowWrap : 'normal',
|
|
112
|
+
top: (rect.top + pixels(style.borderTopWidth) + startY - control.scrollTop) + 'px',
|
|
113
|
+
whiteSpace: multiline ? style.whiteSpace : 'pre',
|
|
114
|
+
width: Math.max(0, control.clientWidth - left - right) + 'px'
|
|
115
|
+
});
|
|
116
|
+
// A zero-width character gives empty fields and trailing blank lines a measurable baseline.
|
|
117
|
+
this.text.data = (multiline || !control.value) ? (control.value + '\u200b') : control.value;
|
|
118
|
+
const geometry = measureRanges(this.document, offsets, offset => [this.text, Math.min(offset, control.value.length)]);
|
|
119
|
+
this.text.data = '';
|
|
120
|
+
return geometry;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { textPresence } from './text-presence.js';
|
|
2
|
+
export function cursor() {
|
|
3
|
+
return rememberedCursor();
|
|
4
|
+
}
|
|
5
|
+
export function rememberedCursor(source) {
|
|
6
|
+
return textPresence('cursor', {
|
|
7
|
+
local(field, { focused, offsets }) {
|
|
8
|
+
return focused ? { field, offset: offsets.focus } : null;
|
|
9
|
+
},
|
|
10
|
+
remote: participant => participant.cursor,
|
|
11
|
+
send: (context, position) => context.cursor(position)
|
|
12
|
+
}, source);
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { CursorRange } from '../../../shared/protocol.js';
|
|
2
|
+
import type { User } from '../../../shared/protocol.js';
|
|
3
|
+
export type Target = {
|
|
4
|
+
client: string;
|
|
5
|
+
control: HTMLElement;
|
|
6
|
+
user: User;
|
|
7
|
+
} & ({
|
|
8
|
+
offset: number;
|
|
9
|
+
} | {
|
|
10
|
+
ranges: readonly CursorRange[];
|
|
11
|
+
});
|
|
12
|
+
export declare class CursorIndicators {
|
|
13
|
+
private kind;
|
|
14
|
+
private targets;
|
|
15
|
+
private readonly document;
|
|
16
|
+
private readonly window;
|
|
17
|
+
private readonly abort;
|
|
18
|
+
private readonly markers;
|
|
19
|
+
private readonly resize;
|
|
20
|
+
private controls;
|
|
21
|
+
private mirror?;
|
|
22
|
+
private pending;
|
|
23
|
+
constructor(document: Document, kind: 'cursor' | 'range', targets: () => Target[]);
|
|
24
|
+
destroy(): void;
|
|
25
|
+
schedule(): void;
|
|
26
|
+
private render;
|
|
27
|
+
private draw;
|
|
28
|
+
private create;
|
|
29
|
+
private createBlock;
|
|
30
|
+
private createCaret;
|
|
31
|
+
}
|