-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsyncCanvasNodesOnLoad.ts
More file actions
200 lines (183 loc) · 6.04 KB
/
syncCanvasNodesOnLoad.ts
File metadata and controls
200 lines (183 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import type { Editor } from "tldraw";
import type { OnloadArgs } from "roamjs-components/types";
import type { DiscourseNodeShape } from "~/components/canvas/DiscourseNodeUtil";
import calcCanvasNodeSizeAndImg, {
getCanvasNodeKeyImageUrl,
} from "./calcCanvasNodeSizeAndImg";
/**
* Query Roam for current :node/title or :block/string for each uid.
* Returns a map of uid -> title for uids that exist; uids not in the map no longer exist.
*/
const queryTitlesByUids = async (
uids: string[],
): Promise<Map<string, string>> => {
if (uids.length === 0) return new Map();
const results = (await window.roamAlphaAPI.data.async.fast.q(
`[:find ?uid (pull ?e [:node/title :block/string])
:in $ [?uid ...]
:where [?e :block/uid ?uid]]`,
uids,
/* eslint-disable-next-line @typescript-eslint/naming-convention */
)) as [string, { ":node/title"?: string; ":block/string"?: string }][];
const map = new Map<string, string>();
for (const [uid, pull] of results) {
const title = pull?.[":node/title"] ?? pull?.[":block/string"] ?? "";
map.set(uid, title);
}
return map;
};
/** Delete relation arrows and their bindings that reference this node shape, then the node shape. */
const deleteNodeShapeAndRelations = (
editor: Editor,
shape: DiscourseNodeShape,
relationIds: Set<string>,
): void => {
const bindingsToThisShape = Array.from(relationIds).flatMap((typeId) =>
editor.getBindingsToShape(shape.id, typeId),
);
const relationShapeIdsAndType = bindingsToThisShape.map((b) => ({
id: b.fromId,
type: b.type,
}));
const bindingsToDelete = relationShapeIdsAndType.flatMap(({ id, type }) =>
editor.getBindingsFromShape(id, type),
);
const relationShapeIdsToDelete = relationShapeIdsAndType.map((r) => r.id);
const bindingIdsToDelete = bindingsToDelete.map((b) => b.id);
editor
.deleteShapes(relationShapeIdsToDelete)
.deleteBindings(bindingIdsToDelete);
editor.deleteShapes([shape.id]);
};
/**
* On canvas load: sync discourse node shape titles with Roam and remove shapes whose nodes no longer exist.
* - Queries Roam for current title per uid via async.fast.q
* - Updates shapes whose title changed
* - Removes shapes whose uid no longer exists in the graph
*/
export const syncCanvasNodesOnLoad = async ({
editor,
nodeTypeIds,
relationShapeTypeIds,
extensionAPI,
}: {
editor: Editor;
nodeTypeIds: string[];
relationShapeTypeIds: string[];
extensionAPI: OnloadArgs["extensionAPI"];
}): Promise<void> => {
const { discourseNodeShapes, uidToTitle } = await syncCanvasNodeTitlesOnLoad(
editor,
nodeTypeIds,
relationShapeTypeIds,
);
await syncCanvasKeyImagesOnLoad({
editor,
discourseNodeShapes,
uidToTitle,
extensionAPI,
});
};
export const syncCanvasNodeTitlesOnLoad = async (
editor: Editor,
nodeTypeIds: string[],
relationShapeTypeIds: string[],
): Promise<{
discourseNodeShapes: DiscourseNodeShape[];
uidToTitle: Map<string, string>;
}> => {
const nodeTypeSet = new Set(nodeTypeIds);
const relationIds = new Set(relationShapeTypeIds);
const allRecords = editor.store.allRecords();
const discourseNodeShapes = allRecords.filter(
(r) =>
r.typeName === "shape" &&
nodeTypeSet.has((r as DiscourseNodeShape).type) &&
typeof (r as DiscourseNodeShape).props?.uid === "string",
) as DiscourseNodeShape[];
const uids = [...new Set(discourseNodeShapes.map((s) => s.props.uid))];
if (uids.length === 0)
return { discourseNodeShapes: [], uidToTitle: new Map() };
const uidToTitle = await queryTitlesByUids(uids);
const shapesToUpdate: { shape: DiscourseNodeShape; newTitle: string }[] = [];
const shapesToRemove: DiscourseNodeShape[] = [];
for (const shape of discourseNodeShapes) {
const uid = shape.props.uid;
const currentInRoam = uidToTitle.get(uid);
if (currentInRoam === undefined) {
shapesToRemove.push(shape);
} else if ((shape.props.title ?? "") !== (currentInRoam ?? "")) {
shapesToUpdate.push({ shape, newTitle: currentInRoam });
}
}
if (shapesToRemove.length > 0) {
for (const shape of shapesToRemove) {
deleteNodeShapeAndRelations(editor, shape, relationIds);
}
}
if (shapesToUpdate.length > 0) {
editor.updateShapes(
shapesToUpdate.map(({ shape, newTitle }) => ({
id: shape.id,
type: shape.type,
props: { title: newTitle },
})),
);
}
return { discourseNodeShapes, uidToTitle };
};
const syncCanvasKeyImagesOnLoad = async ({
editor,
discourseNodeShapes,
uidToTitle,
extensionAPI,
}: {
editor: Editor;
discourseNodeShapes: DiscourseNodeShape[];
uidToTitle: Map<string, string>;
extensionAPI: OnloadArgs["extensionAPI"];
}): Promise<void> => {
const survivingShapes = discourseNodeShapes.filter((s) =>
uidToTitle.has(s.props.uid),
);
const imageUpdates: {
id: DiscourseNodeShape["id"];
type: string;
props: { imageUrl: string; w: number; h: number };
}[] = [];
// First pass: cheaply fetch imageUrls (no image loading) to find which shapes changed.
const urlResults = await Promise.all(
survivingShapes.map(async (shape) => {
const title = uidToTitle.get(shape.props.uid) ?? shape.props.title ?? "";
const imageUrl = await getCanvasNodeKeyImageUrl({
nodeText: title,
uid: shape.props.uid,
nodeType: shape.type,
extensionAPI,
});
return { shape, title, imageUrl };
}),
);
const changedShapes = urlResults.filter(
({ shape, imageUrl }) => (shape.props.imageUrl ?? "") !== imageUrl,
);
// Second pass: load images only for shapes whose URL changed, to compute new dimensions.
await Promise.all(
changedShapes.map(async ({ shape, title }) => {
const { w, h, imageUrl } = await calcCanvasNodeSizeAndImg({
nodeText: title,
uid: shape.props.uid,
nodeType: shape.type,
extensionAPI,
});
imageUpdates.push({
id: shape.id,
type: shape.type,
props: { imageUrl, w, h },
});
}),
);
if (imageUpdates.length > 0) {
editor.updateShapes(imageUpdates);
}
};