-
Notifications
You must be signed in to change notification settings - Fork 3
/
Canvas.js
134 lines (125 loc) · 3.62 KB
/
Canvas.js
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Tldraw, createTLStore, defaultShapeUtils } from "@tldraw/tldraw";
import { Widget } from "near-social-vm";
import { default as React, useCallback, useState } from "react";
import { useUrlState } from "../../../hooks/useUrlState";
import { ActionButton } from "../../ActionButton";
import { ResponseShapeUtil } from "./ResponseShape";
import SharePanel from "./SharePanel";
import { TldrawLogo } from "./TldrawLogo";
import TopZone from "./TopZone";
import { ZoomIn } from "./ZoomUI";
import { Templates } from "./Templates";
const shapeUtils = [ResponseShapeUtil];
export function UrlStateSync() {
const syncViewport = useCallback((params) => {
window.history.replaceState(
{},
document.title,
window.location.pathname + `?v=${params.v}&p=${params.p}`
);
}, []);
useUrlState(syncViewport);
return null;
}
function TldrawCanvas({
persistance,
autoFocus = true,
hideUi = false,
isReadOnly = false,
initialSnapshot,
}) {
const [store] = useState(() => {
if (initialSnapshot) {
const newStore = createTLStore({
shapeUtils: defaultShapeUtils.concat(shapeUtils),
});
newStore.loadSnapshot(initialSnapshot);
return newStore;
}
});
const handleMount = useCallback(
(editor) => {
window.app = editor;
window.editor = editor;
editor.updateInstanceState({ isReadonly: isReadOnly });
// editor.user.updateUserPreferences({
// id: accountId,
// });
// editor.getInitialMetaForShape = (_shape) => {
// return {
// createdBy: editor.user.getId(),
// createdAt: Date.now(),
// updatedBy: editor.user.getId(),
// updatedAt: Date.now(),
// };
// };
// editor.registerExternalAssetHandler("file", createAssetFromFile);
// editor.registerExternalAssetHandler("url", createAssetFromUrl);
},
[isReadOnly]
);
function loadComponents(c = {}) {
return Object.keys(c).reduce((acc, key) => {
if (!c[key]) {
acc[key] = null;
} else {
if (typeof c[key] === "function") {
acc[key] = c[key];
} else {
const plugin = c[key];
acc[key] = () => (
<div
key={key}
className={`tldraw__${key}`}
style={{ pointerEvents: "all", display: "flex" }}
>
<Widget src={plugin.src} props={{ ...plugin.props, color, name, id }} />
</div>
);
}
}
return acc;
}, {});
}
return (
<div className={"tldraw__editor"}>
<Tldraw
persistenceKey={persistance || "everyone"}
autoFocus={autoFocus}
hideUi={hideUi}
store={store}
shapeUtils={shapeUtils}
onMount={handleMount}
initialState={isReadOnly ? "hand" : "select"}
components={loadComponents({
// props.components
TopPanel: () => (
<div
key={"TopPanel"}
className="tldraw__TopPanel"
style={{ pointerEvents: "all", display: "flex" }}
>
<TopZone path={persistance} />
</div>
),
SharePanel: () => (
<div
key={"SharePanel"}
className="tldraw__SharePanel"
style={{ pointerEvents: "all", display: "flex" }}
>
<SharePanel path={persistance} />
</div>
),
})}
>
<ActionButton path={persistance} />
<TldrawLogo />
<ZoomIn />
<Templates />
<UrlStateSync />
</Tldraw>
</div>
);
}
export default TldrawCanvas;