@@ -13,7 +13,7 @@ import runQuery from "./runQuery";
1313import updateBlock from "roamjs-components/writes/updateBlock" ;
1414import 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
144103type 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 ;
0 commit comments