|
1 | 1 | import {
|
2 | 2 | ApproximationOptions,
|
3 | 3 | BoundingBox2d,
|
4 |
| - Curve2D, |
| 4 | + deserializeCurve2D, |
5 | 5 | make2dCircle,
|
6 | 6 | make2dEllipse,
|
7 | 7 | make2dInerpolatedBSplineCurve,
|
@@ -84,29 +84,6 @@ export class Drawing implements DrawingInterface {
|
84 | 84 | return JSON.stringify(serializeHelper(this.innerShape));
|
85 | 85 | }
|
86 | 86 |
|
87 |
| - static deserializeDrawing(data: string): Drawing { |
88 |
| - function deserializeHelper(json: any): Shape2D { |
89 |
| - if (json["type"] === "CompoundBlueprint") { |
90 |
| - const blueprints = json["blueprints"].map(deserializeHelper); |
91 |
| - return new CompoundBlueprint(blueprints); |
92 |
| - } else if (json["type"] === "Blueprints") { |
93 |
| - const blueprints = json["blueprints"].map(deserializeHelper); |
94 |
| - return new Blueprints(blueprints); |
95 |
| - } else if (json["type"] === "Blueprint") { |
96 |
| - const curves = json["curves"].map((c: string) => |
97 |
| - Curve2D.deserializeCurve(c) |
98 |
| - ); |
99 |
| - return new Blueprint(curves); |
100 |
| - } else { |
101 |
| - throw new Error("Unknown shape type for deserialization"); |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - const json = JSON.parse(data); |
106 |
| - const shape = deserializeHelper(json); |
107 |
| - return new Drawing(shape); |
108 |
| - } |
109 |
| - |
110 | 87 | get boundingBox(): BoundingBox2d {
|
111 | 88 | if (!this.innerShape) return new BoundingBox2d();
|
112 | 89 | return this.innerShape.boundingBox;
|
@@ -304,6 +281,31 @@ export class DrawingPen
|
304 | 281 | }
|
305 | 282 | }
|
306 | 283 |
|
| 284 | +/** |
| 285 | + * Deserializes a drawing from a string. String is expected to be in the format |
| 286 | + * generated by `Drawing.serialize()`. |
| 287 | + */ |
| 288 | +export function deserializeDrawing(data: string): Drawing { |
| 289 | + function deserializeHelper(json: any): Shape2D { |
| 290 | + if (json["type"] === "CompoundBlueprint") { |
| 291 | + const blueprints = json["blueprints"].map(deserializeHelper); |
| 292 | + return new CompoundBlueprint(blueprints); |
| 293 | + } else if (json["type"] === "Blueprints") { |
| 294 | + const blueprints = json["blueprints"].map(deserializeHelper); |
| 295 | + return new Blueprints(blueprints); |
| 296 | + } else if (json["type"] === "Blueprint") { |
| 297 | + const curves = json["curves"].map((c: string) => deserializeCurve2D(c)); |
| 298 | + return new Blueprint(curves); |
| 299 | + } else { |
| 300 | + throw new Error("Unknown shape type for deserialization"); |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + const json = JSON.parse(data); |
| 305 | + const shape = deserializeHelper(json); |
| 306 | + return new Drawing(shape); |
| 307 | +} |
| 308 | + |
307 | 309 | /**
|
308 | 310 | * Creates a drawing pen to programatically draw in 2D.
|
309 | 311 | *
|
|
0 commit comments