Skip to content
Closed
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
104 changes: 0 additions & 104 deletions apps/roam/src/utils/initializeObserversAndListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,109 +140,6 @@ export const initObservers = async ({
},
});

const tableTagObserver = createHTMLObserver({
tag: "TD",
className: "relative",
callback: (el: HTMLElement) => {
if (!(el instanceof HTMLTableCellElement)) return;

const td = el;
if (!td.hasAttribute("data-cell-content")) return;

const content = td.dataset.cellContent || "";
if (!content.includes("#")) return;



const existingTags = td.querySelectorAll(".rm-page-ref--tag");
existingTags.forEach((tag) => {
const tagName = tag.getAttribute("data-tag");
if (
tagName &&
discourseTagSet.has(tagName.toLowerCase()) &&
tag instanceof HTMLSpanElement
) {
tag.removeAttribute("data-attribute-button-rendered");
renderNodeTagPopupButton(
tag,
discourseNodes,
onloadArgs.extensionAPI,
);
}
});

const innerContainers = [
td.querySelector("a.rm-page-ref > span"),
td.querySelector("div.rm-block__input > span"),
td.querySelector("div.rm-block__input"),
td.querySelector("div.roamjs-query-embed span"),
].filter(Boolean);

innerContainers.forEach((innerSpan) => {
if (!innerSpan) return;

const formattedTagsInside =
innerSpan.querySelectorAll(".rm-page-ref--tag");
if (formattedTagsInside.length > 0) return;

const textContent = innerSpan.textContent || "";
const unformattedDiscourseTagsFound = [];

discourseNodes.forEach((node) => {
const tag = node.tag;
if (!tag) return;

const pattern = new RegExp(`#${tag}(?![\\w-])`, "i");
if (pattern.test(textContent)) {
const alreadyFormatted = innerSpan.querySelector(
`.rm-page-ref--tag[data-tag="${tag}"]`,
);
if (!alreadyFormatted) {
unformattedDiscourseTagsFound.push(tag);
}
}
});

if (unformattedDiscourseTagsFound.length === 0) return;

const originalHtml = innerSpan.innerHTML;
const newHtml = originalHtml.replace(/#([\w-]+)/g, (match, tagName) => {
if (
innerSpan.querySelector(`.rm-page-ref--tag[data-tag="${tagName}"]`)
) {
return match;
}

if (discourseTagSet.has(tagName.toLowerCase())) {
return `<span class="rm-page-ref rm-page-ref--tag" data-tag="${tagName}">${match}</span>`;
}

return match;
});

if (originalHtml !== newHtml) {
innerSpan.innerHTML = newHtml;

setTimeout(() => {
const newTags = innerSpan.querySelectorAll(
'.rm-page-ref--tag:not([data-attribute-button-rendered="true"])',
);

newTags.forEach((tag) => {
if (tag instanceof HTMLSpanElement) {
renderNodeTagPopupButton(
tag,
discourseNodes,
onloadArgs.extensionAPI,
);
}
});
}, 50);
}
});
},
});

const pageActionListener = ((
e: CustomEvent<{
action: string;
Expand Down Expand Up @@ -455,7 +352,6 @@ export const initObservers = async ({
linkedReferencesObserver,
graphOverviewExportObserver,
nodeTagPopupButtonObserver,
tableTagObserver,
].filter((o): o is MutationObserver => !!o),
listeners: {
pageActionListener,
Expand Down
244 changes: 62 additions & 182 deletions apps/roam/src/utils/renderNodeTagPopup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useRef, useState, useEffect } from "react";
import React from "react";
import ReactDOM from "react-dom";
import { Button, Popover, Position } from "@blueprintjs/core";
import { renderCreateNodeDialog } from "~/components/CreateNodeDialog";
Expand All @@ -7,109 +7,6 @@ import getUids from "roamjs-components/dom/getUids";
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
import { DiscourseNode } from "./getDiscourseNodes";

const TableEmbedPopup: React.FC<{
parent: HTMLElement;
matchedNode: any;
extensionAPI: OnloadArgs["extensionAPI"];
blockUid?: string;
cleanedBlockText: string;
}> = ({ parent, matchedNode, extensionAPI, blockUid, cleanedBlockText }) => {
const [showPopup, setShowPopup] = useState(false);
const [popupPosition, setPopupPosition] = useState({ x: 0, y: 0 });
const timeoutRef = useRef<NodeJS.Timeout>();
const popupRef = useRef<HTMLDivElement>(null);

const handleMouseEnter = () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}

const rect = parent.getBoundingClientRect();
setPopupPosition({
x: rect.left + rect.width / 2,
y: rect.top - 8,
});
setShowPopup(true);
};

const handleMouseLeave = (e: MouseEvent) => {
const relatedTarget = e.relatedTarget as HTMLElement;
if (relatedTarget && popupRef.current?.contains(relatedTarget)) {
return;
}

timeoutRef.current = setTimeout(() => {
setShowPopup(false);
}, 150);
};

const handlePopupMouseEnter = () => {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};

const handlePopupMouseLeave = () => {
timeoutRef.current = setTimeout(() => {
setShowPopup(false);
}, 100);
};

useEffect(() => {
parent.addEventListener("mouseenter", handleMouseEnter);
parent.addEventListener("mouseleave", handleMouseLeave);
parent.style.cursor = "pointer";

return () => {
parent.removeEventListener("mouseenter", handleMouseEnter);
parent.removeEventListener("mouseleave", handleMouseLeave);
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
}
};
}, []);

if (!showPopup) return null;

return ReactDOM.createPortal(
<div
ref={popupRef}
onMouseEnter={handlePopupMouseEnter}
onMouseLeave={handlePopupMouseLeave}
style={{
position: "fixed",
left: `${popupPosition.x}px`,
top: `${popupPosition.y}px`,
transform: "translate(-50%, -100%)",
zIndex: 10000,
background: "white",
border: "1px solid rgba(16, 22, 26, 0.2)",
borderRadius: "3px",
padding: "4px",
boxShadow: "0 2px 8px rgba(0, 0, 0, 0.15)",
}}
>
<Button
minimal
outlined
small
onClick={() => {
renderCreateNodeDialog({
onClose: () => {},
defaultNodeTypeUid: matchedNode.type,
extensionAPI,
sourceBlockUid: blockUid,
initialTitle: cleanedBlockText,
});
setShowPopup(false);
}}
text={`Create ${matchedNode.text}`}
/>
</div>,
document.body,
);
};

export const renderNodeTagPopupButton = (
parent: HTMLSpanElement,
discourseNodes: DiscourseNode[],
Expand All @@ -118,8 +15,6 @@ export const renderNodeTagPopupButton = (
if (parent.dataset.attributeButtonRendered === "true") return;

parent.dataset.attributeButtonRendered = "true";
const isInTable = !!parent.closest("td.relative");
const isInEmbed = !!parent.closest(".roamjs-query-embed");

const textContent = parent.textContent?.trim() || "";
const tagAttr = parent.getAttribute("data-tag") || textContent;
Expand All @@ -136,80 +31,65 @@ export const renderNodeTagPopupButton = (
const rawBlockText = blockUid ? getTextByBlockUid(blockUid) : "";
const cleanedBlockText = rawBlockText.replace(textContent, "").trim();

if (isInTable && isInEmbed) {
const reactContainer = document.createElement("div");
reactContainer.style.display = "none";
parent.appendChild(reactContainer);

ReactDOM.render(
<TableEmbedPopup
parent={parent}
matchedNode={matchedNode}
extensionAPI={extensionAPI}
blockUid={blockUid}
cleanedBlockText={cleanedBlockText}
/>,
reactContainer,
);
} else {
const wrapper = document.createElement("span");
wrapper.style.position = "relative";
wrapper.style.display = "inline-block";
parent.parentNode?.insertBefore(wrapper, parent);
wrapper.appendChild(parent);

const reactRoot = document.createElement("span");
reactRoot.style.position = "absolute";
reactRoot.style.top = "0";
reactRoot.style.left = "0";
reactRoot.style.width = "100%";
reactRoot.style.height = "100%";
reactRoot.style.pointerEvents = "none";
reactRoot.style.zIndex = "10";
wrapper.appendChild(reactRoot);

ReactDOM.render(
<Popover
content={
<Button
minimal
outlined
onClick={() => {
renderCreateNodeDialog({
onClose: () => {},
defaultNodeTypeUid: matchedNode.type,
extensionAPI,
sourceBlockUid: blockUid,
initialTitle: cleanedBlockText,
});
}}
text={`Create ${matchedNode.text}`}
/>
}
target={
<span
style={{
display: "block",
width: "100%",
height: "100%",
pointerEvents: "auto",
}}
/>
}
interactionKind="hover"
usePortal={true}
portalClassName="dg-popover"
position={Position.TOP}
modifiers={{
offset: {
offset: "0, 10",
},
arrow: {
enabled: false,
},
}}
/>,
reactRoot,
);
}
const wrapper = document.createElement("span");
wrapper.style.position = "relative";
wrapper.style.display = "inline-block";
parent.parentNode?.insertBefore(wrapper, parent);
wrapper.appendChild(parent);

const reactRoot = document.createElement("span");
reactRoot.style.position = "absolute";
reactRoot.style.top = "0";
reactRoot.style.left = "0";
reactRoot.style.width = "100%";
reactRoot.style.height = "100%";
reactRoot.style.pointerEvents = "none";
reactRoot.style.zIndex = "10";
wrapper.appendChild(reactRoot);

const rect = parent.getBoundingClientRect();

ReactDOM.render(
<Popover
content={
<Button
minimal
outlined
onClick={() => {
renderCreateNodeDialog({
onClose: () => {},
defaultNodeTypeUid: matchedNode.type,
extensionAPI,
sourceBlockUid: blockUid,
initialTitle: cleanedBlockText,
});
}}
text={`Create ${matchedNode.text}`}
/>
}
target={
<span
style={{
display: "block",
width: `${rect.width}px`,
height: `${rect.height}px`,
pointerEvents: "auto",
}}
/>
}
interactionKind="hover"
usePortal={true}
portalClassName="dg-popover"
position={Position.TOP}
modifiers={{
offset: {
offset: "0, 10",
},
arrow: {
enabled: false,
},
}}
/>,
reactRoot,
);
};