@abraca/dabra 2.90.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/ContentManager.ts
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ContentManager — read/write document content via markdown ↔ Y.js conversion.
|
|
3
|
-
*
|
|
4
|
-
* Extracted from `mcp/tools/content.ts` and `cli/commands/content.ts`.
|
|
5
|
-
*/
|
|
6
|
-
import * as Y from "yjs";
|
|
7
|
-
import type { PageMeta } from "./DocTypes.ts";
|
|
8
|
-
import { toPlain, patchEntry } from "./DocUtils.ts";
|
|
9
|
-
import { reconcileDocCover } from "./CoverReconcile.ts";
|
|
10
|
-
import {
|
|
11
|
-
yjsToMarkdown,
|
|
12
|
-
populateYDocFromMarkdown,
|
|
13
|
-
parseFrontmatter,
|
|
14
|
-
buildHeadingElement,
|
|
15
|
-
buildParagraphElement,
|
|
16
|
-
buildBulletListElement,
|
|
17
|
-
buildOrderedListElement,
|
|
18
|
-
buildTaskListElement,
|
|
19
|
-
buildCodeBlockElement,
|
|
20
|
-
buildBlockquoteElement,
|
|
21
|
-
buildHorizontalRuleElement,
|
|
22
|
-
buildBlocksFromMarkdown,
|
|
23
|
-
readBlocksFromFragment,
|
|
24
|
-
type DocumentBlock,
|
|
25
|
-
} from "./DocConverters.ts";
|
|
26
|
-
export type { DocumentBlock } from "./DocConverters.ts";
|
|
27
|
-
import type { DocumentManager } from "./DocumentManager.ts";
|
|
28
|
-
|
|
29
|
-
export interface DocumentContent {
|
|
30
|
-
label: string;
|
|
31
|
-
type?: string;
|
|
32
|
-
meta?: PageMeta;
|
|
33
|
-
title: string;
|
|
34
|
-
markdown: string;
|
|
35
|
-
children: Array<{
|
|
36
|
-
id: string;
|
|
37
|
-
label: string;
|
|
38
|
-
type?: string;
|
|
39
|
-
meta?: PageMeta;
|
|
40
|
-
}>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export class ContentManager {
|
|
44
|
-
constructor(private dm: DocumentManager) {}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Read document content as markdown.
|
|
48
|
-
*
|
|
49
|
-
* Returns the markdown body, tree-derived label/type/meta, and immediate
|
|
50
|
-
* children. `title` mirrors `label` (the tree entry's display name) — it
|
|
51
|
-
* is *not* derived from a TipTap `documentHeader`, and the markdown body
|
|
52
|
-
* does NOT include YAML frontmatter. Callers that want frontmatter-style
|
|
53
|
-
* round-tripping should serialise `meta`/`type` themselves on top of the
|
|
54
|
-
* returned markdown.
|
|
55
|
-
*/
|
|
56
|
-
async read(docId: string): Promise<DocumentContent> {
|
|
57
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
58
|
-
const fragment = provider.document.getXmlFragment("default");
|
|
59
|
-
|
|
60
|
-
// Get tree metadata + immediate children
|
|
61
|
-
const treeMap = this.dm.getTreeMap();
|
|
62
|
-
let label = "Untitled";
|
|
63
|
-
let type: string | undefined;
|
|
64
|
-
let meta: PageMeta | undefined;
|
|
65
|
-
const childrenWithOrder: Array<{
|
|
66
|
-
id: string;
|
|
67
|
-
label: string;
|
|
68
|
-
type?: string;
|
|
69
|
-
meta?: PageMeta;
|
|
70
|
-
order: number;
|
|
71
|
-
}> = [];
|
|
72
|
-
|
|
73
|
-
if (treeMap) {
|
|
74
|
-
const raw = treeMap.get(docId);
|
|
75
|
-
if (raw) {
|
|
76
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
77
|
-
label = (entry.label as string) || label;
|
|
78
|
-
type = entry.type as string | undefined;
|
|
79
|
-
meta = entry.meta as PageMeta | undefined;
|
|
80
|
-
}
|
|
81
|
-
// Collect immediate children sorted by order
|
|
82
|
-
treeMap.forEach((rawChild: unknown, id: string) => {
|
|
83
|
-
const value = toPlain(rawChild) as Record<string, unknown>;
|
|
84
|
-
if (value && value.parentId === docId) {
|
|
85
|
-
childrenWithOrder.push({
|
|
86
|
-
id,
|
|
87
|
-
label: (value.label as string) || "Untitled",
|
|
88
|
-
type: value.type as string | undefined,
|
|
89
|
-
meta: value.meta as PageMeta | undefined,
|
|
90
|
-
order: (value.order as number) ?? 0,
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
childrenWithOrder.sort((a, b) => a.order - b.order);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const children = childrenWithOrder.map(({ id, label, type, meta }) => ({
|
|
98
|
-
id,
|
|
99
|
-
label,
|
|
100
|
-
type,
|
|
101
|
-
meta,
|
|
102
|
-
}));
|
|
103
|
-
|
|
104
|
-
const { markdown } = yjsToMarkdown(fragment);
|
|
105
|
-
const title = label;
|
|
106
|
-
|
|
107
|
-
return { label, type, meta, title, markdown, children };
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Write markdown content to a document.
|
|
112
|
-
* Supports optional YAML frontmatter for title and metadata.
|
|
113
|
-
*
|
|
114
|
-
* @param mode - "replace" clears existing content first (default),
|
|
115
|
-
* "append" adds to the end.
|
|
116
|
-
*/
|
|
117
|
-
async write(
|
|
118
|
-
docId: string,
|
|
119
|
-
markdown: string,
|
|
120
|
-
mode: "replace" | "append" = "replace",
|
|
121
|
-
): Promise<void> {
|
|
122
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
123
|
-
const doc = provider.document;
|
|
124
|
-
const fragment = doc.getXmlFragment("default");
|
|
125
|
-
|
|
126
|
-
// Parse optional frontmatter
|
|
127
|
-
const { title, meta, body } = parseFrontmatter(markdown);
|
|
128
|
-
|
|
129
|
-
// Update tree metadata if frontmatter provided title or meta
|
|
130
|
-
if (title || Object.keys(meta).length > 0) {
|
|
131
|
-
const treeMap = this.dm.getTreeMap();
|
|
132
|
-
const rootDoc = this.dm.rootDocument;
|
|
133
|
-
if (treeMap && rootDoc) {
|
|
134
|
-
const entry = treeMap.get(docId);
|
|
135
|
-
if (entry) {
|
|
136
|
-
rootDoc.transact(() => {
|
|
137
|
-
const cur = toPlain(entry) as Record<
|
|
138
|
-
string,
|
|
139
|
-
unknown
|
|
140
|
-
>;
|
|
141
|
-
const patch: Record<string, unknown> = {
|
|
142
|
-
updatedAt: Date.now(),
|
|
143
|
-
};
|
|
144
|
-
if (title) patch.label = title;
|
|
145
|
-
if (Object.keys(meta).length > 0) {
|
|
146
|
-
patch.meta = {
|
|
147
|
-
...((cur.meta as Record<string, unknown>) ??
|
|
148
|
-
{}),
|
|
149
|
-
...meta,
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
patchEntry(treeMap, docId, patch);
|
|
153
|
-
});
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (mode === "replace") {
|
|
159
|
-
// Clear existing content
|
|
160
|
-
doc.transact(() => {
|
|
161
|
-
while (fragment.length > 0) {
|
|
162
|
-
fragment.delete(0);
|
|
163
|
-
}
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
// Write new content
|
|
168
|
-
const contentToWrite = body || markdown;
|
|
169
|
-
const fallbackTitle = title || "Untitled";
|
|
170
|
-
populateYDocFromMarkdown(fragment, contentToWrite, fallbackTitle);
|
|
171
|
-
|
|
172
|
-
// Reconcile the cover against the media blocks now in the body, so a
|
|
173
|
-
// write that removed (or added) the cover's image keeps the persisted
|
|
174
|
-
// cover metadata honest — the editor is no longer the only path that
|
|
175
|
-
// does this. Cheap no-op when the cover is already valid.
|
|
176
|
-
const reconcileTree = this.dm.getTreeMap();
|
|
177
|
-
if (reconcileTree) reconcileDocCover(reconcileTree, docId, fragment);
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
private async _appendElements(docId: string, els: Y.XmlElement[]): Promise<void> {
|
|
181
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
182
|
-
const doc = provider.document;
|
|
183
|
-
const fragment = doc.getXmlFragment("default");
|
|
184
|
-
doc.transact(() => {
|
|
185
|
-
fragment.insert(fragment.length, els);
|
|
186
|
-
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async appendHeading(docId: string, text: string, opts?: { level?: 1|2|3|4|5|6 }): Promise<void> {
|
|
190
|
-
return this._appendElements(docId, [buildHeadingElement(text, opts?.level)]);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async appendParagraph(docId: string, text: string): Promise<void> {
|
|
194
|
-
return this._appendElements(docId, [buildParagraphElement(text)]);
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async appendBulletList(docId: string, items: string[]): Promise<void> {
|
|
198
|
-
return this._appendElements(docId, [buildBulletListElement(items)]);
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
async appendOrderedList(docId: string, items: string[]): Promise<void> {
|
|
202
|
-
return this._appendElements(docId, [buildOrderedListElement(items)]);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
async appendTaskList(docId: string, items: Array<{ text: string; checked?: boolean }>): Promise<void> {
|
|
206
|
-
return this._appendElements(docId, [buildTaskListElement(items)]);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
async appendCodeBlock(docId: string, code: string, opts?: { language?: string }): Promise<void> {
|
|
210
|
-
return this._appendElements(docId, [buildCodeBlockElement(code, opts?.language)]);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
async appendBlockquote(docId: string, text: string): Promise<void> {
|
|
214
|
-
return this._appendElements(docId, [buildBlockquoteElement(text)]);
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
async appendHorizontalRule(docId: string): Promise<void> {
|
|
218
|
-
return this._appendElements(docId, [buildHorizontalRuleElement()]);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
async appendMarkdown(docId: string, markdown: string): Promise<void> {
|
|
222
|
-
return this._appendElements(docId, buildBlocksFromMarkdown(markdown));
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
async getBlocks(docId: string): Promise<DocumentBlock[]> {
|
|
226
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
227
|
-
return readBlocksFromFragment(provider.document.getXmlFragment("default"));
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Get the raw Y.XmlFragment for a document (the 'default' fragment
|
|
232
|
-
* that TipTap uses for document content).
|
|
233
|
-
*/
|
|
234
|
-
async getFragment(docId: string): Promise<Y.XmlFragment> {
|
|
235
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
236
|
-
return provider.document.getXmlFragment("default");
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
/**
|
|
240
|
-
* Get the raw Y.Doc for a document (synced).
|
|
241
|
-
*/
|
|
242
|
-
async getDoc(docId: string): Promise<Y.Doc> {
|
|
243
|
-
const provider = await this.dm.getChildProvider(docId);
|
|
244
|
-
return provider.document;
|
|
245
|
-
}
|
|
246
|
-
}
|
package/src/CoverReconcile.ts
DELETED
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* CoverReconcile — keep a document's cover-image metadata in sync with the
|
|
3
|
-
* image/video file blocks that actually live in its content.
|
|
4
|
-
*
|
|
5
|
-
* The cover (`meta.coverUploadId` / `coverDocId` / `coverMimeType` on the
|
|
6
|
-
* doc's own tree entry) is *persisted* metadata, not derived live — the
|
|
7
|
-
* kanban/gallery/table views read it without loading each card's content.
|
|
8
|
-
* Historically the only thing that reconciled it was cou-sh's TipTap editor
|
|
9
|
-
* (`reconcileCover` in DocRenderer.vue), so any write that did NOT go through
|
|
10
|
-
* that editor — MCP `write_document`, `@abraca/convert`, the CLI, another SDK
|
|
11
|
-
* — could leave an orphaned cover pointing at an image the body no longer
|
|
12
|
-
* contains. This is the shared, write-path-level reconciliation so every
|
|
13
|
-
* content write through the SDK keeps the cover honest.
|
|
14
|
-
*
|
|
15
|
-
* Semantics mirror cou-sh's editor reconciler exactly:
|
|
16
|
-
* - cover set + still references a present image/video → leave it (a
|
|
17
|
-
* user-chosen cover is preserved as long as its image is in the doc);
|
|
18
|
-
* - cover set + its image is gone, others remain → swap to the first;
|
|
19
|
-
* - cover set + no image/video remains → clear the three cover keys;
|
|
20
|
-
* - no cover + at least one image/video → adopt the first as the cover.
|
|
21
|
-
*/
|
|
22
|
-
import * as Y from "yjs";
|
|
23
|
-
import { toPlain, patchEntry } from "./DocUtils.ts";
|
|
24
|
-
|
|
25
|
-
export interface MediaFileBlock {
|
|
26
|
-
uploadId: string;
|
|
27
|
-
docId: string;
|
|
28
|
-
mimeType: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const EXT_MIME: Record<string, string> = {
|
|
32
|
-
png: "image/png",
|
|
33
|
-
jpg: "image/jpeg",
|
|
34
|
-
jpeg: "image/jpeg",
|
|
35
|
-
gif: "image/gif",
|
|
36
|
-
webp: "image/webp",
|
|
37
|
-
avif: "image/avif",
|
|
38
|
-
svg: "image/svg+xml",
|
|
39
|
-
bmp: "image/bmp",
|
|
40
|
-
heic: "image/heic",
|
|
41
|
-
heif: "image/heif",
|
|
42
|
-
mp4: "video/mp4",
|
|
43
|
-
webm: "video/webm",
|
|
44
|
-
mov: "video/quicktime",
|
|
45
|
-
m4v: "video/x-m4v",
|
|
46
|
-
ogv: "video/ogg",
|
|
47
|
-
mkv: "video/x-matroska",
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
function attr(node: Y.XmlElement, name: string): string | undefined {
|
|
51
|
-
const v = node.getAttribute(name);
|
|
52
|
-
return typeof v === "string" && v ? v : undefined;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Resolve a file node's mime type. A `fileBlock` may carry the mime under
|
|
57
|
-
* `mimeType` (cou-sh's TipTap/y-prosemirror node attr) or `mime` (the
|
|
58
|
-
* `@abraca/convert` markdown path), and may carry neither — in which case we
|
|
59
|
-
* infer it from the filename/src extension (the convert sidecar `src` is
|
|
60
|
-
* `.abracadabra/files/<id>-<filename>`). Returns "" when it can't be
|
|
61
|
-
* classified as image/video.
|
|
62
|
-
*/
|
|
63
|
-
function resolveMime(node: Y.XmlElement): string {
|
|
64
|
-
const explicit = attr(node, "mimeType") ?? attr(node, "mime");
|
|
65
|
-
if (explicit) return explicit;
|
|
66
|
-
const name = attr(node, "filename") ?? attr(node, "src") ?? "";
|
|
67
|
-
const ext = name.split(".").pop()?.toLowerCase() ?? "";
|
|
68
|
-
return EXT_MIME[ext] ?? "";
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Recursively collect image/video `fileBlock` nodes from a Y.XmlFragment (or
|
|
73
|
-
* element subtree), in document order. Mirrors the TipTap
|
|
74
|
-
* `doc.descendants()` walk used by cou-sh — fileBlocks may be nested inside
|
|
75
|
-
* other block nodes, so this recurses rather than scanning only top level.
|
|
76
|
-
*/
|
|
77
|
-
export function collectMediaFileBlocks(
|
|
78
|
-
root: Y.XmlFragment | Y.XmlElement,
|
|
79
|
-
): MediaFileBlock[] {
|
|
80
|
-
const out: MediaFileBlock[] = [];
|
|
81
|
-
const visit = (node: Y.AbstractType<unknown>): void => {
|
|
82
|
-
if (node instanceof Y.XmlElement && node.nodeName === "fileBlock") {
|
|
83
|
-
const uploadId = attr(node, "uploadId") ?? attr(node, "upload-id");
|
|
84
|
-
if (uploadId) {
|
|
85
|
-
const mimeType = resolveMime(node);
|
|
86
|
-
if (
|
|
87
|
-
mimeType.startsWith("image/") ||
|
|
88
|
-
mimeType.startsWith("video/")
|
|
89
|
-
) {
|
|
90
|
-
out.push({
|
|
91
|
-
uploadId,
|
|
92
|
-
docId: attr(node, "docId") ?? "",
|
|
93
|
-
mimeType,
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
if (node instanceof Y.XmlElement || node instanceof Y.XmlFragment) {
|
|
99
|
-
for (let i = 0; i < node.length; i++) {
|
|
100
|
-
visit(node.get(i) as Y.AbstractType<unknown>);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
visit(root as unknown as Y.AbstractType<unknown>);
|
|
105
|
-
return out;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Reconcile the cover metadata of `docId`'s tree entry against the media file
|
|
110
|
-
* blocks currently present in `fragment`. No-op when the entry is missing or
|
|
111
|
-
* the cover is already valid, so it is safe (and cheap) to call after every
|
|
112
|
-
* content write. The `treeMap` is the root doc-tree Y.Map; the cover lives on
|
|
113
|
-
* the doc's OWN self-entry (`treeMap.get(docId).meta`).
|
|
114
|
-
*/
|
|
115
|
-
export function reconcileDocCover(
|
|
116
|
-
treeMap: Y.Map<unknown>,
|
|
117
|
-
docId: string,
|
|
118
|
-
fragment: Y.XmlFragment | Y.XmlElement,
|
|
119
|
-
): void {
|
|
120
|
-
const raw = treeMap.get(docId);
|
|
121
|
-
if (!raw) return;
|
|
122
|
-
|
|
123
|
-
const entry = toPlain(raw) as Record<string, unknown>;
|
|
124
|
-
const meta = ((entry.meta as Record<string, unknown>) ?? {}) as Record<
|
|
125
|
-
string,
|
|
126
|
-
unknown
|
|
127
|
-
>;
|
|
128
|
-
const currentCoverId = meta.coverUploadId as string | undefined;
|
|
129
|
-
|
|
130
|
-
const media = collectMediaFileBlocks(fragment);
|
|
131
|
-
|
|
132
|
-
// Cover already references a present image/video → nothing to do.
|
|
133
|
-
if (currentCoverId && media.some((b) => b.uploadId === currentCoverId)) {
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (media.length > 0) {
|
|
138
|
-
// Adopt (no cover) or swap (stale cover) to the first media block.
|
|
139
|
-
const best = media[0]!;
|
|
140
|
-
if (
|
|
141
|
-
currentCoverId === best.uploadId &&
|
|
142
|
-
meta.coverDocId === (best.docId || docId) &&
|
|
143
|
-
meta.coverMimeType === best.mimeType
|
|
144
|
-
) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
patchEntry(treeMap, docId, {
|
|
148
|
-
meta: {
|
|
149
|
-
...meta,
|
|
150
|
-
coverUploadId: best.uploadId,
|
|
151
|
-
coverDocId: best.docId || docId,
|
|
152
|
-
coverMimeType: best.mimeType,
|
|
153
|
-
},
|
|
154
|
-
updatedAt: Date.now(),
|
|
155
|
-
});
|
|
156
|
-
return;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
// No media remains. Clear the cover keys only if one was set.
|
|
160
|
-
if (!currentCoverId && !meta.coverDocId && !meta.coverMimeType) return;
|
|
161
|
-
const {
|
|
162
|
-
coverUploadId: _cu,
|
|
163
|
-
coverDocId: _cd,
|
|
164
|
-
coverMimeType: _cm,
|
|
165
|
-
...rest
|
|
166
|
-
} = meta;
|
|
167
|
-
patchEntry(treeMap, docId, { meta: rest, updatedAt: Date.now() });
|
|
168
|
-
}
|