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