@@ -11,6 +11,7 @@ import getDiscourseNodes, {
1111} from "~/utils/getDiscourseNodes" ;
1212import { getNewDiscourseNodeText } from "~/utils/formatUtils" ;
1313import MenuItemSelect from "roamjs-components/components/MenuItemSelect" ;
14+ import createBlock from "roamjs-components/writes/createBlock" ;
1415
1516export type CreateNodeDialogProps = {
1617 onClose : ( ) => void ;
@@ -48,6 +49,20 @@ const CreateNodeDialog = ({
4849 if ( ! title . trim ( ) ) return ;
4950 setLoading ( true ) ;
5051
52+ const query = `[:find ?parentUid ?order
53+ :keys parentUid order
54+ :in $ ?block-uid
55+ :where
56+ [?e :block/uid ?block-uid]
57+ [?e :block/order ?order]
58+ [?p :block/children ?e]
59+ [?p :block/uid ?parentUid]
60+ ]` ;
61+
62+ const blockData = (
63+ await window . roamAlphaAPI . data . async . q ( query , sourceBlockUid )
64+ ) ?. [ 0 ] as unknown as { parentUid : string ; order : number } ;
65+
5166 const formattedTitle = await getNewDiscourseNodeText ( {
5267 text : title . trim ( ) ,
5368 nodeType : selectedType . type ,
@@ -71,7 +86,16 @@ const CreateNodeDialog = ({
7186 // the correct reference. The reference format should be determined by the
7287 // node's specification.
7388 const pageRef = `[[${ formattedTitle } ]]` ;
74- await updateBlock ( { uid : sourceBlockUid , text : pageRef } ) ;
89+ const newBlockUid = await createBlock ( {
90+ node : { text : pageRef } ,
91+ parentUid : blockData . parentUid ,
92+ order : blockData . order ,
93+ } ) ;
94+
95+ await window . roamAlphaAPI . moveBlock ( {
96+ block : { uid : sourceBlockUid } ,
97+ location : { "parent-uid" : newBlockUid , order : 0 } ,
98+ } ) ;
7599
76100 const newCursorPosition = pageRef . length ;
77101 const windowId =
0 commit comments