|
| 1 | +import React, { useEffect, useRef, useState } from "react"; |
| 2 | +import { Dialog, Classes, InputGroup, Label, Button } from "@blueprintjs/core"; |
| 3 | +import renderOverlay from "roamjs-components/util/renderOverlay"; |
| 4 | +import createDiscourseNode from "~/utils/createDiscourseNode"; |
| 5 | +import { OnloadArgs } from "roamjs-components/types"; |
| 6 | +import updateBlock from "roamjs-components/writes/updateBlock"; |
| 7 | +import { render as renderToast } from "roamjs-components/components/Toast"; |
| 8 | +import getDiscourseNodes, { |
| 9 | + DiscourseNode, |
| 10 | + excludeDefaultNodes, |
| 11 | +} from "~/utils/getDiscourseNodes"; |
| 12 | +import { getNewDiscourseNodeText } from "~/utils/formatUtils"; |
| 13 | +import MenuItemSelect from "roamjs-components/components/MenuItemSelect"; |
| 14 | + |
| 15 | +export type CreateNodeDialogProps = { |
| 16 | + onClose: () => void; |
| 17 | + defaultNodeTypeUid: string; |
| 18 | + extensionAPI: OnloadArgs["extensionAPI"]; |
| 19 | + sourceBlockUid?: string; |
| 20 | + initialTitle: string; |
| 21 | +}; |
| 22 | + |
| 23 | +const CreateNodeDialog = ({ |
| 24 | + onClose, |
| 25 | + defaultNodeTypeUid, |
| 26 | + extensionAPI, |
| 27 | + sourceBlockUid, |
| 28 | + initialTitle, |
| 29 | +}: CreateNodeDialogProps) => { |
| 30 | + const discourseNodes = getDiscourseNodes().filter(excludeDefaultNodes); |
| 31 | + const defaultNodeType = |
| 32 | + discourseNodes.find((n) => n.type === defaultNodeTypeUid) || |
| 33 | + discourseNodes[0]; |
| 34 | + |
| 35 | + const [title, setTitle] = useState(initialTitle); |
| 36 | + const [selectedType, setSelectedType] = |
| 37 | + useState<DiscourseNode>(defaultNodeType); |
| 38 | + const [loading, setLoading] = useState(false); |
| 39 | + const inputRef = useRef<HTMLInputElement>(null); |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (inputRef.current) { |
| 43 | + inputRef.current.focus(); |
| 44 | + } |
| 45 | + }, []); |
| 46 | + |
| 47 | + const onCreate = async () => { |
| 48 | + if (!title.trim()) return; |
| 49 | + setLoading(true); |
| 50 | + |
| 51 | + const formattedTitle = await getNewDiscourseNodeText({ |
| 52 | + text: title.trim(), |
| 53 | + nodeType: selectedType.type, |
| 54 | + blockUid: sourceBlockUid, |
| 55 | + }); |
| 56 | + |
| 57 | + if (!formattedTitle) { |
| 58 | + setLoading(false); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const newPageUid = await createDiscourseNode({ |
| 63 | + text: formattedTitle, |
| 64 | + configPageUid: selectedType.type, |
| 65 | + extensionAPI, |
| 66 | + }); |
| 67 | + |
| 68 | + if (sourceBlockUid) { |
| 69 | + // TODO: This assumes the new node is always a page. If the specification |
| 70 | + // defines it as a block (e.g., "is in page with title"), this will not create |
| 71 | + // the correct reference. The reference format should be determined by the |
| 72 | + // node's specification. |
| 73 | + const pageRef = `[[${formattedTitle}]]`; |
| 74 | + await updateBlock({ uid: sourceBlockUid, text: pageRef }); |
| 75 | + |
| 76 | + const newCursorPosition = pageRef.length; |
| 77 | + const windowId = |
| 78 | + window.roamAlphaAPI.ui.getFocusedBlock?.()?.["window-id"] || "main"; |
| 79 | + |
| 80 | + await window.roamAlphaAPI.ui.setBlockFocusAndSelection({ |
| 81 | + location: { "block-uid": sourceBlockUid, "window-id": windowId }, |
| 82 | + selection: { start: newCursorPosition }, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + renderToast({ |
| 87 | + id: `discourse-node-created-${Date.now()}`, |
| 88 | + intent: "success", |
| 89 | + timeout: 10000, |
| 90 | + content: ( |
| 91 | + <span> |
| 92 | + Created node{" "} |
| 93 | + <a |
| 94 | + className="cursor-pointer font-medium text-blue-500 hover:underline" |
| 95 | + onClick={async (event) => { |
| 96 | + if (event.shiftKey) { |
| 97 | + await window.roamAlphaAPI.ui.rightSidebar.addWindow({ |
| 98 | + window: { |
| 99 | + "block-uid": newPageUid, |
| 100 | + type: "outline", |
| 101 | + }, |
| 102 | + }); |
| 103 | + } else { |
| 104 | + await window.roamAlphaAPI.ui.mainWindow.openPage({ |
| 105 | + page: { uid: newPageUid }, |
| 106 | + }); |
| 107 | + } |
| 108 | + }} |
| 109 | + > |
| 110 | + [[{formattedTitle}]] |
| 111 | + </a> |
| 112 | + </span> |
| 113 | + ), |
| 114 | + }); |
| 115 | + setLoading(false); |
| 116 | + onClose(); |
| 117 | + }; |
| 118 | + |
| 119 | + return ( |
| 120 | + <Dialog |
| 121 | + isOpen={true} |
| 122 | + onClose={onClose} |
| 123 | + title="Create Discourse Node" |
| 124 | + autoFocus={false} |
| 125 | + > |
| 126 | + <div className={Classes.DIALOG_BODY}> |
| 127 | + <div className="flex flex-col gap-4"> |
| 128 | + <div> |
| 129 | + <label className="mb-1 block font-bold">Title</label> |
| 130 | + <InputGroup |
| 131 | + placeholder={`This is a potential ${selectedType.text.toLowerCase()}`} |
| 132 | + value={title} |
| 133 | + onChange={(e) => setTitle(e.currentTarget.value)} |
| 134 | + inputRef={inputRef} |
| 135 | + /> |
| 136 | + </div> |
| 137 | + |
| 138 | + <Label> |
| 139 | + Type |
| 140 | + <MenuItemSelect |
| 141 | + items={discourseNodes.map((n) => n.type)} |
| 142 | + transformItem={(t) => |
| 143 | + discourseNodes.find((n) => n.type === t)?.text || t |
| 144 | + } |
| 145 | + activeItem={selectedType.type} |
| 146 | + onItemSelect={(t) => { |
| 147 | + const nt = discourseNodes.find((n) => n.type === t); |
| 148 | + if (nt) setSelectedType(nt); |
| 149 | + }} |
| 150 | + /> |
| 151 | + </Label> |
| 152 | + </div> |
| 153 | + </div> |
| 154 | + <div className={Classes.DIALOG_FOOTER}> |
| 155 | + <div className={Classes.DIALOG_FOOTER_ACTIONS}> |
| 156 | + <Button minimal onClick={onClose} disabled={loading}> |
| 157 | + Cancel |
| 158 | + </Button> |
| 159 | + <Button |
| 160 | + intent="primary" |
| 161 | + onClick={onCreate} |
| 162 | + disabled={!title.trim() || loading} |
| 163 | + loading={loading} |
| 164 | + > |
| 165 | + Create |
| 166 | + </Button> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + </Dialog> |
| 170 | + ); |
| 171 | +}; |
| 172 | + |
| 173 | +export const renderCreateNodeDialog = (props: CreateNodeDialogProps) => |
| 174 | + renderOverlay({ |
| 175 | + Overlay: CreateNodeDialog, |
| 176 | + props, |
| 177 | + }); |
0 commit comments