@idraw/util 0.2.0-alpha.8 → 0.3.0-alpha.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
@@ -68,12 +68,12 @@ function createUUID() {
68
68
  function str4() {
69
69
  return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
70
70
  }
71
- return "" + str4() + str4() + "-" + str4() + "-" + str4() + "-" + str4() + "-" + str4() + str4() + str4();
71
+ return "".concat(str4()).concat(str4(), "-").concat(str4(), "-").concat(str4(), "-").concat(str4(), "-").concat(str4()).concat(str4()).concat(str4());
72
72
  }
73
73
 
74
74
  function deepClone(target) {
75
75
  function _clone(t) {
76
- var type = is(t);
76
+ var type = is$1(t);
77
77
  if (['Null', 'Number', 'String', 'Boolean', 'Undefined'].indexOf(type) >= 0) {
78
78
  return t;
79
79
  }
@@ -95,7 +95,7 @@ function deepClone(target) {
95
95
  }
96
96
  return _clone(target);
97
97
  }
98
- function is(data) {
98
+ function is$1(data) {
99
99
  return Object.prototype.toString.call(data).replace(/[\]|\[]{1,1}/ig, '').split(' ')[1];
100
100
  }
101
101
 
@@ -138,6 +138,16 @@ var istype = {
138
138
  },
139
139
  };
140
140
 
141
+ var __assign = function() {
142
+ __assign = Object.assign || function __assign(t) {
143
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
144
+ s = arguments[i];
145
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
146
+ }
147
+ return t;
148
+ };
149
+ return __assign.apply(this, arguments);
150
+ };
141
151
  function __awaiter(thisArg, _arguments, P, generator) {
142
152
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
143
153
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -178,7 +188,7 @@ function __generator(thisArg, body) {
178
188
  function parseHTMLToDataURL(html, opts) {
179
189
  var width = opts.width, height = opts.height;
180
190
  return new Promise(function (resolve, reject) {
181
- var _svg = "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + (width || '') + "\" height = \"" + (height || '') + "\">\n <foreignObject width=\"100%\" height=\"100%\">\n <div xmlns = \"http://www.w3.org/1999/xhtml\">\n " + html + "\n </div>\n </foreignObject>\n </svg>\n ";
191
+ var _svg = "\n <svg xmlns=\"http://www.w3.org/2000/svg\" width=\"".concat(width || '', "\" height = \"").concat(height || '', "\">\n <foreignObject width=\"100%\" height=\"100%\">\n <div xmlns = \"http://www.w3.org/1999/xhtml\">\n ").concat(html, "\n </div>\n </foreignObject>\n </svg>\n ");
182
192
  var blob = new Blob([_svg], { type: 'image/svg+xml;charset=utf-8' });
183
193
  var reader = new FileReader();
184
194
  reader.readAsDataURL(blob);
@@ -254,32 +264,435 @@ function loadHTML(html, opts) {
254
264
  });
255
265
  }
256
266
 
257
- var index = {
258
- time: {
259
- delay: delay,
260
- compose: compose,
261
- throttle: throttle,
262
- },
263
- loader: {
264
- loadImage: loadImage,
265
- loadSVG: loadSVG,
266
- loadHTML: loadHTML,
267
- },
268
- file: {
269
- downloadImageFromCanvas: downloadImageFromCanvas,
270
- },
271
- color: {
272
- toColorHexStr: toColorHexStr,
273
- toColorHexNum: toColorHexNum,
274
- isColorStr: isColorStr,
275
- },
276
- uuid: {
277
- createUUID: createUUID
278
- },
279
- istype: istype,
280
- data: {
281
- deepClone: deepClone,
267
+ var Context = (function () {
268
+ function Context(ctx, opts) {
269
+ this._opts = opts;
270
+ this._ctx = ctx;
271
+ this._transform = {
272
+ scale: 1,
273
+ scrollX: 0,
274
+ scrollY: 0,
275
+ };
276
+ }
277
+ Context.prototype.getContext = function () {
278
+ return this._ctx;
279
+ };
280
+ Context.prototype.resetSize = function (opts) {
281
+ this._opts = __assign(__assign({}, this._opts), opts);
282
+ };
283
+ Context.prototype.calcDeviceNum = function (num) {
284
+ return num * this._opts.devicePixelRatio;
285
+ };
286
+ Context.prototype.calcScreenNum = function (num) {
287
+ return num / this._opts.devicePixelRatio;
288
+ };
289
+ Context.prototype.getSize = function () {
290
+ return {
291
+ width: this._opts.width,
292
+ height: this._opts.height,
293
+ contextWidth: this._opts.contextWidth,
294
+ contextHeight: this._opts.contextHeight,
295
+ devicePixelRatio: this._opts.devicePixelRatio,
296
+ };
297
+ };
298
+ Context.prototype.setTransform = function (config) {
299
+ this._transform = __assign(__assign({}, this._transform), config);
300
+ };
301
+ Context.prototype.getTransform = function () {
302
+ return {
303
+ scale: this._transform.scale,
304
+ scrollX: this._transform.scrollX,
305
+ scrollY: this._transform.scrollY,
306
+ };
307
+ };
308
+ Context.prototype.setFillStyle = function (color) {
309
+ this._ctx.fillStyle = color;
310
+ };
311
+ Context.prototype.fill = function (fillRule) {
312
+ return this._ctx.fill(fillRule || 'nonzero');
313
+ };
314
+ Context.prototype.arc = function (x, y, radius, startAngle, endAngle, anticlockwise) {
315
+ return this._ctx.arc(this._doSize(x), this._doSize(y), this._doSize(radius), startAngle, endAngle, anticlockwise);
316
+ };
317
+ Context.prototype.rect = function (x, y, w, h) {
318
+ return this._ctx.rect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
319
+ };
320
+ Context.prototype.fillRect = function (x, y, w, h) {
321
+ return this._ctx.fillRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
322
+ };
323
+ Context.prototype.clearRect = function (x, y, w, h) {
324
+ return this._ctx.clearRect(this._doSize(x), this._doSize(y), this._doSize(w), this._doSize(h));
325
+ };
326
+ Context.prototype.beginPath = function () {
327
+ return this._ctx.beginPath();
328
+ };
329
+ Context.prototype.closePath = function () {
330
+ return this._ctx.closePath();
331
+ };
332
+ Context.prototype.lineTo = function (x, y) {
333
+ return this._ctx.lineTo(this._doSize(x), this._doSize(y));
334
+ };
335
+ Context.prototype.moveTo = function (x, y) {
336
+ return this._ctx.moveTo(this._doSize(x), this._doSize(y));
337
+ };
338
+ Context.prototype.arcTo = function (x1, y1, x2, y2, radius) {
339
+ return this._ctx.arcTo(this._doSize(x1), this._doSize(y1), this._doSize(x2), this._doSize(y2), this._doSize(radius));
340
+ };
341
+ Context.prototype.setLineWidth = function (w) {
342
+ return this._ctx.lineWidth = this._doSize(w);
343
+ };
344
+ Context.prototype.setLineDash = function (nums) {
345
+ var _this = this;
346
+ return this._ctx.setLineDash(nums.map(function (n) { return _this._doSize(n); }));
347
+ };
348
+ Context.prototype.isPointInPath = function (x, y) {
349
+ return this._ctx.isPointInPath(this._doX(x), this._doY(y));
350
+ };
351
+ Context.prototype.isPointInPathWithoutScroll = function (x, y) {
352
+ return this._ctx.isPointInPath(this._doSize(x), this._doSize(y));
353
+ };
354
+ Context.prototype.setStrokeStyle = function (color) {
355
+ this._ctx.strokeStyle = color;
356
+ };
357
+ Context.prototype.stroke = function () {
358
+ return this._ctx.stroke();
359
+ };
360
+ Context.prototype.translate = function (x, y) {
361
+ return this._ctx.translate(this._doSize(x), this._doSize(y));
362
+ };
363
+ Context.prototype.rotate = function (angle) {
364
+ return this._ctx.rotate(angle);
365
+ };
366
+ Context.prototype.drawImage = function () {
367
+ var args = [];
368
+ for (var _i = 0; _i < arguments.length; _i++) {
369
+ args[_i] = arguments[_i];
370
+ }
371
+ var image = args[0];
372
+ var sx = args[1];
373
+ var sy = args[2];
374
+ var sw = args[3];
375
+ var sh = args[4];
376
+ var dx = args[args.length - 4];
377
+ var dy = args[args.length - 3];
378
+ var dw = args[args.length - 2];
379
+ var dh = args[args.length - 1];
380
+ if (args.length === 9) {
381
+ return this._ctx.drawImage(image, this._doSize(sx), this._doSize(sy), this._doSize(sw), this._doSize(sh), this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
382
+ }
383
+ else {
384
+ return this._ctx.drawImage(image, this._doSize(dx), this._doSize(dy), this._doSize(dw), this._doSize(dh));
385
+ }
386
+ };
387
+ Context.prototype.createPattern = function (image, repetition) {
388
+ return this._ctx.createPattern(image, repetition);
389
+ };
390
+ Context.prototype.measureText = function (text) {
391
+ return this._ctx.measureText(text);
392
+ };
393
+ Context.prototype.setTextAlign = function (align) {
394
+ this._ctx.textAlign = align;
395
+ };
396
+ Context.prototype.fillText = function (text, x, y, maxWidth) {
397
+ if (maxWidth !== undefined) {
398
+ return this._ctx.fillText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
399
+ }
400
+ else {
401
+ return this._ctx.fillText(text, this._doSize(x), this._doSize(y));
402
+ }
403
+ };
404
+ Context.prototype.strokeText = function (text, x, y, maxWidth) {
405
+ if (maxWidth !== undefined) {
406
+ return this._ctx.strokeText(text, this._doSize(x), this._doSize(y), this._doSize(maxWidth));
407
+ }
408
+ else {
409
+ return this._ctx.strokeText(text, this._doSize(x), this._doSize(y));
410
+ }
411
+ };
412
+ Context.prototype.setFont = function (opts) {
413
+ var strList = [];
414
+ if (opts.fontWeight === 'bold') {
415
+ strList.push("".concat(opts.fontWeight));
416
+ }
417
+ strList.push("".concat(this._doSize(opts.fontSize || 12), "px"));
418
+ strList.push("".concat(opts.fontFamily || 'sans-serif'));
419
+ this._ctx.font = "".concat(strList.join(' '));
420
+ };
421
+ Context.prototype.setTextBaseline = function (baseline) {
422
+ this._ctx.textBaseline = baseline;
423
+ };
424
+ Context.prototype.setGlobalAlpha = function (alpha) {
425
+ this._ctx.globalAlpha = alpha;
426
+ };
427
+ Context.prototype.save = function () {
428
+ this._ctx.save();
429
+ };
430
+ Context.prototype.restore = function () {
431
+ this._ctx.restore();
432
+ };
433
+ Context.prototype.scale = function (ratioX, ratioY) {
434
+ this._ctx.scale(ratioX, ratioY);
435
+ };
436
+ Context.prototype.setShadowColor = function (color) {
437
+ this._ctx.shadowColor = color;
438
+ };
439
+ Context.prototype.setShadowOffsetX = function (offsetX) {
440
+ this._ctx.shadowOffsetX = this._doSize(offsetX);
441
+ };
442
+ Context.prototype.setShadowOffsetY = function (offsetY) {
443
+ this._ctx.shadowOffsetY = this._doSize(offsetY);
444
+ };
445
+ Context.prototype.setShadowBlur = function (blur) {
446
+ this._ctx.shadowBlur = this._doSize(blur);
447
+ };
448
+ Context.prototype.ellipse = function (x, y, radiusX, radiusY, rotation, startAngle, endAngle, counterclockwise) {
449
+ this._ctx.ellipse(this._doSize(x), this._doSize(y), this._doSize(radiusX), this._doSize(radiusY), rotation, startAngle, endAngle, counterclockwise);
450
+ };
451
+ Context.prototype._doSize = function (num) {
452
+ return this._opts.devicePixelRatio * num;
453
+ };
454
+ Context.prototype._doX = function (x) {
455
+ var _a = this._transform, scale = _a.scale, scrollX = _a.scrollX;
456
+ var _x = (x - scrollX) / scale;
457
+ return this._doSize(_x);
458
+ };
459
+ Context.prototype._doY = function (y) {
460
+ var _a = this._transform, scale = _a.scale, scrollY = _a.scrollY;
461
+ var _y = (y - scrollY) / scale;
462
+ return this._doSize(_y);
463
+ };
464
+ return Context;
465
+ }());
466
+
467
+ function number(value) {
468
+ return (typeof value === 'number' && (value > 0 || value <= 0));
469
+ }
470
+ function x(value) {
471
+ return number(value);
472
+ }
473
+ function y(value) {
474
+ return number(value);
475
+ }
476
+ function w(value) {
477
+ return (typeof value === 'number' && value >= 0);
478
+ }
479
+ function h(value) {
480
+ return (typeof value === 'number' && value >= 0);
481
+ }
482
+ function angle(value) {
483
+ return (typeof value === 'number' && value >= -360 && value <= 360);
484
+ }
485
+ function borderWidth(value) {
486
+ return w(value);
487
+ }
488
+ function borderRadius(value) {
489
+ return number(value) && value >= 0;
490
+ }
491
+ function color(value) {
492
+ return isColorStr(value);
493
+ }
494
+ function imageURL(value) {
495
+ return (typeof value === 'string' && /^(http:\/\/|https:\/\/|\.\/|\/)/.test("".concat(value)));
496
+ }
497
+ function imageBase64(value) {
498
+ return (typeof value === 'string' && /^(data:image\/)/.test("".concat(value)));
499
+ }
500
+ function imageSrc(value) {
501
+ return (imageBase64(value) || imageURL(value));
502
+ }
503
+ function svg(value) {
504
+ return (typeof value === 'string' && /^(<svg[\s]{1,}|<svg>)/i.test("".concat(value).trim()) && /<\/[\s]{0,}svg>$/i.test("".concat(value).trim()));
505
+ }
506
+ function html(value) {
507
+ var result = false;
508
+ if (typeof value === 'string') {
509
+ var div = document.createElement('div');
510
+ div.innerHTML = value;
511
+ if (div.children.length > 0) {
512
+ result = true;
513
+ }
514
+ div = null;
282
515
  }
516
+ return result;
517
+ }
518
+ function text(value) {
519
+ return typeof value === 'string';
520
+ }
521
+ function fontSize(value) {
522
+ return number(value) && value > 0;
523
+ }
524
+ function lineHeight(value) {
525
+ return number(value) && value > 0;
526
+ }
527
+ function strokeWidth(value) {
528
+ return number(value) && value > 0;
529
+ }
530
+ function textAlign(value) {
531
+ return ['center', 'left', 'right'].includes(value);
532
+ }
533
+ function fontFamily(value) {
534
+ return typeof value === 'string' && value.length > 0;
535
+ }
536
+ function fontWeight(value) {
537
+ return ['bold'].includes(value);
538
+ }
539
+ var is = {
540
+ x: x,
541
+ y: y,
542
+ w: w,
543
+ h: h,
544
+ angle: angle,
545
+ number: number,
546
+ borderWidth: borderWidth,
547
+ borderRadius: borderRadius,
548
+ color: color,
549
+ imageSrc: imageSrc,
550
+ imageURL: imageURL,
551
+ imageBase64: imageBase64,
552
+ svg: svg,
553
+ html: html,
554
+ text: text,
555
+ fontSize: fontSize,
556
+ lineHeight: lineHeight,
557
+ textAlign: textAlign,
558
+ fontFamily: fontFamily,
559
+ fontWeight: fontWeight,
560
+ strokeWidth: strokeWidth,
283
561
  };
284
562
 
285
- module.exports = index;
563
+ function attrs(attrs) {
564
+ var x = attrs.x, y = attrs.y, w = attrs.w, h = attrs.h, angle = attrs.angle;
565
+ if (!(is.x(x) && is.y(y) && is.w(w) && is.h(h) && is.angle(angle))) {
566
+ return false;
567
+ }
568
+ if (!(angle >= -360 && angle <= 360)) {
569
+ return false;
570
+ }
571
+ return true;
572
+ }
573
+ function box(desc) {
574
+ if (desc === void 0) { desc = {}; }
575
+ var borderColor = desc.borderColor, borderRadius = desc.borderRadius, borderWidth = desc.borderWidth;
576
+ if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
577
+ return false;
578
+ }
579
+ if (desc.hasOwnProperty('borderRadius') && !is.number(borderRadius)) {
580
+ return false;
581
+ }
582
+ if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
583
+ return false;
584
+ }
585
+ return true;
586
+ }
587
+ function rectDesc(desc) {
588
+ var bgColor = desc.bgColor;
589
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
590
+ return false;
591
+ }
592
+ if (!box(desc)) {
593
+ return false;
594
+ }
595
+ return true;
596
+ }
597
+ function circleDesc(desc) {
598
+ var bgColor = desc.bgColor, borderColor = desc.borderColor, borderWidth = desc.borderWidth;
599
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
600
+ return false;
601
+ }
602
+ if (desc.hasOwnProperty('borderColor') && !is.color(borderColor)) {
603
+ return false;
604
+ }
605
+ if (desc.hasOwnProperty('borderWidth') && !is.number(borderWidth)) {
606
+ return false;
607
+ }
608
+ return true;
609
+ }
610
+ function imageDesc(desc) {
611
+ var src = desc.src;
612
+ if (!is.imageSrc(src)) {
613
+ return false;
614
+ }
615
+ return true;
616
+ }
617
+ function svgDesc(desc) {
618
+ var svg = desc.svg;
619
+ if (!is.svg(svg)) {
620
+ return false;
621
+ }
622
+ return true;
623
+ }
624
+ function htmlDesc(desc) {
625
+ var html = desc.html;
626
+ if (!is.html(html)) {
627
+ return false;
628
+ }
629
+ return true;
630
+ }
631
+ function textDesc(desc) {
632
+ var text = desc.text, color = desc.color, fontSize = desc.fontSize, lineHeight = desc.lineHeight, fontFamily = desc.fontFamily, textAlign = desc.textAlign, fontWeight = desc.fontWeight, bgColor = desc.bgColor, strokeWidth = desc.strokeWidth, strokeColor = desc.strokeColor;
633
+ if (!is.text(text)) {
634
+ return false;
635
+ }
636
+ if (!is.color(color)) {
637
+ return false;
638
+ }
639
+ if (!is.fontSize(fontSize)) {
640
+ return false;
641
+ }
642
+ if (desc.hasOwnProperty('bgColor') && !is.color(bgColor)) {
643
+ return false;
644
+ }
645
+ if (desc.hasOwnProperty('fontWeight') && !is.fontWeight(fontWeight)) {
646
+ return false;
647
+ }
648
+ if (desc.hasOwnProperty('lineHeight') && !is.lineHeight(lineHeight)) {
649
+ return false;
650
+ }
651
+ if (desc.hasOwnProperty('fontFamily') && !is.fontFamily(fontFamily)) {
652
+ return false;
653
+ }
654
+ if (desc.hasOwnProperty('textAlign') && !is.textAlign(textAlign)) {
655
+ return false;
656
+ }
657
+ if (desc.hasOwnProperty('strokeWidth') && !is.strokeWidth(strokeWidth)) {
658
+ return false;
659
+ }
660
+ if (desc.hasOwnProperty('strokeColor') && !is.color(strokeColor)) {
661
+ return false;
662
+ }
663
+ if (!box(desc)) {
664
+ return false;
665
+ }
666
+ return true;
667
+ }
668
+ var check = {
669
+ attrs: attrs,
670
+ textDesc: textDesc,
671
+ rectDesc: rectDesc,
672
+ circleDesc: circleDesc,
673
+ imageDesc: imageDesc,
674
+ svgDesc: svgDesc,
675
+ htmlDesc: htmlDesc,
676
+ };
677
+
678
+ var default_1 = /*#__PURE__*/Object.freeze({
679
+ __proto__: null,
680
+ is: is,
681
+ check: check,
682
+ delay: delay,
683
+ compose: compose,
684
+ throttle: throttle,
685
+ loadImage: loadImage,
686
+ loadSVG: loadSVG,
687
+ loadHTML: loadHTML,
688
+ downloadImageFromCanvas: downloadImageFromCanvas,
689
+ toColorHexStr: toColorHexStr,
690
+ toColorHexNum: toColorHexNum,
691
+ isColorStr: isColorStr,
692
+ createUUID: createUUID,
693
+ istype: istype,
694
+ deepClone: deepClone,
695
+ Context: Context
696
+ });
697
+
698
+ module.exports = default_1;