-
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
devin-ai-integration[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -129,15 +129,38 @@ export type DiscourseContextType = { | |
| nodes: Record<string, DiscourseNode & { index: number }>; | ||
| // { [Relation.Label] => DiscourseRelation[] } | ||
| relations: Record<string, DiscourseRelation[]>; | ||
| lastAppEvent: string; | ||
| lastActions: HistoryEntry<TLRecord>[]; | ||
| }; | ||
|
|
||
| export const discourseContext: DiscourseContextType = { | ||
| nodes: {}, | ||
| relations: {}, | ||
| lastAppEvent: "", | ||
|
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 1 handled |
||
| lastActions: [], | ||
| }; | ||
|
|
||
| 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; | ||
|
|
@@ -358,9 +381,7 @@ const TldrawCanvasShared = ({ | |
| const appRef = useRef<Editor | null>(null); | ||
| const lastInsertRef = useRef<VecModel>(); | ||
| const containerRef = useRef<HTMLDivElement>(null); | ||
| const lastActionsRef = useRef<HistoryEntry<TLRecord>[]>( | ||
| discourseContext.lastActions, | ||
| ); | ||
| const lastActionsRef = useRef<HistoryEntry<TLRecord>[]>([]); | ||
|
|
||
| const [isConvertToDialogOpen, setConvertToDialogOpen] = useState(false); | ||
|
|
||
|
|
@@ -725,6 +746,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]); | ||
| const { store, needsUpgrade, performUpgrade, error, isLoading } = | ||
| useStoreAdapter(storeAdapterArgs); | ||
| const migratedCloudStoreRef = useRef<string | null>(null); | ||
|
|
@@ -788,10 +820,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; | ||
| const app = appRef.current; | ||
| if (!app) return; | ||
| const { x, y } = app.getViewportScreenCenter(); | ||
|
|
@@ -829,7 +865,7 @@ const TldrawCanvasShared = ({ | |
| actionListener, | ||
| ); | ||
| }; | ||
| }, [appRef, allNodes]); | ||
| }, [appRef, allNodes, pageUid]); | ||
|
|
||
| // Catch a custom event we used patch-package to add | ||
| useEffect(() => { | ||
|
|
@@ -917,6 +953,10 @@ const TldrawCanvasShared = ({ | |
| tabIndex={-1} | ||
| onDragOver={handleDragOver} | ||
| onDrop={handleDrop} | ||
| onPointerDownCapture={() => { | ||
| if (!appRef.current) return; | ||
| setActiveCanvas({ pageUid, editor: appRef.current }); | ||
| }} | ||
| > | ||
| {isCloudflareSync && ( | ||
| <div | ||
|
|
@@ -987,6 +1027,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 +1043,10 @@ const TldrawCanvasShared = ({ | |
|
|
||
| appRef.current = app; | ||
|
|
||
| if (!activeCanvasPageUid || activeCanvasPageUid === pageUid) { | ||
| setActiveCanvas({ pageUid, editor: app }); | ||
| } | ||
|
|
||
| void syncCanvasNodeTitlesOnLoad( | ||
| app, | ||
| allNodes.map((n) => n.type), | ||
|
|
@@ -1022,8 +1067,6 @@ const TldrawCanvasShared = ({ | |
| app.on("event", (event) => { | ||
| const e = event as TLPointerEventInfo; | ||
|
|
||
| discourseContext.lastAppEvent = e.name; | ||
|
|
||
| // Handle relation creation on pointer_down | ||
| handleRelationCreation(app, e); | ||
|
|
||
|
|
||
| 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mutating this could effect the state for other open canvases??? we do set this up in tldraw.tsx line 455
This is for you Michael it seems correct to me but can give an authortative answer here