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