@@ -81,6 +81,7 @@ import ConvertToDialog from "./ConvertToDialog";
8181import { createArrowShapeMigrations } from "./DiscourseRelationShape/discourseRelationMigrations" ;
8282import ToastListener , { dispatchToastEvent } from "./ToastListener" ;
8383import sendErrorEmail from "~/utils/sendErrorEmail" ;
84+ import { TLDRAW_DATA_ATTRIBUTE } from "./tldrawStyles" ;
8485
8586declare global {
8687 interface Window {
@@ -467,8 +468,7 @@ const TldrawCanvas = ({ title }: { title: string }) => {
467468
468469 return (
469470 < div
470- className = { `z-10 h-full w-full overflow-hidden rounded-md border border-gray-300 bg-white ${ maximized ? "absolute inset-0" : "relative" } ` }
471- id = { `roamjs-tldraw-canvas-container` }
471+ className = { `roamjs-tldraw-canvas-container z-10 h-full w-full overflow-hidden rounded-md border border-gray-300 bg-white ${ maximized ? "absolute inset-0" : "relative" } ` }
472472 ref = { containerRef }
473473 tabIndex = { - 1 }
474474 >
@@ -845,31 +845,91 @@ const InsideEditorAndUiContext = ({
845845 return < CustomContextMenu extensionAPI = { extensionAPI } allNodes = { allNodes } /> ;
846846} ;
847847
848- export const renderTldrawCanvas = ( {
848+ const renderTldrawCanvasHelper = ( {
849849 title,
850850 onloadArgs,
851+ h1,
852+ rootSelector,
853+ minHeight,
854+ height,
851855} : {
852856 title : string ;
853857 onloadArgs : OnloadArgs ;
858+ h1 : HTMLHeadingElement ;
859+ rootSelector : string ;
860+ minHeight : string ;
861+ height : string ;
854862} ) => {
855- const children = document . querySelector < HTMLDivElement > (
856- ".roam-article .rm-block-children" ,
863+ // Find the root element using the H1 context to avoid scope issues
864+ const rootElement = h1 . closest ( rootSelector ) as HTMLDivElement ;
865+ if ( ! rootElement ) return ( ) => { } ;
866+
867+ if ( rootElement . hasAttribute ( TLDRAW_DATA_ATTRIBUTE ) ) return ( ) => { } ;
868+
869+ const blockChildrenContainer =
870+ rootElement . querySelector < HTMLDivElement > ( ".rm-block-children" ) ;
871+ if ( ! blockChildrenContainer ) return ( ) => { } ;
872+
873+ rootElement . setAttribute ( TLDRAW_DATA_ATTRIBUTE , "true" ) ;
874+
875+ const canvasWrapperEl = document . createElement ( "div" ) ;
876+ canvasWrapperEl . style . minHeight = minHeight ;
877+ canvasWrapperEl . style . height = height ;
878+
879+ blockChildrenContainer . parentElement ?. insertBefore (
880+ canvasWrapperEl ,
881+ blockChildrenContainer . nextSibling ,
857882 ) ;
858- if (
859- children &&
860- children . parentElement &&
861- ! children . hasAttribute ( "data-roamjs-discourse-playground" )
862- ) {
863- children . setAttribute ( "data-roamjs-discourse-playground" , "true" ) ;
864- const parent = document . createElement ( "div" ) ;
865- children . parentElement . appendChild ( parent ) ;
866- parent . style . minHeight = "500px" ;
867- parent . style . height = "70vh" ;
868- renderWithUnmount (
869- < ExtensionApiContextProvider { ...onloadArgs } >
870- < TldrawCanvas title = { title } />
871- </ ExtensionApiContextProvider > ,
872- parent ,
873- ) ;
874- }
883+
884+ const unmount = renderWithUnmount (
885+ < ExtensionApiContextProvider { ...onloadArgs } >
886+ < TldrawCanvas title = { title } />
887+ </ ExtensionApiContextProvider > ,
888+ canvasWrapperEl ,
889+ ) ;
890+
891+ const originalUnmount = unmount ;
892+ return ( ) => {
893+ originalUnmount ( ) ;
894+ rootElement . removeAttribute ( TLDRAW_DATA_ATTRIBUTE ) ;
895+ canvasWrapperEl . remove ( ) ;
896+ } ;
897+ } ;
898+
899+ export const renderTldrawCanvas = ( {
900+ title,
901+ onloadArgs,
902+ h1,
903+ } : {
904+ title : string ;
905+ onloadArgs : OnloadArgs ;
906+ h1 : HTMLHeadingElement ;
907+ } ) => {
908+ return renderTldrawCanvasHelper ( {
909+ title,
910+ onloadArgs,
911+ h1,
912+ rootSelector : ".roam-article" ,
913+ minHeight : "500px" ,
914+ height : "70vh" ,
915+ } ) ;
916+ } ;
917+
918+ export const renderTldrawCanvasInSidebar = ( {
919+ title,
920+ onloadArgs,
921+ h1,
922+ } : {
923+ title : string ;
924+ onloadArgs : OnloadArgs ;
925+ h1 : HTMLHeadingElement ;
926+ } ) => {
927+ return renderTldrawCanvasHelper ( {
928+ title,
929+ onloadArgs,
930+ h1,
931+ rootSelector : ".rm-sidebar-outline" ,
932+ minHeight : "400px" ,
933+ height : "60vh" ,
934+ } ) ;
875935} ;
0 commit comments