@anu3ev/fabric-image-editor 0.9.24 → 0.9.25

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 (2) hide show
  1. package/dist/main.js +120 -62
  2. package/package.json +1 -1
package/dist/main.js CHANGED
@@ -3404,72 +3404,24 @@ var Ut = class {
3404
3404
  }
3405
3405
  }
3406
3406
  async exportObjectAsImageFile(t = {}) {
3407
- let { object: n, fileName: r, contentType: i, exportAsBase64: o = !1, exportAsBlob: s = !1 } = t, { canvas: c, workerManager: l } = this.editor, u = n || c.getActiveObject(), d = i ?? "image/png", f = this.getFormatFromContentType(d) || "png", p = r ?? `image.${f}`;
3408
- if (!u) return this.editor.errorManager.emitError({
3407
+ let { object: n, exportAsBase64: r = !1, exportAsBlob: i = !1 } = t, { canvas: a } = this.editor, o = n || a.getActiveObject(), s = this._createObjectExportRequest({
3408
+ object: o ?? void 0,
3409
+ options: t
3410
+ });
3411
+ if (!e._hasExportObject({ request: s })) return this.editor.errorManager.emitError({
3409
3412
  origin: "ImageManager",
3410
3413
  method: "exportObjectAsImageFile",
3411
3414
  code: "NO_OBJECT_SELECTED",
3412
3415
  message: "Не выбран объект для экспорта",
3413
3416
  data: {
3414
- contentType: d,
3415
- fileName: p,
3416
- exportAsBase64: o,
3417
- exportAsBlob: s
3417
+ contentType: s.contentType,
3418
+ fileName: s.fileName,
3419
+ exportAsBase64: r,
3420
+ exportAsBlob: i
3418
3421
  }
3419
3422
  }), null;
3420
- let { contentType: m, format: h = "" } = u, g = i ?? m ?? "image/png", _ = this.getFormatFromContentType(g) || h || "png", v = r ?? `image.${_}`;
3421
3423
  try {
3422
- if (_ === "svg") {
3423
- let t = u.toSVG(), n = {
3424
- object: u,
3425
- image: e._exportSVGStringAsFile(t, {
3426
- exportAsBase64: o,
3427
- exportAsBlob: s,
3428
- fileName: v
3429
- }),
3430
- format: _,
3431
- contentType: "image/svg+xml",
3432
- fileName: v.replace(/\.[^/.]+$/, ".svg")
3433
- };
3434
- return c.fire("editor:object-exported", n), n;
3435
- }
3436
- if (o && u instanceof a) {
3437
- let e = await createImageBitmap(u.getElement()), t = {
3438
- object: u,
3439
- image: await l.post("toDataURL", {
3440
- contentType: g,
3441
- quality: 1,
3442
- bitmap: e
3443
- }, [e]),
3444
- format: _,
3445
- contentType: g,
3446
- fileName: v
3447
- };
3448
- return c.fire("editor:object-exported", t), t;
3449
- }
3450
- let t = u.toCanvasElement({ enableRetinaScaling: !1 }), n = await new Promise((e, n) => {
3451
- t.toBlob((t) => {
3452
- t ? e(t) : n(/* @__PURE__ */ Error("Failed to create Blob from canvas"));
3453
- });
3454
- });
3455
- if (s) {
3456
- let e = {
3457
- object: u,
3458
- image: n,
3459
- format: _,
3460
- contentType: g,
3461
- fileName: v
3462
- };
3463
- return c.fire("editor:object-exported", e), e;
3464
- }
3465
- let r = {
3466
- object: u,
3467
- image: new File([n], v, { type: g }),
3468
- format: _,
3469
- contentType: g,
3470
- fileName: v
3471
- };
3472
- return c.fire("editor:object-exported", r), r;
3424
+ return s.format === "svg" ? this._exportSvgObject({ request: s }) : e._canExportImageElementAsBase64({ request: s }) ? await this._exportImageElementAsBase64({ request: s }) : await this._exportRenderedObject({ request: s });
3473
3425
  } catch (e) {
3474
3426
  return this.editor.errorManager.emitError({
3475
3427
  origin: "ImageManager",
@@ -3477,14 +3429,120 @@ var Ut = class {
3477
3429
  code: "IMAGE_EXPORT_FAILED",
3478
3430
  message: `Ошибка экспорта объекта: ${e.message}`,
3479
3431
  data: {
3480
- contentType: g,
3481
- fileName: v,
3482
- exportAsBase64: o,
3483
- exportAsBlob: s
3432
+ contentType: s.contentType,
3433
+ fileName: s.fileName,
3434
+ exportAsBase64: r,
3435
+ exportAsBlob: i
3484
3436
  }
3485
3437
  }), null;
3486
3438
  }
3487
3439
  }
3440
+ _createObjectExportRequest({ object: e, options: t }) {
3441
+ let { fileName: n, contentType: r, exportAsBase64: i = !1, exportAsBlob: a = !1 } = t, { contentType: o, format: s = "" } = e || {}, c = r ?? o ?? "image/png", l = this.getFormatFromContentType(c) || s || "png";
3442
+ return {
3443
+ object: e,
3444
+ contentType: c,
3445
+ format: l,
3446
+ fileName: n ?? `image.${l}`,
3447
+ exportAsBase64: i,
3448
+ exportAsBlob: a
3449
+ };
3450
+ }
3451
+ static _hasExportObject({ request: e }) {
3452
+ return !!e.object;
3453
+ }
3454
+ static _canExportImageElementAsBase64({ request: t }) {
3455
+ return !t.exportAsBase64 || !(t.object instanceof a) ? !1 : !e._hasVisibleImageCrop({ image: t.object });
3456
+ }
3457
+ static _hasVisibleImageCrop({ image: t }) {
3458
+ let n = Number(t.cropX ?? 0), r = Number(t.cropY ?? 0), i = Number(t.width ?? 0), a = Number(t.height ?? 0), o = e._getImageElementSize({ image: t });
3459
+ return !!(n || r || o.width && i && i < o.width || o.height && a && a < o.height);
3460
+ }
3461
+ static _getImageElementSize({ image: e }) {
3462
+ let t = e.getElement();
3463
+ return {
3464
+ width: t.naturalWidth || t.videoWidth || t.width || 0,
3465
+ height: t.naturalHeight || t.videoHeight || t.height || 0
3466
+ };
3467
+ }
3468
+ _exportSvgObject({ request: t }) {
3469
+ let { canvas: n } = this.editor, r = t.object.toSVG(), i = e._exportSVGStringAsFile(r, {
3470
+ exportAsBase64: t.exportAsBase64,
3471
+ exportAsBlob: t.exportAsBlob,
3472
+ fileName: t.fileName
3473
+ }), a = {
3474
+ object: t.object,
3475
+ image: i,
3476
+ format: t.format,
3477
+ contentType: "image/svg+xml",
3478
+ fileName: t.fileName.replace(/\.[^/.]+$/, ".svg")
3479
+ };
3480
+ return n.fire("editor:object-exported", a), a;
3481
+ }
3482
+ async _exportImageElementAsBase64({ request: e }) {
3483
+ let { canvas: t, workerManager: n } = this.editor, r = await createImageBitmap(e.object.getElement()), i = await n.post("toDataURL", {
3484
+ contentType: e.contentType,
3485
+ quality: 1,
3486
+ bitmap: r
3487
+ }, [r]), a = {
3488
+ object: e.object,
3489
+ image: i,
3490
+ format: e.format,
3491
+ contentType: e.contentType,
3492
+ fileName: e.fileName
3493
+ };
3494
+ return t.fire("editor:object-exported", a), a;
3495
+ }
3496
+ async _exportRenderedObject({ request: e }) {
3497
+ let { canvas: t } = this.editor, n = await this._createObjectBlob({ request: e });
3498
+ if (e.exportAsBlob) {
3499
+ let r = {
3500
+ object: e.object,
3501
+ image: n,
3502
+ format: e.format,
3503
+ contentType: e.contentType,
3504
+ fileName: e.fileName
3505
+ };
3506
+ return t.fire("editor:object-exported", r), r;
3507
+ }
3508
+ if (e.exportAsBase64) {
3509
+ let r = await this._convertBlobToDataUrl({
3510
+ blob: n,
3511
+ contentType: e.contentType
3512
+ }), i = {
3513
+ object: e.object,
3514
+ image: r,
3515
+ format: e.format,
3516
+ contentType: e.contentType,
3517
+ fileName: e.fileName
3518
+ };
3519
+ return t.fire("editor:object-exported", i), i;
3520
+ }
3521
+ let r = new File([n], e.fileName, { type: e.contentType }), i = {
3522
+ object: e.object,
3523
+ image: r,
3524
+ format: e.format,
3525
+ contentType: e.contentType,
3526
+ fileName: e.fileName
3527
+ };
3528
+ return t.fire("editor:object-exported", i), i;
3529
+ }
3530
+ async _createObjectBlob({ request: e }) {
3531
+ let t = e.object.toCanvasElement({ enableRetinaScaling: !1 });
3532
+ return new Promise((n, r) => {
3533
+ t.toBlob((e) => {
3534
+ e ? n(e) : r(/* @__PURE__ */ Error("Failed to create Blob from canvas"));
3535
+ }, e.contentType, 1);
3536
+ });
3537
+ }
3538
+ async _convertBlobToDataUrl({ blob: e, contentType: t }) {
3539
+ let { workerManager: n } = this.editor, r = await createImageBitmap(e);
3540
+ return n.post("toDataURL", {
3541
+ contentType: t,
3542
+ quality: 1,
3543
+ bitmap: r
3544
+ }, [r]);
3545
+ }
3488
3546
  revokeBlobUrls() {
3489
3547
  this._createdBlobUrls.forEach(URL.revokeObjectURL), this._createdBlobUrls = [];
3490
3548
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anu3ev/fabric-image-editor",
3
- "version": "0.9.24",
3
+ "version": "0.9.25",
4
4
  "description": "TypeScript image editor library built on FabricJS, allowing you to create instances with an integrated montage area and providing an API to modify and manage state.",
5
5
  "module": "dist/main.js",
6
6
  "files": [