Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions apps/roam/src/components/DiscourseNodeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const NodeMenu = ({
setTimeout(() => {
const currentText = textarea.value;
const cursorPos = textarea.selectionStart;
const textToInsert = `#${tag} `;
const textToInsert = `#${tag.replace(/^#/, "")} `;

const newText = `${currentText.substring(
0,
Expand Down Expand Up @@ -249,9 +249,13 @@ const NodeMenu = ({
<MenuItem
key={item.text}
data-node={item.type}
data-tag={item.tag}
data-tag={item.tag?.replace(/^#/, "")}
text={
showNodeTypes ? item.text : item.tag ? `#${item.tag}` : ""
showNodeTypes
? item.text
: item.tag
? `#${item.tag.replace(/^#/, "")}`
: ""
}
active={i === activeIndex}
onMouseEnter={() => setActiveIndex(i)}
Expand Down
2 changes: 1 addition & 1 deletion apps/roam/src/components/settings/NodeConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ const NodeConfig = ({
onChange={handleTagChange}
onBlur={handleTagBlur}
error={tagError}
placeholder={`${node.text}`}
placeholder={`#${node.text.toLowerCase()}`}
/>
</div>
}
Expand Down
14 changes: 13 additions & 1 deletion apps/roam/src/utils/initializeObserversAndListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
findBlockElementFromSelection,
} from "~/utils/renderTextSelectionPopup";
import { renderNodeTagPopupButton } from "./renderNodeTagPopup";
import { formatHexColor } from "~/components/settings/DiscourseNodeCanvasSettings";

const debounce = (fn: () => void, delay = 250) => {
let timeout: number;
Expand Down Expand Up @@ -102,7 +103,18 @@ export const initObservers = async ({
className: "rm-page-ref--tag",
tag: "SPAN",
callback: (s: HTMLSpanElement) => {
renderNodeTagPopupButton(s, onloadArgs.extensionAPI);
const tag = s.getAttribute("data-tag");
if (tag) {
for (const node of getDiscourseNodes()) {
if (tag.toLowerCase() === node.tag?.toLowerCase()) {
renderNodeTagPopupButton(s, node, onloadArgs.extensionAPI);
if (node.canvasSettings?.color) {
s.style.color = formatHexColor(node.canvasSettings.color);
}
break;
}
}
}
},
});

Expand Down
29 changes: 11 additions & 18 deletions apps/roam/src/utils/renderNodeTagPopup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { renderCreateNodeDialog } from "~/components/CreateNodeDialog";
import { OnloadArgs } from "roamjs-components/types";
import getUids from "roamjs-components/dom/getUids";
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
import getDiscourseNodes from "./getDiscourseNodes";
import { type DiscourseNode } from "./getDiscourseNodes";

export const renderNodeTagPopupButton = (
parent: HTMLSpanElement,
matchedNode: DiscourseNode,
extensionAPI: OnloadArgs["extensionAPI"],
) => {
if (parent.dataset.attributeButtonRendered === "true") return;

const rect = parent.getBoundingClientRect();
parent.dataset.attributeButtonRendered = "true";
const wrapper = document.createElement("span");
wrapper.style.position = "relative";
Expand All @@ -24,26 +26,14 @@ export const renderNodeTagPopupButton = (
reactRoot.style.position = "absolute";
reactRoot.style.top = "0";
reactRoot.style.left = "0";
reactRoot.style.width = "100%";
reactRoot.style.height = "100%";
reactRoot.style.width = `${rect.width}px`;
reactRoot.style.height = `${rect.height}px`;
reactRoot.style.pointerEvents = "none";
reactRoot.style.zIndex = "10";

wrapper.appendChild(reactRoot);

const textContent = parent.textContent?.trim() || "";
const tagAttr = parent.getAttribute("data-tag") || textContent;
const tag = tagAttr.replace(/^#/, "").toLowerCase();
const discourseNodes = getDiscourseNodes();
const discourseTagSet = new Set(
discourseNodes.map((n) => n.tag?.toLowerCase()).filter(Boolean),
);
if (!discourseTagSet.has(tag)) return;

const matchedNode = discourseNodes.find((n) => n.tag?.toLowerCase() === tag);

if (!matchedNode) return;

const blockInputElement = parent.closest(".rm-block__input");
const blockUid = blockInputElement
? getUids(blockInputElement as HTMLDivElement).blockUid
Expand Down Expand Up @@ -74,8 +64,11 @@ export const renderNodeTagPopupButton = (
<span
style={{
display: "block",
width: "100%",
height: "100%",
top: "0",
left: "0",
width: `${rect.width}px`,
height: `${rect.height}px`,
position: "absolute",
pointerEvents: "auto",
}}
/>
Expand All @@ -84,7 +77,7 @@ export const renderNodeTagPopupButton = (
position={Position.TOP}
modifiers={{
offset: {
offset: "0, 10",
offset: `${rect.width / 2}px, 10`,
},
arrow: {
enabled: false,
Expand Down