Skip to content

Commit d79a736

Browse files
committed
Simplify: just extract helpers, drop the wrapper
Step back from the insertTemplateBlocks orchestrator. The PR is just two function extractions: - hasSmartBlockSyntax and createBlocksFromTemplate are exported from createDiscourseNode.ts at module scope. - handleImageCreation stays at module scope (used only by createDiscourseNode). createDiscourseNode keeps its original if/else SmartBlocks orchestration; convertPageToNodeFromCommand keeps its inline orchestration too. Both sites now call the shared helpers.
1 parent 6f37b49 commit d79a736

2 files changed

Lines changed: 76 additions & 62 deletions

File tree

apps/roam/src/utils/createDiscourseNode.ts

Lines changed: 43 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import runQuery from "./runQuery";
1313
import updateBlock from "roamjs-components/writes/updateBlock";
1414
import posthog from "posthog-js";
1515

16-
const hasSmartBlockSyntax = (node: RoamBasicNode): boolean => {
16+
export const hasSmartBlockSyntax = (node: RoamBasicNode): boolean => {
1717
if (node.text.includes("<%")) return true;
1818
if (node.children) return node.children.some(hasSmartBlockSyntax);
1919
return false;
@@ -80,65 +80,24 @@ const handleImageCreation = async ({
8080
}
8181
};
8282

83-
export const insertTemplateBlocks = async ({
84-
configPageUid,
83+
export const createBlocksFromTemplate = async ({
84+
templateNode,
8585
parentUid,
86-
discourseNodes,
87-
text,
88-
imageUrl,
89-
extensionAPI,
9086
}: {
91-
configPageUid: string;
87+
templateNode: RoamBasicNode;
9288
parentUid: string;
93-
discourseNodes?: ReturnType<typeof getDiscourseNodes>;
94-
text?: string;
95-
imageUrl?: string;
96-
extensionAPI?: OnloadArgs["extensionAPI"];
9789
}) => {
98-
const nodeTree = getFullTreeByParentUid(configPageUid).children;
99-
const templateNode = getSubTree({ tree: nodeTree, key: "template" });
100-
101-
if (templateNode.children.length) {
102-
const useSmartBlocks = hasSmartBlockSyntax(templateNode);
103-
104-
if (useSmartBlocks && window.roamjs?.extension?.smartblocks) {
105-
void window.roamjs.extension.smartblocks.triggerSmartblock({
106-
srcUid: templateNode.uid,
107-
targetUid: parentUid,
108-
});
109-
} else {
110-
if (useSmartBlocks) {
111-
renderToast({
112-
content:
113-
"This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.",
114-
id: "smartblocks-extension-disabled",
115-
intent: "warning",
116-
});
117-
}
118-
const existingChildren = getFullTreeByParentUid(parentUid).children || [];
119-
const orderOffset = existingChildren.length;
120-
await Promise.all(
121-
stripUid(templateNode.children).map((node, order) =>
122-
createBlock({
123-
node,
124-
order: orderOffset + order,
125-
parentUid,
126-
}),
127-
),
128-
);
129-
}
130-
}
131-
132-
if (discourseNodes && text !== undefined) {
133-
await handleImageCreation({
134-
pageUid: parentUid,
135-
discourseNodes,
136-
configPageUid,
137-
imageUrl,
138-
extensionAPI,
139-
text,
140-
});
141-
}
90+
const existingChildren = getFullTreeByParentUid(parentUid).children || [];
91+
const lastOrder = existingChildren.length;
92+
await Promise.all(
93+
stripUid(templateNode.children).map((node, order) =>
94+
createBlock({
95+
node,
96+
order: lastOrder + order,
97+
parentUid,
98+
}),
99+
),
100+
);
142101
};
143102

144103
type Props = {
@@ -223,13 +182,37 @@ const createDiscourseNode = async ({
223182
return pageUid;
224183
}
225184

226-
await insertTemplateBlocks({
227-
configPageUid,
228-
parentUid: pageUid,
185+
const nodeTree = getFullTreeByParentUid(configPageUid).children;
186+
const templateNode = getSubTree({
187+
tree: nodeTree,
188+
key: "template",
189+
});
190+
191+
const useSmartBlocks = hasSmartBlockSyntax(templateNode);
192+
193+
if (useSmartBlocks && !window.roamjs?.extension?.smartblocks) {
194+
renderToast({
195+
content:
196+
"This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.",
197+
id: "smartblocks-extension-disabled",
198+
intent: "warning",
199+
});
200+
await createBlocksFromTemplate({ templateNode, parentUid: pageUid });
201+
} else if (useSmartBlocks && window.roamjs?.extension?.smartblocks) {
202+
window.roamjs.extension.smartblocks?.triggerSmartblock({
203+
srcUid: templateNode.uid,
204+
targetUid: pageUid,
205+
});
206+
} else {
207+
await createBlocksFromTemplate({ templateNode, parentUid: pageUid });
208+
}
209+
await handleImageCreation({
210+
pageUid,
229211
discourseNodes,
230-
text,
212+
configPageUid,
231213
imageUrl,
232214
extensionAPI,
215+
text,
233216
});
234217
handleOpenInSidebar(pageUid);
235218
return pageUid;

apps/roam/src/utils/registerCommandPaletteCommands.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ import {
2020
onPageRefObserverChange,
2121
} from "./pageRefObserverHandlers";
2222
import findDiscourseNode from "~/utils/findDiscourseNode";
23-
import { insertTemplateBlocks } from "~/utils/createDiscourseNode";
23+
import {
24+
hasSmartBlockSyntax,
25+
createBlocksFromTemplate,
26+
} from "~/utils/createDiscourseNode";
27+
import getFullTreeByParentUid from "roamjs-components/queries/getFullTreeByParentUid";
28+
import getSubTree from "roamjs-components/util/getSubTree";
2429
import { HIDE_METADATA_KEY } from "~/data/userSettings";
2530
import posthog from "posthog-js";
2631
import { extractRef } from "roamjs-components/util";
@@ -163,7 +168,33 @@ export const convertPageToNodeFromCommand = (
163168
await window.roamAlphaAPI.data.page.update({
164169
page: { uid: pageUid, title: formattedTitle },
165170
});
166-
await insertTemplateBlocks({ configPageUid, parentUid: pageUid });
171+
172+
const nodeTree = getFullTreeByParentUid(configPageUid).children;
173+
const templateNode = getSubTree({ tree: nodeTree, key: "template" });
174+
if (templateNode.children.length > 0) {
175+
const useSmartBlocks = hasSmartBlockSyntax(templateNode);
176+
177+
if (useSmartBlocks && window.roamjs?.extension?.smartblocks) {
178+
void window.roamjs.extension.smartblocks?.triggerSmartblock({
179+
srcUid: templateNode.uid,
180+
targetUid: pageUid,
181+
});
182+
} else {
183+
if (useSmartBlocks) {
184+
renderToast({
185+
content:
186+
"This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template.",
187+
id: "smartblocks-extension-disabled",
188+
intent: "warning",
189+
});
190+
}
191+
await createBlocksFromTemplate({
192+
templateNode,
193+
parentUid: pageUid,
194+
});
195+
}
196+
}
197+
167198
return pageUid;
168199
},
169200
onSuccess: async () => {},

0 commit comments

Comments
 (0)