-
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 all 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,61 @@ | ||
| 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 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 CanvasEmbedPlaceholder = ({ message }: { message: string }) => ( | ||
| <div className="dg-canvas-embed-placeholder flex items-center justify-center rounded-md border border-dashed border-gray-300 text-sm text-[#8a9ba8]"> | ||
| {message} | ||
| </div> | ||
| ); | ||
|
|
||
| export const renderCanvasEmbed = ( | ||
| button: HTMLElement, | ||
| onloadArgs: OnloadArgs, | ||
| ) => { | ||
| button.hidden = true; | ||
|
|
||
| if (!button.parentElement) return; | ||
|
|
||
| const title = extractCanvasTitle(button); | ||
| if (!title) 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; | ||
| } | ||
|
|
||
| const wrapper = document.createElement("div"); | ||
| wrapper.className = "dg-canvas-embed my-2 w-full overflow-hidden rounded-md"; | ||
| wrapper.onmousedown = (e: MouseEvent) => e.stopPropagation(); | ||
| 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,71 @@ | ||
| import React, { useCallback, useEffect, useState } from "react"; | ||
| import { Dialog } from "@blueprintjs/core"; | ||
| import AutocompleteInput from "roamjs-components/components/AutocompleteInput"; | ||
| import renderOverlay, { | ||
| RoamOverlayProps, | ||
| } from "roamjs-components/util/renderOverlay"; | ||
| import { getCanvasPageTitles } from "~/utils/isCanvasPage"; | ||
|
|
||
| type CanvasEmbedDialogProps = { | ||
| onSelect: (title: string) => void; | ||
| }; | ||
|
|
||
| const CanvasEmbedDialog = ({ | ||
| isOpen, | ||
| onClose, | ||
| onSelect, | ||
| }: RoamOverlayProps<CanvasEmbedDialogProps>) => { | ||
| const [canvasPages, setCanvasPages] = useState<string[] | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| void getCanvasPageTitles().then(setCanvasPages); | ||
| }, []); | ||
|
|
||
| const handleSetValue = useCallback( | ||
| (title: string) => { | ||
| if (canvasPages?.includes(title)) { | ||
| onSelect(title); | ||
| onClose(); | ||
| } | ||
| }, | ||
| [canvasPages, onSelect, onClose], | ||
| ); | ||
|
|
||
| const renderContent = () => { | ||
| if (canvasPages === null) | ||
| return ( | ||
| <div className="text-sm text-[#5c7080]">Loading canvas pages...</div> | ||
| ); | ||
| if (canvasPages.length === 0) | ||
| return ( | ||
| <div className="text-sm text-[#5c7080]">No canvas pages found</div> | ||
| ); | ||
| return ( | ||
| <AutocompleteInput | ||
| setValue={handleSetValue} | ||
| options={canvasPages} | ||
| placeholder="Search canvas pages..." | ||
| autoFocus | ||
| autoSelectFirstOption={false} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog | ||
| isOpen={isOpen} | ||
| onClose={onClose} | ||
| title="Embed Canvas" | ||
| className="dg-canvas-embed-dialog pb-0" | ||
| > | ||
| <div className="p-4">{renderContent()}</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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { updateBlock } from "roamjs-components/writes"; | ||
| import { renderCanvasEmbedDialog } from "~/components/canvas/CanvasEmbedDialog"; | ||
|
|
||
| type SlashCommandContext = { | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| "block-uid": string; | ||
| }; | ||
|
|
||
| type SlashCommandApi = { | ||
| addCommand: (cmd: { | ||
| label: string; | ||
| callback: (context: SlashCommandContext) => void; | ||
| }) => void; | ||
| removeCommand: (cmd: { label: string }) => void; | ||
| }; | ||
|
|
||
| const getSlashCommandApi = (): SlashCommandApi => | ||
| (window.roamAlphaAPI.ui as unknown as { slashCommand: SlashCommandApi }) | ||
| .slashCommand; | ||
|
|
||
| const SLASH_COMMANDS: { | ||
| label: string; | ||
| callback: (context: SlashCommandContext) => void; | ||
| }[] = [ | ||
| { | ||
| label: "DG: Embed canvas", | ||
| callback: (context) => { | ||
| const uid = context["block-uid"]; | ||
| if (!uid) return; | ||
| renderCanvasEmbedDialog({ | ||
| onSelect: (title: string) => { | ||
| void updateBlock({ | ||
| uid, | ||
| text: `{{dg-canvas: [[${title}]]}}`, | ||
| }); | ||
| }, | ||
| }); | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| export const registerSlashCommands = (): (() => void) => { | ||
| const api = getSlashCommandApi(); | ||
| for (const cmd of SLASH_COMMANDS) api.addCommand(cmd); | ||
| return () => { | ||
| for (const { label } of SLASH_COMMANDS) api.removeCommand({ label }); | ||
| }; | ||
| }; | ||
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.