From ffe9103da6eb7f93819f6c0d0ddf0905ccfc4524 Mon Sep 17 00:00:00 2001 From: codetorso Date: Sat, 29 Jun 2024 20:08:46 +0530 Subject: user canvas (2/2) --- .../ui/components/canvas/components/canvas.tsx | 94 -------- .../canvas/components/draggableComponent.tsx | 71 ------- .../components/canvas/components/dropComponent.tsx | 203 ------------------ .../canvas/components/enabledComp copy.tsx | 22 -- .../components/canvas/components/enabledComp.tsx | 22 -- .../ui/components/canvas/components/savesnap.tsx | 43 ---- .../ui/components/canvas/components/textCard.tsx | 45 ---- .../components/canvas/components/twitterCard.tsx | 84 -------- packages/ui/components/canvas/lib/context.ts | 18 -- .../ui/components/canvas/lib/createAssetUrl.ts | 94 -------- packages/ui/components/canvas/lib/createEmbeds.ts | 236 --------------------- packages/ui/components/canvas/lib/loadSnap.ts | 14 -- 12 files changed, 946 deletions(-) delete mode 100644 packages/ui/components/canvas/components/canvas.tsx delete mode 100644 packages/ui/components/canvas/components/draggableComponent.tsx delete mode 100644 packages/ui/components/canvas/components/dropComponent.tsx delete mode 100644 packages/ui/components/canvas/components/enabledComp copy.tsx delete mode 100644 packages/ui/components/canvas/components/enabledComp.tsx delete mode 100644 packages/ui/components/canvas/components/savesnap.tsx delete mode 100644 packages/ui/components/canvas/components/textCard.tsx delete mode 100644 packages/ui/components/canvas/components/twitterCard.tsx delete mode 100644 packages/ui/components/canvas/lib/context.ts delete mode 100644 packages/ui/components/canvas/lib/createAssetUrl.ts delete mode 100644 packages/ui/components/canvas/lib/createEmbeds.ts delete mode 100644 packages/ui/components/canvas/lib/loadSnap.ts (limited to 'packages') diff --git a/packages/ui/components/canvas/components/canvas.tsx b/packages/ui/components/canvas/components/canvas.tsx deleted file mode 100644 index f3e4c090..00000000 --- a/packages/ui/components/canvas/components/canvas.tsx +++ /dev/null @@ -1,94 +0,0 @@ -import { useCallback, useEffect, useRef, useState } from "react"; -import { Editor, Tldraw, setUserPreferences, TLStoreWithStatus } from "tldraw"; -import { createAssetFromUrl } from "../lib/createAssetUrl"; -import "tldraw/tldraw.css"; -import { components } from "./enabledComp"; -import { twitterCardUtil } from "./twitterCard"; -import { textCardUtil } from "./textCard"; -import createEmbedsFromUrl from "../lib/createEmbeds"; -import { loadRemoteSnapshot } from "../lib/loadSnap"; -import { SaveStatus } from "./savesnap"; -import { getAssetUrls } from "@tldraw/assets/selfHosted"; -import { memo } from "react"; -import DragContext from "../lib/context"; -import DropZone from "./dropComponent"; -// import "./canvas.css"; - -export const Canvas = memo(() => { - const [isDraggingOver, setIsDraggingOver] = useState(false); - const Dragref = useRef(null); - - const handleDragOver = (event: any) => { - event.preventDefault(); - setIsDraggingOver(true); - console.log("entere"); - }; - - useEffect(() => { - const divElement = Dragref.current; - if (divElement) { - divElement.addEventListener("dragover", handleDragOver); - } - return () => { - if (divElement) { - divElement.removeEventListener("dragover", handleDragOver); - } - }; - }, []); - - return ( - -
- -
-
- ); -}); - -const TldrawComponent = memo(() => { - const [storeWithStatus, setStoreWithStatus] = useState({ - status: "loading", - }); - useEffect(() => { - const fetchStore = async () => { - const store = await loadRemoteSnapshot(); - - setStoreWithStatus({ - store: store, - status: "not-synced", - }); - }; - - fetchStore(); - }, []); - - const handleMount = useCallback((editor: Editor) => { - (window as any).app = editor; - (window as any).editor = editor; - editor.registerExternalAssetHandler("url", createAssetFromUrl); - editor.registerExternalContentHandler("url", ({ url, point, sources }) => { - createEmbedsFromUrl({ url, point, sources, editor }); - }); - }, []); - - setUserPreferences({ id: "supermemory" }); - - const assetUrls = getAssetUrls(); - return ( -
- -
- -
- -
-
- ); -}); diff --git a/packages/ui/components/canvas/components/draggableComponent.tsx b/packages/ui/components/canvas/components/draggableComponent.tsx deleted file mode 100644 index d0832e81..00000000 --- a/packages/ui/components/canvas/components/draggableComponent.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import Image from "next/image"; -import { useRef, useState } from "react"; - -interface DraggableComponentsProps { - content: string; - extraInfo?: string; - icon: string; - iconAlt: string; -} - -export default function DraggableComponentsContainer({ - content, -}: { - content: DraggableComponentsProps[]; -}) { - return ( -
- {content.map((i) => { - return ( - - ); - })} -
- ); -} - -function DraggableComponents({ - content, - extraInfo, - icon, - iconAlt, -}: DraggableComponentsProps) { - const [isDragging, setIsDragging] = useState(false); - const containerRef = useRef(null); - - const handleDragStart = (event: React.DragEvent) => { - setIsDragging(true); - if (containerRef.current) { - // Serialize the children as a string for dataTransfer - const childrenHtml = containerRef.current.innerHTML; - event.dataTransfer.setData("text/html", childrenHtml); - } - }; - - const handleDragEnd = () => { - setIsDragging(false); - }; - - return ( -
- {iconAlt} -
-
-

{content}

-
-

{extraInfo}

-
-
- ); -} diff --git a/packages/ui/components/canvas/components/dropComponent.tsx b/packages/ui/components/canvas/components/dropComponent.tsx deleted file mode 100644 index 0374f367..00000000 --- a/packages/ui/components/canvas/components/dropComponent.tsx +++ /dev/null @@ -1,203 +0,0 @@ -import React, { useRef, useCallback, useEffect, useContext } from "react"; -import { useEditor } from "tldraw"; -import DragContext, { DragContextType, useDragContext } from "../lib/context"; -import { handleExternalDroppedContent } from "../lib/createEmbeds"; - -const stripHtmlTags = (html: string): string => { - const div = document.createElement("div"); - div.innerHTML = html; - return div.textContent || div.innerText || ""; -}; - -function formatTextToRatio(text: string) { - const totalWidth = text.length; - const maxLineWidth = Math.floor(totalWidth / 4); - - const words = text.split(" "); - let lines = []; - let currentLine = ""; - - words.forEach((word) => { - // Check if adding the next word exceeds the maximum line width - if ((currentLine + word).length <= maxLineWidth) { - currentLine += (currentLine ? " " : "") + word; - } else { - // If the current line is full, push it to new line - lines.push(currentLine); - currentLine = word; - } - }); - if (currentLine) { - lines.push(currentLine); - } - return lines.join("\n"); -} - -function DropZone() { - const dropRef = useRef(null); - const { isDraggingOver, setIsDraggingOver } = useDragContext(); - - const editor = useEditor(); - - const handleDragLeave = () => { - setIsDraggingOver(false); - console.log("leaver"); - }; - - useEffect(() => { - setInterval(() => { - editor.selectAll(); - const shapes = editor.getSelectedShapes(); - const text = shapes.filter((s) => s.type === "text"); - console.log("hrhh", text); - }, 5000); - }, []); - - const handleDrop = useCallback((event: DragEvent) => { - event.preventDefault(); - setIsDraggingOver(false); - const dt = event.dataTransfer; - if (!dt) { - return; - } - const items = dt.items; - - for (let i = 0; i < items.length; i++) { - if (items[i]!.kind === "file" && items[i]!.type.startsWith("image/")) { - const file = items[i]!.getAsFile(); - if (file) { - const reader = new FileReader(); - reader.onload = (e) => { - if (e.target) { - // setDroppedImage(e.target.result as string); - } - }; - reader.readAsDataURL(file); - } - } else if (items[i]!.kind === "string") { - items[i]!.getAsString((data) => { - const cleanText = stripHtmlTags(data); - const onethree = formatTextToRatio(cleanText); - handleExternalDroppedContent({ editor, text: onethree }); - }); - } - } - }, []); - - useEffect(() => { - const divElement = dropRef.current; - if (divElement) { - divElement.addEventListener("drop", handleDrop); - divElement.addEventListener("dragleave", handleDragLeave); - } - return () => { - if (divElement) { - divElement.removeEventListener("drop", handleDrop); - divElement.addEventListener("dragleave", handleDragLeave); - } - }; - }, []); - - return ( -
- {isDraggingOver && ( - <> -
- -
-
- -
-
- -
-
- -
-

Drop here to add Content on Canvas

- - )} -
- ); -} - -function TopRight() { - return ( - - - - ); -} - -function TopLeft() { - return ( - - - - ); -} - -function BottomLeft() { - return ( - - - - ); -} - -function BottomRight() { - return ( - - - - ); -} - -export default DropZone; diff --git a/packages/ui/components/canvas/components/enabledComp copy.tsx b/packages/ui/components/canvas/components/enabledComp copy.tsx deleted file mode 100644 index 85811b82..00000000 --- a/packages/ui/components/canvas/components/enabledComp copy.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { TLUiComponents } from "tldraw"; - -export const components: Partial = { - ActionsMenu: null, - MainMenu: null, - QuickActions: null, - TopPanel: null, - DebugPanel: null, - DebugMenu: null, - PageMenu: null, - // Minimap: null, - // ContextMenu: null, - // HelpMenu: null, - // ZoomMenu: null, - // StylePanel: null, - // NavigationPanel: null, - // Toolbar: null, - // KeyboardShortcutsDialog: null, - // HelperButtons: null, - // SharePanel: null, - // MenuPanel: null, -}; \ No newline at end of file diff --git a/packages/ui/components/canvas/components/enabledComp.tsx b/packages/ui/components/canvas/components/enabledComp.tsx deleted file mode 100644 index 85811b82..00000000 --- a/packages/ui/components/canvas/components/enabledComp.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { TLUiComponents } from "tldraw"; - -export const components: Partial = { - ActionsMenu: null, - MainMenu: null, - QuickActions: null, - TopPanel: null, - DebugPanel: null, - DebugMenu: null, - PageMenu: null, - // Minimap: null, - // ContextMenu: null, - // HelpMenu: null, - // ZoomMenu: null, - // StylePanel: null, - // NavigationPanel: null, - // Toolbar: null, - // KeyboardShortcutsDialog: null, - // HelperButtons: null, - // SharePanel: null, - // MenuPanel: null, -}; \ No newline at end of file diff --git a/packages/ui/components/canvas/components/savesnap.tsx b/packages/ui/components/canvas/components/savesnap.tsx deleted file mode 100644 index f82e97e3..00000000 --- a/packages/ui/components/canvas/components/savesnap.tsx +++ /dev/null @@ -1,43 +0,0 @@ -import { useCallback, useEffect, useState } from "react"; -import { debounce, useEditor } from "tldraw"; - -export function SaveStatus() { - const [save, setSave] = useState("saved!"); - const editor = useEditor(); - - const debouncedSave = useCallback( - debounce(async () => { - const snapshot = editor.store.getSnapshot(); - localStorage.setItem("saved", JSON.stringify(snapshot)); - - const res = await fetch( - "https://learning-cf.pruthvirajthinks.workers.dev/post/page3", - { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - data: snapshot, - }), - }, - ); - - console.log(await res.json()); - setSave("saved!"); - }, 3000), - [editor], // Dependency array ensures the function is not recreated on every render - ); - - useEffect(() => { - const unsubscribe = editor.store.listen( - () => { - setSave("saving..."); - debouncedSave(); - }, - { scope: "document", source: "user" }, - ); - - return () => unsubscribe(); // Cleanup on unmount - }, [editor, debouncedSave]); - - return ; -} \ No newline at end of file diff --git a/packages/ui/components/canvas/components/textCard.tsx b/packages/ui/components/canvas/components/textCard.tsx deleted file mode 100644 index b24dae52..00000000 --- a/packages/ui/components/canvas/components/textCard.tsx +++ /dev/null @@ -1,45 +0,0 @@ -import { BaseBoxShapeUtil, HTMLContainer, TLBaseShape } from "tldraw"; - -type ITextCardShape = TLBaseShape< - "Textcard", - { w: number; h: number; content: string; extrainfo: string } ->; - -export class textCardUtil extends BaseBoxShapeUtil { - static override type = "Textcard" as const; - - getDefaultProps(): ITextCardShape["props"] { - return { - w: 100, - h: 50, - content: "", - extrainfo: "", - }; - } - - component(s: ITextCardShape) { - return ( - -
-

{s.props.content}

-

- {s.props.extrainfo} -

-
-
- ); - } - - indicator(shape: ITextCardShape) { - return ; - } -} diff --git a/packages/ui/components/canvas/components/twitterCard.tsx b/packages/ui/components/canvas/components/twitterCard.tsx deleted file mode 100644 index c5582a98..00000000 --- a/packages/ui/components/canvas/components/twitterCard.tsx +++ /dev/null @@ -1,84 +0,0 @@ -import { BaseBoxShapeUtil, HTMLContainer, TLBaseShape, toDomPrecision } from "tldraw"; - -type ITwitterCardShape = TLBaseShape< - "Twittercard", - { w: number; h: number; url: string } ->; - -export class twitterCardUtil extends BaseBoxShapeUtil { - static override type = "Twittercard" as const; - - getDefaultProps(): ITwitterCardShape["props"] { - return { - w: 500, - h: 550, - url: "", - }; - } - - component(s: ITwitterCardShape) { - return ( - - - - ); - } - - indicator(shape: ITwitterCardShape) { - return ; - } -} - -function TwitterPost({ - isInteractive, - width, - height, - url, -}: { - isInteractive: boolean; - width: number; - height: number; - url: string; -}) { - const link = (() => { - try { - const urlObj = new URL(url); - const path = urlObj.pathname; - return path; - } catch (error) { - console.error("Invalid URL", error); - return null; - } - })(); - - return ( -