-
Notifications
You must be signed in to change notification settings - Fork 4
ENG-797: Handle multiple canvases loaded at the same time #928
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f057c8d
4adf7ac
447b95a
1b84834
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -140,6 +140,33 @@ export const discourseContext: DiscourseContextType = { | |||||||||||||||||||||||||
| lastActions: [], | ||||||||||||||||||||||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. needs to stay, because we use this to log on error
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We use this to send log of last actions taken by user in case of error. Should not remove this. |
||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| let activeCanvasPageUid: string | null = null; | ||||||||||||||||||||||||||
| let activeCanvasEditor: Editor | null = null; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const setActiveCanvas = ({ | ||||||||||||||||||||||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Surface 3 handled, we track the active canvas and ensure only that canvas is focused, so only this canvas responds to the clipboard events |
||||||||||||||||||||||||||
| pageUid, | ||||||||||||||||||||||||||
| editor, | ||||||||||||||||||||||||||
| }: { | ||||||||||||||||||||||||||
| pageUid: string; | ||||||||||||||||||||||||||
| editor: Editor | null; | ||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||
| if (activeCanvasPageUid === pageUid && activeCanvasEditor === editor) { | ||||||||||||||||||||||||||
| if (editor && !editor.getInstanceState().isFocused) editor.focus(); | ||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (activeCanvasEditor && activeCanvasEditor !== editor) { | ||||||||||||||||||||||||||
| activeCanvasEditor.blur(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| activeCanvasPageUid = pageUid; | ||||||||||||||||||||||||||
| activeCanvasEditor = editor; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (editor && !editor.getInstanceState().isFocused) { | ||||||||||||||||||||||||||
| editor.focus(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| export const DEFAULT_WIDTH = 160; | ||||||||||||||||||||||||||
| export const DEFAULT_HEIGHT = 64; | ||||||||||||||||||||||||||
| export const MAX_WIDTH = "400px"; | ||||||||||||||||||||||||||
|
|
@@ -725,6 +752,17 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
| inSidebar: !!containerRef.current?.closest(".rm-sidebar-outline"), | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| }, [pageUid]); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||
| return () => { | ||||||||||||||||||||||||||
| const editor = appRef.current; | ||||||||||||||||||||||||||
| if (activeCanvasPageUid === pageUid && activeCanvasEditor === editor) { | ||||||||||||||||||||||||||
| activeCanvasEditor?.blur(); | ||||||||||||||||||||||||||
| activeCanvasPageUid = null; | ||||||||||||||||||||||||||
| activeCanvasEditor = null; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| }, [pageUid]); | ||||||||||||||||||||||||||
sid597 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| const { store, needsUpgrade, performUpgrade, error, isLoading } = | ||||||||||||||||||||||||||
| useStoreAdapter(storeAdapterArgs); | ||||||||||||||||||||||||||
| const migratedCloudStoreRef = useRef<string | null>(null); | ||||||||||||||||||||||||||
|
|
@@ -788,10 +826,14 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
| uid?: string; | ||||||||||||||||||||||||||
| val?: string; | ||||||||||||||||||||||||||
| shapeId?: TLShapeId; | ||||||||||||||||||||||||||
| targetCanvasPageUid?: string; | ||||||||||||||||||||||||||
| onRefresh: () => void; | ||||||||||||||||||||||||||
| }>, | ||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||
| if (!/canvas/i.test(e.detail.action)) return; | ||||||||||||||||||||||||||
| const targetCanvasPageUid = | ||||||||||||||||||||||||||
| e.detail.targetCanvasPageUid ?? activeCanvasPageUid; | ||||||||||||||||||||||||||
| if (targetCanvasPageUid !== pageUid) return; | ||||||||||||||||||||||||||
|
Comment on lines
+834
to
+836
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actions will be lost when This can happen when:
Fix by handling the null case explicitly: const targetCanvasPageUid = e.detail.targetCanvasPageUid;
if (targetCanvasPageUid !== undefined && targetCanvasPageUid !== pageUid) return;
if (targetCanvasPageUid === undefined && activeCanvasPageUid !== null && activeCanvasPageUid !== pageUid) return;
Suggested change
Spotted by Graphite
Comment on lines
+834
to
+836
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Canvas actions silently dropped when active canvas unmounts while another canvas remains open When two canvases are open (e.g., main window + sidebar) and the active one unmounts (user navigates away from the main page), the cleanup at
Suggested change
Was this helpful? React with 👍 or 👎 to provide feedback. |
||||||||||||||||||||||||||
| const app = appRef.current; | ||||||||||||||||||||||||||
| if (!app) return; | ||||||||||||||||||||||||||
| const { x, y } = app.getViewportScreenCenter(); | ||||||||||||||||||||||||||
|
|
@@ -829,7 +871,7 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
| actionListener, | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||
| }, [appRef, allNodes]); | ||||||||||||||||||||||||||
| }, [appRef, allNodes, pageUid]); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| // Catch a custom event we used patch-package to add | ||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||
|
|
@@ -917,6 +959,10 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
| tabIndex={-1} | ||||||||||||||||||||||||||
| onDragOver={handleDragOver} | ||||||||||||||||||||||||||
| onDrop={handleDrop} | ||||||||||||||||||||||||||
| onPointerDownCapture={() => { | ||||||||||||||||||||||||||
| if (!appRef.current) return; | ||||||||||||||||||||||||||
| setActiveCanvas({ pageUid, editor: appRef.current }); | ||||||||||||||||||||||||||
| }} | ||||||||||||||||||||||||||
sid597 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| > | ||||||||||||||||||||||||||
| {isCloudflareSync && ( | ||||||||||||||||||||||||||
| <div | ||||||||||||||||||||||||||
|
|
@@ -987,6 +1033,7 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
| <TldrawEditor | ||||||||||||||||||||||||||
| // baseUrl="https://samepage.network/assets/tldraw/" | ||||||||||||||||||||||||||
| // instanceId={initialState.instanceId} | ||||||||||||||||||||||||||
| autoFocus={false} | ||||||||||||||||||||||||||
| initialState="select" | ||||||||||||||||||||||||||
| shapeUtils={[...defaultShapeUtils, ...customShapeUtils]} | ||||||||||||||||||||||||||
| tools={[...defaultTools, ...defaultShapeTools, ...customTools]} | ||||||||||||||||||||||||||
|
|
@@ -1002,6 +1049,10 @@ const TldrawCanvasShared = ({ | |||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| appRef.current = app; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (!activeCanvasPageUid || activeCanvasPageUid === pageUid) { | ||||||||||||||||||||||||||
| setActiveCanvas({ pageUid, editor: app }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void syncCanvasNodeTitlesOnLoad( | ||||||||||||||||||||||||||
| app, | ||||||||||||||||||||||||||
| allNodes.map((n) => n.type), | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,7 +229,11 @@ const ResultRow = ({ | |
| return ( | ||
| <Button | ||
| {...buttonProps} | ||
| onClick={() => { | ||
| onClick={(event) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pass this to the correct canvas |
||
| const targetCanvasPageUid = | ||
| event.currentTarget.closest<HTMLElement>( | ||
| ".roamjs-tldraw-canvas-container[data-page-uid]", | ||
| )?.dataset.pageUid || undefined; | ||
| document.dispatchEvent( | ||
| new CustomEvent("roamjs:query-builder:action", { | ||
| detail: { | ||
|
|
@@ -238,6 +242,7 @@ const ResultRow = ({ | |
| val: r["text"], | ||
| onRefresh, | ||
| queryUid: parentUid, | ||
| targetCanvasPageUid, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.