@@ -13,12 +13,6 @@ import runQuery from "./runQuery";
1313import updateBlock from "roamjs-components/writes/updateBlock" ;
1414import posthog from "posthog-js" ;
1515
16- export const hasSmartBlockSyntax = ( node : RoamBasicNode ) : boolean => {
17- if ( node . text . includes ( "<%" ) ) return true ;
18- if ( node . children ) return node . children . some ( hasSmartBlockSyntax ) ;
19- return false ;
20- } ;
21-
2216const handleImageCreation = async ( {
2317 pageUid,
2418 discourseNodes,
@@ -82,22 +76,74 @@ const handleImageCreation = async ({
8276
8377export const createBlocksFromTemplate = async ( {
8478 templateNode,
85- parentUid,
79+ pageUid,
80+ discourseNodes,
81+ configPageUid,
82+ imageUrl,
83+ extensionAPI,
84+ text,
8685} : {
8786 templateNode : RoamBasicNode ;
88- parentUid : string ;
87+ pageUid : string ;
88+ discourseNodes : ReturnType < typeof getDiscourseNodes > ;
89+ configPageUid : string ;
90+ imageUrl ?: string ;
91+ extensionAPI ?: OnloadArgs [ "extensionAPI" ] ;
92+ text : string ;
8993} ) => {
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- ) ;
94+ const createBlocksFromTemplate = async ( ) => {
95+ await Promise . all (
96+ stripUid ( templateNode . children ) . map ( ( { uid, ...node } , order ) =>
97+ createBlock ( {
98+ node,
99+ order,
100+ parentUid : pageUid ,
101+ } ) ,
102+ ) ,
103+ ) ;
104+
105+ // Add image to page if imageUrl is provided
106+ await handleImageCreation ( {
107+ pageUid,
108+ discourseNodes,
109+ configPageUid,
110+ imageUrl,
111+ extensionAPI,
112+ text,
113+ } ) ;
114+ } ;
115+
116+ const hasSmartBlockSyntax = ( node : RoamBasicNode ) => {
117+ if ( node . text . includes ( "<%" ) ) return true ;
118+ if ( node . children ) return node . children . some ( hasSmartBlockSyntax ) ;
119+ return false ;
120+ } ;
121+ const useSmartBlocks = hasSmartBlockSyntax ( templateNode ) ;
122+
123+ if ( useSmartBlocks && ! window . roamjs ?. extension ?. smartblocks ) {
124+ renderToast ( {
125+ content :
126+ "This template requires SmartBlocks. Enable SmartBlocks in Roam Depot to use this template." ,
127+ id : "smartblocks-extension-disabled" ,
128+ intent : "warning" ,
129+ } ) ;
130+ await createBlocksFromTemplate ( ) ;
131+ } else if ( useSmartBlocks && window . roamjs ?. extension ?. smartblocks ) {
132+ window . roamjs . extension . smartblocks ?. triggerSmartblock ( {
133+ srcUid : templateNode . uid ,
134+ targetUid : pageUid ,
135+ } ) ;
136+ await handleImageCreation ( {
137+ pageUid,
138+ discourseNodes,
139+ configPageUid,
140+ imageUrl,
141+ extensionAPI,
142+ text,
143+ } ) ;
144+ } else {
145+ await createBlocksFromTemplate ( ) ;
146+ }
101147} ;
102148
103149type Props = {
@@ -188,25 +234,8 @@ const createDiscourseNode = async ({
188234 key : "template" ,
189235 } ) ;
190236
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 ( {
237+ await createBlocksFromTemplate ( {
238+ templateNode,
210239 pageUid,
211240 discourseNodes,
212241 configPageUid,
0 commit comments