Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion apps/roam/src/components/CreateNodeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import getDiscourseNodes, {
} from "~/utils/getDiscourseNodes";
import { getNewDiscourseNodeText } from "~/utils/formatUtils";
import MenuItemSelect from "roamjs-components/components/MenuItemSelect";
import createBlock from "roamjs-components/writes/createBlock";

export type CreateNodeDialogProps = {
onClose: () => void;
Expand Down Expand Up @@ -48,6 +49,20 @@ const CreateNodeDialog = ({
if (!title.trim()) return;
setLoading(true);

const query = `[:find ?parentUid ?order
:keys parentUid order
:in $ ?block-uid
:where
[?e :block/uid ?block-uid]
[?e :block/order ?order]
[?p :block/children ?e]
[?p :block/uid ?parentUid]
]`;

const blockData = (
await window.roamAlphaAPI.data.async.q(query, sourceBlockUid)
)?.[0] as unknown as { parentUid: string; order: number };

const formattedTitle = await getNewDiscourseNodeText({
text: title.trim(),
nodeType: selectedType.type,
Expand All @@ -71,7 +86,16 @@ const CreateNodeDialog = ({
// the correct reference. The reference format should be determined by the
// node's specification.
const pageRef = `[[${formattedTitle}]]`;
await updateBlock({ uid: sourceBlockUid, text: pageRef });
const newBlockUid = await createBlock({
node: { text: pageRef },
parentUid: blockData.parentUid,
order: blockData.order,
});

await window.roamAlphaAPI.moveBlock({
block: { uid: sourceBlockUid },
location: { "parent-uid": newBlockUid, order: 0 },
});

const newCursorPosition = pageRef.length;
const windowId =
Expand Down