@react-fabric/draw 1.0.0-beta.61 → 1.0.0-beta.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. package/dist/cjs/canvas/DrawCanvas.js +36 -114
  2. package/dist/cjs/canvas/{DrawContext.js → Provider.js} +2 -7
  3. package/dist/cjs/canvas/context.js +34 -0
  4. package/dist/cjs/canvas/exportToSvg.js +107 -0
  5. package/dist/cjs/canvas/shapes/avatar/AvatarShapeTool.js +7 -7
  6. package/dist/cjs/canvas/shapes/custom/AudioShape.js +7 -7
  7. package/dist/cjs/canvas/shapes/custom/CardShape.js +9 -9
  8. package/dist/cjs/canvas/shapes/custom/FileShape.js +7 -7
  9. package/dist/cjs/canvas/shapes/custom/ImageShape.js +7 -7
  10. package/dist/cjs/canvas/shapes/custom/VideoShape.js +7 -7
  11. package/dist/esm/canvas/DrawCanvas.js +35 -112
  12. package/dist/esm/canvas/{DrawContext.js → Provider.js} +2 -6
  13. package/dist/esm/canvas/context.js +33 -0
  14. package/dist/esm/canvas/exportToSvg.js +107 -0
  15. package/dist/esm/canvas/shapes/avatar/AvatarShapeTool.js +7 -7
  16. package/dist/esm/canvas/shapes/custom/AudioShape.js +7 -7
  17. package/dist/esm/canvas/shapes/custom/CardShape.js +8 -8
  18. package/dist/esm/canvas/shapes/custom/FileShape.js +7 -7
  19. package/dist/esm/canvas/shapes/custom/ImageShape.js +7 -7
  20. package/dist/esm/canvas/shapes/custom/VideoShape.js +7 -7
  21. package/dist/types/canvas/DrawCanvas.d.ts +1 -2
  22. package/dist/types/canvas/Provider.d.ts +3 -0
  23. package/dist/types/canvas/context.d.ts +5 -0
  24. package/dist/types/canvas/exportToSvg.d.ts +1 -0
  25. package/dist/types/canvas/shapes/avatar/AvatarShapeTool.d.ts +5 -5
  26. package/dist/types/canvas/shapes/avatar/type.d.ts +4 -4
  27. package/package.json +10 -11
  28. package/src/canvas/DrawCanvas.tsx +10 -66
  29. package/src/canvas/Provider.tsx +29 -0
  30. package/src/canvas/{DrawContext.tsx → context.ts} +3 -7
  31. package/src/canvas/exportToSvg.ts +50 -0
  32. package/src/canvas/shapes/avatar/AvatarShapeTool.tsx +0 -3
  33. package/src/canvas/shapes/custom/CardShape.tsx +1 -1
  34. package/dist/types/canvas/DrawContext.d.ts +0 -6
@@ -0,0 +1,3 @@
1
+ import { PropsWithChildren } from "react";
2
+ import { Props } from "./context";
3
+ export declare const DrawContextProvider: ({ children, ...props }: PropsWithChildren<Props>) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,5 @@
1
+ export interface Props {
2
+ renderer?: (props: KeyValue) => AnyObject;
3
+ }
4
+ export declare const DrawContext: import("react").Context<Props>;
5
+ export declare const useDrawContext: () => Props;
@@ -0,0 +1 @@
1
+ export declare function getSvgAsDataUrl(svg: SVGElement): Promise<string>;
@@ -13,11 +13,11 @@ export declare class AvatarShapeTool extends StateNode {
13
13
  export declare class AvatarShapeUtil extends ShapeUtil<AvatarShape> {
14
14
  static type: "avatar";
15
15
  static props: {
16
- color: import("@tldraw/tlschema").EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">;
17
- fill: import("@tldraw/tlschema").EnumStyleProp<"fill" | "none" | "pattern" | "semi" | "solid">;
18
- size: import("@tldraw/tlschema").EnumStyleProp<"l" | "m" | "s" | "xl">;
19
- font: import("@tldraw/tlschema").EnumStyleProp<"draw" | "mono" | "sans" | "serif">;
20
- text: import("@tldraw/validate").Validator<string>;
16
+ color: import("tldraw").EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">;
17
+ fill: import("tldraw").EnumStyleProp<"pattern" | "fill" | "none" | "semi" | "solid">;
18
+ size: import("tldraw").EnumStyleProp<"s" | "l" | "m" | "xl">;
19
+ font: import("tldraw").EnumStyleProp<"draw" | "mono" | "sans" | "serif">;
20
+ text: import("tldraw").Validator<string>;
21
21
  };
22
22
  el: HTMLDivElement;
23
23
  canEdit: () => boolean;
@@ -1,9 +1,9 @@
1
1
  import { T, type TLBaseShape } from "@tldraw/tldraw";
2
2
  export declare const AvatarShapeProps: {
3
- color: import("@tldraw/tlschema").EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">;
4
- fill: import("@tldraw/tlschema").EnumStyleProp<"fill" | "none" | "pattern" | "semi" | "solid">;
5
- size: import("@tldraw/tlschema").EnumStyleProp<"l" | "m" | "s" | "xl">;
6
- font: import("@tldraw/tlschema").EnumStyleProp<"draw" | "mono" | "sans" | "serif">;
3
+ color: import("tldraw").EnumStyleProp<"black" | "blue" | "green" | "grey" | "light-blue" | "light-green" | "light-red" | "light-violet" | "orange" | "red" | "violet" | "white" | "yellow">;
4
+ fill: import("tldraw").EnumStyleProp<"pattern" | "fill" | "none" | "semi" | "solid">;
5
+ size: import("tldraw").EnumStyleProp<"s" | "l" | "m" | "xl">;
6
+ font: import("tldraw").EnumStyleProp<"draw" | "mono" | "sans" | "serif">;
7
7
  text: T.Validator<string>;
8
8
  };
9
9
  export type AvatarShape = TLBaseShape<"avatar", typeof AvatarShapeProps>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/draw",
3
- "version": "1.0.0-beta.61",
3
+ "version": "1.0.0-beta.63",
4
4
  "description": "RUF: TLDraw canvas",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -33,26 +33,25 @@
33
33
  "watch": "npm-run-all -p watch:rollup watch:dts"
34
34
  },
35
35
  "dependencies": {
36
- "@react-fabric/core": "^1.0.0-beta.61",
37
- "@react-fabric/utilities": "^1.0.0-beta.61",
36
+ "@react-fabric/core": "^1.0.0-beta.63",
37
+ "@react-fabric/utilities": "^1.0.0-beta.63",
38
38
  "@tldraw/assets": "~4.1.2",
39
39
  "@tldraw/editor": "~4.1.2",
40
40
  "@tldraw/tldraw": "~4.1.2",
41
- "classnames": "~2.5.1",
42
41
  "dom-to-image": "~2.6.0"
43
42
  },
44
43
  "peerDependencies": {
45
- "i18next": "~25.6.0",
46
- "react": "~19.2.0",
47
- "react-dom": "~19.2.0",
48
- "react-i18next": "~16.2.1"
44
+ "i18next": "~25.8.18",
45
+ "react": "~19.2.4",
46
+ "react-dom": "~19.2.4",
47
+ "react-i18next": "~16.5.8"
49
48
  },
50
49
  "devDependencies": {
51
50
  "@types/dom-to-image": "~2.6.7"
52
51
  },
53
52
  "engines": {
54
- "node": ">=18.12.0",
55
- "npm": ">=8.19.0"
53
+ "node": ">=22.18.0",
54
+ "npm": ">=10.8.0"
56
55
  },
57
- "gitHead": "a0b0c571bf9c189fa3355bbc3b896ecc4b9d6a04"
56
+ "gitHead": "6e0652b15c6384fbc316842862572e21299a5466"
58
57
  }
@@ -48,11 +48,9 @@ import {
48
48
  type FC,
49
49
  type Ref,
50
50
  } from "react";
51
- import { DrawContextProvider } from "./DrawContext";
52
- import {
53
- AvatarShapeTool,
54
- AvatarShapeUtil,
55
- } from "./shapes/avatar/AvatarShapeTool";
51
+ import { getSvgAsDataUrl } from "./exportToSvg";
52
+ import { DrawContextProvider } from "./Provider";
53
+ import { AvatarShapeTool, AvatarShapeUtil } from "./shapes/avatar/AvatarShapeTool";
56
54
  import { AudioShapeUtil } from "./shapes/custom/AudioShape";
57
55
  import { CardShapeUtil } from "./shapes/custom/CardShape";
58
56
  import { FileShapeUtil } from "./shapes/custom/FileShape";
@@ -66,37 +64,6 @@ const customAssetUrls: TLUiAssetUrlOverrides = {
66
64
  },
67
65
  };
68
66
 
69
- export async function getSvgAsDataUrl(svg: SVGElement) {
70
- const clone = svg.cloneNode(true) as SVGGraphicsElement;
71
- clone.setAttribute("encoding", 'UTF-8"');
72
-
73
- const fileReader = new FileReader();
74
- const imgs = Array.from(clone.querySelectorAll("image"));
75
-
76
- for (const img of imgs) {
77
- const src = img.getAttribute("xlink:href");
78
- if (src) {
79
- if (!src.startsWith("data:")) {
80
- const blob = await (await fetch(src)).blob();
81
- const base64 = await new Promise<string>((resolve, reject) => {
82
- fileReader.onload = () => resolve(fileReader.result as string);
83
- fileReader.onerror = () => reject(fileReader.error);
84
- fileReader.readAsDataURL(blob);
85
- });
86
- img.setAttribute("xlink:href", base64);
87
- }
88
- }
89
- }
90
-
91
- const svgStr = new XMLSerializer().serializeToString(clone);
92
- // NOTE: `unescape` works everywhere although deprecated
93
- const base64SVG =
94
- typeof window !== "undefined"
95
- ? window.btoa(unescape(encodeURIComponent(svgStr)))
96
- : "";
97
- return `data:image/svg+xml;base64,${base64SVG}`;
98
- }
99
-
100
67
  export interface DrawProps {
101
68
  snapshot?: TLEditorSnapshot;
102
69
  onUpdate?: (snapshot: TLEditorSnapshot) => void;
@@ -119,7 +86,7 @@ const TypeMap: KeyValue = {
119
86
  * It also provides a method to export the current pages as snapshots.
120
87
  *
121
88
  * @param {DrawProps} props - The properties for the component.
122
- * @returns {JSX.Element} The rendered DrawCanvas component.
89
+ * @returns {React.ReactElement} The rendered DrawCanvas component.
123
90
  *
124
91
  * @example
125
92
  * ```jsx
@@ -133,12 +100,7 @@ const TypeMap: KeyValue = {
133
100
  * @see {@link https://adarshpastakia.github.io/react-fabric/?path=/story/draw-playground--playground}
134
101
  * @see {@link https://tldraw.com/docs/}
135
102
  */
136
- export const DrawCanvas: FC<DrawProps> = ({
137
- snapshot,
138
- renderer,
139
- onUpdate,
140
- canvasRef,
141
- }) => {
103
+ export const DrawCanvas: FC<DrawProps> = ({ snapshot, renderer, onUpdate, canvasRef }) => {
142
104
  const [editorRef, setEditor] = useState<Editor>();
143
105
  const isDark = useIsDark();
144
106
 
@@ -184,8 +146,7 @@ export const DrawCanvas: FC<DrawProps> = ({
184
146
  }
185
147
  const { type, ...props } = JSON.parse(data);
186
148
  if (type in TypeMap) {
187
- const { x = 0, y = 0 } =
188
- editorRef?.getContainer().getBoundingClientRect() ?? {};
149
+ const { x = 0, y = 0 } = editorRef?.getContainer().getBoundingClientRect() ?? {};
189
150
  editorRef?.createShape({
190
151
  type: TypeMap[type],
191
152
  x: e.clientX - x,
@@ -203,9 +164,7 @@ export const DrawCanvas: FC<DrawProps> = ({
203
164
  () => ({
204
165
  exportPages: async () => {
205
166
  const currentPage = editorRef?.getCurrentPageId();
206
- const pages = Object.keys(
207
- editorRef?.store.getStoreSnapshot().store ?? {},
208
- ).filter((key) => key.startsWith("page:"));
167
+ const pages = Object.keys(editorRef?.store.getStoreSnapshot().store ?? {}).filter((key) => key.startsWith("page:"));
209
168
  const pageSnapshots: KeyValue[] = [];
210
169
  while (pages.length) {
211
170
  const pg: AnyObject = pages.shift();
@@ -241,14 +200,7 @@ export const DrawCanvas: FC<DrawProps> = ({
241
200
  () => (
242
201
  <Tldraw
243
202
  assetUrls={customAssetUrls}
244
- shapeUtils={[
245
- AvatarShapeUtil,
246
- AudioShapeUtil,
247
- ImageShapeUtil,
248
- VideoShapeUtil,
249
- CardShapeUtil,
250
- FileShapeUtil,
251
- ]}
203
+ shapeUtils={[AvatarShapeUtil, AudioShapeUtil, ImageShapeUtil, VideoShapeUtil, CardShapeUtil, FileShapeUtil]}
252
204
  tools={[AvatarShapeTool]}
253
205
  onMount={setEditor}
254
206
  components={{
@@ -258,10 +210,7 @@ export const DrawCanvas: FC<DrawProps> = ({
258
210
  return (
259
211
  <DefaultToolbar {...props}>
260
212
  <DefaultToolbarContent />
261
- <TldrawUiMenuItem
262
- {...tools.avatar}
263
- isSelected={isAvatarSelected}
264
- />
213
+ <TldrawUiMenuItem {...tools.avatar} isSelected={isAvatarSelected} />
265
214
  </DefaultToolbar>
266
215
  );
267
216
  },
@@ -298,12 +247,7 @@ export const DrawCanvas: FC<DrawProps> = ({
298
247
  return (
299
248
  <DrawContextProvider renderer={renderer}>
300
249
  <Section>
301
- <div
302
- className="absolute inset-0"
303
- onDragOver={handleDragOver}
304
- onDrop={handleDrop}
305
- data-ref="drawCanvas"
306
- >
250
+ <div className="absolute inset-0" onDragOver={handleDragOver} onDrop={handleDrop} data-ref="drawCanvas">
307
251
  {TLDraw}
308
252
  </div>
309
253
  </Section>
@@ -0,0 +1,29 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { PropsWithChildren } from "react";
25
+ import { DrawContext, Props } from "./context";
26
+
27
+ export const DrawContextProvider = ({ children, ...props }: PropsWithChildren<Props>) => {
28
+ return <DrawContext.Provider value={props}>{children}</DrawContext.Provider>;
29
+ };
@@ -4,7 +4,7 @@
4
4
  *
5
5
  *
6
6
  * The MIT License (MIT)
7
- * Copyright (c) 2024 Adarsh Pastakia
7
+ * Copyright (c) 2026 Adarsh Pastakia
8
8
  *
9
9
  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
10
  * and associated documentation files (the "Software"), to deal in the Software without restriction,
@@ -23,14 +23,10 @@
23
23
 
24
24
  import { createContext, useContext } from "react";
25
25
 
26
- interface Props {
26
+ export interface Props {
27
27
  renderer?: (props: KeyValue) => AnyObject;
28
28
  }
29
29
 
30
- const DrawContext = createContext<Props>({});
31
-
32
- export const DrawContextProvider = ({ children, ...props }: KeyValue) => {
33
- return <DrawContext.Provider value={props}>{children}</DrawContext.Provider>;
34
- };
30
+ export const DrawContext = createContext<Props>({});
35
31
 
36
32
  export const useDrawContext = () => useContext(DrawContext);
@@ -0,0 +1,50 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2026 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ export async function getSvgAsDataUrl(svg: SVGElement) {
25
+ const clone = svg.cloneNode(true) as SVGGraphicsElement;
26
+ clone.setAttribute("encoding", 'UTF-8"');
27
+
28
+ const fileReader = new FileReader();
29
+ const imgs = Array.from(clone.querySelectorAll("image"));
30
+
31
+ for (const img of imgs) {
32
+ const src = img.getAttribute("xlink:href");
33
+ if (src) {
34
+ if (!src.startsWith("data:")) {
35
+ const blob = await (await fetch(src)).blob();
36
+ const base64 = await new Promise<string>((resolve, reject) => {
37
+ fileReader.onload = () => resolve(fileReader.result as string);
38
+ fileReader.onerror = () => reject(fileReader.error);
39
+ fileReader.readAsDataURL(blob);
40
+ });
41
+ img.setAttribute("xlink:href", base64);
42
+ }
43
+ }
44
+ }
45
+
46
+ const svgStr = new XMLSerializer().serializeToString(clone);
47
+ // NOTE: `unescape` works everywhere although deprecated
48
+ const base64SVG = typeof window !== "undefined" ? window.btoa(unescape(encodeURIComponent(svgStr))) : "";
49
+ return `data:image/svg+xml;base64,${base64SVG}`;
50
+ }
@@ -21,9 +21,6 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- /* eslint-disable @typescript-eslint/restrict-template-expressions */
25
- /* eslint-disable @typescript-eslint/no-base-to-string */
26
-
27
24
  import { useIsDark } from "@react-fabric/core";
28
25
  import { StateNode } from "@tldraw/editor";
29
26
  import {
@@ -31,7 +31,7 @@ import {
31
31
  type TLShapeId,
32
32
  } from "@tldraw/tldraw";
33
33
  import domtoimage from "dom-to-image";
34
- import { useDrawContext } from "../../DrawContext";
34
+ import { useDrawContext } from "../../context";
35
35
 
36
36
  type CardShape = TLBaseShape<
37
37
  "data-card",
@@ -1,6 +0,0 @@
1
- interface Props {
2
- renderer?: (props: KeyValue) => AnyObject;
3
- }
4
- export declare const DrawContextProvider: ({ children, ...props }: KeyValue) => import("react/jsx-runtime").JSX.Element;
5
- export declare const useDrawContext: () => Props;
6
- export {};