-
Notifications
You must be signed in to change notification settings - Fork 6
ENG-1553: Embed canvas in block #988
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eae1fde
ENG-1553 Embed canvas in block
sid597 e2ee387
Fix tsc and eslint errors for slashCommand registration
sid597 08161b0
ENG-1553 Address PR review comments
sid597 56a67d6
ENG-1553 Replace Tailwind utilities with CSS classes in styles.css
sid597 3af3e2c
Merge remote-tracking branch 'origin/main' into eng-1553-embed-canvas…
sid597 e918a8a
ENG-1553 Attach canvas embed mousedown handler to wrapper
sid597 01fc0cf
ENG-1553 Drop unreachable self-embed guard
sid597 383136d
Minimize canvas embed CSS
sid597 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 |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import React from "react"; | ||
| import ExtensionApiContextProvider from "roamjs-components/components/ExtensionApiContext"; | ||
| import { OnloadArgs } from "roamjs-components/types"; | ||
| import renderWithUnmount from "roamjs-components/util/renderWithUnmount"; | ||
| import { getPageTitleValueByHtmlElement } from "roamjs-components/dom"; | ||
| import getBlockUidFromTarget from "roamjs-components/dom/getBlockUidFromTarget"; | ||
| import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid"; | ||
| import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle"; | ||
| import { TldrawCanvas } from "./Tldraw"; | ||
|
|
||
| const BLOCK_TEXT_REGEX = /\{\{dg-canvas:\s*\[\[(.+?)\]\]\s*\}\}/i; | ||
|
|
||
| const extractCanvasTitle = (button: HTMLElement): string | null => { | ||
| const blockUid = getBlockUidFromTarget(button); | ||
| if (!blockUid) return null; | ||
| const blockText = getTextByBlockUid(blockUid); | ||
| if (!blockText) return null; | ||
| const match = blockText.match(BLOCK_TEXT_REGEX); | ||
| if (!match) return null; | ||
| return match[1].trim(); | ||
| }; | ||
|
|
||
| const getCurrentPageTitle = (el: HTMLElement): string | null => { | ||
| try { | ||
| return getPageTitleValueByHtmlElement(el); | ||
| } catch { | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| const CanvasEmbedPlaceholder = ({ message }: { message: string }) => ( | ||
| <div | ||
| style={{ | ||
| display: "flex", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| height: "100px", | ||
| color: "#8a9ba8", | ||
| fontSize: "14px", | ||
| border: "1px dashed #d1d5db", | ||
| borderRadius: "6px", | ||
| }} | ||
| > | ||
| {message} | ||
| </div> | ||
| ); | ||
|
|
||
| export const renderCanvasEmbed = ( | ||
| button: HTMLElement, | ||
| onloadArgs: OnloadArgs, | ||
| ) => { | ||
| button.style.display = "none"; | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
|
|
||
| if (!button.parentElement) return; | ||
|
|
||
| const title = extractCanvasTitle(button); | ||
| if (!title) return; | ||
|
|
||
| const currentPageTitle = getCurrentPageTitle(button); | ||
| if (currentPageTitle === title) { | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
| const wrapper = document.createElement("div"); | ||
| button.parentElement.appendChild(wrapper); | ||
| renderWithUnmount( | ||
| <CanvasEmbedPlaceholder message="Cannot embed a canvas within itself" />, | ||
| wrapper, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const pageUid = getPageUidByPageTitle(title); | ||
| if (!pageUid) { | ||
| const wrapper = document.createElement("div"); | ||
| button.parentElement.appendChild(wrapper); | ||
| renderWithUnmount( | ||
| <CanvasEmbedPlaceholder message={`Canvas not found: ${title}`} />, | ||
| wrapper, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| button.parentElement.onmousedown = (e: MouseEvent) => e.stopPropagation(); | ||
|
sid597 marked this conversation as resolved.
Outdated
sid597 marked this conversation as resolved.
Outdated
|
||
|
|
||
| const wrapper = document.createElement("div"); | ||
| wrapper.className = "dg-canvas-embed"; | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
| wrapper.style.height = "400px"; | ||
| wrapper.style.width = "100%"; | ||
| wrapper.style.overflow = "hidden"; | ||
| wrapper.style.borderRadius = "6px"; | ||
| wrapper.style.margin = "8px 0"; | ||
| button.parentElement.appendChild(wrapper); | ||
|
|
||
| renderWithUnmount( | ||
| <ExtensionApiContextProvider {...onloadArgs}> | ||
| <TldrawCanvas title={title} /> | ||
| </ExtensionApiContextProvider>, | ||
| wrapper, | ||
| ); | ||
| }; | ||
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 |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import React, { useState, useMemo, useCallback } from "react"; | ||
| import { Dialog, InputGroup, Menu, MenuItem } from "@blueprintjs/core"; | ||
| import renderOverlay, { | ||
| RoamOverlayProps, | ||
| } from "roamjs-components/util/renderOverlay"; | ||
| import { DEFAULT_CANVAS_PAGE_FORMAT } from "~/index"; | ||
| import { getFormattedConfigTree } from "~/utils/discourseConfigRef"; | ||
|
|
||
| type CanvasEmbedDialogProps = { | ||
| onSelect: (title: string) => void; | ||
| }; | ||
|
|
||
| const getCanvasPages = (): { title: string; uid: string }[] => { | ||
| const { canvasPageFormat } = getFormattedConfigTree(); | ||
| const format = canvasPageFormat.value || DEFAULT_CANVAS_PAGE_FORMAT; | ||
| const regexSource = `^${format.replace(/\*/g, ".+")}$`; | ||
| const escaped = regexSource.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); | ||
|
|
||
| try { | ||
| const results = window.roamAlphaAPI.q(`[ | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
| :find (pull ?node [:node/title :block/uid]) | ||
| :where | ||
| [(re-pattern "${escaped}") ?regex] | ||
| [?node :node/title ?title] | ||
| [(re-find ?regex ?title)] | ||
| ]`) as [{ title: string; uid: string }][]; | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
|
|
||
| return results | ||
| .map(([r]) => ({ title: r.title, uid: r.uid })) | ||
| .sort((a, b) => a.title.localeCompare(b.title)); | ||
| } catch { | ||
| return []; | ||
| } | ||
| }; | ||
|
|
||
| const CanvasEmbedDialog = ({ | ||
| isOpen, | ||
| onClose, | ||
| onSelect, | ||
| }: RoamOverlayProps<CanvasEmbedDialogProps>) => { | ||
| const [filter, setFilter] = useState(""); | ||
| const [activeIndex, setActiveIndex] = useState(0); | ||
| const canvasPages = useMemo(getCanvasPages, []); | ||
|
|
||
| const filtered = useMemo(() => { | ||
| if (!filter) return canvasPages; | ||
| const lower = filter.toLowerCase(); | ||
| return canvasPages.filter((p) => p.title.toLowerCase().includes(lower)); | ||
| }, [filter, canvasPages]); | ||
|
|
||
| const handleSelect = useCallback( | ||
| (title: string) => { | ||
| onSelect(title); | ||
| onClose(); | ||
| }, | ||
| [onSelect, onClose], | ||
| ); | ||
|
|
||
| const handleKeyDown = useCallback( | ||
| (e: React.KeyboardEvent) => { | ||
| if (e.key === "ArrowDown") { | ||
| e.preventDefault(); | ||
| setActiveIndex((i) => Math.min(i + 1, filtered.length - 1)); | ||
| } else if (e.key === "ArrowUp") { | ||
| e.preventDefault(); | ||
| setActiveIndex((i) => Math.max(i - 1, 0)); | ||
| } else if (e.key === "Enter" && filtered.length > 0) { | ||
| e.preventDefault(); | ||
| handleSelect(filtered[activeIndex].title); | ||
| } | ||
| }, | ||
| [filtered, activeIndex, handleSelect], | ||
| ); | ||
|
|
||
| return ( | ||
| <Dialog | ||
| isOpen={isOpen} | ||
| onClose={onClose} | ||
| title="Embed Canvas" | ||
| style={{ width: 400, paddingBottom: 0 }} | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
| > | ||
| <div style={{ padding: "16px" }}> | ||
| <InputGroup | ||
|
sid597 marked this conversation as resolved.
Outdated
|
||
| placeholder="Search canvas pages..." | ||
| value={filter} | ||
| onChange={(e) => { | ||
| setFilter(e.target.value); | ||
| setActiveIndex(0); | ||
| }} | ||
| autoFocus | ||
| onKeyDown={handleKeyDown} | ||
| /> | ||
| <Menu | ||
| style={{ | ||
| maxHeight: "300px", | ||
| overflowY: "auto", | ||
| marginTop: "8px", | ||
| }} | ||
| > | ||
| {filtered.length === 0 ? ( | ||
| <MenuItem disabled text="No canvas pages found" /> | ||
| ) : ( | ||
| filtered.map((page, i) => ( | ||
| <MenuItem | ||
| key={page.uid} | ||
| text={page.title} | ||
| active={i === activeIndex} | ||
| onClick={() => handleSelect(page.title)} | ||
| /> | ||
| )) | ||
| )} | ||
| </Menu> | ||
| </div> | ||
| </Dialog> | ||
| ); | ||
| }; | ||
|
|
||
| export const renderCanvasEmbedDialog = (props: CanvasEmbedDialogProps) => | ||
| renderOverlay({ | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| Overlay: CanvasEmbedDialog, | ||
| props, | ||
| }); | ||
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
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.