customized-fabric 1.2.3 → 1.2.5
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 +83 -46
- 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,16 +37,21 @@ 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 });
|
41
44
|
}
|
45
|
+
else {
|
46
|
+
if (options?.maskUrl) {
|
47
|
+
this.loadMask(options?.maskUrl);
|
48
|
+
}
|
49
|
+
}
|
42
50
|
if (options?.image) {
|
43
51
|
const { image } = options;
|
44
52
|
const { url, data, clipPath } = image;
|
45
53
|
if (url && data && clipPath) {
|
46
|
-
const loadedImage = await (0, utils_1.
|
54
|
+
const loadedImage = await (0, utils_1.loadImage)(image?.url);
|
47
55
|
if (loadedImage) {
|
48
56
|
const { top, left, scaleX, width, originX, originY } = data;
|
49
57
|
const ratioWidth = (width * scaleX) / (clipPath.width * clipPath.scaleX);
|
@@ -66,31 +74,15 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
66
74
|
}
|
67
75
|
}
|
68
76
|
},
|
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({
|
77
|
+
loadImage: async function (image) {
|
78
|
+
const loadedImage = await (0, utils_1.loadImage)(image);
|
79
|
+
if (loadedImage?.width && loadedImage?.height) {
|
80
|
+
loadedImage.set({
|
83
81
|
originX: "center",
|
84
82
|
originY: "center",
|
85
83
|
});
|
86
84
|
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("");
|
85
|
+
this.imageObject = loadedImage;
|
94
86
|
this.add(this.imageObject);
|
95
87
|
this.fitImage(this.imageObject);
|
96
88
|
}
|
@@ -110,41 +102,58 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
110
102
|
}
|
111
103
|
this.set(attributes);
|
112
104
|
this.rectObject.set(attributes);
|
105
|
+
this.maskObject?.set({
|
106
|
+
scaleX: this.width / this.maskObject.width,
|
107
|
+
scaleY: this.height / this.maskObject.height,
|
108
|
+
});
|
113
109
|
this.fitImage(this.imageObject);
|
114
110
|
this.canvas?.renderAll?.();
|
115
111
|
},
|
116
112
|
fitImage: function (image) {
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
113
|
+
if (image) {
|
114
|
+
const imageScaleX = this.width / (image?.width ?? 1);
|
115
|
+
const imageScaleY = this.height / (image?.height ?? 1);
|
116
|
+
imageScaleX > imageScaleY
|
117
|
+
? image.set({ scaleX: imageScaleY, scaleY: imageScaleY })
|
118
|
+
: image.set({ scaleX: imageScaleX, scaleY: imageScaleX });
|
119
|
+
}
|
122
120
|
},
|
123
121
|
getSettings: function (attribute) {
|
124
122
|
return this.get(attribute);
|
125
123
|
},
|
126
124
|
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
|
-
}
|
125
|
+
const loadedImage = await (0, utils_1.loadImage)(image);
|
134
126
|
if (loadedImage) {
|
135
127
|
loadedImage.set({
|
136
128
|
originX: "left",
|
137
129
|
originY: "top",
|
138
130
|
top: this.top,
|
139
131
|
left: this.left,
|
140
|
-
|
141
|
-
|
142
|
-
|
132
|
+
});
|
133
|
+
if (this.maskUrl) {
|
134
|
+
const maskObject = await (0, utils_1.loadImage)(this.maskUrl);
|
135
|
+
maskObject?.set({
|
143
136
|
top: this.top,
|
144
137
|
left: this.left,
|
145
138
|
absolutePositioned: true,
|
146
|
-
|
147
|
-
|
139
|
+
scaleX: this.width / (maskObject?.width ?? this.width),
|
140
|
+
scaleY: this.height / (maskObject?.height ?? this.height),
|
141
|
+
});
|
142
|
+
loadedImage.set({
|
143
|
+
clipPath: maskObject,
|
144
|
+
});
|
145
|
+
}
|
146
|
+
else {
|
147
|
+
loadedImage.set({
|
148
|
+
clipPath: new fabric_1.fabric.Rect({
|
149
|
+
width: this.width,
|
150
|
+
height: this.height,
|
151
|
+
top: this.top,
|
152
|
+
left: this.left,
|
153
|
+
absolutePositioned: true,
|
154
|
+
}),
|
155
|
+
});
|
156
|
+
}
|
148
157
|
this.fitImage(loadedImage);
|
149
158
|
this.uploadedImage = loadedImage;
|
150
159
|
const canvas = this?.canvas;
|
@@ -154,10 +163,12 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
154
163
|
return this.uploadedImage;
|
155
164
|
},
|
156
165
|
removeUploadedImage: function () {
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
166
|
+
if (this.uploadedImage) {
|
167
|
+
const canvas = this?.canvas;
|
168
|
+
canvas?.remove(this.uploadedImage);
|
169
|
+
this.uploadedImage = undefined;
|
170
|
+
canvas?.renderAll();
|
171
|
+
}
|
161
172
|
},
|
162
173
|
toImageObject: function () {
|
163
174
|
if (this.uploadedImage) {
|
@@ -183,6 +194,31 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
|
|
183
194
|
return {};
|
184
195
|
}
|
185
196
|
},
|
197
|
+
loadMask: async function (maskInput) {
|
198
|
+
const loadedMask = await (0, utils_1.loadImage)(maskInput);
|
199
|
+
if (loadedMask?.width && loadedMask?.height) {
|
200
|
+
loadedMask.set({
|
201
|
+
originX: "center",
|
202
|
+
originY: "center",
|
203
|
+
scaleX: this.width / loadedMask.width,
|
204
|
+
scaleY: this.height / loadedMask.height,
|
205
|
+
absolutePositioned: false,
|
206
|
+
});
|
207
|
+
this.remove(this.imageObject);
|
208
|
+
this.remove(this.maskObject);
|
209
|
+
this.maskObject = loadedMask;
|
210
|
+
this.add(this.maskObject);
|
211
|
+
this.imageObject && this.add(this.imageObject);
|
212
|
+
this.set({ clipPath: this.maskObject });
|
213
|
+
this.canvas?.renderAll();
|
214
|
+
}
|
215
|
+
},
|
216
|
+
removeMask: async function () {
|
217
|
+
this.set({ clipPath: undefined, maskUrl: undefined });
|
218
|
+
this.remove(this.maskObject);
|
219
|
+
this.maskObject = undefined;
|
220
|
+
this.canvas?.renderAll();
|
221
|
+
},
|
186
222
|
});
|
187
223
|
const toImagePlaceholderObject = (imagePlaceholderObject) => {
|
188
224
|
return {
|
@@ -193,6 +229,7 @@ const toImagePlaceholderObject = (imagePlaceholderObject) => {
|
|
193
229
|
name: imagePlaceholderObject?.name,
|
194
230
|
locked: imagePlaceholderObject?.locked,
|
195
231
|
isAdditional: imagePlaceholderObject?.isAdditional,
|
232
|
+
maskUrl: imagePlaceholderObject?.maskUrl,
|
196
233
|
};
|
197
234
|
};
|
198
235
|
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,
|