plotboilerplate 1.19.0 → 1.20.1

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.
Files changed (94) hide show
  1. package/README.md +2 -1
  2. package/dist/index.esm.js +81 -27
  3. package/dist/index.esm.js.map +1 -1
  4. package/dist/index.esm.min.js +1 -1
  5. package/dist/index.esm.min.js.map +1 -1
  6. package/dist/plotboilerplate.browser.min.js +1 -1
  7. package/dist/plotboilerplate.browser.min.js.map +1 -1
  8. package/dist/plotboilerplate.js +81 -39
  9. package/dist/plotboilerplate.min.js +1 -1
  10. package/package.json +1 -1
  11. package/src/cjs/Bounds.d.ts +4 -5
  12. package/src/cjs/Bounds.js +6 -8
  13. package/src/cjs/Bounds.js.map +1 -1
  14. package/src/cjs/PlotBoilerplate.d.ts +2 -1
  15. package/src/cjs/PlotBoilerplate.js +9 -3
  16. package/src/cjs/PlotBoilerplate.js.map +1 -1
  17. package/src/cjs/VEllipseSector.d.ts +4 -3
  18. package/src/cjs/VEllipseSector.js +15 -8
  19. package/src/cjs/VEllipseSector.js.map +1 -1
  20. package/src/cjs/Vertex.d.ts +33 -1
  21. package/src/cjs/Vertex.js +43 -1
  22. package/src/cjs/Vertex.js.map +1 -1
  23. package/src/cjs/drawutilssvg.d.ts +2 -1
  24. package/src/cjs/drawutilssvg.js +7 -19
  25. package/src/cjs/drawutilssvg.js.map +1 -1
  26. package/src/cjs/entry.js +1 -0
  27. package/src/cjs/interfaces/core.d.ts +3 -0
  28. package/src/cjs/interfaces/core.js +1 -0
  29. package/src/cjs/interfaces/core.js.map +1 -1
  30. package/src/cjs/utils/algorithms/detectPaths.d.ts +3 -2
  31. package/src/cjs/utils/creategui.js +7 -0
  32. package/src/cjs/utils/datastructures/Color.d.ts +1 -1
  33. package/src/cjs/utils/datastructures/Color.js +4 -2
  34. package/src/cjs/utils/datastructures/Color.js.map +1 -1
  35. package/src/cjs/utils/io/FileDrop.d.ts +11 -1
  36. package/src/cjs/utils/io/FileDrop.js +41 -10
  37. package/src/cjs/utils/io/FileDrop.js.map +1 -1
  38. package/src/cjs/utils/parsers/svg/parseSVGPathData.d.ts +27 -0
  39. package/src/cjs/utils/parsers/svg/parseSVGPathData.js +486 -0
  40. package/src/cjs/utils/parsers/svg/parseSVGPathData.js.map +1 -0
  41. package/src/cjs/utils/parsers/svg/splitSVGPathData.d.ts +20 -0
  42. package/src/cjs/utils/parsers/svg/splitSVGPathData.js +53 -0
  43. package/src/cjs/utils/parsers/svg/splitSVGPathData.js.map +1 -0
  44. package/src/cjs/utils/parsers/svg/types.d.ts +63 -0
  45. package/src/cjs/utils/parsers/svg/types.js +17 -0
  46. package/src/cjs/utils/parsers/svg/types.js.map +1 -0
  47. package/src/esm/Bounds.d.ts +4 -5
  48. package/src/esm/Bounds.js +6 -8
  49. package/src/esm/Bounds.js.map +1 -1
  50. package/src/esm/PlotBoilerplate.d.ts +2 -1
  51. package/src/esm/PlotBoilerplate.js +10 -4
  52. package/src/esm/PlotBoilerplate.js.map +1 -1
  53. package/src/esm/VEllipseSector.d.ts +4 -3
  54. package/src/esm/VEllipseSector.js +15 -8
  55. package/src/esm/VEllipseSector.js.map +1 -1
  56. package/src/esm/Vertex.d.ts +33 -1
  57. package/src/esm/Vertex.js +43 -1
  58. package/src/esm/Vertex.js.map +1 -1
  59. package/src/esm/drawutilssvg.d.ts +2 -1
  60. package/src/esm/drawutilssvg.js +7 -19
  61. package/src/esm/drawutilssvg.js.map +1 -1
  62. package/src/esm/entry.js +1 -0
  63. package/src/esm/interfaces/core.d.ts +3 -0
  64. package/src/esm/interfaces/core.js +1 -0
  65. package/src/esm/interfaces/core.js.map +1 -1
  66. package/src/esm/utils/algorithms/detectPaths.d.ts +3 -2
  67. package/src/esm/utils/algorithms/detectPaths.js.map +1 -1
  68. package/src/esm/utils/datastructures/Color.d.ts +1 -1
  69. package/src/esm/utils/datastructures/Color.js +4 -2
  70. package/src/esm/utils/datastructures/Color.js.map +1 -1
  71. package/src/esm/utils/io/FileDrop.d.ts +11 -1
  72. package/src/esm/utils/io/FileDrop.js +41 -10
  73. package/src/esm/utils/io/FileDrop.js.map +1 -1
  74. package/src/esm/utils/parsers/svg/parseSVGPathData.d.ts +27 -0
  75. package/src/esm/utils/parsers/svg/parseSVGPathData.js +482 -0
  76. package/src/esm/utils/parsers/svg/parseSVGPathData.js.map +1 -0
  77. package/src/esm/utils/parsers/svg/splitSVGPathData.d.ts +20 -0
  78. package/src/esm/utils/parsers/svg/splitSVGPathData.js +49 -0
  79. package/src/esm/utils/parsers/svg/splitSVGPathData.js.map +1 -0
  80. package/src/esm/utils/parsers/svg/types.d.ts +63 -0
  81. package/src/esm/utils/parsers/svg/types.js +16 -0
  82. package/src/esm/utils/parsers/svg/types.js.map +1 -0
  83. package/src/ts/Bounds.ts +6 -8
  84. package/src/ts/PlotBoilerplate.ts +11 -4
  85. package/src/ts/VEllipseSector.ts +22 -15
  86. package/src/ts/Vertex.ts +46 -1
  87. package/src/ts/drawutilssvg.ts +8 -19
  88. package/src/ts/interfaces/core.ts +3 -0
  89. package/src/ts/utils/algorithms/detectPaths.ts +1 -1
  90. package/src/ts/utils/datastructures/Color.ts +8 -4
  91. package/src/ts/utils/io/FileDrop.ts +51 -10
  92. package/src/ts/utils/parsers/svg/parseSVGPathData.ts +631 -0
  93. package/src/ts/utils/parsers/svg/splitSVGPathData.ts +54 -0
  94. package/src/ts/utils/parsers/svg/types.ts +90 -0
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Defining some basic SVG command types.
3
+ *
4
+ * As these types do not define all very possible path data strings they should cover the most daily use-cases.
5
+ *
6
+ * For more insight see
7
+ * https://www.w3.org/TR/SVG/paths.html
8
+ *
9
+ *
10
+ * @author Ikaros Kappler
11
+ * @date 2022-12-21 (winter solstice night – yes the days & nights are dark and I have nothing else to do)
12
+ * @modified 2023-01-17 Added multiple parameter sets.
13
+ * @version 0.0.2-alpha
14
+ */
15
+ export {};
16
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../ts/utils/parsers/svg/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG"}
package/src/ts/Bounds.ts CHANGED
@@ -7,7 +7,8 @@
7
7
  * @modified 2021-06-21 (mid-summer) Added `getCenter` method.
8
8
  * @modified 2022-02-01 Added the `toString` function.
9
9
  * @modified 2022-10-09 Added the `fromDimension` function.
10
- * @version 1.5.0
10
+ * @modified 2022-11-28 Added the `clone` method.
11
+ * @version 1.6.0
11
12
  **/
12
13
 
13
14
  import { Polygon } from "./Polygon";
@@ -114,17 +115,14 @@ export class Bounds implements IBounds, XYDimension {
114
115
  /**
115
116
  * Clone this bounds object (create a deep clone).
116
117
  *
117
- * Note: the returned format might change in the future, so please do not
118
- * rely on the returned string format.
119
- *
120
118
  * @method clone
121
119
  * @instance
122
120
  * @memberof Bounds
123
- * @returns {string} Creates a deep clone of this bounds object. The returned object's `min` and `max` instances are `Vertex` instances.
121
+ * @returns {Bounds} Creates a deep clone of this bounds object.
124
122
  */
125
- // clone() {
126
- // return new Bounds(new Vertex(this.min.x, this.min.y), new Vertex(this.max.x, this.max.y));
127
- // }
123
+ clone() {
124
+ return new Bounds({ x: this.min.x, y: this.min.y }, { x: this.max.x, y: this.max.y });
125
+ }
128
126
 
129
127
  /**
130
128
  * Compute the minimal bounding box for a given set of vertices.
@@ -77,7 +77,8 @@
77
77
  * @modified 2022-08-01 Added `title` to the params.
78
78
  * @modified 2022-10-25 Added the `origin` to the default draw config.
79
79
  * @modified 2022-11-06 Adding an XML declaration to the SVG export routine.
80
- * @version 1.16.0
80
+ * @modified 2022-11-23 Added the `drawRaster` (default=true) option to the config/drawconfig.
81
+ * @version 1.17.0
81
82
  *
82
83
  * @file PlotBoilerplate
83
84
  * @fileoverview The main class.
@@ -413,6 +414,7 @@ export class PlotBoilerplate {
413
414
  offsetX: f.num(config, "offsetX", 0.0),
414
415
  offsetY: f.num(config, "offsetY", 0.0),
415
416
  rasterGrid: f.bool(config, "rasterGrid", true),
417
+ drawRaster: f.bool(config, "drawRaster", true),
416
418
  rasterScaleX: f.num(config, "rasterScaleX", 1.0),
417
419
  rasterScaleY: f.num(config, "rasterScaleY", 1.0),
418
420
  rasterAdjustFactor: f.num(config, "rasterAdjustdFactror", 2.0),
@@ -467,6 +469,7 @@ export class PlotBoilerplate {
467
469
  drawHandleLines: f.bool(config, "drawHandleLines", true),
468
470
  drawHandlePoints: f.bool(config, "drawHandlePoints", true),
469
471
  drawGrid: f.bool(config, "drawGrid", true),
472
+ drawRaster: f.bool(config, "drawRaster", true),
470
473
  bezier: {
471
474
  color: "#00a822",
472
475
  lineWidth: 2,
@@ -1021,7 +1024,7 @@ export class PlotBoilerplate {
1021
1024
  * @return The vertex near the given position or undefined if none was found there.
1022
1025
  **/
1023
1026
  getVertexNear(pixelPosition: XYCoords, pixelTolerance: number): Vertex | undefined {
1024
- var p: IDraggable | undefined = this.locatePointNear(
1027
+ const p: IDraggable | null = this.locatePointNear(
1025
1028
  this.transformMousePosition(pixelPosition.x, pixelPosition.y),
1026
1029
  pixelTolerance / Math.min(this.config.cssScaleX ?? 1.0, this.config.cssScaleY ?? 1.0)
1027
1030
  );
@@ -1460,8 +1463,12 @@ export class PlotBoilerplate {
1460
1463
  * @return {void}
1461
1464
  **/
1462
1465
  drawAll(renderTime: number, draw: DrawLib<any>, fill: DrawLib<any>) {
1463
- this.drawGrid(draw);
1464
- if (this.config.drawOrigin) this.drawOrigin(draw);
1466
+ if (this.config.drawRaster) {
1467
+ this.drawGrid(draw);
1468
+ }
1469
+ if (this.config.drawOrigin) {
1470
+ this.drawOrigin(draw);
1471
+ }
1465
1472
  this.drawDrawables(renderTime, draw, fill);
1466
1473
  this.drawVertices(renderTime, draw);
1467
1474
  this.drawSelectPolygon(draw);
@@ -2,10 +2,11 @@
2
2
  * Implementation of elliptic sectors.
3
3
  * Note that sectors are constructed in clockwise direction.
4
4
  *
5
- * @author Ikaros Kappler
6
- * @date 2021-02-26
5
+ * @author Ikaros Kappler
6
+ * @date 2021-02-26
7
7
  * @modified 2022-02-02 Added the `destroy` method.
8
- * @version 1.1.0
8
+ * @modified 2022-11-01 Tweaked the `endpointToCenterParameters` function to handle negative values, too, without errors.
9
+ * @version 1.1.1
9
10
  */
10
11
 
11
12
  import { CubicBezierCurve } from "./CubicBezierCurve";
@@ -139,19 +140,23 @@ export class VEllipseSector {
139
140
  curves.push(curve);
140
141
  } else {
141
142
  // Find intersection
142
- let intersection: Vertex | undefined = startTangent.intersection(endTangent);
143
+ let intersection: Vertex | null = startTangent.intersection(endTangent);
143
144
  // What if intersection is undefined?
144
145
  // --> This *can* not happen if segmentCount > 2 and height and width of the ellipse are not zero.
145
146
 
146
- let startDiff: Vertex = startPoint.difference(intersection);
147
- let endDiff: Vertex = endPoint.difference(intersection);
148
- let curve: CubicBezierCurve = new CubicBezierCurve(
149
- startPoint.clone(),
150
- endPoint.clone(),
151
- startPoint.clone().add(startDiff.scale(threshold)),
152
- endPoint.clone().add(endDiff.scale(threshold))
153
- );
154
- curves.push(curve);
147
+ if (intersection) {
148
+ // It's VERY LIKELY hat this ALWAYS happens; it's just a typesave variant.
149
+ // Intersection cannot be null.
150
+ let startDiff: Vertex = startPoint.difference(intersection);
151
+ let endDiff: Vertex = endPoint.difference(intersection);
152
+ let curve: CubicBezierCurve = new CubicBezierCurve(
153
+ startPoint.clone(),
154
+ endPoint.clone(),
155
+ startPoint.clone().add(startDiff.scale(threshold)),
156
+ endPoint.clone().add(endDiff.scale(threshold))
157
+ );
158
+ curves.push(curve);
159
+ }
155
160
  }
156
161
  startPoint = endPoint;
157
162
  curAngle = nextAngle;
@@ -268,7 +273,7 @@ export class VEllipseSector {
268
273
 
269
274
  // Bring all angles into the correct order
270
275
  // Idea: use splice or slice here?
271
- var angles = [];
276
+ var angles: Array<number> = [];
272
277
  for (var i = 0; i < ellipseAngles.length; i++) {
273
278
  angles.push(ellipseAngles[(startIndex + i) % ellipseAngles.length]);
274
279
  }
@@ -355,7 +360,8 @@ export class VEllipseSector {
355
360
 
356
361
  // Step 2 + 3: compute center
357
362
  const sign: number = fa === fs ? -1 : 1;
358
- const M: number = sqrt((prx * pry - prx * py - pry * px) / (prx * py + pry * px)) * sign;
363
+ // const M: number = sqrt((prx * pry - prx * py - pry * px) / (prx * py + pry * px)) * sign;
364
+ const M: number = sqrt(Math.abs((prx * pry - prx * py - pry * px) / (prx * py + pry * px))) * sign;
359
365
 
360
366
  const _cx: number = (M * (rx * y)) / ry;
361
367
  const _cy: number = (M * (-ry * x)) / rx;
@@ -367,6 +373,7 @@ export class VEllipseSector {
367
373
  const center: Vertex = new Vertex(cx, cy);
368
374
  const axis: Vertex = center.clone().addXY(rx, ry);
369
375
  const ellipse: VEllipse = new VEllipse(center, axis, 0);
376
+ // console.log("VELLIPSE::::::", ellipse);
370
377
  ellipse.rotate(phi);
371
378
 
372
379
  const startAngle: number = new Line(ellipse.center, new Vertex(x1, y1)).angle();
package/src/ts/Vertex.ts CHANGED
@@ -28,7 +28,8 @@
28
28
  * @modified 2022-01-31 Added `Vertex.utils.arrayToJSON`.
29
29
  * @modified 2022-02-02 Added the `destroy` method.
30
30
  * @modified 2022-02-02 Cleared the `Vertex.toSVGString` function (deprecated). Use `drawutilssvg` instead.
31
- * @version 2.7.0
31
+ * @modified 2022-11-28 Added the `subXY`, `subX` and `subY` methods to the `Vertex` class.
32
+ * @version 2.8.0
32
33
  *
33
34
  * @file Vertex
34
35
  * @public
@@ -342,6 +343,50 @@ export class Vertex implements XYCoords, SVGSerializable {
342
343
  return this;
343
344
  }
344
345
 
346
+ /**
347
+ * Substract the passed amounts from the x- and y- components of this vertex.
348
+ *
349
+ * @method subXY
350
+ * @param {number} x - The amount to substract from x.
351
+ * @param {number} y - The amount to substract from y.
352
+ * @return {Vertex} this
353
+ * @instance
354
+ * @memberof Vertex
355
+ **/
356
+ subXY(amountX: number, amountY: number): Vertex {
357
+ this.x -= amountX;
358
+ this.y -= amountY;
359
+ return this;
360
+ }
361
+
362
+ /**
363
+ * Substract the passed amounts from the x-component of this vertex.
364
+ *
365
+ * @method addX
366
+ * @param {number} x - The amount to substract from x.
367
+ * @return {Vertex} this
368
+ * @instance
369
+ * @memberof Vertex
370
+ **/
371
+ subX(amountX: number): Vertex {
372
+ this.x -= amountX;
373
+ return this;
374
+ }
375
+
376
+ /**
377
+ * Substract the passed amounts from the y-component of this vertex.
378
+ *
379
+ * @method subY
380
+ * @param {number} y - The amount to substract from y.
381
+ * @return {Vertex} this
382
+ * @instance
383
+ * @memberof Vertex
384
+ **/
385
+ subY(amountY: number): Vertex {
386
+ this.y -= amountY;
387
+ return this;
388
+ }
389
+
345
390
  /**
346
391
  * Check if this vertex equals the passed one.
347
392
  * <br>
@@ -34,7 +34,8 @@
34
34
  * @modified 2022-03-26 Added the private `nodeDefs` and `bufferedNodeDefs` attributes.
35
35
  * @modified 2022-03-26 Added the `texturedPoly` function to draw textures polygons.
36
36
  * @modified 2022-07-26 Adding `alpha` to the `image(...)` function.
37
- * @version 1.6.1
37
+ * @modified 2022-11-10 Tweaking some type issues.
38
+ * @version 1.6.2
38
39
  **/
39
40
 
40
41
  import { CircleSector } from "./CircleSector";
@@ -259,7 +260,7 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
259
260
  "text": "Text"
260
261
  };
261
262
  // Question: why isn't this working if the svgNode is created dynamically? (nodeStyle.sheet is null)
262
- const rules = [];
263
+ const rules: Array<string> = [];
263
264
  for (var k in keys) {
264
265
  const className: string = keys[k];
265
266
  const drawSettings: DrawSettings | undefined = drawConfig[k];
@@ -1321,10 +1322,10 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1321
1322
  this.curId = curId + "_text";
1322
1323
  const textNode: SVGElement = this.makeNode("text");
1323
1324
  node.appendChild(textNode);
1324
- textNode.setAttribute("font-family", options.fontFamily); // May be undefined
1325
- textNode.setAttribute("font-size", options.fontSize ? `${options.fontSize * this.scale.x}` : null);
1326
- textNode.setAttribute("font-style", options.fontStyle ? `${options.fontStyle}` : null);
1327
- textNode.setAttribute("font-weight", options.fontWeight ? `${options.fontWeight}` : null);
1325
+ textNode.setAttribute("font-family", options.fontFamily ?? ""); // May be undefined
1326
+ textNode.setAttribute("font-size", options.fontSize ? `${options.fontSize * this.scale.x}` : "");
1327
+ textNode.setAttribute("font-style", options.fontStyle ? `${options.fontStyle}` : "");
1328
+ textNode.setAttribute("font-weight", options.fontWeight ? `${options.fontWeight}` : "");
1328
1329
  textNode.setAttribute("text-anchor", textAlign);
1329
1330
  textNode.setAttribute("transform-origin", "0 0");
1330
1331
  textNode.setAttribute("transform", rotate);
@@ -1488,19 +1489,7 @@ export class drawutilssvg implements DrawLib<void | SVGElement> {
1488
1489
  const _sy = (index: number): void => {
1489
1490
  data[index] = scale.y * Number(data[index]);
1490
1491
  };
1491
- const stx = (value: number): number => {
1492
- return offset.x + scale.x * value;
1493
- };
1494
- const sty = (value: number): number => {
1495
- return offset.y + scale.y * value;
1496
- };
1497
- // scale only {x,y}
1498
- const sx = (value: number): number => {
1499
- return scale.x * value;
1500
- };
1501
- const sy = (value: number): number => {
1502
- return scale.y * value;
1503
- };
1492
+
1504
1493
  var i: number = 0;
1505
1494
  var lastPoint: XYCoords = { x: NaN, y: NaN };
1506
1495
  // "save last point"
@@ -16,6 +16,7 @@
16
16
  * @modified 2021-11-16 Added `text` options to the `DrawConfig`.
17
17
  * @modified 2022-08-01 Added `title` param to the `PBParams` interface.
18
18
  * @modified 2022-10-25 Added `origin` param to the `DrawConfig` interface.
19
+ * @modified 2022-11-23 Added `drawRaster` to the `Config` interface.
19
20
  **/
20
21
 
21
22
  import { Vertex } from "../Vertex";
@@ -108,6 +109,7 @@ export interface Config {
108
109
  offsetX?: number; // The initial x-offset. Default is 0.0. Note that autoAdjustOffset=true overrides these values.
109
110
  offsetY?: number; // The initial y-offset. Default is 0.0. Note that autoAdjustOffset=true overrides these values.
110
111
  rasterGrid?: boolean; // If set to true the background grid will be drawn rastered.
112
+ drawRaster?: boolean; // If set to false, no raster or grid will be drawn at all.
111
113
  rasterScaleX?: number; // Define the default horizontal raster scale (default=1.0).
112
114
  rasterScaleY?: number; // Define the default vertical raster scale (default=1.0).
113
115
  rasterAdjustFactor?: number; // The exponential limit for wrapping down the grid. (2.0 means: halve the grid each 2.0*n zoom step).
@@ -176,6 +178,7 @@ export interface DrawConfig {
176
178
  drawHandleLines: boolean;
177
179
  drawHandlePoints: boolean;
178
180
  drawGrid: boolean;
181
+ drawRaster: boolean; // If set to false, no raster or grid will be drawn at all.
179
182
  bezier: {
180
183
  color: string;
181
184
  lineWidth: number;
@@ -177,7 +177,7 @@ const detectAdjacentPath = (
177
177
  * @param {number=1.0} epsilon - (optional) An epsilon to use to tell if two plane points should be considered 'equal'.
178
178
  * @returns {Array<GenericPath>} An array containing all detected path (consisting of adjacent path segments of the original set).
179
179
  */
180
- export const detectPaths = (segments: Array<PathSegment>, epsilon?: number): Array<Path> => {
180
+ export const detectPaths = (segments: Array<PathSegment>, epsilon?: number): Array<GenericPath> => {
181
181
  const eps = typeof epsilon === "undefined" || epsilon < 0 ? 1.0 : epsilon;
182
182
  const isSegmentVisited: Array<Visitation> = initVisitationArray(segments, eps);
183
183
  const resultPaths: Array<GenericPath> = [];
@@ -532,9 +532,13 @@ export class Color {
532
532
  * @param {string} str - The string representation to parse.
533
533
  * @return {Color} The color instance that's represented by the given string.
534
534
  */
535
- static parse(str: string): Color {
536
- if (typeof str == "undefined") return null;
537
- if ((str = str.trim().toLowerCase()).length == 0) return null;
535
+ static parse(str: string): Color | null {
536
+ if (typeof str == "undefined") {
537
+ return null;
538
+ }
539
+ if ((str = str.trim().toLowerCase()).length == 0) {
540
+ return null;
541
+ }
538
542
  if (str.startsWith("#")) return Color.makeHEX(str.substring(1, str.length));
539
543
  if (str.startsWith("rgb")) {
540
544
  var parts = str.match(/^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,?\s*(\d*(?:\.\d+\s*)?)\)$/);
@@ -555,7 +559,7 @@ export class Color {
555
559
 
556
560
  private static Sanitizer = {
557
561
  RGB: function (...args: any[]) {
558
- var o = [];
562
+ var o: number[] = [];
559
563
  if (arguments.length == 0) {
560
564
  return [];
561
565
  }
@@ -13,11 +13,13 @@
13
13
  * @author Ikaros Kappler
14
14
  * @date 2021-10-13
15
15
  * @modified 2022-01-31 (ported from the ngdg project, then generalized)
16
- * @version 2.0.0
16
+ * @modified 2023-01-03 Fixing some minor type issues and adding SVG reading capabilities.
17
+ * @version 2.1.0
17
18
  */
18
19
 
19
20
  type IDroppedCallbackJSON = (jsonData: object) => void;
20
21
  type IDroppedCallbackText = (textData: string) => void;
22
+ type IDroppedCallbackSVG = (svgDocument: Document) => void;
21
23
  type IDroppedCallbackBinary = (binaryData: Blob, file: File) => void;
22
24
 
23
25
  export class FileDrop {
@@ -53,6 +55,14 @@ export class FileDrop {
53
55
  */
54
56
  private fileDroppedCallbackBinary: IDroppedCallbackBinary | null;
55
57
 
58
+ /**
59
+ * The SVG file drop callback.
60
+ * @private
61
+ * @memberof FileDrop
62
+ * @member {HTMLElement}
63
+ */
64
+ private fileDroppedCallbackSVG: IDroppedCallbackSVG | null;
65
+
56
66
  /**
57
67
  *
58
68
  * @param {HTMLElement} element - The element you wish to operate as the drop zone (like <body/>).
@@ -92,6 +102,10 @@ export class FileDrop {
92
102
  this.fileDroppedCallbackText = callback;
93
103
  }
94
104
 
105
+ onFileSVGDropped(callback: IDroppedCallbackSVG) {
106
+ this.fileDroppedCallbackSVG = callback;
107
+ }
108
+
95
109
  /**
96
110
  * Internally handle a drop event.
97
111
  *
@@ -103,7 +117,7 @@ export class FileDrop {
103
117
  event.stopPropagation();
104
118
  this.element.style.opacity = "1.0";
105
119
 
106
- if (!event.dataTransfer.files || event.dataTransfer.files.length === 0) {
120
+ if (!event.dataTransfer || !event.dataTransfer.files || event.dataTransfer.files.length === 0) {
107
121
  // No files were dropped
108
122
  return;
109
123
  }
@@ -111,36 +125,63 @@ export class FileDrop {
111
125
  // Multiple file drop is not nupported
112
126
  return;
113
127
  }
114
- if (!this.fileDroppedCallbackJSON) {
115
- // No handling callback defined.
116
- return;
117
- }
128
+ // if (!this.fileDroppedCallbackJSON) {
129
+ // // No handling callback defined.
130
+ // return;
131
+ // }
118
132
  if (event.dataTransfer.files[0]) {
119
133
  const file: File = event.dataTransfer.files[0];
120
134
  // console.log("file", file);
121
- if (file.type.match(/json.*/) && this.fileDroppedCallbackJSON) {
135
+ if (file.type.match(/json.*/) && this.fileDroppedCallbackJSON !== null) {
122
136
  var reader = new FileReader();
123
137
  reader.onload = (readEvent: ProgressEvent<FileReader>) => {
138
+ if (!readEvent.target) {
139
+ console.warn("Cannot process JSON ProgressEvent data: target is null.");
140
+ return;
141
+ }
124
142
  // Finished reading file data.
125
143
  const jsonObject: object = JSON.parse(readEvent.target.result as string);
126
144
  // TODO: what happens on fail?
127
- this.fileDroppedCallbackJSON(jsonObject);
145
+ this.fileDroppedCallbackJSON && this.fileDroppedCallbackJSON(jsonObject);
128
146
  };
129
147
  reader.readAsText(file); // start reading the file data.
130
148
  } else if (file.type.match(/text\/plain.*/) && this.fileDroppedCallbackText) {
131
149
  var reader = new FileReader();
132
150
  reader.onload = (readEvent: ProgressEvent<FileReader>) => {
151
+ if (!readEvent.target) {
152
+ console.warn("Cannot process Text ProgressEvent data: target is null.");
153
+ return;
154
+ }
133
155
  // Finished reading file data.
134
- this.fileDroppedCallbackText(readEvent.target.result as string);
156
+ this.fileDroppedCallbackText && this.fileDroppedCallbackText(readEvent.target.result as string);
135
157
  };
136
158
  reader.readAsText(file); // start reading the file data.
137
159
  } else if (this.fileDroppedCallbackBinary) {
138
160
  var reader = new FileReader();
139
161
  reader.onload = (readEvent: ProgressEvent<FileReader>) => {
162
+ if (!readEvent.target) {
163
+ console.warn("Cannot process Binary ProgressEvent data: target is null.");
164
+ return;
165
+ }
140
166
  // Finished reading file data.
141
- this.fileDroppedCallbackBinary(new Blob([readEvent.target.result]), file);
167
+ this.fileDroppedCallbackBinary &&
168
+ this.fileDroppedCallbackBinary(new Blob([readEvent.target.result as ArrayBuffer]), file);
142
169
  };
143
170
  reader.readAsBinaryString(file); // start reading the file data.
171
+ } else if (this.fileDroppedCallbackSVG) {
172
+ var reader = new FileReader();
173
+ reader.onload = (readEvent: ProgressEvent<FileReader>) => {
174
+ if (!readEvent.target) {
175
+ console.warn("Cannot process SVG ProgressEvent data: target is null.");
176
+ return;
177
+ }
178
+ const parser = new DOMParser();
179
+ const doc = parser.parseFromString(readEvent.target.result as string, "image/svg+xml");
180
+
181
+ // Finished reading file data.
182
+ this.fileDroppedCallbackSVG && this.fileDroppedCallbackSVG(doc);
183
+ };
184
+ reader.readAsText(file); // start reading the file data.
144
185
  }
145
186
  }
146
187
  };