watermark-js-plus 0.9.12 → 0.10.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/dist/index.esm.js CHANGED
@@ -29,6 +29,17 @@ function __extends(d, b) {
29
29
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
30
30
  }
31
31
 
32
+ var __assign = function() {
33
+ __assign = Object.assign || function __assign(t) {
34
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
35
+ s = arguments[i];
36
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
37
+ }
38
+ return t;
39
+ };
40
+ return __assign.apply(this, arguments);
41
+ };
42
+
32
43
  function __awaiter(thisArg, _arguments, P, generator) {
33
44
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
34
45
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -442,12 +453,13 @@ var bootstrap = (function () {
442
453
  }
443
454
  });
444
455
 
445
- // import { WatermarkOptions } from '../types'
446
456
  var WatermarkCanvas = /** @class */ (function () {
447
- function WatermarkCanvas() {
457
+ function WatermarkCanvas(args, options) {
458
+ this.props = args;
459
+ this.options = options;
460
+ this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
461
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
448
462
  }
449
- // constructor (args: Partial<WatermarkOptions> = {}) {
450
- // }
451
463
  /**
452
464
  * Create an HD canvas.
453
465
  * @param width - width of canvas
@@ -476,224 +488,13 @@ var WatermarkCanvas = /** @class */ (function () {
476
488
  ctx.restore();
477
489
  ctx.clearRect(0, 0, canvas.width, canvas.height);
478
490
  };
479
- return WatermarkCanvas;
480
- }());
481
-
482
- var GridLayout = /** @class */ (function () {
483
- function GridLayout(args, partialCanvas) {
484
- var _a, _b, _c, _d;
485
- this.options = args;
486
- this.partialWidth = this.options.width;
487
- this.partialHeight = this.options.height;
488
- this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
489
- this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
490
- this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
491
- this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
492
- this.partialCanvas = partialCanvas;
493
- }
494
- GridLayout.prototype.draw = function () {
495
- var _a, _b;
496
- var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
497
- var layoutContext = layoutCanvas.getContext('2d');
498
- for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
499
- for (var colIndex = 0; colIndex < this.cols; colIndex++) {
500
- if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
501
- continue;
502
- }
503
- layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage(this.partialCanvas, this.partialWidth * colIndex + this.gap[0] * colIndex, this.partialHeight * rowIndex + this.gap[1] * rowIndex, this.partialWidth, this.partialHeight);
504
- }
505
- }
506
- return layoutCanvas;
491
+ WatermarkCanvas.prototype.getCanvas = function () {
492
+ return this.canvas;
507
493
  };
508
- return GridLayout;
509
- }());
510
-
511
- var renderLayout = function (options, partialCanvas) {
512
- switch (options.layout) {
513
- case 'grid':
514
- return new GridLayout(options, partialCanvas).draw();
515
- default:
516
- return partialCanvas;
517
- }
518
- };
519
- var generateBackgroundSize = function (options) {
520
- var _a, _b, _c;
521
- switch (options.layout) {
522
- case 'grid':
523
- {
524
- var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
525
- var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
526
- var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
527
- return [
528
- options.width * cols + gap[0] * cols,
529
- options.height * rows + gap[1] * rows
530
- ];
531
- }
532
- default:
533
- return [
534
- options.width,
535
- options.height
536
- ];
537
- }
538
- };
539
-
540
- /**
541
- * Watermark class
542
- */
543
- var Watermark = /** @class */ (function () {
544
- /**
545
- * Watermark constructor
546
- * @param args - watermark args
547
- */
548
- function Watermark(args) {
549
- if (args === void 0) { args = {}; }
550
- this.parentElement = document.body;
551
- this.props = args;
552
- this.options = Object.assign({}, initialOptions, args);
553
- this.changeParentElement(this.options.parent);
554
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
555
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
556
- bootstrap();
557
- }
558
- Watermark.prototype.changeOptions = function (args, mode) {
559
- if (args === void 0) { args = {}; }
560
- if (mode === void 0) { mode = 'overwrite'; }
561
- this.initConfigData(args, mode);
494
+ WatermarkCanvas.prototype.clear = function () {
495
+ WatermarkCanvas.clearCanvas(this.canvas);
562
496
  };
563
- /**
564
- * Creating a watermark.
565
- */
566
- Watermark.prototype.create = function () {
567
- var _a, _b, _c, _d;
568
- return __awaiter(this, void 0, void 0, function () {
569
- var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
570
- return __generator(this, function (_e) {
571
- switch (_e.label) {
572
- case 0:
573
- if (!this.validateUnique()) {
574
- return [2 /*return*/];
575
- }
576
- if (!this.validateContent()) {
577
- return [2 /*return*/];
578
- }
579
- firstDraw = isUndefined(this.watermarkDom);
580
- return [4 /*yield*/, this.draw()];
581
- case 1:
582
- _e.sent();
583
- this.layoutCanvas = renderLayout(this.options, this.canvas);
584
- image = convertImage(this.layoutCanvas);
585
- WatermarkCanvas.clearCanvas(this.canvas);
586
- this.watermarkDom = document.createElement('div');
587
- watermarkInnerDom = document.createElement('div');
588
- this.watermarkDom.__WATERMARK__ = 'watermark';
589
- this.watermarkDom.__WATERMARK__INSTANCE__ = this;
590
- parentElementType = this.checkParentElementType();
591
- this.watermarkDom.style.cssText = "\n z-index: ".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
592
- backgroundSize = generateBackgroundSize(this.options);
593
- watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, "!important;\n ");
594
- this.watermarkDom.append(watermarkInnerDom);
595
- this.parentElement.appendChild(this.watermarkDom);
596
- if (this.options.mutationObserve) {
597
- try {
598
- this.bindMutationObserve();
599
- }
600
- catch (e) {
601
- (_b = (_a = this.options).onObserveError) === null || _b === void 0 ? void 0 : _b.call(_a);
602
- }
603
- }
604
- firstDraw && ((_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c));
605
- return [2 /*return*/];
606
- }
607
- });
608
- });
609
- };
610
- /**
611
- * Delete this watermark.
612
- */
613
- Watermark.prototype.destroy = function () {
614
- this.remove();
615
- this.watermarkDom = undefined;
616
- };
617
- Watermark.prototype.check = function () {
618
- return __awaiter(this, void 0, void 0, function () {
619
- return __generator(this, function (_a) {
620
- switch (_a.label) {
621
- case 0:
622
- if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
623
- this.remove();
624
- return [4 /*yield*/, this.create()];
625
- case 1:
626
- _a.sent();
627
- _a.label = 2;
628
- case 2: return [2 /*return*/];
629
- }
630
- });
631
- });
632
- };
633
- Watermark.prototype.remove = function () {
634
- var _a, _b, _c, _d, _e, _f, _g;
635
- (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
636
- (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
637
- (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
638
- if (!isUndefined(this.checkWatermarkElementRequestID)) {
639
- cancelAnimationFrame(this.checkWatermarkElementRequestID);
640
- }
641
- (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
642
- (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
643
- };
644
- Watermark.prototype.initConfigData = function (args, mode) {
645
- var _this = this;
646
- if (mode === void 0) { mode = 'overwrite'; }
647
- this.props = args;
648
- if (mode === 'overwrite') {
649
- this.options = Object.assign({}, initialOptions, args);
650
- }
651
- else {
652
- Object.keys(args).forEach(function (key) {
653
- _this.options[key] = args[key];
654
- });
655
- }
656
- this.changeParentElement(this.options.parent);
657
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
658
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
659
- // document.body?.appendChild(this.canvas)
660
- };
661
- Watermark.prototype.changeParentElement = function (parent) {
662
- if (typeof parent === 'string') {
663
- var parentElement = document.querySelector(parent);
664
- parentElement && (this.parentElement = parentElement);
665
- }
666
- else {
667
- this.parentElement = parent;
668
- }
669
- };
670
- Watermark.prototype.validateUnique = function () {
671
- var result = true;
672
- if (this.options.unique) {
673
- this.parentElement.childNodes.forEach(function (node) {
674
- if (!result) {
675
- return;
676
- }
677
- if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
678
- result = false;
679
- // throw new Error('duplicate watermark error')
680
- }
681
- });
682
- }
683
- return result;
684
- };
685
- Watermark.prototype.validateContent = function () {
686
- switch (this.options.contentType) {
687
- case 'image':
688
- return Object.hasOwnProperty.call(this.options, 'image');
689
- case 'multi-line-text':
690
- case 'rich-text':
691
- case 'text':
692
- return this.options.content.length > 0;
693
- }
694
- return false;
695
- };
696
- Watermark.prototype.draw = function () {
497
+ WatermarkCanvas.prototype.draw = function () {
697
498
  var _this = this;
698
499
  var ctx = this.canvas.getContext('2d');
699
500
  if (ctx === null) {
@@ -734,7 +535,7 @@ var Watermark = /** @class */ (function () {
734
535
  }
735
536
  });
736
537
  };
737
- Watermark.prototype.setStyle = function (ctx) {
538
+ WatermarkCanvas.prototype.setStyle = function (ctx) {
738
539
  var _a;
739
540
  var propName = 'fillStyle';
740
541
  if (this.options.textType === 'stroke') {
@@ -771,7 +572,7 @@ var Watermark = /** @class */ (function () {
771
572
  this.options.extraDrawFunc(ctx);
772
573
  }
773
574
  };
774
- Watermark.prototype.createLinearGradient = function (ctx) {
575
+ WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
775
576
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
776
577
  var gradient = ctx.createLinearGradient(getValue((_c = (_b = (_a = this.options.advancedStyle) === null || _a === void 0 ? void 0 : _a.params) === null || _b === void 0 ? void 0 : _b.linear) === null || _c === void 0 ? void 0 : _c.x0, this.recommendOptions.advancedStyleParams.linear.x0), getValue((_f = (_e = (_d = this.options.advancedStyle) === null || _d === void 0 ? void 0 : _d.params) === null || _e === void 0 ? void 0 : _e.linear) === null || _f === void 0 ? void 0 : _f.y0, 0), getValue((_j = (_h = (_g = this.options.advancedStyle) === null || _g === void 0 ? void 0 : _g.params) === null || _h === void 0 ? void 0 : _h.linear) === null || _j === void 0 ? void 0 : _j.x1, this.recommendOptions.advancedStyleParams.linear.x1), getValue((_m = (_l = (_k = this.options.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.linear) === null || _m === void 0 ? void 0 : _m.y1, 0));
777
578
  (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
@@ -779,7 +580,7 @@ var Watermark = /** @class */ (function () {
779
580
  });
780
581
  return gradient;
781
582
  };
782
- Watermark.prototype.createConicGradient = function (ctx) {
583
+ WatermarkCanvas.prototype.createConicGradient = function (ctx) {
783
584
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
784
585
  var gradient = ctx.createConicGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.conic) === null || _d === void 0 ? void 0 : _d.startAngle, 0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.conic) === null || _h === void 0 ? void 0 : _h.x, this.recommendOptions.advancedStyleParams.conic.x), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.conic) === null || _m === void 0 ? void 0 : _m.y, this.recommendOptions.advancedStyleParams.conic.y));
785
586
  (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.colorStops) === null || _q === void 0 ? void 0 : _q.forEach(function (item) {
@@ -787,7 +588,7 @@ var Watermark = /** @class */ (function () {
787
588
  });
788
589
  return gradient;
789
590
  };
790
- Watermark.prototype.createRadialGradient = function (ctx) {
591
+ WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
791
592
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2;
792
593
  var gradient = ctx.createRadialGradient(getValue((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.radial) === null || _d === void 0 ? void 0 : _d.x0, this.recommendOptions.advancedStyleParams.radial.x0), getValue((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.radial) === null || _h === void 0 ? void 0 : _h.y0, this.recommendOptions.advancedStyleParams.radial.y0), getValue((_m = (_l = (_k = (_j = this.options) === null || _j === void 0 ? void 0 : _j.advancedStyle) === null || _k === void 0 ? void 0 : _k.params) === null || _l === void 0 ? void 0 : _l.radial) === null || _m === void 0 ? void 0 : _m.r0, this.recommendOptions.advancedStyleParams.radial.r0), getValue((_r = (_q = (_p = (_o = this.options) === null || _o === void 0 ? void 0 : _o.advancedStyle) === null || _p === void 0 ? void 0 : _p.params) === null || _q === void 0 ? void 0 : _q.radial) === null || _r === void 0 ? void 0 : _r.x1, this.recommendOptions.advancedStyleParams.radial.x1), getValue((_v = (_u = (_t = (_s = this.options) === null || _s === void 0 ? void 0 : _s.advancedStyle) === null || _t === void 0 ? void 0 : _t.params) === null || _u === void 0 ? void 0 : _u.radial) === null || _v === void 0 ? void 0 : _v.y1, this.recommendOptions.advancedStyleParams.radial.y1), getValue((_z = (_y = (_x = (_w = this.options) === null || _w === void 0 ? void 0 : _w.advancedStyle) === null || _x === void 0 ? void 0 : _x.params) === null || _y === void 0 ? void 0 : _y.radial) === null || _z === void 0 ? void 0 : _z.r1, this.recommendOptions.advancedStyleParams.radial.r1));
793
594
  (_2 = (_1 = (_0 = this.options) === null || _0 === void 0 ? void 0 : _0.advancedStyle) === null || _1 === void 0 ? void 0 : _1.colorStops) === null || _2 === void 0 ? void 0 : _2.forEach(function (item) {
@@ -795,18 +596,18 @@ var Watermark = /** @class */ (function () {
795
596
  });
796
597
  return gradient;
797
598
  };
798
- Watermark.prototype.createPattern = function (ctx) {
599
+ WatermarkCanvas.prototype.createPattern = function (ctx) {
799
600
  var _a, _b, _c, _d, _e, _f, _g, _h;
800
601
  return ctx.createPattern((_d = (_c = (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.advancedStyle) === null || _b === void 0 ? void 0 : _b.params) === null || _c === void 0 ? void 0 : _c.pattern) === null || _d === void 0 ? void 0 : _d.image, ((_h = (_g = (_f = (_e = this.options) === null || _e === void 0 ? void 0 : _e.advancedStyle) === null || _f === void 0 ? void 0 : _f.params) === null || _g === void 0 ? void 0 : _g.pattern) === null || _h === void 0 ? void 0 : _h.repetition) || '');
801
602
  };
802
- Watermark.prototype.setText = function (ctx, params) {
603
+ WatermarkCanvas.prototype.setText = function (ctx, params) {
803
604
  var methodName = 'fillText';
804
605
  if (this.options.textType === 'stroke') {
805
606
  methodName = 'strokeText';
806
607
  }
807
608
  ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
808
609
  };
809
- Watermark.prototype.drawText = function (ctx, resolve) {
610
+ WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
810
611
  this.setText(ctx, {
811
612
  text: this.options.content,
812
613
  x: 0,
@@ -815,7 +616,7 @@ var Watermark = /** @class */ (function () {
815
616
  });
816
617
  resolve(ctx.canvas);
817
618
  };
818
- Watermark.prototype.drawImage = function (ctx, resolve) {
619
+ WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
819
620
  var _this = this;
820
621
  loadImage(this.options.image).then(function (image) {
821
622
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
@@ -824,7 +625,7 @@ var Watermark = /** @class */ (function () {
824
625
  resolve(ctx.canvas);
825
626
  });
826
627
  };
827
- Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
628
+ WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
828
629
  var _this = this;
829
630
  // image.width = this.options.width
830
631
  // image.height = this.options.height
@@ -849,7 +650,7 @@ var Watermark = /** @class */ (function () {
849
650
  });
850
651
  resolve(ctx.canvas);
851
652
  };
852
- Watermark.prototype.drawRichText = function (ctx, resolve) {
653
+ WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
853
654
  var _this = this;
854
655
  var obj = createCustomContentSVG(ctx, this.options);
855
656
  loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
@@ -858,7 +659,7 @@ var Watermark = /** @class */ (function () {
858
659
  resolve(ctx.canvas);
859
660
  });
860
661
  };
861
- Watermark.prototype.getImageRect = function (image) {
662
+ WatermarkCanvas.prototype.getImageRect = function (image) {
862
663
  var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
863
664
  switch (true) {
864
665
  case rect.width !== 0 && rect.height === 0:
@@ -874,7 +675,7 @@ var Watermark = /** @class */ (function () {
874
675
  }
875
676
  return rect;
876
677
  };
877
- Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
678
+ WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
878
679
  var _a, _b;
879
680
  var result = {
880
681
  x: -imageWidth / 2,
@@ -918,6 +719,223 @@ var Watermark = /** @class */ (function () {
918
719
  !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
919
720
  return result;
920
721
  };
722
+ return WatermarkCanvas;
723
+ }());
724
+
725
+ var GridLayout = /** @class */ (function () {
726
+ function GridLayout(args, partialCanvas) {
727
+ var _a, _b, _c, _d;
728
+ this.options = args;
729
+ this.partialWidth = this.options.width;
730
+ this.partialHeight = this.options.height;
731
+ this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
732
+ this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
733
+ this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
734
+ this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
735
+ this.partialCanvas = partialCanvas;
736
+ }
737
+ GridLayout.prototype.draw = function () {
738
+ var _a, _b, _c, _d, _e, _f, _g, _h;
739
+ var layoutCanvas = WatermarkCanvas.createCanvas(((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.width) || (this.partialWidth * this.cols + this.gap[0] * this.cols), ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.height) || (this.partialHeight * this.rows + this.gap[1] * this.rows));
740
+ var layoutContext = layoutCanvas.getContext('2d');
741
+ if ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.backgroundImage) {
742
+ layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.backgroundImage, 0, 0, (_e = this.options.gridLayoutOptions) === null || _e === void 0 ? void 0 : _e.width, (_f = this.options.gridLayoutOptions) === null || _f === void 0 ? void 0 : _f.height);
743
+ }
744
+ for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
745
+ for (var colIndex = 0; colIndex < this.cols; colIndex++) {
746
+ if (!((_h = (_g = this.matrix) === null || _g === void 0 ? void 0 : _g[rowIndex]) === null || _h === void 0 ? void 0 : _h[colIndex])) {
747
+ continue;
748
+ }
749
+ layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage(this.partialCanvas, this.partialWidth * colIndex + this.gap[0] * colIndex, this.partialHeight * rowIndex + this.gap[1] * rowIndex, this.partialWidth, this.partialHeight);
750
+ }
751
+ }
752
+ return layoutCanvas;
753
+ };
754
+ return GridLayout;
755
+ }());
756
+
757
+ var renderLayout = function (options, partialCanvas) {
758
+ switch (options.layout) {
759
+ case 'grid':
760
+ return new GridLayout(options, partialCanvas).draw();
761
+ default:
762
+ return partialCanvas;
763
+ }
764
+ };
765
+ var generateBackgroundSize = function (options) {
766
+ var _a, _b, _c;
767
+ switch (options.layout) {
768
+ case 'grid':
769
+ {
770
+ var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
771
+ var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
772
+ var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
773
+ return [
774
+ options.width * cols + gap[0] * cols,
775
+ options.height * rows + gap[1] * rows
776
+ ];
777
+ }
778
+ default:
779
+ return [
780
+ options.width,
781
+ options.height
782
+ ];
783
+ }
784
+ };
785
+
786
+ /**
787
+ * Watermark class
788
+ */
789
+ var Watermark = /** @class */ (function () {
790
+ /**
791
+ * Watermark constructor
792
+ * @param args - watermark args
793
+ */
794
+ function Watermark(args) {
795
+ if (args === void 0) { args = {}; }
796
+ this.parentElement = document.body;
797
+ this.props = args;
798
+ this.options = Object.assign({}, initialOptions, args);
799
+ this.changeParentElement(this.options.parent);
800
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
801
+ bootstrap();
802
+ }
803
+ Watermark.prototype.changeOptions = function (args, mode) {
804
+ if (args === void 0) { args = {}; }
805
+ if (mode === void 0) { mode = 'overwrite'; }
806
+ this.initConfigData(args, mode);
807
+ };
808
+ /**
809
+ * Creating a watermark.
810
+ */
811
+ Watermark.prototype.create = function () {
812
+ var _a, _b, _c, _d, _e, _f, _g;
813
+ return __awaiter(this, void 0, void 0, function () {
814
+ var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
815
+ return __generator(this, function (_h) {
816
+ switch (_h.label) {
817
+ case 0:
818
+ if (!this.validateUnique()) {
819
+ return [2 /*return*/];
820
+ }
821
+ if (!this.validateContent()) {
822
+ return [2 /*return*/];
823
+ }
824
+ firstDraw = isUndefined(this.watermarkDom);
825
+ return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
826
+ case 1:
827
+ _h.sent();
828
+ this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
829
+ image = convertImage(this.layoutCanvas);
830
+ (_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
831
+ this.watermarkDom = document.createElement('div');
832
+ watermarkInnerDom = document.createElement('div');
833
+ this.watermarkDom.__WATERMARK__ = 'watermark';
834
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
835
+ parentElementType = this.checkParentElementType();
836
+ this.watermarkDom.style.cssText = "\n z-index: ".concat(this.options.zIndex, "!important;display:block!important;visibility:visible!important;transform:none!important;scale:none!important;\n ").concat(parentElementType === 'custom' ? 'top:0!important;bottom:0!important;left:0!important;right:0!important;height:100%!important;pointer-events:none!important;position:absolute!important;' : 'position:relative!important;', "\n ");
837
+ backgroundSize = generateBackgroundSize(this.options);
838
+ watermarkInnerDom.style.cssText = "\n display:block!important;visibility:visible!important;pointer-events:none;top:0;bottom:0;left:0;right:0;transform:none!important;scale:none!important;\n position:".concat(parentElementType === 'root' ? 'fixed' : 'absolute', "!important;-webkit-print-color-adjust:exact!important;width:100%!important;height:100%!important;\n z-index:").concat(this.options.zIndex, "!important;background-image:url(").concat(image, ")!important;background-repeat:").concat(this.options.backgroundRepeat, "!important;\n background-size:").concat(backgroundSize[0], "px ").concat(backgroundSize[1], "px!important;background-position:").concat(this.options.backgroundPosition, "!important;\n ");
839
+ this.watermarkDom.append(watermarkInnerDom);
840
+ this.parentElement.appendChild(this.watermarkDom);
841
+ if (this.options.mutationObserve) {
842
+ try {
843
+ this.bindMutationObserve();
844
+ }
845
+ catch (e) {
846
+ (_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
847
+ }
848
+ }
849
+ firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
850
+ return [2 /*return*/];
851
+ }
852
+ });
853
+ });
854
+ };
855
+ /**
856
+ * Delete this watermark.
857
+ */
858
+ Watermark.prototype.destroy = function () {
859
+ this.remove();
860
+ this.watermarkDom = undefined;
861
+ };
862
+ Watermark.prototype.check = function () {
863
+ return __awaiter(this, void 0, void 0, function () {
864
+ return __generator(this, function (_a) {
865
+ switch (_a.label) {
866
+ case 0:
867
+ if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
868
+ this.remove();
869
+ return [4 /*yield*/, this.create()];
870
+ case 1:
871
+ _a.sent();
872
+ _a.label = 2;
873
+ case 2: return [2 /*return*/];
874
+ }
875
+ });
876
+ });
877
+ };
878
+ Watermark.prototype.remove = function () {
879
+ var _a, _b, _c, _d, _e, _f, _g;
880
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
881
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
882
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
883
+ if (!isUndefined(this.checkWatermarkElementRequestID)) {
884
+ cancelAnimationFrame(this.checkWatermarkElementRequestID);
885
+ }
886
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
887
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
888
+ };
889
+ Watermark.prototype.initConfigData = function (args, mode) {
890
+ var _this = this;
891
+ if (mode === void 0) { mode = 'overwrite'; }
892
+ this.props = args;
893
+ if (mode === 'overwrite') {
894
+ this.options = Object.assign({}, initialOptions, args);
895
+ }
896
+ else {
897
+ Object.keys(args).forEach(function (key) {
898
+ _this.options[key] = args[key];
899
+ });
900
+ }
901
+ this.changeParentElement(this.options.parent);
902
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
903
+ };
904
+ Watermark.prototype.changeParentElement = function (parent) {
905
+ if (typeof parent === 'string') {
906
+ var parentElement = document.querySelector(parent);
907
+ parentElement && (this.parentElement = parentElement);
908
+ }
909
+ else {
910
+ this.parentElement = parent;
911
+ }
912
+ };
913
+ Watermark.prototype.validateUnique = function () {
914
+ var result = true;
915
+ if (this.options.unique) {
916
+ this.parentElement.childNodes.forEach(function (node) {
917
+ if (!result) {
918
+ return;
919
+ }
920
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
921
+ result = false;
922
+ // throw new Error('duplicate watermark error')
923
+ }
924
+ });
925
+ }
926
+ return result;
927
+ };
928
+ Watermark.prototype.validateContent = function () {
929
+ switch (this.options.contentType) {
930
+ case 'image':
931
+ return Object.hasOwnProperty.call(this.options, 'image');
932
+ case 'multi-line-text':
933
+ case 'rich-text':
934
+ case 'text':
935
+ return this.options.content.length > 0;
936
+ }
937
+ return false;
938
+ };
921
939
  Watermark.prototype.checkParentElementType = function () {
922
940
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
923
941
  return 'root';
@@ -1058,5 +1076,48 @@ var BlindWatermark = /** @class */ (function (_super) {
1058
1076
  return BlindWatermark;
1059
1077
  }(Watermark));
1060
1078
 
1061
- export { BlindWatermark, Watermark };
1079
+ /**
1080
+ * ImageWatermark class
1081
+ */
1082
+ var ImageWatermark = /** @class */ (function () {
1083
+ /**
1084
+ * ImageWatermark constructor
1085
+ * @param args - image watermark args
1086
+ */
1087
+ function ImageWatermark(args) {
1088
+ if (args === void 0) { args = {}; }
1089
+ var _a;
1090
+ this.drew = false;
1091
+ this.props = args;
1092
+ this.options = Object.assign({}, initialOptions, args);
1093
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1094
+ this.originalSrc = (_a = this.props.dom) === null || _a === void 0 ? void 0 : _a.src;
1095
+ this.backgroundImage = this.getBackgroundImage();
1096
+ }
1097
+ ImageWatermark.prototype.create = function () {
1098
+ var _a, _b, _c, _d, _e;
1099
+ if (this.drew) {
1100
+ return;
1101
+ }
1102
+ (_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw();
1103
+ this.options.layout = 'grid';
1104
+ this.options.gridLayoutOptions = __assign(__assign({}, this.options.gridLayoutOptions), { width: (_b = this.backgroundImage) === null || _b === void 0 ? void 0 : _b.width, height: (_c = this.backgroundImage) === null || _c === void 0 ? void 0 : _c.height, backgroundImage: this.backgroundImage });
1105
+ this.layoutCanvas = renderLayout(this.options, (_d = this.watermarkCanvas) === null || _d === void 0 ? void 0 : _d.getCanvas());
1106
+ this.options.dom.src = convertImage(this.layoutCanvas);
1107
+ (_e = this.watermarkCanvas) === null || _e === void 0 ? void 0 : _e.clear();
1108
+ this.drew = true;
1109
+ };
1110
+ ImageWatermark.prototype.destroy = function () {
1111
+ this.options.dom.src = this.originalSrc;
1112
+ this.drew = false;
1113
+ };
1114
+ ImageWatermark.prototype.getBackgroundImage = function () {
1115
+ if (this.options.dom) {
1116
+ return this.options.dom;
1117
+ }
1118
+ };
1119
+ return ImageWatermark;
1120
+ }());
1121
+
1122
+ export { BlindWatermark, ImageWatermark, Watermark };
1062
1123
  //# sourceMappingURL=index.esm.js.map