watermark-js-plus 0.9.11 → 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
@@ -466,224 +467,25 @@ var WatermarkCanvas = /** @class */ (function () {
466
467
  (_a = canvas.getContext('2d')) === null || _a === void 0 ? void 0 : _a.setTransform(ratio, 0, 0, ratio, 0, 0);
467
468
  return canvas;
468
469
  };
469
- return WatermarkCanvas;
470
- }());
471
-
472
- var GridLayout = /** @class */ (function () {
473
- function GridLayout(args, partialCanvas) {
474
- var _a, _b, _c, _d;
475
- this.options = args;
476
- this.partialWidth = this.options.width;
477
- this.partialHeight = this.options.height;
478
- this.rows = ((_a = this.options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.rows) || 1;
479
- this.cols = ((_b = this.options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.cols) || 1;
480
- this.matrix = ((_c = this.options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.matrix) || generateMatrix(this.rows, this.cols, 1);
481
- this.gap = ((_d = this.options.gridLayoutOptions) === null || _d === void 0 ? void 0 : _d.gap) || [0, 0];
482
- this.partialCanvas = partialCanvas;
483
- }
484
- GridLayout.prototype.draw = function () {
485
- var _a, _b;
486
- var layoutCanvas = WatermarkCanvas.createCanvas(this.partialWidth * this.cols + this.gap[0] * this.cols, this.partialHeight * this.rows + this.gap[1] * this.rows);
487
- var layoutContext = layoutCanvas.getContext('2d');
488
- for (var rowIndex = 0; rowIndex < this.rows; rowIndex++) {
489
- for (var colIndex = 0; colIndex < this.cols; colIndex++) {
490
- if (!((_b = (_a = this.matrix) === null || _a === void 0 ? void 0 : _a[rowIndex]) === null || _b === void 0 ? void 0 : _b[colIndex])) {
491
- continue;
492
- }
493
- 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);
494
- }
495
- }
496
- return layoutCanvas;
497
- };
498
- return GridLayout;
499
- }());
500
-
501
- var renderLayout = function (options, partialCanvas) {
502
- switch (options.layout) {
503
- case 'grid':
504
- return new GridLayout(options, partialCanvas).draw();
505
- default:
506
- return partialCanvas;
507
- }
508
- };
509
- var generateBackgroundSize = function (options) {
510
- var _a, _b, _c;
511
- switch (options.layout) {
512
- case 'grid':
513
- {
514
- var cols = ((_a = options.gridLayoutOptions) === null || _a === void 0 ? void 0 : _a.cols) || 1;
515
- var rows = ((_b = options.gridLayoutOptions) === null || _b === void 0 ? void 0 : _b.rows) || 1;
516
- var gap = ((_c = options.gridLayoutOptions) === null || _c === void 0 ? void 0 : _c.gap) || [0, 0];
517
- return [
518
- options.width * cols + gap[0] * cols,
519
- options.height * rows + gap[1] * rows
520
- ];
521
- }
522
- default:
523
- return [
524
- options.width,
525
- options.height
526
- ];
527
- }
528
- };
529
-
530
- /**
531
- * Watermark class
532
- */
533
- var Watermark = /** @class */ (function () {
534
- /**
535
- * Watermark constructor
536
- * @param args - watermark args
537
- */
538
- function Watermark(args) {
539
- if (args === void 0) { args = {}; }
540
- this.parentElement = document.body;
541
- this.props = args;
542
- this.options = Object.assign({}, initialOptions, args);
543
- this.changeParentElement(this.options.parent);
544
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
545
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
546
- bootstrap();
547
- }
548
- Watermark.prototype.changeOptions = function (args, mode) {
549
- if (args === void 0) { args = {}; }
550
- if (mode === void 0) { mode = 'overwrite'; }
551
- this.initConfigData(args, mode);
552
- };
553
470
  /**
554
- * Creating a watermark.
471
+ * Clean the canvas
472
+ * @param canvas
555
473
  */
556
- Watermark.prototype.create = function () {
557
- var _a, _b, _c, _d;
558
- return __awaiter(this, void 0, void 0, function () {
559
- var firstDraw, image, watermarkInnerDom, parentElementType, backgroundSize;
560
- return __generator(this, function (_e) {
561
- switch (_e.label) {
562
- case 0:
563
- if (!this.validateUnique()) {
564
- return [2 /*return*/];
565
- }
566
- if (!this.validateContent()) {
567
- return [2 /*return*/];
568
- }
569
- firstDraw = isUndefined(this.watermarkDom);
570
- return [4 /*yield*/, this.draw()];
571
- case 1:
572
- _e.sent();
573
- this.layoutCanvas = renderLayout(this.options, this.canvas);
574
- image = convertImage(this.layoutCanvas);
575
- this.clearCanvas();
576
- this.watermarkDom = document.createElement('div');
577
- watermarkInnerDom = document.createElement('div');
578
- this.watermarkDom.__WATERMARK__ = 'watermark';
579
- this.watermarkDom.__WATERMARK__INSTANCE__ = this;
580
- parentElementType = this.checkParentElementType();
581
- 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 ");
582
- backgroundSize = generateBackgroundSize(this.options);
583
- 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 ");
584
- this.watermarkDom.append(watermarkInnerDom);
585
- this.parentElement.appendChild(this.watermarkDom);
586
- if (this.options.mutationObserve) {
587
- try {
588
- this.bindMutationObserve();
589
- }
590
- catch (e) {
591
- (_b = (_a = this.options).onObserveError) === null || _b === void 0 ? void 0 : _b.call(_a);
592
- }
593
- }
594
- firstDraw && ((_d = (_c = this.options).onSuccess) === null || _d === void 0 ? void 0 : _d.call(_c));
595
- return [2 /*return*/];
596
- }
597
- });
598
- });
599
- };
600
- /**
601
- * Delete this watermark.
602
- */
603
- Watermark.prototype.destroy = function () {
604
- this.remove();
605
- this.watermarkDom = undefined;
606
- };
607
- Watermark.prototype.check = function () {
608
- return __awaiter(this, void 0, void 0, function () {
609
- return __generator(this, function (_a) {
610
- switch (_a.label) {
611
- case 0:
612
- if (!!document.body.contains(this.watermarkDom)) return [3 /*break*/, 2];
613
- this.remove();
614
- return [4 /*yield*/, this.create()];
615
- case 1:
616
- _a.sent();
617
- _a.label = 2;
618
- case 2: return [2 /*return*/];
619
- }
620
- });
621
- });
622
- };
623
- Watermark.prototype.remove = function () {
624
- var _a, _b, _c, _d, _e, _f, _g;
625
- (_b = (_a = this.options).onBeforeDestroy) === null || _b === void 0 ? void 0 : _b.call(_a);
626
- (_c = this.observer) === null || _c === void 0 ? void 0 : _c.disconnect();
627
- (_d = this.parentObserve) === null || _d === void 0 ? void 0 : _d.disconnect();
628
- if (!isUndefined(this.checkWatermarkElementRequestID)) {
629
- cancelAnimationFrame(this.checkWatermarkElementRequestID);
630
- }
631
- (_e = this.watermarkDom) === null || _e === void 0 ? void 0 : _e.remove();
632
- (_g = (_f = this.options).onDestroyed) === null || _g === void 0 ? void 0 : _g.call(_f);
633
- };
634
- Watermark.prototype.initConfigData = function (args, mode) {
635
- var _this = this;
636
- if (mode === void 0) { mode = 'overwrite'; }
637
- this.props = args;
638
- if (mode === 'overwrite') {
639
- this.options = Object.assign({}, initialOptions, args);
640
- }
641
- else {
642
- Object.keys(args).forEach(function (key) {
643
- _this.options[key] = args[key];
644
- });
645
- }
646
- this.changeParentElement(this.options.parent);
647
- this.canvas = WatermarkCanvas.createCanvas(this.options.width, this.options.height);
648
- this.recommendOptions = generateRecommendOptions(this.canvas, this.options, this.props);
649
- // document.body?.appendChild(this.canvas)
650
- };
651
- Watermark.prototype.changeParentElement = function (parent) {
652
- if (typeof parent === 'string') {
653
- var parentElement = document.querySelector(parent);
654
- parentElement && (this.parentElement = parentElement);
655
- }
656
- else {
657
- this.parentElement = parent;
474
+ WatermarkCanvas.clearCanvas = function (canvas) {
475
+ var ctx = canvas.getContext('2d');
476
+ if (ctx === null) {
477
+ throw new Error('get context error');
658
478
  }
479
+ ctx.restore();
480
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
659
481
  };
660
- Watermark.prototype.validateUnique = function () {
661
- var result = true;
662
- if (this.options.unique) {
663
- this.parentElement.childNodes.forEach(function (node) {
664
- if (!result) {
665
- return;
666
- }
667
- if (Object.hasOwnProperty.call(node, '__WATERMARK__')) {
668
- result = false;
669
- // throw new Error('duplicate watermark error')
670
- }
671
- });
672
- }
673
- return result;
482
+ WatermarkCanvas.prototype.getCanvas = function () {
483
+ return this.canvas;
674
484
  };
675
- Watermark.prototype.validateContent = function () {
676
- switch (this.options.contentType) {
677
- case 'image':
678
- return Object.hasOwnProperty.call(this.options, 'image');
679
- case 'multi-line-text':
680
- case 'rich-text':
681
- case 'text':
682
- return this.options.content.length > 0;
683
- }
684
- return false;
485
+ WatermarkCanvas.prototype.clear = function () {
486
+ WatermarkCanvas.clearCanvas(this.canvas);
685
487
  };
686
- Watermark.prototype.draw = function () {
488
+ WatermarkCanvas.prototype.draw = function () {
687
489
  var _this = this;
688
490
  var ctx = this.canvas.getContext('2d');
689
491
  if (ctx === null) {
@@ -724,7 +526,7 @@ var Watermark = /** @class */ (function () {
724
526
  }
725
527
  });
726
528
  };
727
- Watermark.prototype.setStyle = function (ctx) {
529
+ WatermarkCanvas.prototype.setStyle = function (ctx) {
728
530
  var _a;
729
531
  var propName = 'fillStyle';
730
532
  if (this.options.textType === 'stroke') {
@@ -761,7 +563,7 @@ var Watermark = /** @class */ (function () {
761
563
  this.options.extraDrawFunc(ctx);
762
564
  }
763
565
  };
764
- Watermark.prototype.createLinearGradient = function (ctx) {
566
+ WatermarkCanvas.prototype.createLinearGradient = function (ctx) {
765
567
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
766
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));
767
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) {
@@ -769,7 +571,7 @@ var Watermark = /** @class */ (function () {
769
571
  });
770
572
  return gradient;
771
573
  };
772
- Watermark.prototype.createConicGradient = function (ctx) {
574
+ WatermarkCanvas.prototype.createConicGradient = function (ctx) {
773
575
  var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
774
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));
775
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) {
@@ -777,7 +579,7 @@ var Watermark = /** @class */ (function () {
777
579
  });
778
580
  return gradient;
779
581
  };
780
- Watermark.prototype.createRadialGradient = function (ctx) {
582
+ WatermarkCanvas.prototype.createRadialGradient = function (ctx) {
781
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;
782
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));
783
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) {
@@ -785,18 +587,18 @@ var Watermark = /** @class */ (function () {
785
587
  });
786
588
  return gradient;
787
589
  };
788
- Watermark.prototype.createPattern = function (ctx) {
590
+ WatermarkCanvas.prototype.createPattern = function (ctx) {
789
591
  var _a, _b, _c, _d, _e, _f, _g, _h;
790
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) || '');
791
593
  };
792
- Watermark.prototype.setText = function (ctx, params) {
594
+ WatermarkCanvas.prototype.setText = function (ctx, params) {
793
595
  var methodName = 'fillText';
794
596
  if (this.options.textType === 'stroke') {
795
597
  methodName = 'strokeText';
796
598
  }
797
599
  ctx[methodName] && ctx[methodName](params.text, params.x, params.y, params.maxWidth);
798
600
  };
799
- Watermark.prototype.drawText = function (ctx, resolve) {
601
+ WatermarkCanvas.prototype.drawText = function (ctx, resolve) {
800
602
  this.setText(ctx, {
801
603
  text: this.options.content,
802
604
  x: 0,
@@ -805,7 +607,7 @@ var Watermark = /** @class */ (function () {
805
607
  });
806
608
  resolve(ctx.canvas);
807
609
  };
808
- Watermark.prototype.drawImage = function (ctx, resolve) {
610
+ WatermarkCanvas.prototype.drawImage = function (ctx, resolve) {
809
611
  var _this = this;
810
612
  loadImage(this.options.image).then(function (image) {
811
613
  var _a = _this.getImageRect(image), imageWidth = _a.width, imageHeight = _a.height;
@@ -814,7 +616,7 @@ var Watermark = /** @class */ (function () {
814
616
  resolve(ctx.canvas);
815
617
  });
816
618
  };
817
- Watermark.prototype.drawMultiLineText = function (ctx, resolve) {
619
+ WatermarkCanvas.prototype.drawMultiLineText = function (ctx, resolve) {
818
620
  var _this = this;
819
621
  // image.width = this.options.width
820
622
  // image.height = this.options.height
@@ -839,7 +641,7 @@ var Watermark = /** @class */ (function () {
839
641
  });
840
642
  resolve(ctx.canvas);
841
643
  };
842
- Watermark.prototype.drawRichText = function (ctx, resolve) {
644
+ WatermarkCanvas.prototype.drawRichText = function (ctx, resolve) {
843
645
  var _this = this;
844
646
  var obj = createCustomContentSVG(ctx, this.options);
845
647
  loadImage(convertSVGToImage(obj.element), obj.width, obj.height).then(function (image) {
@@ -848,7 +650,7 @@ var Watermark = /** @class */ (function () {
848
650
  resolve(ctx.canvas);
849
651
  });
850
652
  };
851
- Watermark.prototype.getImageRect = function (image) {
653
+ WatermarkCanvas.prototype.getImageRect = function (image) {
852
654
  var rect = { width: this.options.imageWidth || 0, height: this.options.imageHeight || 0 };
853
655
  switch (true) {
854
656
  case rect.width !== 0 && rect.height === 0:
@@ -864,7 +666,7 @@ var Watermark = /** @class */ (function () {
864
666
  }
865
667
  return rect;
866
668
  };
867
- Watermark.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
669
+ WatermarkCanvas.prototype.getDrawImagePosition = function (imageWidth, imageHeight) {
868
670
  var _a, _b;
869
671
  var result = {
870
672
  x: -imageWidth / 2,
@@ -908,6 +710,220 @@ var Watermark = /** @class */ (function () {
908
710
  !isUndefined((_b = this.props) === null || _b === void 0 ? void 0 : _b.translateY) && (result.y = 0);
909
711
  return result;
910
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
+ };
911
927
  Watermark.prototype.checkParentElementType = function () {
912
928
  if (['html', 'body'].includes(this.parentElement.tagName.toLocaleLowerCase())) {
913
929
  return 'root';
@@ -991,15 +1007,6 @@ var Watermark = /** @class */ (function () {
991
1007
  characterData: true // 节点内容或节点文本的变动。
992
1008
  });
993
1009
  };
994
- Watermark.prototype.clearCanvas = function () {
995
- var ctx = this.canvas.getContext('2d');
996
- if (ctx === null) {
997
- throw new Error('get context error');
998
- }
999
- ctx.restore();
1000
- ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
1001
- // this.canvas.width = this.options.w
1002
- };
1003
1010
  return Watermark;
1004
1011
  }());
1005
1012