labellife-design-tool 0.7.0 → 0.9.0
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/README.md +51 -0
- package/dist/lib/CanvasEditor.d.ts +11 -2
- package/dist/lib/CanvasEditor.d.ts.map +1 -1
- package/dist/lib/index.js +43 -4
- package/dist/lib/lib/index.d.ts +1 -1
- package/dist/lib/lib/index.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -66,6 +66,57 @@ setUnsplashAccessKey('your-unsplash-access-key');
|
|
|
66
66
|
// UNSPLASH_ACCESS_KEY=your-key npm start
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
### Using Refs with CanvasEditor
|
|
70
|
+
|
|
71
|
+
The `CanvasEditor` component supports React refs, allowing you to access internal methods and properties:
|
|
72
|
+
|
|
73
|
+
```tsx
|
|
74
|
+
import { useRef } from 'react';
|
|
75
|
+
import { CanvasEditor, CanvasEditorRef } from 'labellife-design-tool';
|
|
76
|
+
|
|
77
|
+
function MyEditor() {
|
|
78
|
+
const editorRef = useRef<CanvasEditorRef>(null);
|
|
79
|
+
|
|
80
|
+
const handleExport = () => {
|
|
81
|
+
// Access methods via the ref
|
|
82
|
+
if (editorRef.current) {
|
|
83
|
+
// Export to PNG
|
|
84
|
+
editorRef.current.exportToPNG();
|
|
85
|
+
|
|
86
|
+
// Or get the current design
|
|
87
|
+
const currentDesign = editorRef.current.getDesign();
|
|
88
|
+
console.log('Current design:', currentDesign);
|
|
89
|
+
|
|
90
|
+
// Access the Konva Stage directly
|
|
91
|
+
const stage = editorRef.current.stage;
|
|
92
|
+
// Do something with the stage...
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return (
|
|
97
|
+
<div>
|
|
98
|
+
<CanvasEditor
|
|
99
|
+
ref={editorRef}
|
|
100
|
+
name="Editor with Ref"
|
|
101
|
+
config={{
|
|
102
|
+
export: { png: true, jpg: true, json: true },
|
|
103
|
+
multiPage: true
|
|
104
|
+
}}
|
|
105
|
+
/>
|
|
106
|
+
<button onClick={handleExport}>Export</button>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The `CanvasEditorRef` interface provides the following properties and methods:
|
|
113
|
+
|
|
114
|
+
- `stage`: Direct access to the Konva.Stage instance
|
|
115
|
+
- `exportToPNG()`: Export the canvas to a PNG file
|
|
116
|
+
- `exportToJPG()`: Export the canvas to a JPG file
|
|
117
|
+
- `exportToJSON()`: Export the design to a JSON file
|
|
118
|
+
- `getDesign()`: Get the current design object
|
|
119
|
+
|
|
69
120
|
### Advanced Usage with Types
|
|
70
121
|
|
|
71
122
|
```tsx
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import React from "react";
|
|
2
|
+
import Konva from "konva";
|
|
3
|
+
import { CanvasDesign } from "./types";
|
|
2
4
|
import { Config } from "./types/Config";
|
|
3
|
-
|
|
5
|
+
export interface CanvasEditorRef {
|
|
6
|
+
stage: Konva.Stage | null;
|
|
7
|
+
exportToPNG: () => void;
|
|
8
|
+
exportToJPG: () => void;
|
|
9
|
+
exportToJSON: () => void;
|
|
10
|
+
getDesign: () => CanvasDesign;
|
|
11
|
+
}
|
|
12
|
+
declare const CanvasEditor: React.ForwardRefExoticComponent<{
|
|
4
13
|
name: string;
|
|
5
14
|
config?: Config;
|
|
6
|
-
}
|
|
15
|
+
} & React.RefAttributes<CanvasEditorRef>>;
|
|
7
16
|
export default CanvasEditor;
|
|
8
17
|
//# sourceMappingURL=CanvasEditor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CanvasEditor.d.ts","sourceRoot":"","sources":["../../src/CanvasEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"CanvasEditor.d.ts","sourceRoot":"","sources":["../../src/CanvasEditor.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAoF,MAAM,OAAO,CAAC;AAGzG,OAAO,KAAK,MAAM,OAAO,CAAC;AAyC1B,OAAO,EAEL,YAAY,EAKb,MAAM,SAAS,CAAC;AAgCjB,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC;IAC1B,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,WAAW,EAAE,MAAM,IAAI,CAAC;IACxB,YAAY,EAAE,MAAM,IAAI,CAAC;IACzB,SAAS,EAAE,MAAM,YAAY,CAAC;CAC/B;AAED,QAAA,MAAM,YAAY;UAAuC,MAAM;aAAW,MAAM;yCA29B9E,CAAC;AAEH,eAAe,YAAY,CAAC"}
|
package/dist/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// src/CanvasEditor.tsx
|
|
2
|
-
import { useState as useState3, useRef as useRef5, useEffect as useEffect4, useCallback as useCallback2 } from "react";
|
|
2
|
+
import { useState as useState3, useRef as useRef5, useEffect as useEffect4, useCallback as useCallback2, forwardRef, useImperativeHandle } from "react";
|
|
3
3
|
import { Stage, Layer, Rect as Rect2 } from "react-konva";
|
|
4
4
|
import {
|
|
5
5
|
PlusCircle
|
|
@@ -3045,6 +3045,31 @@ var exportToJSON = (design) => {
|
|
|
3045
3045
|
document.body.removeChild(link);
|
|
3046
3046
|
URL.revokeObjectURL(url);
|
|
3047
3047
|
};
|
|
3048
|
+
var canvasToBlob = (stage, format = "png", options) => {
|
|
3049
|
+
if (!stage) {
|
|
3050
|
+
return Promise.reject(new Error("Stage is required"));
|
|
3051
|
+
}
|
|
3052
|
+
const oldScale = stage.scaleX();
|
|
3053
|
+
stage.scale({ x: 1, y: 1 });
|
|
3054
|
+
const pixelRatio = options?.pixelRatio ?? 2;
|
|
3055
|
+
const quality = options?.quality ?? 0.9;
|
|
3056
|
+
const dataUrl = stage.toDataURL({
|
|
3057
|
+
pixelRatio,
|
|
3058
|
+
mimeType: format === "jpg" ? "image/jpeg" : "image/png",
|
|
3059
|
+
quality: format === "jpg" ? quality : undefined
|
|
3060
|
+
});
|
|
3061
|
+
stage.scale({ x: oldScale, y: oldScale });
|
|
3062
|
+
return new Promise((resolve, reject) => {
|
|
3063
|
+
const byteString = atob(dataUrl.split(",")[1]);
|
|
3064
|
+
const mimeType = format === "jpg" ? "image/jpeg" : "image/png";
|
|
3065
|
+
const arrayBuffer = new ArrayBuffer(byteString.length);
|
|
3066
|
+
const uint8Array = new Uint8Array(arrayBuffer);
|
|
3067
|
+
for (let i = 0;i < byteString.length; i++) {
|
|
3068
|
+
uint8Array[i] = byteString.charCodeAt(i);
|
|
3069
|
+
}
|
|
3070
|
+
resolve(new Blob([arrayBuffer], { type: mimeType }));
|
|
3071
|
+
});
|
|
3072
|
+
};
|
|
3048
3073
|
var importFromJSON = (event, onLoad, onError, onInputsRequired) => {
|
|
3049
3074
|
const file = event.target.files?.[0];
|
|
3050
3075
|
if (file) {
|
|
@@ -3571,10 +3596,10 @@ var TemplateInputModal_default = TemplateInputModal;
|
|
|
3571
3596
|
|
|
3572
3597
|
// src/CanvasEditor.tsx
|
|
3573
3598
|
import { jsxDEV as jsxDEV18 } from "react/jsx-dev-runtime";
|
|
3574
|
-
var CanvasEditor = ({
|
|
3599
|
+
var CanvasEditor = forwardRef(({
|
|
3575
3600
|
name,
|
|
3576
3601
|
config
|
|
3577
|
-
}) => {
|
|
3602
|
+
}, ref) => {
|
|
3578
3603
|
const [design, setDesign] = useState3({
|
|
3579
3604
|
id: Date.now().toString(),
|
|
3580
3605
|
name: "Untitled Design",
|
|
@@ -3607,6 +3632,19 @@ var CanvasEditor = ({
|
|
|
3607
3632
|
const [showInputModal, setShowInputModal] = useState3(false);
|
|
3608
3633
|
const [pendingInputs, setPendingInputs] = useState3([]);
|
|
3609
3634
|
const stageRef = useRef5(null);
|
|
3635
|
+
useImperativeHandle(ref, () => ({
|
|
3636
|
+
stage: stageRef.current,
|
|
3637
|
+
exportToPNG: () => {
|
|
3638
|
+
if (stageRef.current)
|
|
3639
|
+
exportToPNG(stageRef.current, design);
|
|
3640
|
+
},
|
|
3641
|
+
exportToJPG: () => {
|
|
3642
|
+
if (stageRef.current)
|
|
3643
|
+
exportToJPG(stageRef.current, design);
|
|
3644
|
+
},
|
|
3645
|
+
exportToJSON: () => exportToJSON(design),
|
|
3646
|
+
getDesign: () => design
|
|
3647
|
+
}), [design]);
|
|
3610
3648
|
const fileInputRef = useRef5(null);
|
|
3611
3649
|
const jsonInputRef = useRef5(null);
|
|
3612
3650
|
const containerRef = useRef5(null);
|
|
@@ -4368,7 +4406,7 @@ var CanvasEditor = ({
|
|
|
4368
4406
|
}, undefined, false, undefined, this)
|
|
4369
4407
|
]
|
|
4370
4408
|
}, undefined, true, undefined, this);
|
|
4371
|
-
};
|
|
4409
|
+
});
|
|
4372
4410
|
var CanvasEditor_default = CanvasEditor;
|
|
4373
4411
|
export {
|
|
4374
4412
|
setUnsplashAccessKey,
|
|
@@ -4380,6 +4418,7 @@ export {
|
|
|
4380
4418
|
exportToJSON,
|
|
4381
4419
|
exportToJPG,
|
|
4382
4420
|
convertTemplateToCanvasDesign,
|
|
4421
|
+
canvasToBlob,
|
|
4383
4422
|
UrlImageElement,
|
|
4384
4423
|
ShapeElement,
|
|
4385
4424
|
FONT_FAMILIES,
|
package/dist/lib/lib/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
export { default as CanvasEditor } from '../CanvasEditor';
|
|
6
6
|
export * from '../types';
|
|
7
|
-
export { exportToPNG, exportToJPG, exportToJSON, importFromJSON, findRequiredInputs, replaceUserInputs, convertTemplateToCanvasDesign, } from '../utils/exportImportUtils';
|
|
7
|
+
export { exportToPNG, exportToJPG, exportToJSON, importFromJSON, findRequiredInputs, replaceUserInputs, convertTemplateToCanvasDesign, canvasToBlob, } from '../utils/exportImportUtils';
|
|
8
8
|
export * from '../elements';
|
|
9
9
|
export { FONT_FAMILIES, DEFAULT_COLORS, CANVAS_PRESETS } from '../constants';
|
|
10
10
|
export { setUnsplashAccessKey, getUnsplashAccessKey } from '../config';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG1D,cAAc,UAAU,CAAC;AAGzB,OAAO,EACL,WAAW,EACX,WAAW,EACX,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,iBAAiB,EACjB,6BAA6B,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,OAAO,IAAI,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAG1D,cAAc,UAAU,CAAC;AAGzB,OAAO,EACL,WAAW,EACX,WAAW,EACX,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,iBAAiB,EACjB,6BAA6B,EAC7B,YAAY,GACb,MAAM,4BAA4B,CAAC;AAGpC,cAAc,aAAa,CAAC;AAG5B,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG7E,OAAO,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC"}
|