watermark-js-plus 0.9.12 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs.js CHANGED
@@ -31,6 +31,17 @@ function __extends(d, b) {
31
31
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
32
32
  }
33
33
 
34
+ var __assign = function() {
35
+ __assign = Object.assign || function __assign(t) {
36
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
37
+ s = arguments[i];
38
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
39
+ }
40
+ return t;
41
+ };
42
+ return __assign.apply(this, arguments);
43
+ };
44
+
34
45
  function __awaiter(thisArg, _arguments, P, generator) {
35
46
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
36
47
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -444,12 +455,13 @@ var bootstrap = (function () {
444
455
  }
445
456
  });
446
457
 
447
- // import { WatermarkOptions } from '../types'
448
458
  var WatermarkCanvas = /** @class */ (function () {
449
- function WatermarkCanvas() {
459
+ function WatermarkCanvas(args, options) {
460
+ this.props = args;
461
+ this.options = options;
462
+ this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
463
+ this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
450
464
  }
451
- // constructor (args: Partial<WatermarkOptions> = {}) {
452
- // }
453
465
  /**
454
466
  * Create an HD canvas.
455
467
  * @param width - width of canvas
@@ -478,224 +490,13 @@ var WatermarkCanvas = /** @class */ (function () {
478
490
  ctx.restore();
479
491
  ctx.clearRect(0, 0, canvas.width, canvas.height);
480
492
  };
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;
493
+ WatermarkCanvas.prototype.getCanvas = function () {
494
+ return this.canvas;
509
495
  };
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);
496
+ WatermarkCanvas.prototype.clear = function () {
497
+ WatermarkCanvas.clearCanvas(this.canvas);
564
498
  };
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 () {
499
+ WatermarkCanvas.prototype.draw = function () {
699
500
  var _this = this;
700
501
  var ctx = this.canvas.getContext('2d');
701
502
  if (ctx === null) {
@@ -736,7 +537,7 @@ var Watermark = /** @class */ (function () {
736
537
  }
737
538
  });
738
539
  };
739
- Watermark.prototype.setStyle = function (ctx) {
540
+ WatermarkCanvas.prototype.setStyle = function (ctx) {
740
541
  var _a;
741
542
  var propName = 'fillStyle';
742
543
  if (this.options.textType === 'stroke') {
@@ -773,7 +574,7 @@ var Watermark = /** @class */ (function () {
773
574
  this.options.extraDrawFunc(ctx);
774
575
  }
775
576
  };
776
- Watermark.prototype.createLinearGradient = function (ctx) {
577
+ WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
777
578
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
778
579
  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
580
  (_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 +582,7 @@ var Watermark = /** @class */ (function () {
781
582
  });
782
583
  return gradient;
783
584
  };
784
- Watermark.prototype.createConicGradient = function (ctx) {
585
+ WatermarkCanvas.prototype.createConicGradient = function (ctx) {
785
586
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
786
587
  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
588
  (_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 +590,7 @@ var Watermark = /** @class */ (function () {
789
590
  });
790
591
  return gradient;
791
592
  };
792
- Watermark.prototype.createRadialGradient = function (ctx) {
593
+ WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
793
594
  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
595
  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
596
  (_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 +598,18 @@ var Watermark = /** @class */ (function () {
797
598
  });
798
599
  return gradient;
799
600
  };
800
- Watermark.prototype.createPattern = function (ctx) {
601
+ WatermarkCanvas.prototype.createPattern = function (ctx) {
801
602
  var _a, _b, _c, _d, _e, _f, _g, _h;
802
603
  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
604
  };
804
- Watermark.prototype.setText = function (ctx, params) {
605
+ WatermarkCanvas.prototype.setText = function (ctx, params) {
805
606
  var methodName = 'fillText';
806
607
  if (this.options.textType === 'stroke') {
807
608
  methodName = 'strokeText';
808
609
  }
809
610
  ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
810
611
  };
811
- Watermark.prototype.drawText = function (ctx, resolve) {
612
+ WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
812
613
  this.setText(ctx, {
813
614
  text: this.options.content,
814
615
  x: 0,
@@ -817,7 +618,7 @@ var Watermark = /** @class */ (function () {
817
618
  });
818
619
  resolve(ctx.canvas);
819
620
  };
820
- Watermark.prototype.drawImage = function (ctx, resolve) {
621
+ WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
821
622
  var _this = this;
822
623
  loadImage(this.options.image).then(function (image) {
823
624
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
@@ -826,7 +627,7 @@ var Watermark = /** @class */ (function () {
826
627
  resolve(ctx.canvas);
827
628
  });
828
629
  };
829
- Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
630
+ WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
830
631
  var _this = this;
831
632
  // image.width = this.options.width
832
633
  // image.height = this.options.height
@@ -851,7 +652,7 @@ var Watermark = /** @class */ (function () {
851
652
  });
852
653
  resolve(ctx.canvas);
853
654
  };
854
- Watermark.prototype.drawRichText = function (ctx, resolve) {
655
+ WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
855
656
  var _this = this;
856
657
  var obj = createCustomContentSVG(ctx, this.options);
857
658
  loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
@@ -860,7 +661,7 @@ var Watermark = /** @class */ (function () {
860
661
  resolve(ctx.canvas);
861
662
  });
862
663
  };
863
- Watermark.prototype.getImageRect = function (image) {
664
+ WatermarkCanvas.prototype.getImageRect = function (image) {
864
665
  var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
865
666
  switch (true) {
866
667
  case rect.width !== 0 && rect.height === 0:
@@ -876,7 +677,7 @@ var Watermark = /** @class */ (function () {
876
677
  }
877
678
  return rect;
878
679
  };
879
- Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
680
+ WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
880
681
  var _a, _b;
881
682
  var result = {
882
683
  x: -imageWidth / 2,
@@ -920,6 +721,223 @@ var Watermark = /** @class */ (function () {
920
721
  !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
921
722
  return result;
922
723
  };
724
+ return WatermarkCanvas;
725
+ }());
726
+
727
+ var GridLayout = /** @class */ (function () {
728
+ function GridLayout(args, partialCanvas) {
729
+ var _a, _b, _c, _d;
730
+ this.options = args;
731
+ this.partialWidth = this.options.width;
732
+ this.partialHeight = this.options.height;
733
+ this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
734
+ this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
735
+ this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
736
+ this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
737
+ this.partialCanvas = partialCanvas;
738
+ }
739
+ GridLayout.prototype.draw = function () {
740
+ var _a, _b, _c, _d, _e, _f, _g, _h;
741
+ var layoutCanvas = WatermarkCanvas.createCanvas(((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.width) || (this.partialWidth * this.cols + this.gap[0] * this.cols), ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.height) || (this.partialHeight * this.rows + this.gap[1] * this.rows));
742
+ var layoutContext = layoutCanvas.getContext('2d');
743
+ if ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.backgroundImage) {
744
+ layoutContext === null || layoutContext === void 0 ? void 0 : layoutContext.drawImage((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.backgroundImage, 0, 0, (_e = this.options.gridLayoutOptions) === null || _e === void 0 ? void 0 : _e.width, (_f = this.options.gridLayoutOptions) === null || _f === void 0 ? void 0 : _f.height);
745
+ }
746
+ for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
747
+ for (var colIndex = 0; colIndex < this.cols; colIndex++) {
748
+ if (!((_h = (_g = this.matrix) === null || _g === void 0 ? void 0 : _g[rowIndex]) === null || _h === void 0 ? void 0 : _h[colIndex])) {
749
+ continue;
750
+ }
751
+ 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);
752
+ }
753
+ }
754
+ return layoutCanvas;
755
+ };
756
+ return GridLayout;
757
+ }());
758
+
759
+ var renderLayout = function (options, partialCanvas) {
760
+ switch (options.layout) {
761
+ case 'grid':
762
+ return new GridLayout(options, partialCanvas).draw();
763
+ default:
764
+ return partialCanvas;
765
+ }
766
+ };
767
+ var generateBackgroundSize = function (options) {
768
+ var _a, _b, _c;
769
+ switch (options.layout) {
770
+ case 'grid':
771
+ {
772
+ var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
773
+ var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
774
+ var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
775
+ return [
776
+ options.width * cols + gap[0] * cols,
777
+ options.height * rows + gap[1] * rows
778
+ ];
779
+ }
780
+ default:
781
+ return [
782
+ options.width,
783
+ options.height
784
+ ];
785
+ }
786
+ };
787
+
788
+ /**
789
+ * Watermark class
790
+ */
791
+ var Watermark = /** @class */ (function () {
792
+ /**
793
+ * Watermark constructor
794
+ * @param args - watermark args
795
+ */
796
+ function Watermark(args) {
797
+ if (args === void 0) { args = {}; }
798
+ this.parentElement = document.body;
799
+ this.props = args;
800
+ this.options = Object.assign({}, initialOptions, args);
801
+ this.changeParentElement(this.options.parent);
802
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
803
+ bootstrap();
804
+ }
805
+ Watermark.prototype.changeOptions = function (args, mode) {
806
+ if (args === void 0) { args = {}; }
807
+ if (mode === void 0) { mode = 'overwrite'; }
808
+ this.initConfigData(args, mode);
809
+ };
810
+ /**
811
+ * Creating a watermark.
812
+ */
813
+ Watermark.prototype.create = function () {
814
+ var _a, _b, _c, _d, _e, _f, _g;
815
+ return __awaiter(this, void 0, void 0, function () {
816
+ var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
817
+ return __generator(this, function (_h) {
818
+ switch (_h.label) {
819
+ case 0:
820
+ if (!this.validateUnique()) {
821
+ return [2 /*return*/];
822
+ }
823
+ if (!this.validateContent()) {
824
+ return [2 /*return*/];
825
+ }
826
+ firstDraw = isUndefined(this.watermarkDom);
827
+ return [4 /*yield*/, ((_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw())];
828
+ case 1:
829
+ _h.sent();
830
+ this.layoutCanvas = renderLayout(this.options, (_b = this.watermarkCanvas) === null || _b === void 0 ? void 0 : _b.getCanvas());
831
+ image = convertImage(this.layoutCanvas);
832
+ (_c = this.watermarkCanvas) === null || _c === void 0 ? void 0 : _c.clear();
833
+ this.watermarkDom = document.createElement('div');
834
+ watermarkInnerDom = document.createElement('div');
835
+ this.watermarkDom.__WATERMARK__ = 'watermark';
836
+ this.watermarkDom.__WATERMARK__INSTANCE__ = this;
837
+ parentElementType = this.checkParentElementType();
838
+ 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 ");
839
+ backgroundSize = generateBackgroundSize(this.options);
840
+ 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 ");
841
+ this.watermarkDom.append(watermarkInnerDom);
842
+ this.parentElement.appendChild(this.watermarkDom);
843
+ if (this.options.mutationObserve) {
844
+ try {
845
+ this.bindMutationObserve();
846
+ }
847
+ catch (e) {
848
+ (_e = (_d = this.options).onObserveError) === null || _e === void 0 ? void 0 : _e.call(_d);
849
+ }
850
+ }
851
+ firstDraw && ((_g = (_f = this.options).onSuccess) === null || _g === void 0 ? void 0 : _g.call(_f));
852
+ return [2 /*return*/];
853
+ }
854
+ });
855
+ });
856
+ };
857
+ /**
858
+ * Delete this watermark.
859
+ */
860
+ Watermark.prototype.destroy = function () {
861
+ this.remove();
862
+ this.watermarkDom = undefined;
863
+ };
864
+ Watermark.prototype.check = function () {
865
+ return __awaiter(this, void 0, void 0, function () {
866
+ return __generator(this, function (_a) {
867
+ switch (_a.label) {
868
+ case 0:
869
+ if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
870
+ this.remove();
871
+ return [4 /*yield*/, this.create()];
872
+ case 1:
873
+ _a.sent();
874
+ _a.label = 2;
875
+ case 2: return [2 /*return*/];
876
+ }
877
+ });
878
+ });
879
+ };
880
+ Watermark.prototype.remove = function () {
881
+ var _a, _b, _c, _d, _e, _f, _g;
882
+ (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
883
+ (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
884
+ (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
885
+ if (!isUndefined(this.checkWatermarkElementRequestID)) {
886
+ cancelAnimationFrame(this.checkWatermarkElementRequestID);
887
+ }
888
+ (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
889
+ (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
890
+ };
891
+ Watermark.prototype.initConfigData = function (args, mode) {
892
+ var _this = this;
893
+ if (mode === void 0) { mode = 'overwrite'; }
894
+ this.props = args;
895
+ if (mode === 'overwrite') {
896
+ this.options = Object.assign({}, initialOptions, args);
897
+ }
898
+ else {
899
+ Object.keys(args).forEach(function (key) {
900
+ _this.options[key] = args[key];
901
+ });
902
+ }
903
+ this.changeParentElement(this.options.parent);
904
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
905
+ };
906
+ Watermark.prototype.changeParentElement = function (parent) {
907
+ if (typeof parent === 'string') {
908
+ var parentElement = document.querySelector(parent);
909
+ parentElement && (this.parentElement = parentElement);
910
+ }
911
+ else {
912
+ this.parentElement = parent;
913
+ }
914
+ };
915
+ Watermark.prototype.validateUnique = function () {
916
+ var result = true;
917
+ if (this.options.unique) {
918
+ this.parentElement.childNodes.forEach(function (node) {
919
+ if (!result) {
920
+ return;
921
+ }
922
+ if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
923
+ result = false;
924
+ // throw new Error('duplicate watermark error')
925
+ }
926
+ });
927
+ }
928
+ return result;
929
+ };
930
+ Watermark.prototype.validateContent = function () {
931
+ switch (this.options.contentType) {
932
+ case 'image':
933
+ return Object.hasOwnProperty.call(this.options, 'image');
934
+ case 'multi-line-text':
935
+ case 'rich-text':
936
+ case 'text':
937
+ return this.options.content.length > 0;
938
+ }
939
+ return false;
940
+ };
923
941
  Watermark.prototype.checkParentElementType = function () {
924
942
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
925
943
  return 'root';
@@ -1060,6 +1078,50 @@ var BlindWatermark = /** @class */ (function (_super) {
1060
1078
  return BlindWatermark;
1061
1079
  }(Watermark));
1062
1080
 
1081
+ /**
1082
+ * ImageWatermark class
1083
+ */
1084
+ var ImageWatermark = /** @class */ (function () {
1085
+ /**
1086
+ * ImageWatermark constructor
1087
+ * @param args - image watermark args
1088
+ */
1089
+ function ImageWatermark(args) {
1090
+ if (args === void 0) { args = {}; }
1091
+ var _a;
1092
+ this.drew = false;
1093
+ this.props = args;
1094
+ this.options = Object.assign({}, initialOptions, args);
1095
+ this.watermarkCanvas = new WatermarkCanvas(this.props, this.options);
1096
+ this.originalSrc = (_a = this.props.dom) === null || _a === void 0 ? void 0 : _a.src;
1097
+ this.backgroundImage = this.getBackgroundImage();
1098
+ }
1099
+ ImageWatermark.prototype.create = function () {
1100
+ var _a, _b, _c, _d, _e;
1101
+ if (this.drew) {
1102
+ return;
1103
+ }
1104
+ (_a = this.watermarkCanvas) === null || _a === void 0 ? void 0 : _a.draw();
1105
+ this.options.layout = 'grid';
1106
+ this.options.gridLayoutOptions = __assign(__assign({}, this.options.gridLayoutOptions), { width: (_b = this.backgroundImage) === null || _b === void 0 ? void 0 : _b.width, height: (_c = this.backgroundImage) === null || _c === void 0 ? void 0 : _c.height, backgroundImage: this.backgroundImage });
1107
+ this.layoutCanvas = renderLayout(this.options, (_d = this.watermarkCanvas) === null || _d === void 0 ? void 0 : _d.getCanvas());
1108
+ this.options.dom.src = convertImage(this.layoutCanvas);
1109
+ (_e = this.watermarkCanvas) === null || _e === void 0 ? void 0 : _e.clear();
1110
+ this.drew = true;
1111
+ };
1112
+ ImageWatermark.prototype.destroy = function () {
1113
+ this.options.dom.src = this.originalSrc;
1114
+ this.drew = false;
1115
+ };
1116
+ ImageWatermark.prototype.getBackgroundImage = function () {
1117
+ if (this.options.dom) {
1118
+ return this.options.dom;
1119
+ }
1120
+ };
1121
+ return ImageWatermark;
1122
+ }());
1123
+
1063
1124
  exports.BlindWatermark = BlindWatermark;
1125
+ exports.ImageWatermark = ImageWatermark;
1064
1126
  exports.Watermark = Watermark;
1065
1127
  //# sourceMappingURL=index.cjs.js.map