From 895d02a19c9ce100076f9ea95c205c8faad1948c Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Sat, 19 Oct 2024 00:42:23 -0400 Subject: [PATCH] embeds work! --- src/components/Board.tsx | 8 +++-- src/shapes/EmbedShapeUtil.tsx | 59 +++++++++++++++++++++++++++++++++++ src/tools/EmbedTool.ts | 9 ++++++ src/ui-overrides.tsx | 12 +++++++ worker/TldrawDurableObject.ts | 8 ++++- 5 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 src/shapes/EmbedShapeUtil.tsx create mode 100644 src/tools/EmbedTool.ts diff --git a/src/components/Board.tsx b/src/components/Board.tsx index 42d5427..30009c3 100644 --- a/src/components/Board.tsx +++ b/src/components/Board.tsx @@ -12,6 +12,8 @@ import { VideoChatTool } from '@/tools/VideoChatTool' import { VideoChatShape } from '@/shapes/VideoChatShapeUtil' import { multiplayerAssetStore } from '../client/multiplayerAssetStore' import { customSchema } from '../../worker/TldrawDurableObject' +import { EmbedShape } from '@/shapes/EmbedShapeUtil' +import { EmbedTool } from '@/tools/EmbedTool' import React, { useState } from 'react'; import { ChatBox } from '@/shapes/ChatBoxShapeUtil'; @@ -19,8 +21,8 @@ import { components, uiOverrides } from '@/ui-overrides' const WORKER_URL = `https://jeffemmett-canvas.jeffemmett.workers.dev` -const shapeUtils = [ChatBoxShape, VideoChatShape] -const tools = [ChatBoxTool, VideoChatTool]; // Array of tools +const shapeUtils = [ChatBoxShape, VideoChatShape, EmbedShape] +const tools = [ChatBoxTool, VideoChatTool, EmbedTool]; // Array of tools export function Board() { const { slug } = useParams<{ slug: string }>(); // Ensure this is inside the Board component @@ -107,4 +109,4 @@ async function unfurlBookmarkUrl({ url }: { url: string }): Promise; + +export class EmbedShape extends BaseBoxShapeUtil { + static override type = 'Embed'; + + getDefaultProps(): IEmbedShape['props'] { + return { + url: null, + w: 640, + h: 480, + }; + } + + indicator(shape: IEmbedShape) { + return ; + } + + component(shape: IEmbedShape) { + const [inputUrl, setInputUrl] = useState(shape.props.url || ''); + + const handleSubmit = useCallback((e: React.FormEvent) => { + e.preventDefault(); + this.editor.updateShape({ id: shape.id, type: 'Embed', props: { ...shape.props, url: inputUrl } }); + }, [inputUrl]); + + if (!shape.props.url) { + return ( +
+
+ setInputUrl(e.target.value)} + placeholder="Enter URL" + style={{ width: shape.props.w, height: shape.props.h }} + /> + +
+
+ ); + } + + return ( +
+