Skip to content

Commit e86b1c5

Browse files
[ENG-1123] Right sidebar page title (#729)
* Fix: Use canvas page title for auto-generated relations in sidebar - Store canvas page UID on editor instance when mounting - Update relation creation to use canvas page UID instead of main page UID - Fixes issue where relations created from canvas in right sidebar incorrectly use main page title Co-authored-by: doantranghp2000 <doantranghp2000@gmail.com> * Refactor: Use discourseContext for canvas page UID - Add canvasPageUid to DiscourseContextType instead of monkey-patching editor - Store canvas page UID in discourseContext on mount - Use discourseContext.canvasPageUid in relation creation - Removes @ts-expect-error comments and follows existing patterns Co-authored-by: Trang Doan <trangdoan982@users.noreply.github.com> * Fix: Support multiple canvases with WeakMap - Replace single canvasPageUid with editorToPageUid WeakMap - Maps each editor instance to its specific canvas page UID - Fixes issue where multiple canvases in sidebars would share wrong page UID - Ensures relation creation uses correct canvas page even with multiple open Co-authored-by: Trang Doan <trangdoan982@users.noreply.github.com> * light cleanup * Refactor: Use DOM-based approach for canvas page UID - Add data-page-uid attribute to canvas container div - Read page UID from DOM using focus/active element detection - Supports multiple canvases by detecting which one is being interacted with - Removes WeakMap complexity - simpler, cleaner solution - No state management needed - single source of truth in DOM Co-authored-by: Trang Doan <trangdoan982@users.noreply.github.com> * small cleanup * cleanup --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Trang Doan <trangdoan982@users.noreply.github.com>
1 parent 8e9e2fc commit e86b1c5

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

apps/roam/src/components/canvas/DiscourseRelationShape/DiscourseRelationUtil.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,33 @@ import { getSetting } from "~/utils/extensionSettings";
6767
import { createReifiedRelation } from "~/utils/createReifiedBlock";
6868
import { discourseContext, isPageUid } from "~/components/canvas/Tldraw";
6969
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
70+
71+
/**
72+
* Get the canvas page UID from the DOM by finding the canvas container
73+
* Uses the focused/active canvas when multiple are open
74+
*/
75+
const getCanvasPageUidFromDOM = (): string | null => {
76+
// Find all canvas containers
77+
const containers = document.querySelectorAll<HTMLElement>(
78+
".roamjs-tldraw-canvas-container[data-page-uid]",
79+
);
80+
81+
if (containers.length === 0) return null;
82+
if (containers.length === 1) {
83+
return containers[0].getAttribute("data-page-uid");
84+
}
85+
86+
// With multiple canvases, find the one that contains the currently focused element
87+
// or has been recently interacted with (has :focus-within)
88+
for (const container of containers) {
89+
if (container.matches(":focus-within") || container.contains(document.activeElement)) {
90+
return container.getAttribute("data-page-uid");
91+
}
92+
}
93+
94+
// Fallback: use the first container
95+
return containers[0].getAttribute("data-page-uid");
96+
};
7097
import { InputTextNode } from "roamjs-components/types";
7198
import createBlock from "roamjs-components/writes/createBlock";
7299
import createPage from "roamjs-components/writes/createPage";
@@ -76,7 +103,6 @@ import {
76103
BaseDiscourseNodeUtil,
77104
DiscourseNodeShape,
78105
} from "~/components/canvas/DiscourseNodeUtil";
79-
import getCurrentPageUid from "roamjs-components/dom/getCurrentPageUid";
80106
import getPageTitleByPageUid from "roamjs-components/queries/getPageTitleByPageUid";
81107
import { AddReferencedNodeType } from "./DiscourseRelationTool";
82108
import { dispatchToastEvent } from "~/components/canvas/ToastListener";
@@ -654,8 +680,12 @@ export const createAllRelationShapeUtils = (
654680
relation,
655681
target,
656682
}));
657-
const parentUid = getCurrentPageUid();
658-
const title = getPageTitleByPageUid(parentUid);
683+
// Get canvas page UID from the DOM (supports multiple canvases)
684+
const canvasPageUid = getCanvasPageUidFromDOM();
685+
if (!canvasPageUid) {
686+
return deleteAndWarn("No canvas page UID found");
687+
}
688+
const title = getPageTitleByPageUid(canvasPageUid);
659689
await triplesToBlocks({
660690
defaultPageTitle: `Auto generated from [[${title}]]`,
661691
toPage: async (title: string, blocks: InputTextNode[]) => {

0 commit comments

Comments
 (0)