customized-fabric 1.2.3 → 1.2.4
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/lib/GroupLayoutObject/constants.d.ts +4 -0
- package/lib/GroupLayoutObject/constants.js +7 -0
- package/lib/GroupLayoutObject/index.d.ts +19 -0
- package/lib/GroupLayoutObject/index.js +26 -0
- package/lib/GroupLayoutObject/interfaces.d.ts +3 -0
- package/lib/GroupLayoutObject/interfaces.js +2 -0
- package/lib/ImagePlaceholderObject/index.d.ts +6 -3
- package/lib/ImagePlaceholderObject/index.js +68 -48
- package/lib/ImagePlaceholderObject/interfaces.d.ts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +12 -1
- package/package.json +1 -1
@@ -0,0 +1,19 @@
|
|
1
|
+
import { fabric } from "fabric";
|
2
|
+
import { ObjectId } from "../utils/objectId";
|
3
|
+
import { IGroupLayoutOptions } from "./interfaces";
|
4
|
+
export declare const toGroupLayoutObject: (groupLayoutObject: GroupLayout) => {
|
5
|
+
_id: ObjectId | undefined;
|
6
|
+
id: ObjectId | undefined;
|
7
|
+
personalizeId: number | undefined;
|
8
|
+
layerId: number | undefined;
|
9
|
+
name: string | undefined;
|
10
|
+
locked: boolean | undefined;
|
11
|
+
isAdditional: boolean | undefined;
|
12
|
+
};
|
13
|
+
export default class GroupLayout extends fabric.ActiveSelection {
|
14
|
+
_id?: ObjectId;
|
15
|
+
layerId?: number;
|
16
|
+
locked?: boolean;
|
17
|
+
isAdditional?: boolean;
|
18
|
+
constructor(options?: IGroupLayoutOptions);
|
19
|
+
}
|
@@ -0,0 +1,26 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.toGroupLayoutObject = void 0;
|
4
|
+
const fabric_1 = require("fabric");
|
5
|
+
const GroupLayoutClass = fabric_1.fabric.util.createClass(fabric_1.fabric.ActiveSelection, {
|
6
|
+
initialize: async function (options) { },
|
7
|
+
});
|
8
|
+
const toGroupLayoutObject = (groupLayoutObject) => {
|
9
|
+
return {
|
10
|
+
_id: groupLayoutObject._id,
|
11
|
+
id: groupLayoutObject._id,
|
12
|
+
personalizeId: groupLayoutObject.layerId,
|
13
|
+
layerId: groupLayoutObject.layerId,
|
14
|
+
name: groupLayoutObject.name,
|
15
|
+
locked: groupLayoutObject.locked,
|
16
|
+
isAdditional: groupLayoutObject?.isAdditional,
|
17
|
+
};
|
18
|
+
};
|
19
|
+
exports.toGroupLayoutObject = toGroupLayoutObject;
|
20
|
+
class GroupLayout extends fabric_1.fabric.ActiveSelection {
|
21
|
+
constructor(options) {
|
22
|
+
super();
|
23
|
+
return new GroupLayoutClass(options);
|
24
|
+
}
|
25
|
+
}
|
26
|
+
exports.default = GroupLayout;
|
@@ -9,22 +9,25 @@ export declare const toImagePlaceholderObject: (imagePlaceholderObject: ImagePla
|
|
9
9
|
name: string | undefined;
|
10
10
|
locked: boolean | undefined;
|
11
11
|
isAdditional: boolean | undefined;
|
12
|
+
maskUrl: string | undefined;
|
12
13
|
};
|
13
14
|
export default class ImagePlaceholder extends fabric.Group {
|
14
15
|
_id?: ObjectId;
|
15
16
|
layerId?: number;
|
16
17
|
locked?: boolean;
|
17
18
|
imageFile?: File;
|
19
|
+
maskUrl?: string;
|
18
20
|
isAdditional?: boolean;
|
19
|
-
loadImageFromFile?: (imageFile: File) => void;
|
20
|
-
loadImageFromUrl?: (imageUrl: string) => void;
|
21
|
-
loadUploadedImage?: (imageFile: File) => Promise<fabric.Image>;
|
22
21
|
getSettings?: (attribute: string) => any;
|
23
22
|
setSizes?: (options: {
|
24
23
|
width?: number;
|
25
24
|
height?: number;
|
26
25
|
}) => void;
|
27
26
|
toImageObject?: () => object;
|
27
|
+
loadImage?: (imageInput: File | string) => void;
|
28
|
+
loadUploadedImage?: (imageFile: File) => Promise<fabric.Image>;
|
28
29
|
removeUploadedImage?: () => void;
|
30
|
+
loadMask?: (maskInput: File | string) => void;
|
31
|
+
removeMask?: () => void;
|
29
32
|
constructor(options?: IImagePlaceholderOptions);
|
30
33
|
}
|
@@ -16,13 +16,16 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
16
16
|
objectCaching: false,
|
17
17
|
...constants_1.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES.stroke,
|
18
18
|
});
|
19
|
-
|
20
|
-
const group = new fabric_1.fabric.Group([this.rectObject, this.imageObject]);
|
19
|
+
const group = new fabric_1.fabric.Group([this.rectObject]);
|
21
20
|
this.set(group);
|
22
21
|
this.on("scaling", () => {
|
23
22
|
let width = this.width * this.scaleX;
|
24
23
|
let height = this.height * this.scaleY;
|
25
24
|
this.setSizes({ width, height });
|
25
|
+
this.maskObject?.set({
|
26
|
+
scaleX: width / this.maskObject.width,
|
27
|
+
scaleY: height / this.maskObject.height,
|
28
|
+
});
|
26
29
|
this.canvas?.renderAll();
|
27
30
|
});
|
28
31
|
this.set({
|
@@ -34,7 +37,7 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
34
37
|
objectCaching: false,
|
35
38
|
});
|
36
39
|
if (options?.imageFile) {
|
37
|
-
this.
|
40
|
+
this.loadImage(options?.imageFile);
|
38
41
|
}
|
39
42
|
if (options?.hideStroke) {
|
40
43
|
this.rectObject.set({ strokeWidth: 0 });
|
@@ -43,7 +46,7 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
43
46
|
const { image } = options;
|
44
47
|
const { url, data, clipPath } = image;
|
45
48
|
if (url && data && clipPath) {
|
46
|
-
const loadedImage = await (0, utils_1.
|
49
|
+
const loadedImage = await (0, utils_1.loadImage)(image?.url);
|
47
50
|
if (loadedImage) {
|
48
51
|
const { top, left, scaleX, width, originX, originY } = data;
|
49
52
|
const ratioWidth = (width * scaleX) / (clipPath.width * clipPath.scaleX);
|
@@ -65,32 +68,19 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
65
68
|
}
|
66
69
|
}
|
67
70
|
}
|
71
|
+
if (options?.maskUrl) {
|
72
|
+
this.loadMask(options?.maskUrl);
|
73
|
+
}
|
68
74
|
},
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
crossOrigin: "anonymous",
|
74
|
-
});
|
75
|
-
},
|
76
|
-
loadImageFromFile: async function (imageFile) {
|
77
|
-
const loadedImage = await (0, utils_1.loadImageFromFile)(imageFile);
|
78
|
-
this.loadImage(loadedImage);
|
79
|
-
},
|
80
|
-
loadImage: function (image) {
|
81
|
-
if (image.width && image.height) {
|
82
|
-
image.set({
|
75
|
+
loadImage: async function (image) {
|
76
|
+
const loadedImage = await (0, utils_1.loadImage)(image);
|
77
|
+
if (loadedImage?.width && loadedImage?.height) {
|
78
|
+
loadedImage.set({
|
83
79
|
originX: "center",
|
84
80
|
originY: "center",
|
85
81
|
});
|
86
82
|
this.remove(this.imageObject);
|
87
|
-
this.imageObject =
|
88
|
-
this.add(this.imageObject);
|
89
|
-
this.fitImage(this.imageObject);
|
90
|
-
}
|
91
|
-
else {
|
92
|
-
this.remove(this.imageObject);
|
93
|
-
this.imageObject = new fabric_1.fabric.Image("");
|
83
|
+
this.imageObject = loadedImage;
|
94
84
|
this.add(this.imageObject);
|
95
85
|
this.fitImage(this.imageObject);
|
96
86
|
}
|
@@ -110,41 +100,50 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
110
100
|
}
|
111
101
|
this.set(attributes);
|
112
102
|
this.rectObject.set(attributes);
|
103
|
+
this.maskObject?.set({
|
104
|
+
scaleX: this.width / this.maskObject.width,
|
105
|
+
scaleY: this.height / this.maskObject.height,
|
106
|
+
});
|
113
107
|
this.fitImage(this.imageObject);
|
114
108
|
this.canvas?.renderAll?.();
|
115
109
|
},
|
116
110
|
fitImage: function (image) {
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
111
|
+
if (image) {
|
112
|
+
const imageScaleX = this.width / (image?.width ?? 1);
|
113
|
+
const imageScaleY = this.height / (image?.height ?? 1);
|
114
|
+
imageScaleX > imageScaleY
|
115
|
+
? image.set({ scaleX: imageScaleY, scaleY: imageScaleY })
|
116
|
+
: image.set({ scaleX: imageScaleX, scaleY: imageScaleX });
|
117
|
+
}
|
122
118
|
},
|
123
119
|
getSettings: function (attribute) {
|
124
120
|
return this.get(attribute);
|
125
121
|
},
|
126
122
|
loadUploadedImage: async function (image) {
|
127
|
-
|
128
|
-
if (image instanceof File) {
|
129
|
-
loadedImage = await (0, utils_1.loadImageFromFile)(image);
|
130
|
-
}
|
131
|
-
else if (typeof image == "string") {
|
132
|
-
loadedImage = await (0, utils_1.loadImageFromUrl)(image);
|
133
|
-
}
|
123
|
+
const loadedImage = await (0, utils_1.loadImage)(image);
|
134
124
|
if (loadedImage) {
|
135
125
|
loadedImage.set({
|
136
126
|
originX: "left",
|
137
127
|
originY: "top",
|
138
128
|
top: this.top,
|
139
129
|
left: this.left,
|
140
|
-
clipPath: new fabric_1.fabric.Rect({
|
141
|
-
width: this.width,
|
142
|
-
height: this.height,
|
143
|
-
top: this.top,
|
144
|
-
left: this.left,
|
145
|
-
absolutePositioned: true,
|
146
|
-
}),
|
147
130
|
});
|
131
|
+
if (this.maskObject) {
|
132
|
+
loadedImage.set({
|
133
|
+
clipPath: this.maskObject,
|
134
|
+
});
|
135
|
+
}
|
136
|
+
else {
|
137
|
+
loadedImage.set({
|
138
|
+
clipPath: new fabric_1.fabric.Rect({
|
139
|
+
width: this.width,
|
140
|
+
height: this.height,
|
141
|
+
top: this.top,
|
142
|
+
left: this.left,
|
143
|
+
absolutePositioned: true,
|
144
|
+
}),
|
145
|
+
});
|
146
|
+
}
|
148
147
|
this.fitImage(loadedImage);
|
149
148
|
this.uploadedImage = loadedImage;
|
150
149
|
const canvas = this?.canvas;
|
@@ -154,10 +153,12 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
154
153
|
return this.uploadedImage;
|
155
154
|
},
|
156
155
|
removeUploadedImage: function () {
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
156
|
+
if (this.uploadedImage) {
|
157
|
+
const canvas = this?.canvas;
|
158
|
+
canvas?.remove(this.uploadedImage);
|
159
|
+
this.uploadedImage = undefined;
|
160
|
+
canvas?.renderAll();
|
161
|
+
}
|
161
162
|
},
|
162
163
|
toImageObject: function () {
|
163
164
|
if (this.uploadedImage) {
|
@@ -183,6 +184,24 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
183
184
|
return {};
|
184
185
|
}
|
185
186
|
},
|
187
|
+
loadMask: async function (maskInput) {
|
188
|
+
const loadedMask = await (0, utils_1.loadImage)(maskInput);
|
189
|
+
if (loadedMask?.width && loadedMask?.height) {
|
190
|
+
loadedMask.set({
|
191
|
+
originX: "center",
|
192
|
+
originY: "center",
|
193
|
+
scaleX: this.width / loadedMask.width,
|
194
|
+
scaleY: this.height / loadedMask.height,
|
195
|
+
absolutePositioned: false,
|
196
|
+
});
|
197
|
+
this.maskObject = loadedMask;
|
198
|
+
this.set({ clipPath: this.maskObject });
|
199
|
+
this.canvas?.renderAll();
|
200
|
+
}
|
201
|
+
},
|
202
|
+
removeMask: async function () {
|
203
|
+
this.set({ clipPath: undefined });
|
204
|
+
},
|
186
205
|
});
|
187
206
|
const toImagePlaceholderObject = (imagePlaceholderObject) => {
|
188
207
|
return {
|
@@ -193,6 +212,7 @@ const toImagePlaceholderObject = (imagePlaceholderObject) => {
|
|
193
212
|
name: imagePlaceholderObject?.name,
|
194
213
|
locked: imagePlaceholderObject?.locked,
|
195
214
|
isAdditional: imagePlaceholderObject?.isAdditional,
|
215
|
+
maskUrl: imagePlaceholderObject?.maskUrl,
|
196
216
|
};
|
197
217
|
};
|
198
218
|
exports.toImagePlaceholderObject = toImagePlaceholderObject;
|
package/lib/utils/index.d.ts
CHANGED
@@ -6,6 +6,7 @@ export declare const loadFontFromUrl: (url: string, name: string) => Promise<voi
|
|
6
6
|
export declare const isFontLoaded: (name: string) => boolean;
|
7
7
|
export declare const loadImageFromFile: (image: File) => Promise<fabric.Image>;
|
8
8
|
export declare const loadImageFromUrl: (url: string) => Promise<fabric.Image>;
|
9
|
+
export declare const loadImage: (imageInput: File | string) => Promise<fabric.Image | undefined>;
|
9
10
|
export declare const lockObject: (object: fabric.Object | any, locked: boolean, selectable?: boolean) => void;
|
10
11
|
export declare const lockAllObjects: (canvas: fabric.Canvas, locked: boolean, selectable?: boolean) => void;
|
11
12
|
export declare const getObject: (object: any, options?: {
|
package/lib/utils/index.js
CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
23
|
return result;
|
24
24
|
};
|
25
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
26
|
-
exports.getObject = exports.lockAllObjects = exports.lockObject = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
|
26
|
+
exports.getObject = exports.lockAllObjects = exports.lockObject = exports.loadImage = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
|
27
27
|
const fabric_1 = require("fabric");
|
28
28
|
const constants_1 = require("../constants");
|
29
29
|
const ClipartObject_1 = __importStar(require("../ClipartObject"));
|
@@ -75,6 +75,17 @@ const loadImageFromUrl = (url) => {
|
|
75
75
|
});
|
76
76
|
};
|
77
77
|
exports.loadImageFromUrl = loadImageFromUrl;
|
78
|
+
const loadImage = async (imageInput) => {
|
79
|
+
let loadedImage = undefined;
|
80
|
+
if (imageInput instanceof File) {
|
81
|
+
loadedImage = await (0, exports.loadImageFromFile)(imageInput);
|
82
|
+
}
|
83
|
+
else if (typeof imageInput == "string") {
|
84
|
+
loadedImage = await (0, exports.loadImageFromUrl)(imageInput);
|
85
|
+
}
|
86
|
+
return loadedImage;
|
87
|
+
};
|
88
|
+
exports.loadImage = loadImage;
|
78
89
|
const lockObject = (object, locked, selectable) => {
|
79
90
|
object.set({
|
80
91
|
hasControls: !locked,
|