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