-
Notifications
You must be signed in to change notification settings - Fork 303
/
canvas.tsx
94 lines (84 loc) · 2.77 KB
/
canvas.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { useCallback, useEffect, useMemo, 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<boolean>(false);
const Dragref = useRef<HTMLDivElement | null>(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 (
<DragContext.Provider value={{ isDraggingOver, setIsDraggingOver }}>
<div ref={Dragref} className="w-full h-full">
<TldrawComponent />
</div>
</DragContext.Provider>
);
});
const TldrawComponent = memo(() => {
const [storeWithStatus, setStoreWithStatus] = useState<TLStoreWithStatus>({
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", isDarkMode: true });
const assetUrls = getAssetUrls();
return (
<div className="w-full h-full">
<Tldraw
className="relative"
assetUrls={assetUrls}
components={components}
store={storeWithStatus}
shapeUtils={[twitterCardUtil, textCardUtil]}
onMount={handleMount}
>
<div className="absolute left-1/2 top-0 z-[1000000] flex -translate-x-1/2 gap-2 bg-[#2C3439] text-[#B3BCC5]">
<SaveStatus />
</div>
<DropZone />
</Tldraw>
</div>
);
});