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