Skip to content

Commit 2ad7f73

Browse files
authored
ENG-1792: Add edit block button to rendered dg-canvas (#1203)
* ENG-1792: Add edit block button to rendered dg-canvas Overlay Button on the embedded canvas that focuses the underlying {{dg-canvas}} block via setBlockFocusAndSelection, using the same getUids/.roam-block idiom as the ResultsView Edit Block action so it works in the right sidebar too. * ENG-1792: Type the block container lookup as nullable Review follow-up: getUids accepts a null element and returns empty ids, so use the closest() generic instead of a non-null assertion. * ENG-1792: Move edit button to bottom right of the canvas Top right overlaps the tldraw style panel; bottom right is the corner the plugin already uses for its cloud sync icon, offset left so both fit.
1 parent b09fb99 commit 2ad7f73

1 file changed

Lines changed: 36 additions & 1 deletion

File tree

apps/roam/src/components/canvas/CanvasEmbed.tsx

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import React from "react";
2+
import { Button } from "@blueprintjs/core";
3+
import posthog from "posthog-js";
24
import ExtensionApiContextProvider from "roamjs-components/components/ExtensionApiContext";
35
import { OnloadArgs } from "roamjs-components/types";
46
import renderWithUnmount from "roamjs-components/util/renderWithUnmount";
57
import getBlockUidFromTarget from "roamjs-components/dom/getBlockUidFromTarget";
8+
import getUids from "roamjs-components/dom/getUids";
69
import getTextByBlockUid from "roamjs-components/queries/getTextByBlockUid";
710
import getPageUidByPageTitle from "roamjs-components/queries/getPageUidByPageTitle";
811
import { TldrawCanvas } from "./Tldraw";
@@ -28,6 +31,36 @@ const CanvasEmbedPlaceholder = ({ message }: { message: string }) => (
2831
</div>
2932
);
3033

34+
const handleEditBlock = (location: { blockUid: string; windowId: string }) => {
35+
posthog.capture("Canvas Embed: Edit Block Clicked");
36+
void window.roamAlphaAPI.ui.setBlockFocusAndSelection({
37+
location: {
38+
"block-uid": location.blockUid,
39+
"window-id": location.windowId,
40+
},
41+
});
42+
};
43+
44+
const CanvasEmbedChrome = ({
45+
title,
46+
location,
47+
}: {
48+
title: string;
49+
location: { blockUid: string; windowId: string };
50+
}) => (
51+
<div className="relative h-full w-full">
52+
<TldrawCanvas title={title} />
53+
<Button
54+
className="absolute bottom-2 right-8 z-20"
55+
icon="edit"
56+
minimal
57+
small
58+
title="Edit Block"
59+
onClick={() => handleEditBlock(location)}
60+
/>
61+
</div>
62+
);
63+
3164
export const renderCanvasEmbed = (
3265
button: HTMLElement,
3366
onloadArgs: OnloadArgs,
@@ -50,6 +83,8 @@ export const renderCanvasEmbed = (
5083
return;
5184
}
5285

86+
const location = getUids(button.closest<HTMLDivElement>(".roam-block"));
87+
5388
const wrapper = document.createElement("div");
5489
wrapper.className = "dg-canvas-embed my-2 w-full overflow-hidden rounded-md";
5590
wrapper.style.height = "400px";
@@ -58,7 +93,7 @@ export const renderCanvasEmbed = (
5893

5994
renderWithUnmount(
6095
<ExtensionApiContextProvider {...onloadArgs}>
61-
<TldrawCanvas title={title} />
96+
<CanvasEmbedChrome title={title} location={location} />
6297
</ExtensionApiContextProvider>,
6398
wrapper,
6499
);

0 commit comments

Comments
 (0)