@daasboard/doc 0.0.0-stage → 0.1.1
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 +5 -0
- package/README.md +6 -2
- package/dist/DocumentAdapter.d.ts +41 -0
- package/dist/DocumentAdapter.js +34 -0
- package/dist/LoroPageDoc.d.ts +137 -0
- package/dist/LoroPageDoc.js +2662 -0
- package/dist/MemoryDocument.d.ts +42 -0
- package/dist/MemoryDocument.js +868 -0
- package/dist/connectors.d.ts +8 -0
- package/dist/connectors.js +44 -0
- package/dist/imageHashes.d.ts +3 -0
- package/dist/imageHashes.js +61 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +12 -0
- package/dist/normalize.d.ts +50 -0
- package/dist/normalize.js +203 -0
- package/dist/presence/RoomPresence.d.ts +107 -0
- package/dist/presence/RoomPresence.js +515 -0
- package/dist/schema.d.ts +728 -0
- package/dist/schema.js +891 -0
- package/dist/text.d.ts +80 -0
- package/dist/text.js +244 -0
- package/dist/textDelta.d.ts +27 -0
- package/dist/textDelta.js +688 -0
- package/dist/transport/WsTransport.d.ts +178 -0
- package/dist/transport/WsTransport.js +1314 -0
- package/dist/transport/connectRoom.d.ts +47 -0
- package/dist/transport/connectRoom.js +303 -0
- package/dist/transport/prefix.d.ts +4 -0
- package/dist/transport/prefix.js +27 -0
- package/dist/transport/slices.d.ts +9 -0
- package/dist/transport/slices.js +151 -0
- package/dist/transport/snapshot.d.ts +46 -0
- package/dist/transport/snapshot.js +248 -0
- package/dist/transport/token.d.ts +2 -0
- package/dist/transport/token.js +6 -0
- package/package.json +34 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnchorId, ConnectorData, ConnectorEndpoint, Rect } from "@daasboard/render-core";
|
|
2
|
+
export declare const isPoint: (v: unknown) => v is [number, number];
|
|
3
|
+
export declare function endpointOf(ep: unknown): ConnectorEndpoint;
|
|
4
|
+
export declare function endpointValue(v: unknown): unknown;
|
|
5
|
+
export declare const pointKeyOf: (end: "start" | "end") => "startAt" | "endAt";
|
|
6
|
+
export declare function validAnchors(raw: unknown): Record<AnchorId, [number, number]> | undefined;
|
|
7
|
+
export declare function completeConn(raw: Record<string, unknown>, box: Rect): ConnectorData;
|
|
8
|
+
export declare function connIsPartial(raw: unknown): boolean;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { stripUndefined } from "./DocumentAdapter.js";
|
|
2
|
+
export const isPoint = (v) => Array.isArray(v) && v.length === 2 && typeof v[0] === "number" && typeof v[1] === "number";
|
|
3
|
+
export function endpointOf(ep) {
|
|
4
|
+
if (!ep || typeof ep !== "object")
|
|
5
|
+
return {};
|
|
6
|
+
const { at: _legacy, ...rest } = ep;
|
|
7
|
+
return rest;
|
|
8
|
+
}
|
|
9
|
+
export function endpointValue(v) {
|
|
10
|
+
if (!v || typeof v !== "object")
|
|
11
|
+
return v;
|
|
12
|
+
const ep = stripUndefined(endpointOf(v));
|
|
13
|
+
if (ep.anchor !== undefined)
|
|
14
|
+
delete ep.side;
|
|
15
|
+
return ep;
|
|
16
|
+
}
|
|
17
|
+
export const pointKeyOf = (end) => (end === "start" ? "startAt" : "endAt");
|
|
18
|
+
export function validAnchors(raw) {
|
|
19
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw))
|
|
20
|
+
return undefined;
|
|
21
|
+
let out;
|
|
22
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
23
|
+
if (!isPoint(v))
|
|
24
|
+
continue;
|
|
25
|
+
(out ??= {})[k] = v;
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
export function completeConn(raw, box) {
|
|
30
|
+
const c = raw;
|
|
31
|
+
return {
|
|
32
|
+
...raw,
|
|
33
|
+
start: endpointOf(c.start),
|
|
34
|
+
end: endpointOf(c.end),
|
|
35
|
+
startAt: isPoint(c.startAt) ? c.startAt : [box.x, box.y],
|
|
36
|
+
endAt: isPoint(c.endAt) ? c.endAt : [box.x + box.w, box.y + box.h],
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function connIsPartial(raw) {
|
|
40
|
+
if (!raw || typeof raw !== "object")
|
|
41
|
+
return true;
|
|
42
|
+
const c = raw;
|
|
43
|
+
return !c.start || typeof c.start !== "object" || !c.end || typeof c.end !== "object" || !isPoint(c.startAt) || !isPoint(c.endAt);
|
|
44
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export function imageHashesInHistory(doc, since) {
|
|
2
|
+
if (doc.isShallow())
|
|
3
|
+
throw new Error("image history requires a full Loro document");
|
|
4
|
+
const end = doc.oplogVersion();
|
|
5
|
+
try {
|
|
6
|
+
if (since) {
|
|
7
|
+
const comparison = end.compare(since);
|
|
8
|
+
if (comparison === undefined || comparison < 0) {
|
|
9
|
+
throw new Error("history scan starts beyond the document's version");
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
const updates = doc.exportJsonUpdates(since, end, false);
|
|
13
|
+
if (updates.schema_version !== 1)
|
|
14
|
+
throw new Error("unsupported Loro operation schema version");
|
|
15
|
+
const hashes = new Set();
|
|
16
|
+
for (const change of updates.changes) {
|
|
17
|
+
for (const { content } of change.ops) {
|
|
18
|
+
if (content.type === "counter")
|
|
19
|
+
continue;
|
|
20
|
+
switch (content.type) {
|
|
21
|
+
case "insert":
|
|
22
|
+
case "set":
|
|
23
|
+
if ("value" in content)
|
|
24
|
+
collectValue(content.value, hashes);
|
|
25
|
+
break;
|
|
26
|
+
case "mark":
|
|
27
|
+
collectValue(content.style_value, hashes);
|
|
28
|
+
break;
|
|
29
|
+
case "delete":
|
|
30
|
+
case "move":
|
|
31
|
+
case "create":
|
|
32
|
+
case "mark_end":
|
|
33
|
+
break;
|
|
34
|
+
default:
|
|
35
|
+
throw new Error("unknown Loro operation cannot establish complete image history");
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return [...hashes].sort();
|
|
40
|
+
}
|
|
41
|
+
finally {
|
|
42
|
+
end.free();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function collectValue(value, hashes) {
|
|
46
|
+
const pending = [value];
|
|
47
|
+
while (pending.length) {
|
|
48
|
+
const current = pending.pop();
|
|
49
|
+
if (current === null || typeof current !== "object" || ArrayBuffer.isView(current))
|
|
50
|
+
continue;
|
|
51
|
+
if (!Array.isArray(current)) {
|
|
52
|
+
const map = current;
|
|
53
|
+
if (typeof map.hash === "string" && /^[0-9a-f]{64}$/.test(map.hash)
|
|
54
|
+
&& Object.hasOwn(map, "w") && Object.hasOwn(map, "h") && Object.hasOwn(map, "mime")) {
|
|
55
|
+
hashes.add(map.hash);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const nested of Object.values(current))
|
|
59
|
+
pending.push(nested);
|
|
60
|
+
}
|
|
61
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { type DocumentAdapter, type Transaction, type PropPath, type TextDelta, type TextDeltaOp, type ChangeMeta, TYPING_MERGE_MS, getPath, setPath } from "./DocumentAdapter.js";
|
|
2
|
+
export { MemoryDocument, type MemoryDocumentOptions } from "./MemoryDocument.js";
|
|
3
|
+
export { LoroPageDoc, SCHEMA_VERSION, MAX_UNDO_ENTRIES, type ObjectId, type LoroPageDocOptions } from "./LoroPageDoc.js";
|
|
4
|
+
export { readText, readTextFromDelta, writeText, plainText, contentToFlat, flatToContent, loroToFlat, deltaToFlat, flatToLoroDelta, isCodePointBoundary, snapDown, snapUp, sameAttrs, isEmptyTextContent, normalizeTextContent, runFromAttrs, paragraphFromAttrs, runAttrs, paragraphAttrs, HARD_LINE_BREAK, TEXT_STYLE_CONFIG, INLINE_KEYS, PARA_KEYS, TEXT_CASES, type Flat, type Attrs } from "./text.js";
|
|
5
|
+
export { applyTextDelta, applyTextDeltaToParagraphs, flatTextOf, applyDeltaToString, formatChangesLoroText, validateTextDelta, deltaLength, textDeltaIsEmpty, textDeltaIsKeystroke, textDeltaOnlyClears, sameTextContent, rewriteDelta, toLoroDelta, ensureTrailingNewline, emptyTextContent, isClearValue } from "./textDelta.js";
|
|
6
|
+
export { WsTransport, type TransportStatus, type TransportEvents, type TransportStats, type TransportOptions, type BootstrapOutcome, type RoomGrant, type TransportFailure } from "./transport/WsTransport.js";
|
|
7
|
+
export { fetchSnapshot, snapshotUrl, retryAfterMs, frontierText, type StagedSnapshot, type SnapshotFetch, type SnapshotFetchOptions } from "./transport/snapshot.js";
|
|
8
|
+
export type { RoomToken } from "./transport/token.js";
|
|
9
|
+
export { RoomPresence, MAX_PRESENCE_BYTES, MAX_PRESENCE_SELECTION, MAX_PRESENCE_DRAG_OBJECTS, PRESENCE_TIMEOUT_MS, type PresenceOptions, type PresenceUser, type PresencePoint, type PresenceGhost, type PresenceDrag, type PresenceCaret, type RemotePresence } from "./presence/RoomPresence.js";
|
|
10
|
+
export { normalize, normalizeNode, sceneState, resolveStyle, resolveStyleValue, instanceRef, isTokenRef, NODE_KEYS, CONTAINER_KINDS, type NodeKey, type RawNode, type PageState, type PageView, type Problem, type NormalizedNode, isTextContent } from "./normalize.js";
|
|
11
|
+
export { hashAsset, ASSET_HASH_SLICE_BYTES, encodeThumbHash, decodeThumbHash, validateThumbHash, sanitizeImageRef, sanitizeEmbedRef, type AssetHashOptions, type DecodedThumb } from "@daasboard/render-core";
|
|
12
|
+
export type { AssetHash } from "./schema.js";
|
|
13
|
+
export { imageHashesInHistory } from "./imageHashes.js";
|
|
14
|
+
export { connectRoom, ConnectRoomError, type ConnectRoomOptions, type ConnectRoomErrorCode, type RoomConnection, type RoomConnectionState, type RoomConnectionStatus } from "./transport/connectRoom.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { TYPING_MERGE_MS, getPath, setPath } from "./DocumentAdapter.js";
|
|
2
|
+
export { MemoryDocument } from "./MemoryDocument.js";
|
|
3
|
+
export { LoroPageDoc, SCHEMA_VERSION, MAX_UNDO_ENTRIES } from "./LoroPageDoc.js";
|
|
4
|
+
export { readText, readTextFromDelta, writeText, plainText, contentToFlat, flatToContent, loroToFlat, deltaToFlat, flatToLoroDelta, isCodePointBoundary, snapDown, snapUp, sameAttrs, isEmptyTextContent, normalizeTextContent, runFromAttrs, paragraphFromAttrs, runAttrs, paragraphAttrs, HARD_LINE_BREAK, TEXT_STYLE_CONFIG, INLINE_KEYS, PARA_KEYS, TEXT_CASES } from "./text.js";
|
|
5
|
+
export { applyTextDelta, applyTextDeltaToParagraphs, flatTextOf, applyDeltaToString, formatChangesLoroText, validateTextDelta, deltaLength, textDeltaIsEmpty, textDeltaIsKeystroke, textDeltaOnlyClears, sameTextContent, rewriteDelta, toLoroDelta, ensureTrailingNewline, emptyTextContent, isClearValue } from "./textDelta.js";
|
|
6
|
+
export { WsTransport } from "./transport/WsTransport.js";
|
|
7
|
+
export { fetchSnapshot, snapshotUrl, retryAfterMs, frontierText } from "./transport/snapshot.js";
|
|
8
|
+
export { RoomPresence, MAX_PRESENCE_BYTES, MAX_PRESENCE_SELECTION, MAX_PRESENCE_DRAG_OBJECTS, PRESENCE_TIMEOUT_MS } from "./presence/RoomPresence.js";
|
|
9
|
+
export { normalize, normalizeNode, sceneState, resolveStyle, resolveStyleValue, instanceRef, isTokenRef, NODE_KEYS, CONTAINER_KINDS, isTextContent } from "./normalize.js";
|
|
10
|
+
export { hashAsset, ASSET_HASH_SLICE_BYTES, encodeThumbHash, decodeThumbHash, validateThumbHash, sanitizeImageRef, sanitizeEmbedRef } from "@daasboard/render-core";
|
|
11
|
+
export { imageHashesInHistory } from "./imageHashes.js";
|
|
12
|
+
export { connectRoom, ConnectRoomError } from "./transport/connectRoom.js";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { InstanceRef, Scene, SceneObject, Style, TextContent } from "@daasboard/render-core";
|
|
2
|
+
export declare const NODE_KEYS: readonly ["kind", "xywh", "style", "rot", "hidden", "text", "path", "image", "clip", "name", "extId", "locked", "autoWidth", "autoHeight", "anchors", "embed", "conn", "inst"];
|
|
3
|
+
export type NodeKey = (typeof NODE_KEYS)[number];
|
|
4
|
+
export type RawNode = {
|
|
5
|
+
readonly [K in NodeKey]?: unknown;
|
|
6
|
+
};
|
|
7
|
+
export interface PageState {
|
|
8
|
+
roots(): readonly string[];
|
|
9
|
+
children(id: string): readonly string[];
|
|
10
|
+
read(id: string): RawNode;
|
|
11
|
+
}
|
|
12
|
+
export interface Problem {
|
|
13
|
+
id: string;
|
|
14
|
+
reason: string;
|
|
15
|
+
}
|
|
16
|
+
export interface PageView extends Scene {
|
|
17
|
+
problems: Problem[];
|
|
18
|
+
}
|
|
19
|
+
export type NormalizedNode = {
|
|
20
|
+
object: SceneObject;
|
|
21
|
+
problem?: undefined;
|
|
22
|
+
} | {
|
|
23
|
+
object?: undefined;
|
|
24
|
+
problem: string;
|
|
25
|
+
};
|
|
26
|
+
export declare const CONTAINER_KINDS: ReadonlySet<string>;
|
|
27
|
+
export declare const isTextContent: (v: unknown) => v is TextContent;
|
|
28
|
+
export declare const isTokenRef: (v: unknown) => v is {
|
|
29
|
+
token: string;
|
|
30
|
+
value: unknown;
|
|
31
|
+
};
|
|
32
|
+
export declare function resolveStyleValue(key: string, v: unknown): unknown;
|
|
33
|
+
export declare function resolveStyle(raw: Record<string, unknown>): Style;
|
|
34
|
+
export declare function instanceRef(raw: unknown): InstanceRef | undefined;
|
|
35
|
+
export declare function instKeyValue(key: string, value: unknown): {
|
|
36
|
+
ok: true;
|
|
37
|
+
value: unknown;
|
|
38
|
+
} | {
|
|
39
|
+
ok: false;
|
|
40
|
+
};
|
|
41
|
+
export declare function rawOfObject(o: SceneObject, origin?: {
|
|
42
|
+
x: number;
|
|
43
|
+
y: number;
|
|
44
|
+
}): RawNode;
|
|
45
|
+
export declare function normalizeNode(id: string, raw: RawNode, parentAbs: {
|
|
46
|
+
x: number;
|
|
47
|
+
y: number;
|
|
48
|
+
}, parentId: string | null): NormalizedNode;
|
|
49
|
+
export declare function normalize(state: PageState): PageView;
|
|
50
|
+
export declare function sceneState(scene: Scene): PageState;
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { sanitizeImageRef, sanitizeEmbedRef } from "@daasboard/render-core";
|
|
2
|
+
import { connectorBounds } from "@daasboard/render-core";
|
|
3
|
+
import { completeConn, validAnchors } from "./connectors.js";
|
|
4
|
+
export const NODE_KEYS = ["kind", "xywh", "style", "rot", "hidden", "text", "path", "image", "clip", "name", "extId", "locked", "autoWidth", "autoHeight", "anchors", "embed", "conn", "inst"];
|
|
5
|
+
export const CONTAINER_KINDS = new Set(["frame", "group", "section"]);
|
|
6
|
+
const isMap = (v) => !!v && typeof v === "object" && !Array.isArray(v);
|
|
7
|
+
const finite = (v) => typeof v === "number" && Number.isFinite(v);
|
|
8
|
+
const isBox = (v) => Array.isArray(v) && v.length === 4 && v.every(finite);
|
|
9
|
+
const isRun = (v) => isMap(v) && typeof v.text === "string";
|
|
10
|
+
const isParagraph = (v) => isMap(v) && Array.isArray(v.runs) && v.runs.every(isRun);
|
|
11
|
+
export const isTextContent = (v) => isMap(v) && Array.isArray(v.paragraphs) && v.paragraphs.every(isParagraph);
|
|
12
|
+
const isText = isTextContent;
|
|
13
|
+
export const isTokenRef = (v) => isMap(v) && typeof v.token === "string" && "value" in v;
|
|
14
|
+
function describe(v) {
|
|
15
|
+
let s;
|
|
16
|
+
try {
|
|
17
|
+
s = v === undefined ? "undefined" : (JSON.stringify(v) ?? String(v));
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
s = String(v);
|
|
21
|
+
}
|
|
22
|
+
return s.length > 40 ? `${s.slice(0, 37)}...` : s;
|
|
23
|
+
}
|
|
24
|
+
function resolvePaint(p) {
|
|
25
|
+
if (isTokenRef(p))
|
|
26
|
+
return p.value;
|
|
27
|
+
if (isMap(p) && Array.isArray(p.stops) && p.stops.some((s) => isMap(s) && isTokenRef(s.color)))
|
|
28
|
+
return { ...p, stops: p.stops.map((s) => (isMap(s) && isTokenRef(s.color) ? { ...s, color: s.color.value } : s)) };
|
|
29
|
+
return p;
|
|
30
|
+
}
|
|
31
|
+
export function resolveStyleValue(key, v) {
|
|
32
|
+
if (key === "fill")
|
|
33
|
+
return Array.isArray(v) ? v.map(resolvePaint) : resolvePaint(v);
|
|
34
|
+
const literal = isTokenRef(v) ? v.value : v;
|
|
35
|
+
if (key === "textCase")
|
|
36
|
+
return literal === "upper" || literal === "lower" || literal === "none" ? literal : undefined;
|
|
37
|
+
return literal;
|
|
38
|
+
}
|
|
39
|
+
export function resolveStyle(raw) {
|
|
40
|
+
const out = {};
|
|
41
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
42
|
+
if (v === undefined)
|
|
43
|
+
continue;
|
|
44
|
+
out[k] = resolveStyleValue(k, v);
|
|
45
|
+
}
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
export function instanceRef(raw) {
|
|
49
|
+
if (!isMap(raw))
|
|
50
|
+
return undefined;
|
|
51
|
+
let out;
|
|
52
|
+
const def = raw.def;
|
|
53
|
+
if (isMap(def) && typeof def.lib === "string" && typeof def.node === "string")
|
|
54
|
+
(out ??= {}).def = { lib: def.lib, node: def.node };
|
|
55
|
+
if (typeof raw.defName === "string")
|
|
56
|
+
(out ??= {}).defName = raw.defName;
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
59
|
+
export function instKeyValue(key, value) {
|
|
60
|
+
if (value === undefined)
|
|
61
|
+
return key === "def" || key === "defName" ? { ok: true, value: undefined } : { ok: false };
|
|
62
|
+
if (key === "def")
|
|
63
|
+
return isMap(value) && typeof value.lib === "string" && typeof value.node === "string" ? { ok: true, value: { lib: value.lib, node: value.node } } : { ok: false };
|
|
64
|
+
if (key === "defName")
|
|
65
|
+
return typeof value === "string" ? { ok: true, value } : { ok: false };
|
|
66
|
+
return { ok: false };
|
|
67
|
+
}
|
|
68
|
+
export function rawOfObject(o, origin = { x: 0, y: 0 }) {
|
|
69
|
+
const raw = { kind: o.kind, xywh: [o.x - origin.x, o.y - origin.y, o.w, o.h], style: o.style };
|
|
70
|
+
if (o.rot !== 0)
|
|
71
|
+
raw.rot = o.rot;
|
|
72
|
+
for (const k of ["hidden", "text", "path", "image", "clip", "name", "extId", "locked", "autoWidth", "autoHeight", "anchors", "embed", "conn", "inst"])
|
|
73
|
+
if (o[k] !== undefined)
|
|
74
|
+
raw[k] = o[k];
|
|
75
|
+
return raw;
|
|
76
|
+
}
|
|
77
|
+
export function normalizeNode(id, raw, parentAbs, parentId) {
|
|
78
|
+
const kind = raw.kind;
|
|
79
|
+
if (typeof kind !== "string" || kind === "")
|
|
80
|
+
return { problem: kind === undefined || kind === null ? "kind: missing" : `kind: not a string (${describe(kind)})` };
|
|
81
|
+
if (kind === "trash")
|
|
82
|
+
return { problem: "kind: a trash root is not an object" };
|
|
83
|
+
const xywh = raw.xywh;
|
|
84
|
+
if (!isBox(xywh))
|
|
85
|
+
return { problem: xywh === undefined || xywh === null ? "xywh: missing" : `xywh: not four finite numbers (${describe(xywh)})` };
|
|
86
|
+
const rot = raw.rot === undefined || raw.rot === null ? 0 : raw.rot;
|
|
87
|
+
if (!finite(rot))
|
|
88
|
+
return { problem: `rot: not a finite number (${describe(raw.rot)})` };
|
|
89
|
+
const rawStyle = raw.style === undefined || raw.style === null ? {} : raw.style;
|
|
90
|
+
if (!isMap(rawStyle))
|
|
91
|
+
return { problem: `style: not a map (${describe(raw.style)})` };
|
|
92
|
+
const o = {
|
|
93
|
+
id,
|
|
94
|
+
kind: kind,
|
|
95
|
+
parent: parentId,
|
|
96
|
+
x: parentAbs.x + xywh[0],
|
|
97
|
+
y: parentAbs.y + xywh[1],
|
|
98
|
+
w: xywh[2],
|
|
99
|
+
h: xywh[3],
|
|
100
|
+
rot,
|
|
101
|
+
style: resolveStyle(rawStyle),
|
|
102
|
+
version: 0,
|
|
103
|
+
};
|
|
104
|
+
if (raw.hidden)
|
|
105
|
+
o.hidden = true;
|
|
106
|
+
if (isText(raw.text)) {
|
|
107
|
+
o.text = raw.text;
|
|
108
|
+
o.textVersion = 1;
|
|
109
|
+
}
|
|
110
|
+
if (Array.isArray(raw.path))
|
|
111
|
+
o.path = raw.path;
|
|
112
|
+
if (isMap(raw.image))
|
|
113
|
+
o.image = sanitizeImageRef(raw.image);
|
|
114
|
+
if (raw.clip)
|
|
115
|
+
o.clip = true;
|
|
116
|
+
if (typeof raw.name === "string")
|
|
117
|
+
o.name = raw.name;
|
|
118
|
+
if (typeof raw.extId === "string")
|
|
119
|
+
o.extId = raw.extId;
|
|
120
|
+
if (raw.locked)
|
|
121
|
+
o.locked = true;
|
|
122
|
+
if (raw.autoWidth)
|
|
123
|
+
o.autoWidth = true;
|
|
124
|
+
if (raw.autoHeight)
|
|
125
|
+
o.autoHeight = true;
|
|
126
|
+
const anchors = validAnchors(raw.anchors);
|
|
127
|
+
if (anchors)
|
|
128
|
+
o.anchors = anchors;
|
|
129
|
+
if (isMap(raw.embed) && typeof raw.embed.url === "string")
|
|
130
|
+
o.embed = sanitizeEmbedRef(raw.embed);
|
|
131
|
+
const inst = instanceRef(raw.inst);
|
|
132
|
+
if (inst)
|
|
133
|
+
o.inst = inst;
|
|
134
|
+
const rawConn = isMap(raw.conn) ? raw.conn : undefined;
|
|
135
|
+
if (rawConn && (kind === "connector" || Object.keys(rawConn).length > 0))
|
|
136
|
+
o.conn = kind === "connector" ? completeConn(rawConn, o) : rawConn;
|
|
137
|
+
return { object: o };
|
|
138
|
+
}
|
|
139
|
+
export function normalize(state) {
|
|
140
|
+
const objects = new Map();
|
|
141
|
+
const order = [];
|
|
142
|
+
const problems = [];
|
|
143
|
+
const walk = (id, parentAbs, parentId) => {
|
|
144
|
+
const raw = state.read(id);
|
|
145
|
+
if (raw.kind === "trash")
|
|
146
|
+
return false;
|
|
147
|
+
const r = normalizeNode(id, raw, parentAbs, parentId);
|
|
148
|
+
if (!r.object) {
|
|
149
|
+
problems.push({ id, reason: r.problem });
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
const o = r.object;
|
|
153
|
+
objects.set(id, o);
|
|
154
|
+
order.push(id);
|
|
155
|
+
const childAbs = CONTAINER_KINDS.has(o.kind) ? { x: o.x, y: o.y } : parentAbs;
|
|
156
|
+
let live = 0;
|
|
157
|
+
for (const c of state.children(id))
|
|
158
|
+
if (walk(c, childAbs, id))
|
|
159
|
+
live++;
|
|
160
|
+
if (o.kind === "group" && live === 0)
|
|
161
|
+
o.hidden = true;
|
|
162
|
+
return true;
|
|
163
|
+
};
|
|
164
|
+
for (const r of state.roots())
|
|
165
|
+
walk(r, { x: 0, y: 0 }, null);
|
|
166
|
+
const view = { objects, order };
|
|
167
|
+
for (const o of objects.values())
|
|
168
|
+
if (o.kind === "connector" && o.conn)
|
|
169
|
+
Object.assign(o, connectorBounds(o.conn, view));
|
|
170
|
+
return { objects, order, problems };
|
|
171
|
+
}
|
|
172
|
+
export function sceneState(scene) {
|
|
173
|
+
const childrenOf = new Map();
|
|
174
|
+
for (const id of scene.order) {
|
|
175
|
+
const o = scene.objects.get(id);
|
|
176
|
+
if (!o)
|
|
177
|
+
continue;
|
|
178
|
+
const parent = o.parent ?? null;
|
|
179
|
+
let list = childrenOf.get(parent);
|
|
180
|
+
if (!list)
|
|
181
|
+
childrenOf.set(parent, (list = []));
|
|
182
|
+
list.push(id);
|
|
183
|
+
}
|
|
184
|
+
const originOf = (o) => {
|
|
185
|
+
for (let p = o.parent; p;) {
|
|
186
|
+
const po = scene.objects.get(p);
|
|
187
|
+
if (!po)
|
|
188
|
+
break;
|
|
189
|
+
if (CONTAINER_KINDS.has(po.kind))
|
|
190
|
+
return { x: po.x, y: po.y };
|
|
191
|
+
p = po.parent;
|
|
192
|
+
}
|
|
193
|
+
return { x: 0, y: 0 };
|
|
194
|
+
};
|
|
195
|
+
return {
|
|
196
|
+
roots: () => childrenOf.get(null) ?? [],
|
|
197
|
+
children: (id) => childrenOf.get(id) ?? [],
|
|
198
|
+
read: (id) => {
|
|
199
|
+
const o = scene.objects.get(id);
|
|
200
|
+
return o ? rawOfObject(o, originOf(o)) : {};
|
|
201
|
+
},
|
|
202
|
+
};
|
|
203
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { LoroPageDoc } from "../LoroPageDoc.js";
|
|
2
|
+
export declare const MAX_PRESENCE_BYTES: number;
|
|
3
|
+
export declare const MAX_PRESENCE_SELECTION = 128;
|
|
4
|
+
export declare const MAX_PRESENCE_DRAG_OBJECTS = 32;
|
|
5
|
+
export declare const PRESENCE_TIMEOUT_MS = 30000;
|
|
6
|
+
export interface PresenceUser {
|
|
7
|
+
name: string;
|
|
8
|
+
color: string;
|
|
9
|
+
}
|
|
10
|
+
export interface PresencePoint {
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
}
|
|
14
|
+
export interface PresenceGhost {
|
|
15
|
+
id: string;
|
|
16
|
+
x: number;
|
|
17
|
+
y: number;
|
|
18
|
+
w: number;
|
|
19
|
+
h: number;
|
|
20
|
+
rot: number;
|
|
21
|
+
}
|
|
22
|
+
export interface PresenceDrag {
|
|
23
|
+
objects: readonly PresenceGhost[];
|
|
24
|
+
}
|
|
25
|
+
export interface PresenceCaret {
|
|
26
|
+
objectId: string;
|
|
27
|
+
anchor: number;
|
|
28
|
+
head: number;
|
|
29
|
+
}
|
|
30
|
+
export interface PresenceOptions {
|
|
31
|
+
user?: PresenceUser;
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
heartbeatMs?: number;
|
|
34
|
+
}
|
|
35
|
+
export interface RemotePresence {
|
|
36
|
+
connectionId: string;
|
|
37
|
+
subject: string | null;
|
|
38
|
+
user: PresenceUser;
|
|
39
|
+
cursor: PresencePoint | null;
|
|
40
|
+
selection: readonly string[];
|
|
41
|
+
selectionTruncated: boolean;
|
|
42
|
+
drag: PresenceDrag | null;
|
|
43
|
+
caret: PresenceCaret | null;
|
|
44
|
+
}
|
|
45
|
+
export declare class RoomPresence {
|
|
46
|
+
private readonly page;
|
|
47
|
+
private readonly send;
|
|
48
|
+
private readonly local;
|
|
49
|
+
private readonly state;
|
|
50
|
+
private readonly remote;
|
|
51
|
+
private readonly listeners;
|
|
52
|
+
private readonly changedIds;
|
|
53
|
+
private peerViews;
|
|
54
|
+
private membershipChanged;
|
|
55
|
+
private notificationFrame;
|
|
56
|
+
private notificationIsRaf;
|
|
57
|
+
private documentVersion;
|
|
58
|
+
private readonly unsubscribeDocument;
|
|
59
|
+
private readonly dirty;
|
|
60
|
+
private readonly timeout;
|
|
61
|
+
private readonly heartbeatMs;
|
|
62
|
+
private heartbeat;
|
|
63
|
+
private flushTimer;
|
|
64
|
+
private cursorTimer;
|
|
65
|
+
private dragTimer;
|
|
66
|
+
private discoveryTimer;
|
|
67
|
+
private lastDrag;
|
|
68
|
+
private fullAnnouncement;
|
|
69
|
+
private dragClearRepeats;
|
|
70
|
+
private frameTokens;
|
|
71
|
+
private byteTokens;
|
|
72
|
+
private budgetAt;
|
|
73
|
+
private lastCursor;
|
|
74
|
+
private lastDiscovery;
|
|
75
|
+
private connected;
|
|
76
|
+
private active;
|
|
77
|
+
private enabled;
|
|
78
|
+
private disposed;
|
|
79
|
+
constructor(page: LoroPageDoc, send: (bytes: Uint8Array) => boolean, options?: PresenceOptions);
|
|
80
|
+
subscribe(listener: (changedConnectionIds: ReadonlySet<string>) => void): () => void;
|
|
81
|
+
get size(): number;
|
|
82
|
+
private changed;
|
|
83
|
+
setConnected(connected: boolean): void;
|
|
84
|
+
setActive(active: boolean): void;
|
|
85
|
+
setUser(value: PresenceUser): void;
|
|
86
|
+
setCursor(value: PresencePoint | null): void;
|
|
87
|
+
private publishCursor;
|
|
88
|
+
setSelection(ids: Iterable<string>): void;
|
|
89
|
+
setDrag(value: PresenceDrag | null): void;
|
|
90
|
+
setCaret(value: PresenceCaret | null): void;
|
|
91
|
+
private text;
|
|
92
|
+
caretObjectId(connectionId: string): string | null;
|
|
93
|
+
resolveCaret(connectionId: string): PresenceCaret | null;
|
|
94
|
+
peers(): readonly RemotePresence[];
|
|
95
|
+
private updateView;
|
|
96
|
+
receive(connectionId: string, subject: string | null, bytes: Uint8Array): void;
|
|
97
|
+
leave(connectionId: string): void;
|
|
98
|
+
private clearPeers;
|
|
99
|
+
private enable;
|
|
100
|
+
private set;
|
|
101
|
+
private publish;
|
|
102
|
+
private reannounce;
|
|
103
|
+
private flush;
|
|
104
|
+
private startSending;
|
|
105
|
+
private stopSending;
|
|
106
|
+
dispose(): void;
|
|
107
|
+
}
|