|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { |
| 3 | + Dialog, |
| 4 | + Classes, |
| 5 | + InputGroup, |
| 6 | + HTMLSelect, |
| 7 | + Button, |
| 8 | +} from "@blueprintjs/core"; |
| 9 | +import renderOverlay from "roamjs-components/util/renderOverlay"; |
| 10 | +import getDiscourseNodes, { DiscourseNode } from "~/utils/getDiscourseNodes"; |
| 11 | +import createDiscourseNode from "~/utils/createDiscourseNode"; |
| 12 | +import { OnloadArgs } from "roamjs-components/types"; |
| 13 | +import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid"; |
| 14 | +import updateBlock from "roamjs-components/writes/updateBlock"; |
| 15 | +import { render as renderToast } from "roamjs-components/components/Toast"; |
| 16 | +import getUids from "roamjs-components/dom/getUids"; |
| 17 | + |
| 18 | +export type CreateNodeDialogProps = { |
| 19 | + isOpen: boolean; |
| 20 | + onClose: () => void; |
| 21 | + nodeTypes: DiscourseNode[]; |
| 22 | + defaultNodeType: DiscourseNode; |
| 23 | + extensionAPI: OnloadArgs["extensionAPI"]; |
| 24 | + blockUid?: string; |
| 25 | + originalTagText: string; // e.g. "#qa" |
| 26 | + initialTitle: string; // cleaned text without tag |
| 27 | +}; |
| 28 | + |
| 29 | +const CreateNodeDialog = ({ |
| 30 | + isOpen, |
| 31 | + onClose, |
| 32 | + nodeTypes, |
| 33 | + defaultNodeType, |
| 34 | + extensionAPI, |
| 35 | + blockUid, |
| 36 | + originalTagText, |
| 37 | + initialTitle, |
| 38 | +}: CreateNodeDialogProps) => { |
| 39 | + const [title, setTitle] = useState(initialTitle); |
| 40 | + const [selectedType, setSelectedType] = |
| 41 | + useState<DiscourseNode>(defaultNodeType); |
| 42 | + const [loading, setLoading] = useState(false); |
| 43 | + const inputRef = useRef<HTMLInputElement>(null); |
| 44 | + |
| 45 | + useEffect(() => { |
| 46 | + setTimeout(() => inputRef.current?.focus(), 50); |
| 47 | + }, []); |
| 48 | + |
| 49 | + const onCreate = async () => { |
| 50 | + if (!title.trim()) return; |
| 51 | + setLoading(true); |
| 52 | + |
| 53 | + const format = ( |
| 54 | + getDiscourseNodes().find((n) => n.type === selectedType.type)?.format || |
| 55 | + "" |
| 56 | + ).trim(); |
| 57 | + |
| 58 | + let formattedTitle: string; |
| 59 | + if (!format) { |
| 60 | + formattedTitle = title.trim(); |
| 61 | + } else if (/{text}/i.test(format) || /{content}/i.test(format)) { |
| 62 | + formattedTitle = format |
| 63 | + .replace(/{text}/gi, title.trim()) |
| 64 | + .replace(/{content}/gi, title.trim()); |
| 65 | + } else { |
| 66 | + // If no placeholder, append the title after the format string |
| 67 | + formattedTitle = `${format} ${title.trim()}`.trim(); |
| 68 | + } |
| 69 | + |
| 70 | + const newPageUid = await createDiscourseNode({ |
| 71 | + text: formattedTitle, |
| 72 | + configPageUid: selectedType.type, // In DiscourseNode struct type is uid |
| 73 | + extensionAPI, |
| 74 | + }); |
| 75 | + // Replace original tag with new page reference |
| 76 | + if (blockUid) { |
| 77 | + const pageRef = `[[${formattedTitle}]]`; |
| 78 | + await updateBlock({ uid: blockUid, text: pageRef }); |
| 79 | + |
| 80 | + const newCursorPosition = pageRef.length; |
| 81 | + const windowId = |
| 82 | + window.roamAlphaAPI.ui.getFocusedBlock?.()?.["window-id"] || "main"; |
| 83 | + |
| 84 | + if (window.roamAlphaAPI.ui.setBlockFocusAndSelection) { |
| 85 | + window.roamAlphaAPI.ui.setBlockFocusAndSelection({ |
| 86 | + location: { "block-uid": blockUid, "window-id": windowId }, |
| 87 | + selection: { start: newCursorPosition }, |
| 88 | + }); |
| 89 | + } else { |
| 90 | + setTimeout(() => { |
| 91 | + const textareaElements = document.querySelectorAll("textarea"); |
| 92 | + for (const el of textareaElements) { |
| 93 | + if (getUids(el as HTMLTextAreaElement).blockUid === blockUid) { |
| 94 | + (el as HTMLTextAreaElement).focus(); |
| 95 | + (el as HTMLTextAreaElement).setSelectionRange( |
| 96 | + newCursorPosition, |
| 97 | + newCursorPosition, |
| 98 | + ); |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + }, 50); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Toast confirmation |
| 107 | + renderToast({ |
| 108 | + id: `discourse-node-created-${Date.now()}`, |
| 109 | + intent: "success", |
| 110 | + timeout: 10000, |
| 111 | + content: `Created node [[${formattedTitle}]]`, |
| 112 | + }); |
| 113 | + setLoading(false); |
| 114 | + onClose(); |
| 115 | + }; |
| 116 | + |
| 117 | + return ( |
| 118 | + <Dialog |
| 119 | + isOpen={isOpen} |
| 120 | + onClose={onClose} |
| 121 | + title="Create node" |
| 122 | + autoFocus={false} |
| 123 | + > |
| 124 | + <div className={Classes.DIALOG_BODY}> |
| 125 | + <div className="flex flex-col gap-4"> |
| 126 | + <div> |
| 127 | + <label className="mb-1 block font-bold">Title</label> |
| 128 | + <InputGroup |
| 129 | + placeholder={`This is a potential ${selectedType.text.toLowerCase()}`} |
| 130 | + value={title} |
| 131 | + onChange={(e) => setTitle(e.currentTarget.value)} |
| 132 | + inputRef={inputRef} |
| 133 | + /> |
| 134 | + </div> |
| 135 | + |
| 136 | + <div> |
| 137 | + <label className="mb-1 block font-bold">Type</label> |
| 138 | + <HTMLSelect |
| 139 | + fill |
| 140 | + value={selectedType.type} |
| 141 | + onChange={(e) => { |
| 142 | + const nt = nodeTypes.find( |
| 143 | + (n) => n.type === e.currentTarget.value, |
| 144 | + ); |
| 145 | + if (nt) setSelectedType(nt); |
| 146 | + }} |
| 147 | + > |
| 148 | + {nodeTypes.map((nt) => ( |
| 149 | + <option key={nt.type} value={nt.type}> |
| 150 | + {nt.text} |
| 151 | + </option> |
| 152 | + ))} |
| 153 | + </HTMLSelect> |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + <div className={Classes.DIALOG_FOOTER}> |
| 158 | + <div className={Classes.DIALOG_FOOTER_ACTIONS}> |
| 159 | + <Button minimal onClick={onClose} disabled={loading}> |
| 160 | + Cancel |
| 161 | + </Button> |
| 162 | + <Button |
| 163 | + intent="primary" |
| 164 | + onClick={onCreate} |
| 165 | + disabled={!title.trim() || loading} |
| 166 | + loading={loading} |
| 167 | + > |
| 168 | + Create |
| 169 | + </Button> |
| 170 | + </div> |
| 171 | + </div> |
| 172 | + </Dialog> |
| 173 | + ); |
| 174 | +}; |
| 175 | + |
| 176 | +export const renderCreateNodeDialog = ( |
| 177 | + props: Omit<CreateNodeDialogProps, "isOpen">, |
| 178 | +) => |
| 179 | + renderOverlay({ |
| 180 | + Overlay: CreateNodeDialog, |
| 181 | + props: { ...props, isOpen: true }, |
| 182 | + }); |
0 commit comments