Skip to content

Commit a7cb959

Browse files
authored
Refactor Tldraw component to improve canvas rendering logic (#368)
- Simplified the rendering logic by consolidating checks for the root element and child elements. - Updated the attribute handling for TLDRAW_DATA_ATTRIBUTE to ensure proper visibility toggling of Roam blocks. - Enhanced the styling rules in tldrawStyles to maintain consistency in block visibility when the canvas is present.
1 parent 9cf1bc4 commit a7cb959

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

apps/roam/src/components/canvas/Tldraw.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -886,22 +886,31 @@ const renderTldrawCanvasHelper = ({
886886
const rootElement = h1.closest(rootSelector) as HTMLDivElement;
887887
if (!rootElement) return () => {};
888888

889-
if (rootElement.hasAttribute(TLDRAW_DATA_ATTRIBUTE)) return () => {};
890-
891-
const blockChildrenContainer =
889+
const childFromRoot =
892890
rootElement.querySelector<HTMLDivElement>(".rm-block-children");
893-
if (!blockChildrenContainer) return () => {};
894-
895-
rootElement.setAttribute(TLDRAW_DATA_ATTRIBUTE, "true");
891+
if (!childFromRoot) return () => {};
896892

897893
const canvasWrapperEl = document.createElement("div");
898-
canvasWrapperEl.style.minHeight = minHeight;
899-
canvasWrapperEl.style.height = height;
894+
if (
895+
childFromRoot &&
896+
childFromRoot.parentElement &&
897+
!childFromRoot.hasAttribute(TLDRAW_DATA_ATTRIBUTE)
898+
) {
899+
childFromRoot.setAttribute(TLDRAW_DATA_ATTRIBUTE, "true");
900+
const parentEl = childFromRoot.parentElement;
901+
parentEl.appendChild(canvasWrapperEl);
902+
canvasWrapperEl.style.minHeight = minHeight;
903+
canvasWrapperEl.style.height = height;
904+
}
900905

901-
blockChildrenContainer.parentElement?.insertBefore(
902-
canvasWrapperEl,
903-
blockChildrenContainer.nextSibling,
904-
);
906+
// console.log(
907+
// "blockChildrenContainer.parentElement",
908+
// articleChildren.parentElement,
909+
// );
910+
// articleChildren.parentElement?.insertBefore(
911+
// canvasWrapperEl,
912+
// articleChildren.nextSibling,
913+
// );
905914

906915
const unmount = renderWithUnmount(
907916
<ExtensionApiContextProvider {...onloadArgs}>
@@ -913,7 +922,7 @@ const renderTldrawCanvasHelper = ({
913922
const originalUnmount = unmount;
914923
return () => {
915924
originalUnmount();
916-
rootElement.removeAttribute(TLDRAW_DATA_ATTRIBUTE);
925+
childFromRoot.removeAttribute(TLDRAW_DATA_ATTRIBUTE);
917926
canvasWrapperEl.remove();
918927
};
919928
};

apps/roam/src/components/canvas/tldrawStyles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
export const TLDRAW_DATA_ATTRIBUTE = "dg-tldraw-canvas-wrapper";
33
export default `
44
/* Hide Roam Blocks only when canvas is present */
5-
.roam-article[${TLDRAW_DATA_ATTRIBUTE}="true"] .rm-block-children {
5+
.roam-article .rm-block-children[${TLDRAW_DATA_ATTRIBUTE}="true"] {
66
display: none;
77
}
88
99
/* Hide Roam Blocks in sidebar when canvas is present */
10-
.rm-sidebar-outline[${TLDRAW_DATA_ATTRIBUTE}="true"] .rm-block-children {
10+
.rm-sidebar-outline .rm-block-children[${TLDRAW_DATA_ATTRIBUTE}="true"] {
1111
display: none;
1212
}
1313

0 commit comments

Comments
 (0)