@react-fabric/draw 1.0.0-beta.3

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/_virtual/_commonjsHelpers.js +7 -0
  4. package/dist/cjs/_virtual/dom-to-image.js +5 -0
  5. package/dist/cjs/node_modules/dom-to-image/src/dom-to-image.js +777 -0
  6. package/dist/cjs/packages/draw/src/canvas/DrawCanvas.js +299 -0
  7. package/dist/cjs/packages/draw/src/canvas/DrawContext.js +22 -0
  8. package/dist/cjs/packages/draw/src/canvas/shapes/avatar/AvatarShapeTool.js +271 -0
  9. package/dist/cjs/packages/draw/src/canvas/shapes/avatar/IdleState.js +59 -0
  10. package/dist/cjs/packages/draw/src/canvas/shapes/avatar/PointingState.js +84 -0
  11. package/dist/cjs/packages/draw/src/canvas/shapes/avatar/type.js +36 -0
  12. package/dist/cjs/packages/draw/src/canvas/shapes/custom/AudioShape.js +152 -0
  13. package/dist/cjs/packages/draw/src/canvas/shapes/custom/CardShape.js +115 -0
  14. package/dist/cjs/packages/draw/src/canvas/shapes/custom/FileShape.js +128 -0
  15. package/dist/cjs/packages/draw/src/canvas/shapes/custom/ImageShape.js +137 -0
  16. package/dist/cjs/packages/draw/src/canvas/shapes/custom/VideoShape.js +160 -0
  17. package/dist/cjs/packages/draw/src/index.js +7 -0
  18. package/dist/esm/_virtual/_commonjsHelpers.js +7 -0
  19. package/dist/esm/_virtual/dom-to-image.js +5 -0
  20. package/dist/esm/node_modules/dom-to-image/src/dom-to-image.js +777 -0
  21. package/dist/esm/packages/draw/src/canvas/DrawCanvas.js +298 -0
  22. package/dist/esm/packages/draw/src/canvas/DrawContext.js +21 -0
  23. package/dist/esm/packages/draw/src/canvas/shapes/avatar/AvatarShapeTool.js +270 -0
  24. package/dist/esm/packages/draw/src/canvas/shapes/avatar/IdleState.js +59 -0
  25. package/dist/esm/packages/draw/src/canvas/shapes/avatar/PointingState.js +84 -0
  26. package/dist/esm/packages/draw/src/canvas/shapes/avatar/type.js +36 -0
  27. package/dist/esm/packages/draw/src/canvas/shapes/custom/AudioShape.js +152 -0
  28. package/dist/esm/packages/draw/src/canvas/shapes/custom/CardShape.js +115 -0
  29. package/dist/esm/packages/draw/src/canvas/shapes/custom/FileShape.js +128 -0
  30. package/dist/esm/packages/draw/src/canvas/shapes/custom/ImageShape.js +137 -0
  31. package/dist/esm/packages/draw/src/canvas/shapes/custom/VideoShape.js +160 -0
  32. package/dist/esm/packages/draw/src/index.js +3 -0
  33. package/dist/types/canvas/DrawCanvas.d.ts +13 -0
  34. package/dist/types/canvas/DrawContext.d.ts +6 -0
  35. package/dist/types/canvas/shapes/avatar/AvatarShapeTool.d.ts +37 -0
  36. package/dist/types/canvas/shapes/avatar/IdleState.d.ts +8 -0
  37. package/dist/types/canvas/shapes/avatar/PointingState.d.ts +13 -0
  38. package/dist/types/canvas/shapes/avatar/type.d.ts +9 -0
  39. package/dist/types/canvas/shapes/custom/AudioShape.d.ts +18 -0
  40. package/dist/types/canvas/shapes/custom/CardShape.d.ts +23 -0
  41. package/dist/types/canvas/shapes/custom/FileShape.d.ts +18 -0
  42. package/dist/types/canvas/shapes/custom/ImageShape.d.ts +29 -0
  43. package/dist/types/canvas/shapes/custom/VideoShape.d.ts +32 -0
  44. package/dist/types/index.d.ts +1 -0
  45. package/package.json +54 -0
  46. package/src/canvas/DrawCanvas.tsx +256 -0
  47. package/src/canvas/DrawContext.tsx +36 -0
  48. package/src/canvas/shapes/avatar/AvatarShapeTool.tsx +234 -0
  49. package/src/canvas/shapes/avatar/IdleState.ts +61 -0
  50. package/src/canvas/shapes/avatar/PointingState.ts +97 -0
  51. package/src/canvas/shapes/avatar/type.ts +41 -0
  52. package/src/canvas/shapes/custom/AudioShape.tsx +134 -0
  53. package/src/canvas/shapes/custom/CardShape.tsx +106 -0
  54. package/src/canvas/shapes/custom/FileShape.tsx +101 -0
  55. package/src/canvas/shapes/custom/ImageShape.tsx +138 -0
  56. package/src/canvas/shapes/custom/VideoShape.tsx +170 -0
  57. package/src/index.ts +24 -0
@@ -0,0 +1,41 @@
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 {
25
+ DefaultColorStyle,
26
+ DefaultFillStyle,
27
+ DefaultFontStyle,
28
+ DefaultSizeStyle,
29
+ T,
30
+ type TLBaseShape,
31
+ } from "@tldraw/tldraw";
32
+
33
+ export const AvatarShapeProps = {
34
+ color: DefaultColorStyle,
35
+ fill: DefaultFillStyle,
36
+ size: DefaultSizeStyle,
37
+ font: DefaultFontStyle,
38
+ text: T.string,
39
+ };
40
+
41
+ export type AvatarShape = TLBaseShape<"avatar", typeof AvatarShapeProps>;
@@ -0,0 +1,134 @@
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
+ /* eslint-disable jsx-a11y/media-has-caption */
25
+
26
+ import { Format } from "@react-fabric/utilities";
27
+ import {
28
+ HTMLContainer,
29
+ Rectangle2d,
30
+ ShapeUtil,
31
+ type TLBaseShape,
32
+ } from "@tldraw/tldraw";
33
+ import domtoimage from "dom-to-image";
34
+ import { useEffect, useRef } from "react";
35
+
36
+ type AudioShape = TLBaseShape<
37
+ "audio-card",
38
+ {
39
+ src: string;
40
+ poster: string;
41
+ fileName: string;
42
+ fileSize: number;
43
+ }
44
+ >;
45
+
46
+ export class AudioShapeUtil extends ShapeUtil<AudioShape> {
47
+ static override type = "audio-card" as const;
48
+ containerRef?: HTMLElement;
49
+
50
+ canResize = () => false;
51
+
52
+ getDefaultProps(): AudioShape["props"] {
53
+ return {
54
+ src: "",
55
+ poster: "",
56
+ fileName: "",
57
+ fileSize: 0,
58
+ };
59
+ }
60
+
61
+ getGeometry(shape: AudioShape) {
62
+ return new Rectangle2d({
63
+ width: 320,
64
+ height: 96,
65
+ isFilled: true,
66
+ });
67
+ }
68
+
69
+ component(shape: AudioShape) {
70
+ const audioRef = useRef<HTMLVideoElement>(null);
71
+
72
+ useEffect(() => {
73
+ const handler = () => {
74
+ audioRef.current
75
+ ?.closest(".tl-container")
76
+ ?.querySelectorAll<HTMLMediaElement>("video,audio")
77
+ .forEach((el) => {
78
+ if (el !== audioRef.current) el.pause();
79
+ });
80
+ };
81
+ audioRef.current?.addEventListener("play", handler);
82
+ }, []);
83
+
84
+ return (
85
+ <HTMLContainer className="relative grid">
86
+ <div
87
+ data-shape-id={shape.id}
88
+ className="bg-zinc-900 relative p-1 flex flex-col overflow-hidden"
89
+ >
90
+ <audio
91
+ controls
92
+ ref={audioRef}
93
+ src={shape.props.src}
94
+ className="object-contain flex-1 w-full pointer-events-auto overflow-hidden"
95
+ />
96
+ <img
97
+ alt={shape.props.fileName}
98
+ src={shape.props.poster}
99
+ className="object-fill flex-1 pointer-events-auto overflow-hidden hidden"
100
+ />
101
+ <div className="bg-zinc-800/80 text-white items-center py-1 px-2 flex gap-1 text-sm">
102
+ <svg
103
+ xmlns="http://www.w3.org/2000/svg"
104
+ viewBox="0 0 24 24"
105
+ width={16}
106
+ height={16}
107
+ >
108
+ <path d="M12,2A3,3 0 0,1 15,5V11A3,3 0 0,1 12,14A3,3 0 0,1 9,11V5A3,3 0 0,1 12,2M19,11C19,14.53 16.39,17.44 13,17.93V21H11V17.93C7.61,17.44 5,14.53 5,11H7A5,5 0 0,0 12,16A5,5 0 0,0 17,11H19Z" />
109
+ </svg>
110
+ <div className="truncate flex-1">{shape.props.fileName}</div>
111
+ <div>{Format.bytes(shape.props.fileSize ?? 0)}</div>
112
+ </div>
113
+ </div>
114
+ </HTMLContainer>
115
+ );
116
+ }
117
+
118
+ indicator(shape: AudioShape) {
119
+ return <rect />;
120
+ }
121
+
122
+ async toSvg(shape: AudioShape) {
123
+ const el = document.querySelector(`div[data-shape-id="${shape.id}"]`);
124
+ if (!el) return <img src="" alt="svg" />;
125
+
126
+ (el.childNodes.item(0) as HTMLElement).style.display = "none";
127
+ (el.childNodes.item(1) as HTMLElement).style.display = "block";
128
+ return await domtoimage.toPng(el).then(function (dataUrl) {
129
+ (el.childNodes.item(0) as HTMLElement).style.display = "block";
130
+ (el.childNodes.item(1) as HTMLElement).style.display = "none";
131
+ return <img src={dataUrl} alt="svg" />;
132
+ });
133
+ }
134
+ }
@@ -0,0 +1,106 @@
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 {
25
+ HTMLContainer,
26
+ type JsonObject,
27
+ Rectangle2d,
28
+ ShapeUtil,
29
+ type TLBaseShape,
30
+ type TLResizeInfo,
31
+ type TLShapeId,
32
+ } from "@tldraw/tldraw";
33
+ import domtoimage from "dom-to-image";
34
+ import { useDrawContext } from "../../DrawContext";
35
+
36
+ type CardShape = TLBaseShape<
37
+ "data-card",
38
+ {
39
+ w: number;
40
+ h: number;
41
+ } & KeyValue
42
+ >;
43
+
44
+ export class CardShapeUtil extends ShapeUtil<CardShape> {
45
+ static override type = "data-card" as const;
46
+
47
+ onResize(
48
+ shape: CardShape,
49
+ info: TLResizeInfo<CardShape>,
50
+ ):
51
+ | Omit<
52
+ {
53
+ id: TLShapeId;
54
+ meta?: Partial<JsonObject> | undefined;
55
+ props?: Partial<{ w: number; h: number } & KeyValue<any>> | undefined;
56
+ type: "data-card";
57
+ } & Partial<Omit<CardShape, "props" | "type" | "id" | "meta">>,
58
+ "type" | "id"
59
+ >
60
+ | undefined {
61
+ return {
62
+ props: {
63
+ w: Math.max(Math.min(info.initialBounds.w * info.scaleX, 1024), 128),
64
+ h: Math.max(Math.min(info.initialBounds.h * info.scaleY, 768), 128),
65
+ },
66
+ };
67
+ }
68
+
69
+ getDefaultProps(): CardShape["props"] {
70
+ return {
71
+ w: 300,
72
+ h: 200,
73
+ };
74
+ }
75
+
76
+ getGeometry(shape: CardShape) {
77
+ return new Rectangle2d({
78
+ width: shape.props.w,
79
+ height: shape.props.h,
80
+ isFilled: true,
81
+ });
82
+ }
83
+
84
+ component(shape: CardShape) {
85
+ const { renderer } = useDrawContext();
86
+ return (
87
+ <HTMLContainer className="relative grid">
88
+ <div className="relative grid" data-shape-id={shape.id}>
89
+ {renderer?.(shape.props)}
90
+ </div>
91
+ </HTMLContainer>
92
+ );
93
+ }
94
+
95
+ indicator(shape: CardShape) {
96
+ return <rect width={shape.props.w} height={shape.props.h} />;
97
+ }
98
+
99
+ async toSvg(shape: CardShape) {
100
+ const el = document.querySelector(`div[data-shape-id="${shape.id}"]`);
101
+ if (!el) return <img src="" alt="svg" />;
102
+ return await domtoimage.toPng(el).then(function (dataUrl) {
103
+ return <img src={dataUrl} alt="svg" />;
104
+ });
105
+ }
106
+ }
@@ -0,0 +1,101 @@
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 { Format } from "@react-fabric/utilities";
25
+ import {
26
+ HTMLContainer,
27
+ Rectangle2d,
28
+ ShapeUtil,
29
+ type TLBaseShape,
30
+ } from "@tldraw/tldraw";
31
+ import domtoimage from "dom-to-image";
32
+
33
+ type FileShape = TLBaseShape<
34
+ "file-card",
35
+ {
36
+ icon: string;
37
+ fileType: string;
38
+ fileName: string;
39
+ fileSize: number;
40
+ }
41
+ >;
42
+
43
+ export class FileShapeUtil extends ShapeUtil<FileShape> {
44
+ static override type = "file-card" as const;
45
+ containerRef?: HTMLElement;
46
+
47
+ canResize = () => false;
48
+
49
+ getDefaultProps(): FileShape["props"] {
50
+ return {
51
+ icon: "",
52
+ fileType: "",
53
+ fileName: "",
54
+ fileSize: 0,
55
+ };
56
+ }
57
+
58
+ getGeometry(shape: FileShape) {
59
+ return new Rectangle2d({
60
+ width: 320,
61
+ height: 72,
62
+ isFilled: true,
63
+ });
64
+ }
65
+
66
+ component(shape: FileShape) {
67
+ return (
68
+ <HTMLContainer className="relative grid">
69
+ <div
70
+ data-shape-id={shape.id}
71
+ className="relative p-1 flex border rounded m-1 overflow-hidden"
72
+ >
73
+ <img
74
+ src={shape.props.icon}
75
+ alt={shape.props.fileType}
76
+ className="object-contain pointer-events-none overflow-hidden w-12 h-12"
77
+ />
78
+ <div className="flex-1 overflow-hidden text-sm py-1 px-2">
79
+ <div className="items-center flex gap-1">
80
+ <div className="truncate flex-1">{shape.props.fileName}</div>
81
+ <div>{Format.bytes(shape.props.fileSize ?? 0)}</div>
82
+ </div>
83
+ <div>{shape.props.fileType}</div>
84
+ </div>
85
+ </div>
86
+ </HTMLContainer>
87
+ );
88
+ }
89
+
90
+ indicator(shape: FileShape) {
91
+ return <rect />;
92
+ }
93
+
94
+ async toSvg(shape: FileShape) {
95
+ const el = document.querySelector(`div[data-shape-id="${shape.id}"]`);
96
+ if (!el) return <img src="" alt="svg" />;
97
+ return await domtoimage.toPng(el).then(function (dataUrl) {
98
+ return <img src={dataUrl} alt="svg" />;
99
+ });
100
+ }
101
+ }
@@ -0,0 +1,138 @@
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 { Format } from "@react-fabric/utilities";
25
+ import {
26
+ HTMLContainer,
27
+ type JsonObject,
28
+ Rectangle2d,
29
+ ShapeUtil,
30
+ type TLBaseShape,
31
+ type TLResizeInfo,
32
+ type TLShapeId,
33
+ } from "@tldraw/tldraw";
34
+ import domtoimage from "dom-to-image";
35
+
36
+ type ImageShape = TLBaseShape<
37
+ "image-card",
38
+ {
39
+ w: number;
40
+ h: number;
41
+ src: string;
42
+ fileName: string;
43
+ fileSize: number;
44
+ }
45
+ >;
46
+
47
+ export class ImageShapeUtil extends ShapeUtil<ImageShape> {
48
+ static override type = "image-card" as const;
49
+
50
+ onResize(
51
+ shape: ImageShape,
52
+ info: TLResizeInfo<ImageShape>,
53
+ ):
54
+ | Omit<
55
+ {
56
+ id: TLShapeId;
57
+ meta?: Partial<JsonObject> | undefined;
58
+ props?:
59
+ | Partial<{
60
+ w: number;
61
+ h: number;
62
+ src: string;
63
+ fileName: string;
64
+ fileSize: number;
65
+ }>
66
+ | undefined;
67
+ type: "image-card";
68
+ } & Partial<Omit<ImageShape, "props" | "type" | "id" | "meta">>,
69
+ "type" | "id"
70
+ >
71
+ | undefined {
72
+ return {
73
+ props: {
74
+ w: Math.max(Math.min(info.initialBounds.w * info.scaleX, 1024), 128),
75
+ h: Math.max(Math.min(info.initialBounds.h * info.scaleY, 768), 128),
76
+ },
77
+ };
78
+ }
79
+
80
+ getDefaultProps(): ImageShape["props"] {
81
+ return {
82
+ src: "",
83
+ fileName: "",
84
+ fileSize: 0,
85
+ w: 400,
86
+ h: 300,
87
+ };
88
+ }
89
+
90
+ getGeometry(shape: ImageShape) {
91
+ return new Rectangle2d({
92
+ width: shape.props.w,
93
+ height: shape.props.h,
94
+ isFilled: true,
95
+ });
96
+ }
97
+
98
+ component(shape: ImageShape) {
99
+ return (
100
+ <HTMLContainer className="relative grid">
101
+ <div
102
+ data-shape-id={shape.id}
103
+ className="bg-zinc-900 relative p-1 flex flex-col overflow-hidden"
104
+ >
105
+ <img
106
+ src={shape.props.src}
107
+ alt={shape.props.fileName}
108
+ className="object-contain flex-1 pointer-events-none overflow-hidden"
109
+ />
110
+ <div className="bg-zinc-800/80 text-white items-center py-1 px-2 flex gap-1 text-sm">
111
+ <svg
112
+ xmlns="http://www.w3.org/2000/svg"
113
+ viewBox="0 0 24 24"
114
+ width={16}
115
+ height={16}
116
+ >
117
+ <path d="M4,4H7L9,2H15L17,4H20A2,2 0 0,1 22,6V18A2,2 0 0,1 20,20H4A2,2 0 0,1 2,18V6A2,2 0 0,1 4,4M12,7A5,5 0 0,0 7,12A5,5 0 0,0 12,17A5,5 0 0,0 17,12A5,5 0 0,0 12,7M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9Z" />
118
+ </svg>
119
+ <div className="truncate flex-1">{shape.props.fileName}</div>
120
+ <div>{Format.bytes(shape.props.fileSize ?? 0)}</div>
121
+ </div>
122
+ </div>
123
+ </HTMLContainer>
124
+ );
125
+ }
126
+
127
+ indicator(shape: ImageShape) {
128
+ return <rect width={shape.props.w} height={shape.props.h} />;
129
+ }
130
+
131
+ async toSvg(shape: ImageShape) {
132
+ const el = document.querySelector(`div[data-shape-id="${shape.id}"]`);
133
+ if (!el) return <img src="" alt="svg" />;
134
+ return await domtoimage.toPng(el).then(function (dataUrl) {
135
+ return <img src={dataUrl} alt="svg" />;
136
+ });
137
+ }
138
+ }
@@ -0,0 +1,170 @@
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
+ /* eslint-disable jsx-a11y/media-has-caption */
25
+
26
+ import { Format } from "@react-fabric/utilities";
27
+ import {
28
+ HTMLContainer,
29
+ type JsonObject,
30
+ Rectangle2d,
31
+ ShapeUtil,
32
+ type TLBaseShape,
33
+ type TLResizeInfo,
34
+ type TLShapeId,
35
+ } from "@tldraw/tldraw";
36
+ import domtoimage from "dom-to-image";
37
+ import { useEffect, useRef } from "react";
38
+
39
+ type VideoShape = TLBaseShape<
40
+ "video-card",
41
+ {
42
+ w: number;
43
+ h: number;
44
+ src: string;
45
+ poster: string;
46
+ fileName: string;
47
+ fileSize: number;
48
+ }
49
+ >;
50
+
51
+ export class VideoShapeUtil extends ShapeUtil<VideoShape> {
52
+ static override type = "video-card" as const;
53
+ containerRef?: HTMLElement;
54
+
55
+ onResize(
56
+ shape: VideoShape,
57
+ info: TLResizeInfo<VideoShape>,
58
+ ):
59
+ | Omit<
60
+ {
61
+ id: TLShapeId;
62
+ meta?: Partial<JsonObject> | undefined;
63
+ props?:
64
+ | Partial<{
65
+ w: number;
66
+ h: number;
67
+ src: string;
68
+ poster: string;
69
+ fileName: string;
70
+ fileSize: number;
71
+ }>
72
+ | undefined;
73
+ type: "video-card";
74
+ } & Partial<Omit<VideoShape, "props" | "type" | "id" | "meta">>,
75
+ "type" | "id"
76
+ >
77
+ | undefined {
78
+ return {
79
+ props: {
80
+ w: Math.max(Math.min(info.initialBounds.w * info.scaleX, 1024), 128),
81
+ h: Math.max(Math.min(info.initialBounds.h * info.scaleY, 768), 128),
82
+ },
83
+ };
84
+ }
85
+
86
+ getDefaultProps(): VideoShape["props"] {
87
+ return {
88
+ src: "",
89
+ poster: "",
90
+ fileName: "",
91
+ fileSize: 0,
92
+ w: 400,
93
+ h: 300,
94
+ };
95
+ }
96
+
97
+ getGeometry(shape: VideoShape) {
98
+ return new Rectangle2d({
99
+ width: shape.props.w,
100
+ height: shape.props.h,
101
+ isFilled: true,
102
+ });
103
+ }
104
+
105
+ component(shape: VideoShape) {
106
+ const videoRef = useRef<HTMLVideoElement>(null);
107
+
108
+ useEffect(() => {
109
+ const handler = () => {
110
+ videoRef.current
111
+ ?.closest(".tl-container")
112
+ ?.querySelectorAll<HTMLMediaElement>("video,audio")
113
+ .forEach((el) => {
114
+ if (el !== videoRef.current) el.pause();
115
+ });
116
+ };
117
+ videoRef.current?.addEventListener("play", handler);
118
+ }, []);
119
+
120
+ return (
121
+ <HTMLContainer className="relative grid">
122
+ <div
123
+ data-shape-id={shape.id}
124
+ className="bg-zinc-900 relative p-1 flex flex-col overflow-hidden"
125
+ >
126
+ <video
127
+ controls
128
+ ref={videoRef}
129
+ src={shape.props.src}
130
+ poster={shape.props.poster}
131
+ className="object-contain flex-1 pointer-events-auto overflow-hidden"
132
+ />
133
+ <img
134
+ src={shape.props.poster}
135
+ alt={shape.props.fileName}
136
+ className="object-contain flex-1 pointer-events-auto overflow-hidden hidden"
137
+ />
138
+ <div className="bg-zinc-800/80 text-white items-center py-1 px-2 flex gap-1 text-sm">
139
+ <svg
140
+ xmlns="http://www.w3.org/2000/svg"
141
+ viewBox="0 0 24 24"
142
+ width={16}
143
+ height={16}
144
+ >
145
+ <path d="M17,10.5V7A1,1 0 0,0 16,6H4A1,1 0 0,0 3,7V17A1,1 0 0,0 4,18H16A1,1 0 0,0 17,17V13.5L21,17.5V6.5L17,10.5Z" />
146
+ </svg>
147
+ <div className="truncate flex-1">{shape.props.fileName}</div>
148
+ <div>{Format.bytes(shape.props.fileSize ?? 0)}</div>
149
+ </div>
150
+ </div>
151
+ </HTMLContainer>
152
+ );
153
+ }
154
+
155
+ indicator(shape: VideoShape) {
156
+ return <rect width={shape.props.w} height={shape.props.h} />;
157
+ }
158
+
159
+ async toSvg(shape: VideoShape) {
160
+ const el = document.querySelector(`div[data-shape-id="${shape.id}"]`);
161
+ if (!el) return <img src="" alt="svg" />;
162
+ (el.childNodes.item(0) as HTMLElement).style.display = "none";
163
+ (el.childNodes.item(1) as HTMLElement).style.display = "block";
164
+ return await domtoimage.toPng(el).then(function (dataUrl) {
165
+ (el.childNodes.item(0) as HTMLElement).style.display = "block";
166
+ (el.childNodes.item(1) as HTMLElement).style.display = "none";
167
+ return <img src={dataUrl} alt="svg" />;
168
+ });
169
+ }
170
+ }