-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1373: Drag pages onto the canvas to create node shapes #1083
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
Open
sid597
wants to merge
6
commits into
main
Choose a base branch
from
eng-1373-drag-and-drop-pages-to-canvas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
014d57e
ENG-1373: Drag pages onto the canvas to create node shapes
sid597 c6e8e91
ENG-1373: Unregister page-ref draggable handler on unload
sid597 247a42d
ENG-1373: Drag query-result page links and resolve block refs to nodes
sid597 cbee62a
Scope page ref canvas dragging
sid597 4cef969
Format canvas drag changes
sid597 459fba1
Fix canvas block drag fallback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -174,6 +174,8 @@ export const MAX_WIDTH = "400px"; | |
|
|
||
| const ICON_URL = `data:image/svg+xml;utf8,${encodeURIComponent(WHITE_LOGO_SVG)}`; | ||
|
|
||
| const PAGE_REF_REGEX = /^\[\[(.+?)\]\]$/; | ||
|
|
||
| /** Valid file size for asset props; undefined when unknown (e.g. Roam/file API not a real File) to avoid persisting null. */ | ||
| const getValidFileSize = (file: { size?: number }): number | undefined => | ||
| typeof file.size === "number" && Number.isFinite(file.size) && file.size > 0 | ||
|
|
@@ -641,15 +643,63 @@ const TldrawCanvasShared = ({ | |
|
|
||
| // Handle Roam block drag and drop | ||
| useEffect(() => { | ||
| const temporaryDraggableAttribute = "data-roamjs-canvas-page-ref-draggable"; | ||
| let activePageRef: HTMLElement | null = null; | ||
| const isPageRefDragSource = (pageRef: HTMLElement): boolean => | ||
| !!pageRef.closest( | ||
| ".roamjs-query-results-view, .roamjs-discourse-result-panel, .roamjs-discourse-context-overlay-container, .rm-query, .rm-query-content", | ||
| ); | ||
| const clearActivePageRef = () => { | ||
| if (activePageRef?.hasAttribute(temporaryDraggableAttribute)) { | ||
| activePageRef.draggable = false; | ||
| activePageRef.removeAttribute(temporaryDraggableAttribute); | ||
| } | ||
| activePageRef = null; | ||
| }; | ||
| const handlePointerDown = (e: PointerEvent) => { | ||
| if (e.defaultPrevented || e.button !== 0) return; | ||
| const target = e.target as HTMLElement; | ||
| const pageRef = target.closest<HTMLElement>(".rm-page-ref"); | ||
| if (!pageRef || pageRef.draggable || !isPageRefDragSource(pageRef)) | ||
| return; | ||
|
|
||
| activePageRef = pageRef; | ||
| pageRef.draggable = true; | ||
| pageRef.setAttribute(temporaryDraggableAttribute, "true"); | ||
| }; | ||
|
sid597 marked this conversation as resolved.
|
||
| const handleDragStart = (e: DragEvent) => { | ||
| const target = e.target as HTMLElement; | ||
| const uid = getBlockUidFromBullet(target); | ||
|
|
||
| const pageRef = target.closest<HTMLElement>(".rm-page-ref"); | ||
| if (pageRef && isPageRefDragSource(pageRef)) { | ||
| const pageTitle = ( | ||
| pageRef.getAttribute("data-tag") || | ||
| pageRef.getAttribute("data-link-title") || | ||
| pageRef.parentElement?.getAttribute("data-link-title") | ||
| )?.replace(/\\"/g, '"'); | ||
| if (pageTitle) { | ||
| e.dataTransfer?.setData("application/x-roam-page", pageTitle); | ||
| return; | ||
| } | ||
| } | ||
|
sid597 marked this conversation as resolved.
|
||
|
|
||
| const uid = getBlockUidFromBullet(target); | ||
| if (uid) e.dataTransfer?.setData("application/x-roam-uid", uid); | ||
| }; | ||
|
|
||
| document.addEventListener("pointerdown", handlePointerDown); | ||
| document.addEventListener("pointerup", clearActivePageRef); | ||
| document.addEventListener("pointercancel", clearActivePageRef); | ||
| document.addEventListener("dragstart", handleDragStart); | ||
| return () => document.removeEventListener("dragstart", handleDragStart); | ||
| document.addEventListener("dragend", clearActivePageRef); | ||
| return () => { | ||
| clearActivePageRef(); | ||
| document.removeEventListener("pointerdown", handlePointerDown); | ||
| document.removeEventListener("pointerup", clearActivePageRef); | ||
| document.removeEventListener("pointercancel", clearActivePageRef); | ||
| document.removeEventListener("dragstart", handleDragStart); | ||
| document.removeEventListener("dragend", clearActivePageRef); | ||
| }; | ||
| }, []); | ||
|
|
||
| const handleDragOver = (e: React.DragEvent<HTMLDivElement>) => { | ||
|
|
@@ -658,6 +708,22 @@ const TldrawCanvasShared = ({ | |
|
|
||
| const handleDrop = (e: React.DragEvent<HTMLDivElement>) => { | ||
| e.preventDefault(); | ||
|
|
||
| const pageTitle = e.dataTransfer.getData("application/x-roam-page"); | ||
| if (pageTitle && appRef.current && extensionAPI) { | ||
| posthog.capture("Canvas: Roam Page Dropped"); | ||
| const dropPoint = appRef.current.screenToPage({ | ||
| x: e.clientX, | ||
| y: e.clientY, | ||
| }); | ||
| void appRef.current.putExternalContent({ | ||
| type: "text", | ||
| text: `[[${pageTitle}]]`, | ||
| point: dropPoint, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| const uid = e.dataTransfer.getData("application/x-roam-uid"); | ||
|
|
||
| if (!uid || !appRef.current || !extensionAPI) return; | ||
|
|
@@ -1267,30 +1333,36 @@ const InsideEditorAndUiContext = ({ | |
| try { | ||
| const text = content.text ?? ""; | ||
|
|
||
| // Check for page reference: [[pageName]] | ||
| const pageMatch = text.match(/^\[\[(.+?)\]\]$/); | ||
| if (pageMatch?.[1]) { | ||
| const pageName = pageMatch[1]; | ||
| const pageUid = getPageUidByPageTitle(pageName); | ||
| if (!pageUid) return await callDefaultTextHandler(content); | ||
|
|
||
| const tryCreatePageNodeShape = async ( | ||
| title: string, | ||
| ): Promise<boolean> => { | ||
| const pageUid = getPageUidByPageTitle(title); | ||
| if (!pageUid) return false; | ||
| const nodeType = findDiscourseNode({ | ||
| uid: pageUid, | ||
| title: pageName, | ||
| title, | ||
| nodes: allNodes, | ||
| }); | ||
| if (!nodeType) return await callDefaultTextHandler(content); | ||
|
|
||
| if (!nodeType) return false; | ||
| await createDiscourseNodeShape({ | ||
| uid: pageUid, | ||
| nodeText: pageName, | ||
| nodeText: title, | ||
| nodeType: nodeType.type, | ||
| content, | ||
| }); | ||
| posthog.capture("Canvas: Node Added from External Content", { | ||
| source: "page-reference", | ||
| }); | ||
| return; | ||
| return true; | ||
| }; | ||
|
|
||
| // Check for page reference: [[pageName]] | ||
| const pageMatch = text.match(PAGE_REF_REGEX); | ||
| if (pageMatch?.[1]) { | ||
| if (await tryCreatePageNodeShape(pageMatch[1])) { | ||
| posthog.capture("Canvas: Node Added from External Content", { | ||
|
Member
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. "External Content" here can be misleading. Does this mean outside of Roam? |
||
| source: "page-reference", | ||
| }); | ||
| return; | ||
| } | ||
| return await callDefaultTextHandler(content); | ||
| } | ||
|
|
||
| // Check for block reference: ((uid)) | ||
|
|
@@ -1303,6 +1375,14 @@ const InsideEditorAndUiContext = ({ | |
| if (!blockText || !isLive) | ||
| return await callDefaultTextHandler(content); | ||
|
|
||
| const refMatch = blockText.match(PAGE_REF_REGEX); | ||
| if (refMatch?.[1] && (await tryCreatePageNodeShape(refMatch[1]))) { | ||
| posthog.capture("Canvas: Node Added from External Content", { | ||
|
Member
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. "External Content" here can be misleading. Does this mean outside of Roam? |
||
| source: "block-page-reference", | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| await createDiscourseNodeShape({ | ||
| uid, | ||
| nodeText: blockText, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TEMP_DRAG_ATTR