@abraca/dabra 2.89.0 → 2.91.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/package.json +5 -11
- package/src/AbracadabraBaseProvider.ts +0 -963
- package/src/AbracadabraClient.ts +0 -1960
- package/src/AbracadabraProvider.ts +0 -915
- package/src/AbracadabraWS.ts +0 -695
- package/src/BackgroundSyncManager.ts +0 -856
- package/src/BackgroundSyncPersistence.ts +0 -140
- package/src/ChatClient.ts +0 -314
- package/src/CloseEvents.ts +0 -49
- package/src/ContentManager.ts +0 -246
- package/src/CoverReconcile.ts +0 -168
- package/src/CryptoIdentityKeystore.ts +0 -754
- package/src/DeviceRegistrationService.ts +0 -257
- package/src/DocConverters.ts +0 -1859
- package/src/DocKeyManager.ts +0 -163
- package/src/DocTypes.ts +0 -628
- package/src/DocUtils.ts +0 -251
- package/src/DocumentCache.ts +0 -210
- package/src/DocumentManager.ts +0 -410
- package/src/E2EAbracadabraProvider.ts +0 -426
- package/src/E2EOfflineStore.ts +0 -55
- package/src/EncryptedChatClient.ts +0 -173
- package/src/EncryptedY.ts +0 -145
- package/src/EventEmitter.ts +0 -59
- package/src/FileBlobStore.ts +0 -569
- package/src/IdentityDoc.ts +0 -711
- package/src/IncomingMessage.ts +0 -63
- package/src/MessageReceiver.ts +0 -151
- package/src/MessageSender.ts +0 -22
- package/src/MetaManager.ts +0 -194
- package/src/MnemonicKeyDerivation.ts +0 -171
- package/src/NodeConnect.ts +0 -63
- package/src/NodeIdentity.ts +0 -378
- package/src/NotificationsClient.ts +0 -207
- package/src/OfflineStore.ts +0 -294
- package/src/OutgoingMessage.ts +0 -25
- package/src/OutgoingMessages/AuthenticationMessage.ts +0 -23
- package/src/OutgoingMessages/AwarenessMessage.ts +0 -41
- package/src/OutgoingMessages/CloseMessage.ts +0 -17
- package/src/OutgoingMessages/QueryAwarenessMessage.ts +0 -17
- package/src/OutgoingMessages/StatelessMessage.ts +0 -18
- package/src/OutgoingMessages/SubdocMessage.ts +0 -35
- package/src/OutgoingMessages/SyncStepOneMessage.ts +0 -25
- package/src/OutgoingMessages/SyncStepTwoMessage.ts +0 -25
- package/src/OutgoingMessages/UpdateMessage.ts +0 -20
- package/src/QueryClient.ts +0 -209
- package/src/RpcClient.ts +0 -659
- package/src/SchemaTypes.ts +0 -179
- package/src/SearchIndex.ts +0 -250
- package/src/ServerRegistry.ts +0 -220
- package/src/ServerReset.ts +0 -222
- package/src/TokenManager.ts +0 -337
- package/src/TreeManager.ts +0 -894
- package/src/TreeTimestamps.ts +0 -101
- package/src/auth.ts +0 -62
- package/src/awarenessStatesToArray.ts +0 -10
- package/src/index.ts +0 -219
- package/src/messageRecord.ts +0 -121
- package/src/sync/BroadcastChannelSync.ts +0 -235
- package/src/types.ts +0 -790
- package/src/webrtc/AbracadabraWebRTC.ts +0 -683
- package/src/webrtc/DataChannelRouter.ts +0 -179
- package/src/webrtc/DevicePairingChannel.ts +0 -496
- package/src/webrtc/E2EEChannel.ts +0 -195
- package/src/webrtc/FileTransferChannel.ts +0 -367
- package/src/webrtc/ManualSignaling.ts +0 -204
- package/src/webrtc/PeerConnection.ts +0 -133
- package/src/webrtc/SignalingSocket.ts +0 -378
- package/src/webrtc/YjsDataChannel.ts +0 -203
- package/src/webrtc/index.ts +0 -26
- package/src/webrtc/types.ts +0 -177
package/src/TreeManager.ts
DELETED
|
@@ -1,894 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TreeManager — typed ORM layer over the root Y.Doc's "doc-tree" / "doc-trash"
|
|
3
|
-
* Y.Maps.
|
|
4
|
-
*
|
|
5
|
-
* Extracted from `mcp/tools/tree.ts` and `cli/commands/documents.ts` logic.
|
|
6
|
-
* All tree CRUD operations go through this class.
|
|
7
|
-
*/
|
|
8
|
-
import type * as Y from "yjs";
|
|
9
|
-
import type {
|
|
10
|
-
PageMeta,
|
|
11
|
-
TreeEntry,
|
|
12
|
-
TreeNode,
|
|
13
|
-
TreeSearchResult,
|
|
14
|
-
} from "./DocTypes.ts";
|
|
15
|
-
import { resolvePageType } from "./DocTypes.ts";
|
|
16
|
-
import {
|
|
17
|
-
makeEntryMap,
|
|
18
|
-
normalizeRootId,
|
|
19
|
-
patchEntry,
|
|
20
|
-
toPlain,
|
|
21
|
-
} from "./DocUtils.ts";
|
|
22
|
-
import type { DocumentManager } from "./DocumentManager.ts";
|
|
23
|
-
import {
|
|
24
|
-
projectTreeEntry,
|
|
25
|
-
type SchemaRegistryLike,
|
|
26
|
-
type TypedTreeEntry,
|
|
27
|
-
} from "./SchemaTypes.ts";
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Stable total order over tree siblings: `order` ascending, then `id`
|
|
31
|
-
* ascending as a deterministic tiebreak. The legacy scan sorted by
|
|
32
|
-
* `order` alone and left ties to insertion/iteration order — a superset
|
|
33
|
-
* change that makes cursor pagination well-defined.
|
|
34
|
-
*/
|
|
35
|
-
function cmpKey(oa: number, ia: string, ob: number, ib: string): number {
|
|
36
|
-
if (oa !== ob) return oa < ob ? -1 : 1;
|
|
37
|
-
return ia < ib ? -1 : ia > ib ? 1 : 0;
|
|
38
|
-
}
|
|
39
|
-
function cmpEntry(a: TreeEntry, b: TreeEntry): number {
|
|
40
|
-
return cmpKey(a.order ?? 0, a.id, b.order ?? 0, b.id);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Deterministic, convergent cycle break for the parent-pointer forest.
|
|
45
|
-
*
|
|
46
|
-
* Concurrent offline reparents converge (Yjs LWW on each entry's *own*
|
|
47
|
-
* `parentId` key) to a state that can contain a directed cycle — e.g. peer A
|
|
48
|
-
* moves X under Y while peer B moves Y under X yields `X.parent=Y` AND
|
|
49
|
-
* `Y.parent=X`. Both writes touch different keys so both survive, and the
|
|
50
|
-
* whole subtree then floats unreachable from root (the "orphaned subtree"
|
|
51
|
-
* corruption — §3b of OFFLINE-FIRST-TREE-AUDIT). Yjs guarantees every replica
|
|
52
|
-
* converges to the SAME cyclic map, so any repair that is a pure function of
|
|
53
|
-
* that map is itself convergent: every replica picks the identical break and
|
|
54
|
-
* agrees on the result with no coordinator (a lightweight stand-in for the
|
|
55
|
-
* full Kleppmann fold — it loses one move's intent, but never corrupts).
|
|
56
|
-
*
|
|
57
|
-
* Rule: in each cycle, the node with the lexicographically-greatest id is
|
|
58
|
-
* logically reparented to root. Returns the set of such break-node ids — empty
|
|
59
|
-
* when the forest is already acyclic (the overwhelmingly common case, so the
|
|
60
|
-
* hot path adds one O(n) walk and changes no behavior).
|
|
61
|
-
*
|
|
62
|
-
* `parentOf` returns the effective *normalized* parent (null = root); a parent
|
|
63
|
-
* id absent from `ids` is a strand, not a cycle (Tier-0 does not auto-rescue
|
|
64
|
-
* strands). Self-loops (`parentId === id`) are a degenerate one-node cycle and
|
|
65
|
-
* are handled. The Rust provider mirrors this exactly (`cycle_break_ids`).
|
|
66
|
-
*/
|
|
67
|
-
function cycleBreakIds(
|
|
68
|
-
ids: Iterable<string>,
|
|
69
|
-
parentOf: (id: string) => string | null,
|
|
70
|
-
): Set<string> {
|
|
71
|
-
const idSet = new Set(ids);
|
|
72
|
-
const done = new Set<string>();
|
|
73
|
-
const breaks = new Set<string>();
|
|
74
|
-
for (const start of idSet) {
|
|
75
|
-
if (done.has(start)) continue;
|
|
76
|
-
const path: string[] = [];
|
|
77
|
-
const onPath = new Map<string, number>();
|
|
78
|
-
let cur: string | null = start;
|
|
79
|
-
while (cur !== null && idSet.has(cur) && !done.has(cur)) {
|
|
80
|
-
const seenAt = onPath.get(cur);
|
|
81
|
-
if (seenAt !== undefined) {
|
|
82
|
-
// path[seenAt..] is the cycle; break its greatest id.
|
|
83
|
-
let maxId = path[seenAt];
|
|
84
|
-
for (let i = seenAt + 1; i < path.length; i++) {
|
|
85
|
-
if (path[i] > maxId) maxId = path[i];
|
|
86
|
-
}
|
|
87
|
-
breaks.add(maxId);
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
onPath.set(cur, path.length);
|
|
91
|
-
path.push(cur);
|
|
92
|
-
cur = parentOf(cur);
|
|
93
|
-
}
|
|
94
|
-
for (const id of path) done.add(id);
|
|
95
|
-
}
|
|
96
|
-
return breaks;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Opaque, dependency-free cursor over the (order,id) sibling order. */
|
|
100
|
-
function encodeCursor(order: number, id: string): string {
|
|
101
|
-
return encodeURIComponent(JSON.stringify([order ?? 0, id]));
|
|
102
|
-
}
|
|
103
|
-
function decodeCursor(c: string): { order: number; id: string } | null {
|
|
104
|
-
try {
|
|
105
|
-
const v = JSON.parse(decodeURIComponent(c)) as unknown;
|
|
106
|
-
if (
|
|
107
|
-
Array.isArray(v) &&
|
|
108
|
-
typeof v[0] === "number" &&
|
|
109
|
-
typeof v[1] === "string"
|
|
110
|
-
) {
|
|
111
|
-
return { order: v[0], id: v[1] };
|
|
112
|
-
}
|
|
113
|
-
} catch {
|
|
114
|
-
// Garbage / tampered cursor → treat as "from the beginning".
|
|
115
|
-
}
|
|
116
|
-
return null;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface ChildrenPage {
|
|
120
|
-
entries: TreeEntry[];
|
|
121
|
-
/** Pass back as `cursor` for the next page; `null` when exhausted. */
|
|
122
|
-
nextCursor: string | null;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export class TreeManager {
|
|
126
|
-
constructor(private dm: DocumentManager) {}
|
|
127
|
-
|
|
128
|
-
// ── Path-1 prototype: lazy parent→children index ──────────────────────────
|
|
129
|
-
//
|
|
130
|
-
// Gated by `dm.treeIndexEnabled`. When off, every method below falls
|
|
131
|
-
// through to the original whole-map scan (unchanged). When on, tree
|
|
132
|
-
// WALKS resolve against a normalized adjacency index that is rebuilt
|
|
133
|
-
// lazily: an `observeDeep` on the bound `doc-tree` Y.Map only flips a
|
|
134
|
-
// dirty bit, so the O(n) rebuild happens at most once per mutation
|
|
135
|
-
// batch instead of once per read, and reads between writes are
|
|
136
|
-
// O(k)/O(result) instead of O(n)/O(n²). `readEntries`/`get` keep RAW
|
|
137
|
-
// parentId for round-trip consumers and are intentionally NOT indexed.
|
|
138
|
-
private _idxMap: Y.Map<unknown> | null = null;
|
|
139
|
-
private _idxObserver: (() => void) | null = null;
|
|
140
|
-
private _idxDirty = true;
|
|
141
|
-
private _byId = new Map<string, TreeEntry>();
|
|
142
|
-
private _childrenByParent = new Map<string | null, TreeEntry[]>();
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Ensure the index is enabled, bound to the current root doc's tree
|
|
146
|
-
* map, and fresh. Returns `false` when the index is disabled or there
|
|
147
|
-
* is no tree map yet — callers then use the legacy scan path.
|
|
148
|
-
*/
|
|
149
|
-
private ensureIndex(): boolean {
|
|
150
|
-
if (!this.dm.treeIndexEnabled) return false;
|
|
151
|
-
const treeMap = this.dm.getTreeMap();
|
|
152
|
-
if (!treeMap) {
|
|
153
|
-
this.unbindIndex();
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
if (treeMap !== this._idxMap) {
|
|
157
|
-
// Root doc (space) swapped, or first use — rebind the observer.
|
|
158
|
-
this.unbindIndex();
|
|
159
|
-
const obs = () => {
|
|
160
|
-
this._idxDirty = true;
|
|
161
|
-
};
|
|
162
|
-
treeMap.observeDeep(obs);
|
|
163
|
-
this._idxMap = treeMap;
|
|
164
|
-
this._idxObserver = obs;
|
|
165
|
-
this._idxDirty = true;
|
|
166
|
-
}
|
|
167
|
-
if (this._idxDirty) this.rebuildIndex(treeMap);
|
|
168
|
-
return true;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
private unbindIndex(): void {
|
|
172
|
-
if (this._idxMap && this._idxObserver) {
|
|
173
|
-
this._idxMap.unobserveDeep(this._idxObserver);
|
|
174
|
-
}
|
|
175
|
-
this._idxMap = null;
|
|
176
|
-
this._idxObserver = null;
|
|
177
|
-
this._byId = new Map();
|
|
178
|
-
this._childrenByParent = new Map();
|
|
179
|
-
this._idxDirty = true;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
private rebuildIndex(treeMap: Y.Map<unknown>): void {
|
|
183
|
-
const root = this.dm.rootDocId;
|
|
184
|
-
const byId = new Map<string, TreeEntry>();
|
|
185
|
-
treeMap.forEach((raw: unknown, id: string) => {
|
|
186
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
187
|
-
if (typeof value !== "object" || value === null) return;
|
|
188
|
-
byId.set(id, {
|
|
189
|
-
id,
|
|
190
|
-
label: (value.label as string) || "Untitled",
|
|
191
|
-
parentId: normalizeRootId(
|
|
192
|
-
(value.parentId as string | null) ?? null,
|
|
193
|
-
root,
|
|
194
|
-
),
|
|
195
|
-
order: (value.order as number) ?? 0,
|
|
196
|
-
type: value.type as string | undefined,
|
|
197
|
-
meta: value.meta as PageMeta | undefined,
|
|
198
|
-
createdAt: value.createdAt as number | undefined,
|
|
199
|
-
updatedAt: value.updatedAt as number | undefined,
|
|
200
|
-
});
|
|
201
|
-
});
|
|
202
|
-
// Converge concurrent-move cycles: logically reparent each cycle's
|
|
203
|
-
// break node to root so the materialized adjacency is acyclic and
|
|
204
|
-
// fully reachable. Non-mutating — the CRDT is untouched; only this
|
|
205
|
-
// read-side cache is corrected (`repairTree()` persists the fix when
|
|
206
|
-
// a writer wants it). No-op for the common acyclic tree. See
|
|
207
|
-
// {@link cycleBreakIds}.
|
|
208
|
-
const breaks = cycleBreakIds(
|
|
209
|
-
byId.keys(),
|
|
210
|
-
(id) => byId.get(id)?.parentId ?? null,
|
|
211
|
-
);
|
|
212
|
-
for (const id of breaks) {
|
|
213
|
-
const e = byId.get(id);
|
|
214
|
-
if (e) e.parentId = null;
|
|
215
|
-
}
|
|
216
|
-
const childrenByParent = new Map<string | null, TreeEntry[]>();
|
|
217
|
-
for (const entry of byId.values()) {
|
|
218
|
-
let bucket = childrenByParent.get(entry.parentId);
|
|
219
|
-
if (!bucket) {
|
|
220
|
-
bucket = [];
|
|
221
|
-
childrenByParent.set(entry.parentId, bucket);
|
|
222
|
-
}
|
|
223
|
-
bucket.push(entry);
|
|
224
|
-
}
|
|
225
|
-
for (const bucket of childrenByParent.values()) bucket.sort(cmpEntry);
|
|
226
|
-
this._byId = byId;
|
|
227
|
-
this._childrenByParent = childrenByParent;
|
|
228
|
-
this._idxDirty = false;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* Release the deep observer. Optional — the observer is auto-rebound
|
|
233
|
-
* on space switch and becomes moot when the root Y.Doc is GC'd — but
|
|
234
|
-
* available for consumers that want deterministic teardown.
|
|
235
|
-
*/
|
|
236
|
-
dispose(): void {
|
|
237
|
-
this.unbindIndex();
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// ── Reading ───────────────────────────────────────────────────────────────
|
|
241
|
-
|
|
242
|
-
/** Read all tree entries as plain objects. */
|
|
243
|
-
readEntries(): TreeEntry[] {
|
|
244
|
-
const treeMap = this.dm.getTreeMap();
|
|
245
|
-
if (!treeMap) return [];
|
|
246
|
-
|
|
247
|
-
const entries: TreeEntry[] = [];
|
|
248
|
-
treeMap.forEach((raw: unknown, id: string) => {
|
|
249
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
250
|
-
if (typeof value !== "object" || value === null) return;
|
|
251
|
-
entries.push({
|
|
252
|
-
id,
|
|
253
|
-
label: (value.label as string) || "Untitled",
|
|
254
|
-
parentId: (value.parentId as string | null) ?? null,
|
|
255
|
-
order: (value.order as number) ?? 0,
|
|
256
|
-
type: value.type as string | undefined,
|
|
257
|
-
meta: value.meta as PageMeta | undefined,
|
|
258
|
-
createdAt: value.createdAt as number | undefined,
|
|
259
|
-
updatedAt: value.updatedAt as number | undefined,
|
|
260
|
-
});
|
|
261
|
-
});
|
|
262
|
-
return entries;
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
/**
|
|
266
|
-
* Like {@link readEntries} but with every entry's *stored* parentId
|
|
267
|
-
* run through {@link normalizeRootId} (parentId === rootDocId → null),
|
|
268
|
-
* so a cou-sh / orphan-rescue top-level doc (parentId === spaceRoot)
|
|
269
|
-
* resolves to top-level identically to a provider-created one
|
|
270
|
-
* (parentId: null). Without this, the raw `parentId === spaceRoot`
|
|
271
|
-
* form never matches the normalized `null` query and such docs are
|
|
272
|
-
* silently invisible cross-client. Mirrors the Rust provider's
|
|
273
|
-
* `normalized_entries`. readEntries/get keep raw values for
|
|
274
|
-
* round-trip consumers; only tree-walk reads use this.
|
|
275
|
-
*/
|
|
276
|
-
private normalizedEntries(): TreeEntry[] {
|
|
277
|
-
if (this.ensureIndex()) return Array.from(this._byId.values());
|
|
278
|
-
const root = this.dm.rootDocId;
|
|
279
|
-
const entries = this.readEntries().map((e) => ({
|
|
280
|
-
...e,
|
|
281
|
-
parentId: normalizeRootId(e.parentId, root),
|
|
282
|
-
}));
|
|
283
|
-
// Same read-side cycle break as the indexed path (see
|
|
284
|
-
// {@link rebuildIndex} / {@link cycleBreakIds}) so the legacy scan
|
|
285
|
-
// walks an acyclic, fully-reachable tree too. No-op when acyclic.
|
|
286
|
-
const byId = new Map(entries.map((e) => [e.id, e]));
|
|
287
|
-
const breaks = cycleBreakIds(
|
|
288
|
-
byId.keys(),
|
|
289
|
-
(id) => byId.get(id)?.parentId ?? null,
|
|
290
|
-
);
|
|
291
|
-
for (const id of breaks) {
|
|
292
|
-
const e = byId.get(id);
|
|
293
|
-
if (e) e.parentId = null;
|
|
294
|
-
}
|
|
295
|
-
return entries;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
/** Get immediate children of a parent (sorted by order). */
|
|
299
|
-
childrenOf(parentId: string | null): TreeEntry[] {
|
|
300
|
-
const normalized = normalizeRootId(parentId, this.dm.rootDocId);
|
|
301
|
-
if (this.ensureIndex()) {
|
|
302
|
-
// Copy: callers (descendantsOf, find, consumers) must not
|
|
303
|
-
// mutate the cached bucket.
|
|
304
|
-
const bucket = this._childrenByParent.get(normalized);
|
|
305
|
-
return bucket ? bucket.slice() : [];
|
|
306
|
-
}
|
|
307
|
-
return this.normalizedEntries()
|
|
308
|
-
.filter((e) => e.parentId === normalized)
|
|
309
|
-
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
/**
|
|
313
|
-
* Paginated immediate children — the Path-1 surface for large fan-out
|
|
314
|
-
* parents. Walks the same stable (order,id) sibling order as
|
|
315
|
-
* {@link childrenOf}; `cursor` is opaque (round-trip `nextCursor`).
|
|
316
|
-
* `limit` defaults to 100. A stale/garbage cursor restarts from the
|
|
317
|
-
* head rather than throwing. Cursor stability is exact when the index
|
|
318
|
-
* is enabled; on the legacy scan path siblings with equal `order`
|
|
319
|
-
* may shift between calls.
|
|
320
|
-
*/
|
|
321
|
-
childrenOfPage(
|
|
322
|
-
parentId: string | null,
|
|
323
|
-
opts: { limit?: number; cursor?: string | null } = {},
|
|
324
|
-
): ChildrenPage {
|
|
325
|
-
const all = this.childrenOf(parentId);
|
|
326
|
-
const limit =
|
|
327
|
-
opts.limit != null && opts.limit > 0 ? Math.floor(opts.limit) : 100;
|
|
328
|
-
let start = 0;
|
|
329
|
-
if (opts.cursor) {
|
|
330
|
-
const dec = decodeCursor(opts.cursor);
|
|
331
|
-
if (dec) {
|
|
332
|
-
const at = all.findIndex(
|
|
333
|
-
(e) => cmpKey(e.order ?? 0, e.id, dec.order, dec.id) > 0,
|
|
334
|
-
);
|
|
335
|
-
start = at < 0 ? all.length : at;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
const entries = all.slice(start, start + limit);
|
|
339
|
-
const last = entries[entries.length - 1];
|
|
340
|
-
const nextCursor =
|
|
341
|
-
last && start + limit < all.length
|
|
342
|
-
? encodeCursor(last.order ?? 0, last.id)
|
|
343
|
-
: null;
|
|
344
|
-
return { entries, nextCursor };
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
/** Get all descendants recursively. */
|
|
348
|
-
descendantsOf(parentId: string | null): TreeEntry[] {
|
|
349
|
-
const normalized = normalizeRootId(parentId, this.dm.rootDocId);
|
|
350
|
-
if (this.ensureIndex()) {
|
|
351
|
-
const result: TreeEntry[] = [];
|
|
352
|
-
const visited = new Set<string>();
|
|
353
|
-
const walk = (pid: string | null) => {
|
|
354
|
-
if (pid !== null) {
|
|
355
|
-
if (visited.has(pid)) return;
|
|
356
|
-
visited.add(pid);
|
|
357
|
-
}
|
|
358
|
-
const bucket = this._childrenByParent.get(pid);
|
|
359
|
-
if (!bucket) return;
|
|
360
|
-
for (const child of bucket) {
|
|
361
|
-
result.push(child);
|
|
362
|
-
walk(child.id);
|
|
363
|
-
}
|
|
364
|
-
};
|
|
365
|
-
walk(normalized);
|
|
366
|
-
return result;
|
|
367
|
-
}
|
|
368
|
-
const entries = this.normalizedEntries();
|
|
369
|
-
const result: TreeEntry[] = [];
|
|
370
|
-
const visited = new Set<string>();
|
|
371
|
-
|
|
372
|
-
const collect = (pid: string | null) => {
|
|
373
|
-
if (pid !== null && visited.has(pid)) return;
|
|
374
|
-
if (pid !== null) visited.add(pid);
|
|
375
|
-
for (const child of entries
|
|
376
|
-
.filter((e) => e.parentId === pid)
|
|
377
|
-
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0))) {
|
|
378
|
-
result.push(child);
|
|
379
|
-
collect(child.id);
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
collect(normalized);
|
|
383
|
-
return result;
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
/** Build nested tree JSON. */
|
|
387
|
-
buildTree(rootId?: string | null, maxDepth = 3): TreeNode[] {
|
|
388
|
-
const normalized = normalizeRootId(rootId ?? null, this.dm.rootDocId);
|
|
389
|
-
if (this.ensureIndex()) {
|
|
390
|
-
return this._buildTreeIndexed(normalized, maxDepth, 0, new Set());
|
|
391
|
-
}
|
|
392
|
-
const entries = this.normalizedEntries();
|
|
393
|
-
return this._buildTree(entries, normalized, maxDepth, 0, new Set());
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
private _buildTreeIndexed(
|
|
397
|
-
rootId: string | null,
|
|
398
|
-
maxDepth: number,
|
|
399
|
-
currentDepth: number,
|
|
400
|
-
visited: Set<string>,
|
|
401
|
-
): TreeNode[] {
|
|
402
|
-
if (maxDepth >= 0 && currentDepth >= maxDepth) return [];
|
|
403
|
-
const children = this._childrenByParent.get(rootId) ?? [];
|
|
404
|
-
return children
|
|
405
|
-
.filter((e) => !visited.has(e.id))
|
|
406
|
-
.map((entry) => {
|
|
407
|
-
const next = new Set(visited);
|
|
408
|
-
next.add(entry.id);
|
|
409
|
-
return {
|
|
410
|
-
id: entry.id,
|
|
411
|
-
label: entry.label,
|
|
412
|
-
type: entry.type,
|
|
413
|
-
meta: entry.meta,
|
|
414
|
-
order: entry.order,
|
|
415
|
-
children: this._buildTreeIndexed(
|
|
416
|
-
entry.id,
|
|
417
|
-
maxDepth,
|
|
418
|
-
currentDepth + 1,
|
|
419
|
-
next,
|
|
420
|
-
),
|
|
421
|
-
};
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
private _buildTree(
|
|
426
|
-
entries: TreeEntry[],
|
|
427
|
-
rootId: string | null,
|
|
428
|
-
maxDepth: number,
|
|
429
|
-
currentDepth: number,
|
|
430
|
-
visited: Set<string>,
|
|
431
|
-
): TreeNode[] {
|
|
432
|
-
if (maxDepth >= 0 && currentDepth >= maxDepth) return [];
|
|
433
|
-
const children = entries
|
|
434
|
-
.filter((e) => e.parentId === rootId)
|
|
435
|
-
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
436
|
-
|
|
437
|
-
return children
|
|
438
|
-
.filter((e) => !visited.has(e.id))
|
|
439
|
-
.map((entry) => {
|
|
440
|
-
const next = new Set(visited);
|
|
441
|
-
next.add(entry.id);
|
|
442
|
-
return {
|
|
443
|
-
id: entry.id,
|
|
444
|
-
label: entry.label,
|
|
445
|
-
type: entry.type,
|
|
446
|
-
meta: entry.meta,
|
|
447
|
-
order: entry.order,
|
|
448
|
-
children: this._buildTree(
|
|
449
|
-
entries,
|
|
450
|
-
entry.id,
|
|
451
|
-
maxDepth,
|
|
452
|
-
currentDepth + 1,
|
|
453
|
-
next,
|
|
454
|
-
),
|
|
455
|
-
};
|
|
456
|
-
});
|
|
457
|
-
}
|
|
458
|
-
|
|
459
|
-
/**
|
|
460
|
-
* Schema-typed lookup. Returns a `TypedTreeEntry<TMap, N>` when the
|
|
461
|
-
* entry's `type` matches `expectedType`, else `null`. Pure projection
|
|
462
|
-
* over the existing untyped tree — no schema validation is performed
|
|
463
|
-
* here (the entry's data is whatever was last synced; meta correctness
|
|
464
|
-
* is the writer's responsibility, optionally enforced via
|
|
465
|
-
* `MetaManager.setSchema`).
|
|
466
|
-
*
|
|
467
|
-
* Rule 4 alignment: when the entry's type doesn't match, returns null
|
|
468
|
-
* rather than throwing — callers branch on the result.
|
|
469
|
-
*/
|
|
470
|
-
getTyped<TMap extends Record<string, unknown>, N extends keyof TMap & string>(
|
|
471
|
-
_schema: SchemaRegistryLike<TMap>,
|
|
472
|
-
expectedType: N,
|
|
473
|
-
docId: string,
|
|
474
|
-
): TypedTreeEntry<TMap, N> | null {
|
|
475
|
-
return projectTreeEntry<TMap, N>(this.get(docId), expectedType);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
/** Find a single entry by ID. */
|
|
479
|
-
get(docId: string): TreeEntry | null {
|
|
480
|
-
const treeMap = this.dm.getTreeMap();
|
|
481
|
-
if (!treeMap) return null;
|
|
482
|
-
const raw = treeMap.get(docId);
|
|
483
|
-
if (!raw) return null;
|
|
484
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
485
|
-
if (typeof value !== "object" || value === null) return null;
|
|
486
|
-
return {
|
|
487
|
-
id: docId,
|
|
488
|
-
label: (value.label as string) || "Untitled",
|
|
489
|
-
parentId: (value.parentId as string | null) ?? null,
|
|
490
|
-
order: (value.order as number) ?? 0,
|
|
491
|
-
type: value.type as string | undefined,
|
|
492
|
-
meta: value.meta as PageMeta | undefined,
|
|
493
|
-
createdAt: value.createdAt as number | undefined,
|
|
494
|
-
updatedAt: value.updatedAt as number | undefined,
|
|
495
|
-
};
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/** Search by label (case-insensitive substring match). */
|
|
499
|
-
find(query: string, rootId?: string | null): TreeSearchResult[] {
|
|
500
|
-
const entries = this.normalizedEntries();
|
|
501
|
-
const lowerQuery = query.toLowerCase();
|
|
502
|
-
|
|
503
|
-
const normalized = normalizeRootId(rootId ?? null, this.dm.rootDocId);
|
|
504
|
-
const searchSet = normalized ? this.descendantsOf(normalized) : entries;
|
|
505
|
-
|
|
506
|
-
const matches = searchSet.filter((e) =>
|
|
507
|
-
e.label.toLowerCase().includes(lowerQuery),
|
|
508
|
-
);
|
|
509
|
-
|
|
510
|
-
const byId = new Map(entries.map((e) => [e.id, e]));
|
|
511
|
-
return matches.map((entry) => {
|
|
512
|
-
const path: string[] = [];
|
|
513
|
-
let current = entry.parentId;
|
|
514
|
-
const visited = new Set<string>();
|
|
515
|
-
while (current && !visited.has(current)) {
|
|
516
|
-
visited.add(current);
|
|
517
|
-
const parent = byId.get(current);
|
|
518
|
-
if (!parent) break;
|
|
519
|
-
path.unshift(parent.label);
|
|
520
|
-
current = parent.parentId;
|
|
521
|
-
}
|
|
522
|
-
return {
|
|
523
|
-
id: entry.id,
|
|
524
|
-
label: entry.label,
|
|
525
|
-
type: entry.type,
|
|
526
|
-
meta: entry.meta,
|
|
527
|
-
path,
|
|
528
|
-
};
|
|
529
|
-
});
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
// ── Writing ──────────────────────────────────────────────────────────────
|
|
533
|
-
|
|
534
|
-
/** Create a new document in the tree. Returns the new entry. */
|
|
535
|
-
create(opts: {
|
|
536
|
-
parentId?: string | null;
|
|
537
|
-
label: string;
|
|
538
|
-
type?: string;
|
|
539
|
-
meta?: Partial<PageMeta>;
|
|
540
|
-
}): TreeEntry {
|
|
541
|
-
const treeMap = this.dm.getTreeMap();
|
|
542
|
-
const rootDoc = this.dm.rootDocument;
|
|
543
|
-
if (!treeMap || !rootDoc) {
|
|
544
|
-
throw new Error("Not connected");
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
const id = crypto.randomUUID();
|
|
548
|
-
const normalizedParent = normalizeRootId(
|
|
549
|
-
opts.parentId ?? null,
|
|
550
|
-
this.dm.rootDocId,
|
|
551
|
-
);
|
|
552
|
-
const now = Date.now();
|
|
553
|
-
|
|
554
|
-
rootDoc.transact(() => {
|
|
555
|
-
treeMap.set(
|
|
556
|
-
id,
|
|
557
|
-
makeEntryMap({
|
|
558
|
-
label: opts.label,
|
|
559
|
-
parentId: normalizedParent,
|
|
560
|
-
order: now,
|
|
561
|
-
type: opts.type,
|
|
562
|
-
meta: opts.meta as PageMeta,
|
|
563
|
-
createdAt: now,
|
|
564
|
-
updatedAt: now,
|
|
565
|
-
}),
|
|
566
|
-
);
|
|
567
|
-
});
|
|
568
|
-
|
|
569
|
-
return {
|
|
570
|
-
id,
|
|
571
|
-
label: opts.label,
|
|
572
|
-
parentId: normalizedParent,
|
|
573
|
-
order: now,
|
|
574
|
-
type: opts.type,
|
|
575
|
-
meta: opts.meta as PageMeta | undefined,
|
|
576
|
-
createdAt: now,
|
|
577
|
-
updatedAt: now,
|
|
578
|
-
};
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
/**
|
|
582
|
-
* Create a document and auto-apply renderer defaults from the page type's
|
|
583
|
-
* `defaultMetaFields` (toggle fields with `default` values).
|
|
584
|
-
*/
|
|
585
|
-
createWithTypeDefaults(opts: {
|
|
586
|
-
parentId?: string | null;
|
|
587
|
-
label: string;
|
|
588
|
-
type: string;
|
|
589
|
-
extraMeta?: Partial<PageMeta>;
|
|
590
|
-
}): TreeEntry {
|
|
591
|
-
const typeInfo = resolvePageType(opts.type);
|
|
592
|
-
const autoMeta: Record<string, unknown> = {};
|
|
593
|
-
if (typeInfo?.defaultMetaFields) {
|
|
594
|
-
for (const field of typeInfo.defaultMetaFields) {
|
|
595
|
-
if (
|
|
596
|
-
field.type === "toggle" &&
|
|
597
|
-
field.key &&
|
|
598
|
-
field.default !== undefined
|
|
599
|
-
) {
|
|
600
|
-
autoMeta[field.key] = field.default;
|
|
601
|
-
}
|
|
602
|
-
}
|
|
603
|
-
}
|
|
604
|
-
const mergedMeta =
|
|
605
|
-
Object.keys(autoMeta).length > 0 || opts.extraMeta
|
|
606
|
-
? { ...autoMeta, ...(opts.extraMeta ?? {}) }
|
|
607
|
-
: undefined;
|
|
608
|
-
return this.create({
|
|
609
|
-
parentId: opts.parentId,
|
|
610
|
-
label: opts.label,
|
|
611
|
-
type: opts.type,
|
|
612
|
-
meta: mergedMeta,
|
|
613
|
-
});
|
|
614
|
-
}
|
|
615
|
-
|
|
616
|
-
/** Rename a document. */
|
|
617
|
-
rename(docId: string, label: string): void {
|
|
618
|
-
const treeMap = this.dm.getTreeMap();
|
|
619
|
-
const rootDoc = this.dm.rootDocument;
|
|
620
|
-
if (!treeMap || !rootDoc) throw new Error("Not connected");
|
|
621
|
-
|
|
622
|
-
const raw = treeMap.get(docId);
|
|
623
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
624
|
-
|
|
625
|
-
rootDoc.transact(() => {
|
|
626
|
-
patchEntry(treeMap, docId, { label, updatedAt: Date.now() });
|
|
627
|
-
});
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
/** Move a document to a new parent. */
|
|
631
|
-
move(docId: string, newParentId?: string | null, order?: number): void {
|
|
632
|
-
const treeMap = this.dm.getTreeMap();
|
|
633
|
-
const rootDoc = this.dm.rootDocument;
|
|
634
|
-
if (!treeMap || !rootDoc) throw new Error("Not connected");
|
|
635
|
-
|
|
636
|
-
const raw = treeMap.get(docId);
|
|
637
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
638
|
-
|
|
639
|
-
rootDoc.transact(() => {
|
|
640
|
-
patchEntry(treeMap, docId, {
|
|
641
|
-
parentId: normalizeRootId(newParentId ?? null, this.dm.rootDocId),
|
|
642
|
-
order: order ?? Date.now(),
|
|
643
|
-
updatedAt: Date.now(),
|
|
644
|
-
});
|
|
645
|
-
});
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
/**
|
|
649
|
-
* Heal converged move-cycles in the stored tree itself.
|
|
650
|
-
*
|
|
651
|
-
* The read paths already present a cycle-free, fully-reachable tree
|
|
652
|
-
* without writing (see {@link cycleBreakIds}); this *persists* that repair
|
|
653
|
-
* into the CRDT so the stored structure is acyclic too — useful as a
|
|
654
|
-
* one-shot on load, or behind a "repair tree" action. Deterministic and
|
|
655
|
-
* convergent: every replica computes the identical break set, so even
|
|
656
|
-
* concurrent calls from multiple peers write the same `parentId: null` and
|
|
657
|
-
* converge. Returns the number of nodes reparented to root (0 when already
|
|
658
|
-
* acyclic — the common case, where no transaction is opened). Optional —
|
|
659
|
-
* the always-on read guard already makes the corruption invisible.
|
|
660
|
-
*/
|
|
661
|
-
repairTree(): number {
|
|
662
|
-
const treeMap = this.dm.getTreeMap();
|
|
663
|
-
const rootDoc = this.dm.rootDocument;
|
|
664
|
-
if (!treeMap || !rootDoc) throw new Error("Not connected");
|
|
665
|
-
const root = this.dm.rootDocId;
|
|
666
|
-
const parentById = new Map<string, string | null>();
|
|
667
|
-
treeMap.forEach((raw: unknown, id: string) => {
|
|
668
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
669
|
-
if (typeof value !== "object" || value === null) return;
|
|
670
|
-
parentById.set(
|
|
671
|
-
id,
|
|
672
|
-
normalizeRootId((value.parentId as string | null) ?? null, root),
|
|
673
|
-
);
|
|
674
|
-
});
|
|
675
|
-
const breaks = cycleBreakIds(
|
|
676
|
-
parentById.keys(),
|
|
677
|
-
(id) => parentById.get(id) ?? null,
|
|
678
|
-
);
|
|
679
|
-
if (!breaks.size) return 0;
|
|
680
|
-
rootDoc.transact(() => {
|
|
681
|
-
for (const id of breaks) {
|
|
682
|
-
if (treeMap.get(id)) {
|
|
683
|
-
patchEntry(treeMap, id, {
|
|
684
|
-
parentId: null,
|
|
685
|
-
updatedAt: Date.now(),
|
|
686
|
-
});
|
|
687
|
-
}
|
|
688
|
-
}
|
|
689
|
-
});
|
|
690
|
-
return breaks.size;
|
|
691
|
-
}
|
|
692
|
-
|
|
693
|
-
/** Change the page type of a document. */
|
|
694
|
-
changeType(docId: string, type: string): void {
|
|
695
|
-
const treeMap = this.dm.getTreeMap();
|
|
696
|
-
const rootDoc = this.dm.rootDocument;
|
|
697
|
-
if (!treeMap || !rootDoc) throw new Error("Not connected");
|
|
698
|
-
|
|
699
|
-
const raw = treeMap.get(docId);
|
|
700
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
701
|
-
|
|
702
|
-
rootDoc.transact(() => {
|
|
703
|
-
patchEntry(treeMap, docId, { type, updatedAt: Date.now() });
|
|
704
|
-
});
|
|
705
|
-
}
|
|
706
|
-
|
|
707
|
-
/**
|
|
708
|
-
* Soft-delete a document and descendants (move to trash).
|
|
709
|
-
* @returns count of deleted documents
|
|
710
|
-
*/
|
|
711
|
-
delete(docId: string): number {
|
|
712
|
-
const treeMap = this.dm.getTreeMap();
|
|
713
|
-
const trashMap = this.dm.getTrashMap();
|
|
714
|
-
const rootDoc = this.dm.rootDocument;
|
|
715
|
-
if (!treeMap || !trashMap || !rootDoc) {
|
|
716
|
-
throw new Error("Not connected");
|
|
717
|
-
}
|
|
718
|
-
|
|
719
|
-
const now = Date.now();
|
|
720
|
-
let deletedCount = 0;
|
|
721
|
-
// Compute the descendant set and trash it inside ONE synchronous
|
|
722
|
-
// Yjs transaction. transact() callbacks are atomic — no local or
|
|
723
|
-
// remote update interleaves — so a child concurrently added under
|
|
724
|
-
// this subtree cannot be stranded with a trashed parent the way a
|
|
725
|
-
// read-then-separate-write delete allowed.
|
|
726
|
-
rootDoc.transact(() => {
|
|
727
|
-
const entries = this.readEntries();
|
|
728
|
-
const toDelete = [docId, ...this._descendantIds(entries, docId)];
|
|
729
|
-
deletedCount = toDelete.length;
|
|
730
|
-
for (const nid of toDelete) {
|
|
731
|
-
const raw = treeMap.get(nid);
|
|
732
|
-
if (!raw) continue;
|
|
733
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
734
|
-
trashMap.set(nid, {
|
|
735
|
-
label: entry.label || "Untitled",
|
|
736
|
-
parentId: entry.parentId ?? null,
|
|
737
|
-
order: entry.order ?? 0,
|
|
738
|
-
type: entry.type,
|
|
739
|
-
meta: entry.meta,
|
|
740
|
-
deletedAt: now,
|
|
741
|
-
});
|
|
742
|
-
treeMap.delete(nid);
|
|
743
|
-
}
|
|
744
|
-
});
|
|
745
|
-
|
|
746
|
-
return deletedCount;
|
|
747
|
-
}
|
|
748
|
-
|
|
749
|
-
/** Duplicate a document (shallow clone). Returns the new entry. */
|
|
750
|
-
duplicate(docId: string): TreeEntry {
|
|
751
|
-
const treeMap = this.dm.getTreeMap();
|
|
752
|
-
if (!treeMap) throw new Error("Not connected");
|
|
753
|
-
|
|
754
|
-
const raw = treeMap.get(docId);
|
|
755
|
-
if (!raw) throw new Error(`Document ${docId} not found`);
|
|
756
|
-
|
|
757
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
758
|
-
const newId = crypto.randomUUID();
|
|
759
|
-
const now = Date.now();
|
|
760
|
-
const newLabel = ((entry.label as string) || "Untitled") + " (copy)";
|
|
761
|
-
treeMap.set(
|
|
762
|
-
newId,
|
|
763
|
-
makeEntryMap({
|
|
764
|
-
...entry,
|
|
765
|
-
label: newLabel,
|
|
766
|
-
order: now,
|
|
767
|
-
createdAt: now,
|
|
768
|
-
updatedAt: now,
|
|
769
|
-
}),
|
|
770
|
-
);
|
|
771
|
-
|
|
772
|
-
return {
|
|
773
|
-
id: newId,
|
|
774
|
-
label: newLabel,
|
|
775
|
-
parentId: (entry.parentId as string | null) ?? null,
|
|
776
|
-
order: now,
|
|
777
|
-
type: entry.type as string | undefined,
|
|
778
|
-
meta: entry.meta as PageMeta | undefined,
|
|
779
|
-
createdAt: now,
|
|
780
|
-
updatedAt: now,
|
|
781
|
-
};
|
|
782
|
-
}
|
|
783
|
-
|
|
784
|
-
/** Restore a document from trash back into the tree. */
|
|
785
|
-
restore(docId: string): void {
|
|
786
|
-
const treeMap = this.dm.getTreeMap();
|
|
787
|
-
const trashMap = this.dm.getTrashMap();
|
|
788
|
-
const rootDoc = this.dm.rootDocument;
|
|
789
|
-
if (!treeMap || !trashMap || !rootDoc) {
|
|
790
|
-
throw new Error("Not connected");
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
if (!trashMap.get(docId)) {
|
|
794
|
-
throw new Error(`Document ${docId} not found in trash`);
|
|
795
|
-
}
|
|
796
|
-
|
|
797
|
-
const now = Date.now();
|
|
798
|
-
// Restore docId AND its whole trashed subtree. delete() trashes
|
|
799
|
-
// the subtree as independent entries; restoring only the named id
|
|
800
|
-
// stranded every descendant in trash forever (audit RISK 4). The
|
|
801
|
-
// trash snapshot + descendant walk happen INSIDE the transaction,
|
|
802
|
-
// the same atomicity discipline delete() uses.
|
|
803
|
-
rootDoc.transact(() => {
|
|
804
|
-
const trashed = new Map<string, Record<string, unknown>>();
|
|
805
|
-
trashMap.forEach((raw: unknown, id: string) => {
|
|
806
|
-
const v = toPlain(raw) as Record<string, unknown>;
|
|
807
|
-
if (typeof v === "object" && v !== null) trashed.set(id, v);
|
|
808
|
-
});
|
|
809
|
-
|
|
810
|
-
const toRestore: string[] = [];
|
|
811
|
-
const visited = new Set<string>();
|
|
812
|
-
const collect = (id: string) => {
|
|
813
|
-
if (visited.has(id)) return;
|
|
814
|
-
visited.add(id);
|
|
815
|
-
if (!trashed.has(id)) return;
|
|
816
|
-
toRestore.push(id);
|
|
817
|
-
for (const [cid, v] of trashed) {
|
|
818
|
-
if ((v.parentId ?? null) === id) collect(cid);
|
|
819
|
-
}
|
|
820
|
-
};
|
|
821
|
-
collect(docId);
|
|
822
|
-
|
|
823
|
-
for (const id of toRestore) {
|
|
824
|
-
const entry = trashed.get(id) as Record<string, unknown>;
|
|
825
|
-
treeMap.set(
|
|
826
|
-
id,
|
|
827
|
-
makeEntryMap({
|
|
828
|
-
label: entry.label || "Untitled",
|
|
829
|
-
parentId: entry.parentId ?? null,
|
|
830
|
-
order: entry.order ?? now,
|
|
831
|
-
type: entry.type,
|
|
832
|
-
meta: entry.meta,
|
|
833
|
-
createdAt: entry.createdAt ?? now,
|
|
834
|
-
updatedAt: now,
|
|
835
|
-
}),
|
|
836
|
-
);
|
|
837
|
-
trashMap.delete(id);
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
/** List trashed documents. */
|
|
843
|
-
listTrash(): TreeEntry[] {
|
|
844
|
-
const trashMap = this.dm.getTrashMap();
|
|
845
|
-
if (!trashMap) return [];
|
|
846
|
-
|
|
847
|
-
const entries: TreeEntry[] = [];
|
|
848
|
-
trashMap.forEach((raw: unknown, id: string) => {
|
|
849
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
850
|
-
if (typeof value !== "object" || value === null) return;
|
|
851
|
-
entries.push({
|
|
852
|
-
id,
|
|
853
|
-
label: (value.label as string) || "Untitled",
|
|
854
|
-
parentId: (value.parentId as string | null) ?? null,
|
|
855
|
-
order: (value.order as number) ?? 0,
|
|
856
|
-
type: value.type as string | undefined,
|
|
857
|
-
meta: value.meta as PageMeta | undefined,
|
|
858
|
-
});
|
|
859
|
-
});
|
|
860
|
-
return entries;
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
/** Get enabled plugin names from space-plugins map. */
|
|
864
|
-
getEnabledPlugins(): string[] {
|
|
865
|
-
const pluginsMap = this.dm.getPluginsMap();
|
|
866
|
-
if (!pluginsMap) return [];
|
|
867
|
-
|
|
868
|
-
const plugins: string[] = [];
|
|
869
|
-
pluginsMap.forEach((raw: unknown, key: string) => {
|
|
870
|
-
const value = toPlain(raw) as Record<string, unknown>;
|
|
871
|
-
if (value && value.enabled) {
|
|
872
|
-
plugins.push(key);
|
|
873
|
-
}
|
|
874
|
-
});
|
|
875
|
-
return plugins;
|
|
876
|
-
}
|
|
877
|
-
|
|
878
|
-
// ── Internal helpers ──────────────────────────────────────────────────────
|
|
879
|
-
|
|
880
|
-
private _descendantIds(entries: TreeEntry[], parentId: string): string[] {
|
|
881
|
-
const result: string[] = [];
|
|
882
|
-
const visited = new Set<string>();
|
|
883
|
-
const collect = (pid: string) => {
|
|
884
|
-
if (visited.has(pid)) return;
|
|
885
|
-
visited.add(pid);
|
|
886
|
-
for (const child of entries.filter((e) => e.parentId === pid)) {
|
|
887
|
-
result.push(child.id);
|
|
888
|
-
collect(child.id);
|
|
889
|
-
}
|
|
890
|
-
};
|
|
891
|
-
collect(parentId);
|
|
892
|
-
return result;
|
|
893
|
-
}
|
|
894
|
-
}
|