watermark-js-plus 0.9.12 → 0.9.13

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.umd.js CHANGED
@@ -448,12 +448,13 @@
448
448
  }
449
449
  });
450
450
 
451
- // import { WatermarkOptions } from '../types'
452
451
  var WatermarkCanvas = /** @class */ (function () {
453
- function WatermarkCanvas() {
452
+ function WatermarkCanvas(args, options) {
453
+ this.props = args;
454
+ this.options = options;
455
+ this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
456
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
454
457
  }
455
- // constructor (args: Partial<WatermarkOptions> = {}) {
456
- // }
457
458
  /**
458
459
  * Create an HD canvas.
459
460
  * @param width - width of canvas
@@ -482,224 +483,13 @@
482
483
  ctx.restore();
483
484
  ctx.clearRect(0, 0, canvas.width, canvas.height);
484
485
  };
485
- return WatermarkCanvas;
486
- }());
487
-
488
- var GridLayout = /** @class */ (function () {
489
- function GridLayout(args, partialCanvas) {
490
- var _a, _b, _c, _d;
491
- this.options = args;
492
- this.partialWidth = this.options.width;
493
- this.partialHeight = this.options.height;
494
- this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
495
- this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
496
- this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
497
- this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
498
- this.partialCanvas = partialCanvas;
499
- }
500
- GridLayout.prototype.draw = function () {
501
- var _a, _b;
502
- var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
503
- var layoutContext = layoutCanvas.getContext('2d');
504
- for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
505
- for (var colIndex = 0; colIndex < this.cols; colIndex++) {
506
- if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
507
- continue;
508
- }
509
- 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);
510
- }
511
- }
512
- return layoutCanvas;
486
+ WatermarkCanvas.prototype.getCanvas = function () {
487
+ return this.canvas;
513
488
  };
514
- return GridLayout;
515
- }());
516
-
517
- var renderLayout = function (options, partialCanvas) {
518
- switch (options.layout) {
519
- case 'grid':
520
- return new GridLayout(options, partialCanvas).draw();
521
- default:
522
- return partialCanvas;
523
- }
524
- };
525
- var generateBackgroundSize = function (options) {
526
- var _a, _b, _c;
527
- switch (options.layout) {
528
- case 'grid':
529
- {
530
- var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
531
- var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
532
- var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
533
- return [
534
- options.width * cols + gap[0] * cols,
535
- options.height * rows + gap[1] * rows
536
- ];
537
- }
538
- default:
539
- return [
540
- options.width,
541
- options.height
542
- ];
543
- }
544
- };
545
-
546
- /**
547
- * Watermark class
548
- */
549
- var Watermark = /** @class */ (function () {
550
- /**
551
- * Watermark constructor
552
- * @param args - watermark args
553
- */
554
- function Watermark(args) {
555
- if (args === void 0) { args = {}; }
556
- this.parentElement = document.body;
557
- this.props = args;
558
- this.options = Object.assign({}, initialOptions, args);
559
- this.changeParentElement(this.options.parent);
560
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
561
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
562
- bootstrap();
563
- }
564
- Watermark.prototype.changeOptions = function (args, mode) {
565
- if (args === void 0) { args = {}; }
566
- if (mode === void 0) { mode = 'overwrite'; }
567
- this.initConfigData(args, mode);
489
+ WatermarkCanvas.prototype.clear = function () {
490
+ WatermarkCanvas.clearCanvas(this.canvas);
568
491
  };
569
- /**
570
- * Creating a watermark.
571
- */
572
- Watermark.prototype.create = function () {
573
- var _a, _b, _c, _d;
574
- return __awaiter(this, void 0, void 0, function () {
575
- var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
576
- return __generator(this, function (_e) {
577
- switch (_e.label) {
578
- case 0:
579
- if (!this.validateUnique()) {
580
- return [2 /*return*/];
581
- }
582
- if (!this.validateContent()) {
583
- return [2 /*return*/];
584
- }
585
- firstDraw = isUndefined(this.watermarkDom);
586
- return [4 /*yield*/, this.draw()];
587
- case 1:
588
- _e.sent();
589
- this.layoutCanvas = renderLayout(this.options, this.canvas);
590
- image = convertImage(this.layoutCanvas);
591
- WatermarkCanvas.clearCanvas(this.canvas);
592
- this.watermarkDom = document.createElement('div');
593
- watermarkInnerDom = document.createElement('div');
594
- this.watermarkDom.__WATERMARK__ = 'watermark';
595
- this.watermarkDom.__WATERMARK__INSTANCE__ = this;
596
- parentElementType = this.checkParentElementType();
597
- 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 ");
598
- backgroundSize = generateBackgroundSize(this.options);
599
- 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 ");
600
- this.watermarkDom.append(watermarkInnerDom);
601
- this.parentElement.appendChild(this.watermarkDom);
602
- if (this.options.mutationObserve) {
603
- try {
604
- this.bindMutationObserve();
605
- }
606
- catch (e) {
607
- (_b = (_a = this.options).onObserveError) === null || _b === void 0 ? void 0 : _b.call(_a);
608
- }
609
- }
610
- firstDraw && ((_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c));
611
- return [2 /*return*/];
612
- }
613
- });
614
- });
615
- };
616
- /**
617
- * Delete this watermark.
618
- */
619
- Watermark.prototype.destroy = function () {
620
- this.remove();
621
- this.watermarkDom = undefined;
622
- };
623
- Watermark.prototype.check = function () {
624
- return __awaiter(this, void 0, void 0, function () {
625
- return __generator(this, function (_a) {
626
- switch (_a.label) {
627
- case 0:
628
- if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
629
- this.remove();
630
- return [4 /*yield*/, this.create()];
631
- case 1:
632
- _a.sent();
633
- _a.label = 2;
634
- case 2: return [2 /*return*/];
635
- }
636
- });
637
- });
638
- };
639
- Watermark.prototype.remove = function () {
640
- var _a, _b, _c, _d, _e, _f, _g;
641
- (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
642
- (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
643
- (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
644
- if (!isUndefined(this.checkWatermarkElementRequestID)) {
645
- cancelAnimationFrame(this.checkWatermarkElementRequestID);
646
- }
647
- (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
648
- (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
649
- };
650
- Watermark.prototype.initConfigData = function (args, mode) {
651
- var _this = this;
652
- if (mode === void 0) { mode = 'overwrite'; }
653
- this.props = args;
654
- if (mode === 'overwrite') {
655
- this.options = Object.assign({}, initialOptions, args);
656
- }
657
- else {
658
- Object.keys(args).forEach(function (key) {
659
- _this.options[key] = args[key];
660
- });
661
- }
662
- this.changeParentElement(this.options.parent);
663
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
664
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
665
- // document.body?.appendChild(this.canvas)
666
- };
667
- Watermark.prototype.changeParentElement = function (parent) {
668
- if (typeof parent === 'string') {
669
- var parentElement = document.querySelector(parent);
670
- parentElement && (this.parentElement = parentElement);
671
- }
672
- else {
673
- this.parentElement = parent;
674
- }
675
- };
676
- Watermark.prototype.validateUnique = function () {
677
- var result = true;
678
- if (this.options.unique) {
679
- this.parentElement.childNodes.forEach(function (node) {
680
- if (!result) {
681
- return;
682
- }
683
- if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
684
- result = false;
685
- // throw new Error('duplicate watermark error')
686
- }
687
- });
688
- }
689
- return result;
690
- };
691
- Watermark.prototype.validateContent = function () {
692
- switch (this.options.contentType) {
693
- case 'image':
694
- return Object.hasOwnProperty.call(this.options, 'image');
695
- case 'multi-line-text':
696
- case 'rich-text':
697
- case 'text':
698
- return this.options.content.length > 0;
699
- }
700
- return false;
701
- };
702
- Watermark.prototype.draw = function () {
492
+ WatermarkCanvas.prototype.draw = function () {
703
493
  var _this = this;
704
494
  var ctx = this.canvas.getContext('2d');
705
495
  if (ctx === null) {
@@ -740,7 +530,7 @@
740
530
  }
741
531
  });
742
532
  };
743
- Watermark.prototype.setStyle = function (ctx) {
533
+ WatermarkCanvas.prototype.setStyle = function (ctx) {
744
534
  var _a;
745
535
  var propName = 'fillStyle';
746
536
  if (this.options.textType === 'stroke') {
@@ -777,7 +567,7 @@
777
567
  this.options.extraDrawFunc(ctx);
778
568
  }
779
569
  };
780
- Watermark.prototype.createLinearGradient = function (ctx) {
570
+ WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
781
571
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
782
572
  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));
783
573
  (_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) {
@@ -785,7 +575,7 @@
785
575
  });
786
576
  return gradient;
787
577
  };
788
- Watermark.prototype.createConicGradient = function (ctx) {
578
+ WatermarkCanvas.prototype.createConicGradient = function (ctx) {
789
579
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
790
580
  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));
791
581
  (_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) {
@@ -793,7 +583,7 @@
793
583
  });
794
584
  return gradient;
795
585
  };
796
- Watermark.prototype.createRadialGradient = function (ctx) {
586
+ WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
797
587
  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;
798
588
  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));
799
589
  (_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) {
@@ -801,18 +591,18 @@
801
591
  });
802
592
  return gradient;
803
593
  };
804
- Watermark.prototype.createPattern = function (ctx) {
594
+ WatermarkCanvas.prototype.createPattern = function (ctx) {
805
595
  var _a, _b, _c, _d, _e, _f, _g, _h;
806
596
  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) || '');
807
597
  };
808
- Watermark.prototype.setText = function (ctx, params) {
598
+ WatermarkCanvas.prototype.setText = function (ctx, params) {
809
599
  var methodName = 'fillText';
810
600
  if (this.options.textType === 'stroke') {
811
601
  methodName = 'strokeText';
812
602
  }
813
603
  ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
814
604
  };
815
- Watermark.prototype.drawText = function (ctx, resolve) {
605
+ WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
816
606
  this.setText(ctx, {
817
607
  text: this.options.content,
818
608
  x: 0,
@@ -821,7 +611,7 @@
821
611
  });
822
612
  resolve(ctx.canvas);
823
613
  };
824
- Watermark.prototype.drawImage = function (ctx, resolve) {
614
+ WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
825
615
  var _this = this;
826
616
  loadImage(this.options.image).then(function (image) {
827
617
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
@@ -830,7 +620,7 @@
830
620
  resolve(ctx.canvas);
831
621
  });
832
622
  };
833
- Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
623
+ WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
834
624
  var _this = this;
835
625
  // image.width = this.options.width
836
626
  // image.height = this.options.height
@@ -855,7 +645,7 @@
855
645
  });
856
646
  resolve(ctx.canvas);
857
647
  };
858
- Watermark.prototype.drawRichText = function (ctx, resolve) {
648
+ WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
859
649
  var _this = this;
860
650
  var obj = createCustomContentSVG(ctx, this.options);
861
651
  loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
@@ -864,7 +654,7 @@
864
654
  resolve(ctx.canvas);
865
655
  });
866
656
  };
867
- Watermark.prototype.getImageRect = function (image) {
657
+ WatermarkCanvas.prototype.getImageRect = function (image) {
868
658
  var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
869
659
  switch (true) {
870
660
  case rect.width !== 0 && rect.height === 0:
@@ -880,7 +670,7 @@
880
670
  }
881
671
  return rect;
882
672
  };
883
- Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
673
+ WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
884
674
  var _a, _b;
885
675
  var result = {
886
676
  x: -imageWidth / 2,
@@ -924,6 +714,220 @@
924
714
  !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
925
715
  return result;
926
716
  };
717
+ return WatermarkCanvas;
718
+ }());
719
+
720
+ var GridLayout = /** @class */ (function () {
721
+ function GridLayout(args, partialCanvas) {
722
+ var _a, _b, _c, _d;
723
+ this.options = args;
724
+ this.partialWidth = this.options.width;
725
+ this.partialHeight = this.options.height;
726
+ this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
727
+ this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
728
+ this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
729
+ this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
730
+ this.partialCanvas = partialCanvas;
731
+ }
732
+ GridLayout.prototype.draw = function () {
733
+ var _a, _b;
734
+ var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
735
+ var layoutContext = layoutCanvas.getContext('2d');
736
+ for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
737
+ for (var colIndex = 0; colIndex < this.cols; colIndex++) {
738
+ if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
739
+ continue;
740
+ }
741
+ 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);
742
+ }
743
+ }
744
+ return layoutCanvas;
745
+ };
746
+ return GridLayout;
747
+ }());
748
+
749
+ var renderLayout = function (options, partialCanvas) {
750
+ switch (options.layout) {
751
+ case 'grid':
752
+ return new GridLayout(options, partialCanvas).draw();
753
+ default:
754
+ return partialCanvas;
755
+ }
756
+ };
757
+ var generateBackgroundSize = function (options) {
758
+ var _a, _b, _c;
759
+ switch (options.layout) {
760
+ case 'grid':
761
+ {
762
+ var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
763
+ var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
764
+ var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
765
+ return [
766
+ options.width * cols + gap[0] * cols,
767
+ options.height * rows + gap[1] * rows
768
+ ];
769
+ }
770
+ default:
771
+ return [
772
+ options.width,
773
+ options.height
774
+ ];
775
+ }
776
+ };
777
+
778
+ /**
779
+ * Watermark class
780
+ */
781
+ var Watermark = /** @class */ (function () {
782
+ /**
783
+ * Watermark constructor
784
+ * @param args - watermark args
785
+ */
786
+ function Watermark(args) {
787
+ if (args === void 0) { args = {}; }
788
+ this.parentElement = document.body;
789
+ this.props = args;
790
+ this.options = Object.assign({}, initialOptions, args);
791
+ this.changeParentElement(this.options.parent);
792
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
793
+ bootstrap();
794
+ }
795
+ Watermark.prototype.changeOptions = function (args, mode) {
796
+ if (args === void 0) { args = {}; }
797
+ if (mode === void 0) { mode = 'overwrite'; }
798
+ this.initConfigData(args, mode);
799
+ };
800
+ /**
801
+ * Creating a watermark.
802
+ */
803
+ Watermark.prototype.create = function () {
804
+ var _a, _b, _c, _d, _e, _f, _g;
805
+ return __awaiter(this, void 0, void 0, function () {
806
+ var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
807
+ return __generator(this, function (_h) {
808
+ switch (_h.label) {
809
+ case 0:
810
+ if (!this.validateUnique()) {
811
+ return [2 /*return*/];
812
+ }
813
+ if (!this.validateContent()) {
814
+ return [2 /*return*/];
815
+ }
816
+ firstDraw = isUndefined(this.watermarkDom);
817
+ return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
818
+ case 1:
819
+ _h.sent();
820
+ this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
821
+ image = convertImage(this.layoutCanvas);
822
+ (_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
823
+ this.watermarkDom = document.createElement('div');
824
+ watermarkInnerDom = document.createElement('div');
825
+ this.watermarkDom.__WATERMARK__ = 'watermark';
826
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
827
+ parentElementType = this.checkParentElementType();
828
+ 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 ");
829
+ backgroundSize = generateBackgroundSize(this.options);
830
+ 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 ");
831
+ this.watermarkDom.append(watermarkInnerDom);
832
+ this.parentElement.appendChild(this.watermarkDom);
833
+ if (this.options.mutationObserve) {
834
+ try {
835
+ this.bindMutationObserve();
836
+ }
837
+ catch (e) {
838
+ (_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
839
+ }
840
+ }
841
+ firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
842
+ return [2 /*return*/];
843
+ }
844
+ });
845
+ });
846
+ };
847
+ /**
848
+ * Delete this watermark.
849
+ */
850
+ Watermark.prototype.destroy = function () {
851
+ this.remove();
852
+ this.watermarkDom = undefined;
853
+ };
854
+ Watermark.prototype.check = function () {
855
+ return __awaiter(this, void 0, void 0, function () {
856
+ return __generator(this, function (_a) {
857
+ switch (_a.label) {
858
+ case 0:
859
+ if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
860
+ this.remove();
861
+ return [4 /*yield*/, this.create()];
862
+ case 1:
863
+ _a.sent();
864
+ _a.label = 2;
865
+ case 2: return [2 /*return*/];
866
+ }
867
+ });
868
+ });
869
+ };
870
+ Watermark.prototype.remove = function () {
871
+ var _a, _b, _c, _d, _e, _f, _g;
872
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
873
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
874
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
875
+ if (!isUndefined(this.checkWatermarkElementRequestID)) {
876
+ cancelAnimationFrame(this.checkWatermarkElementRequestID);
877
+ }
878
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
879
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
880
+ };
881
+ Watermark.prototype.initConfigData = function (args, mode) {
882
+ var _this = this;
883
+ if (mode === void 0) { mode = 'overwrite'; }
884
+ this.props = args;
885
+ if (mode === 'overwrite') {
886
+ this.options = Object.assign({}, initialOptions, args);
887
+ }
888
+ else {
889
+ Object.keys(args).forEach(function (key) {
890
+ _this.options[key] = args[key];
891
+ });
892
+ }
893
+ this.changeParentElement(this.options.parent);
894
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
895
+ };
896
+ Watermark.prototype.changeParentElement = function (parent) {
897
+ if (typeof parent === 'string') {
898
+ var parentElement = document.querySelector(parent);
899
+ parentElement && (this.parentElement = parentElement);
900
+ }
901
+ else {
902
+ this.parentElement = parent;
903
+ }
904
+ };
905
+ Watermark.prototype.validateUnique = function () {
906
+ var result = true;
907
+ if (this.options.unique) {
908
+ this.parentElement.childNodes.forEach(function (node) {
909
+ if (!result) {
910
+ return;
911
+ }
912
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
913
+ result = false;
914
+ // throw new Error('duplicate watermark error')
915
+ }
916
+ });
917
+ }
918
+ return result;
919
+ };
920
+ Watermark.prototype.validateContent = function () {
921
+ switch (this.options.contentType) {
922
+ case 'image':
923
+ return Object.hasOwnProperty.call(this.options, 'image');
924
+ case 'multi-line-text':
925
+ case 'rich-text':
926
+ case 'text':
927
+ return this.options.content.length > 0;
928
+ }
929
+ return false;
930
+ };
927
931
  Watermark.prototype.checkParentElementType = function () {
928
932
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
929
933
  return 'root';