@react-fabric/draw 1.0.0-beta.11
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.
- package/LICENSE +21 -0
- package/README.md +10 -0
- package/dist/cjs/_virtual/_commonjsHelpers.js +7 -0
- package/dist/cjs/_virtual/dom-to-image.js +5 -0
- package/dist/cjs/node_modules/dom-to-image/src/dom-to-image.js +777 -0
- package/dist/cjs/packages/draw/src/canvas/DrawCanvas.js +326 -0
- package/dist/cjs/packages/draw/src/canvas/DrawContext.js +22 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/avatar/AvatarShapeTool.js +271 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/avatar/IdleState.js +59 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/avatar/PointingState.js +84 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/avatar/type.js +36 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/custom/AudioShape.js +152 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/custom/CardShape.js +115 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/custom/FileShape.js +128 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/custom/ImageShape.js +137 -0
- package/dist/cjs/packages/draw/src/canvas/shapes/custom/VideoShape.js +160 -0
- package/dist/cjs/packages/draw/src/index.js +7 -0
- package/dist/esm/_virtual/_commonjsHelpers.js +7 -0
- package/dist/esm/_virtual/dom-to-image.js +5 -0
- package/dist/esm/node_modules/dom-to-image/src/dom-to-image.js +777 -0
- package/dist/esm/packages/draw/src/canvas/DrawCanvas.js +325 -0
- package/dist/esm/packages/draw/src/canvas/DrawContext.js +21 -0
- package/dist/esm/packages/draw/src/canvas/shapes/avatar/AvatarShapeTool.js +270 -0
- package/dist/esm/packages/draw/src/canvas/shapes/avatar/IdleState.js +59 -0
- package/dist/esm/packages/draw/src/canvas/shapes/avatar/PointingState.js +84 -0
- package/dist/esm/packages/draw/src/canvas/shapes/avatar/type.js +36 -0
- package/dist/esm/packages/draw/src/canvas/shapes/custom/AudioShape.js +152 -0
- package/dist/esm/packages/draw/src/canvas/shapes/custom/CardShape.js +115 -0
- package/dist/esm/packages/draw/src/canvas/shapes/custom/FileShape.js +128 -0
- package/dist/esm/packages/draw/src/canvas/shapes/custom/ImageShape.js +137 -0
- package/dist/esm/packages/draw/src/canvas/shapes/custom/VideoShape.js +160 -0
- package/dist/esm/packages/draw/src/index.js +3 -0
- package/dist/types/canvas/DrawCanvas.d.ts +13 -0
- package/dist/types/canvas/DrawContext.d.ts +6 -0
- package/dist/types/canvas/shapes/avatar/AvatarShapeTool.d.ts +37 -0
- package/dist/types/canvas/shapes/avatar/IdleState.d.ts +8 -0
- package/dist/types/canvas/shapes/avatar/PointingState.d.ts +13 -0
- package/dist/types/canvas/shapes/avatar/type.d.ts +9 -0
- package/dist/types/canvas/shapes/custom/AudioShape.d.ts +18 -0
- package/dist/types/canvas/shapes/custom/CardShape.d.ts +23 -0
- package/dist/types/canvas/shapes/custom/FileShape.d.ts +18 -0
- package/dist/types/canvas/shapes/custom/ImageShape.d.ts +29 -0
- package/dist/types/canvas/shapes/custom/VideoShape.d.ts +32 -0
- package/dist/types/index.d.ts +1 -0
- package/package.json +54 -0
- package/src/canvas/DrawCanvas.tsx +291 -0
- package/src/canvas/DrawContext.tsx +36 -0
- package/src/canvas/shapes/avatar/AvatarShapeTool.tsx +234 -0
- package/src/canvas/shapes/avatar/IdleState.ts +61 -0
- package/src/canvas/shapes/avatar/PointingState.ts +97 -0
- package/src/canvas/shapes/avatar/type.ts +41 -0
- package/src/canvas/shapes/custom/AudioShape.tsx +134 -0
- package/src/canvas/shapes/custom/CardShape.tsx +106 -0
- package/src/canvas/shapes/custom/FileShape.tsx +101 -0
- package/src/canvas/shapes/custom/ImageShape.tsx +138 -0
- package/src/canvas/shapes/custom/VideoShape.tsx +170 -0
- package/src/index.ts +24 -0
|
@@ -0,0 +1,97 @@
|
|
|
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 { StateNode, type TLEventHandlers } from "@tldraw/editor";
|
|
25
|
+
import { createShapeId } from "@tldraw/tldraw";
|
|
26
|
+
import { type AvatarShape } from "./type";
|
|
27
|
+
|
|
28
|
+
export class Pointing extends StateNode {
|
|
29
|
+
static override id = "pointing";
|
|
30
|
+
|
|
31
|
+
shape?: AvatarShape;
|
|
32
|
+
|
|
33
|
+
markId = "";
|
|
34
|
+
|
|
35
|
+
override onPointerUp: TLEventHandlers["onPointerUp"] = () => {
|
|
36
|
+
this.cancel();
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
override onCancel: TLEventHandlers["onCancel"] = () => {
|
|
40
|
+
this.cancel();
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
override onComplete: TLEventHandlers["onComplete"] = () => {
|
|
44
|
+
this.cancel();
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
override onInterrupt: TLEventHandlers["onInterrupt"] = () => {
|
|
48
|
+
this.cancel();
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
override onPointerMove: TLEventHandlers["onPointerMove"] = (info) => {
|
|
52
|
+
if (this.editor.inputs.isDragging) {
|
|
53
|
+
const { originPagePoint } = this.editor.inputs;
|
|
54
|
+
|
|
55
|
+
const id = createShapeId();
|
|
56
|
+
|
|
57
|
+
this.markId = `creating:${id}`;
|
|
58
|
+
|
|
59
|
+
this.editor.mark(this.markId);
|
|
60
|
+
|
|
61
|
+
this.editor
|
|
62
|
+
.createShapes<AvatarShape>([
|
|
63
|
+
{
|
|
64
|
+
id,
|
|
65
|
+
type: "avatar",
|
|
66
|
+
x: originPagePoint.x,
|
|
67
|
+
y: originPagePoint.y,
|
|
68
|
+
props: {
|
|
69
|
+
color: "black",
|
|
70
|
+
fill: "none",
|
|
71
|
+
size: "m",
|
|
72
|
+
font: "draw",
|
|
73
|
+
text: "person",
|
|
74
|
+
} as AnyObject,
|
|
75
|
+
},
|
|
76
|
+
])
|
|
77
|
+
.select(id)
|
|
78
|
+
.setCurrentTool("select.resizing", {
|
|
79
|
+
...info,
|
|
80
|
+
target: "selection",
|
|
81
|
+
handle: "bottom_right",
|
|
82
|
+
isCreating: true,
|
|
83
|
+
creationCursorOffset: { x: 1, y: 1 },
|
|
84
|
+
onInteractionEnd: "avatar",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
cancel() {
|
|
90
|
+
if (this.shape) {
|
|
91
|
+
// the arrow might not have been created yet!
|
|
92
|
+
this.editor.bailToMark(this.markId);
|
|
93
|
+
}
|
|
94
|
+
this.editor.setHintingShapes([]);
|
|
95
|
+
this.parent.transition("idle", {});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -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
|
+
}
|