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