@inweb/markup 25.7.10 → 25.8.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.
@@ -46,31 +46,37 @@ const MarkupMode2Konva = {
46
46
  },
47
47
  Line: {
48
48
  name: "Line",
49
- initializer: (ref: any) => new KonvaLine(null, ref),
49
+ initializer: (ref: any, params = null) => new KonvaLine(params, ref),
50
+ zIndex: 1,
50
51
  },
51
52
  Text: {
52
53
  name: "Text",
53
- initializer: (ref: any) => new KonvaText(null, ref),
54
+ initializer: (ref: any, params = null) => new KonvaText(params, ref),
54
55
  },
55
56
  Rectangle: {
56
57
  name: "Rect",
57
- initializer: (ref: any) => new KonvaRectangle(null, ref),
58
+ initializer: (ref: any, params = null) => new KonvaRectangle(params, ref),
59
+ zIndex: 1,
58
60
  },
59
61
  Ellipse: {
60
62
  name: "Ellipse",
61
- initializer: (ref: any) => new KonvaEllipse(null, ref),
63
+ initializer: (ref: any, params = null) => new KonvaEllipse(params, ref),
64
+ zIndex: 1,
62
65
  },
63
66
  Arrow: {
64
67
  name: "Arrow",
65
- initializer: (ref: any) => new KonvaArrow(null, ref),
68
+ initializer: (ref: any, params = null) => new KonvaArrow(params, ref),
69
+ zIndex: 1,
66
70
  },
67
71
  Image: {
68
72
  name: "Image",
69
- initializer: (ref: any) => new KonvaImage(null, ref),
73
+ initializer: (ref: any, params = null) => new KonvaImage(params, ref),
74
+ zIndex: 0,
70
75
  },
71
76
  Cloud: {
72
77
  name: "Cloud",
73
78
  initializer: (ref: any) => new KonvaCloud(null, ref),
79
+ zIndex: 1,
74
80
  },
75
81
  };
76
82
 
@@ -202,10 +208,7 @@ export class KonvaMarkup implements IMarkup {
202
208
  pan = (event: PanEvent) => {
203
209
  const dX = event.dX / window.devicePixelRatio;
204
210
  const dY = event.dY / window.devicePixelRatio;
205
-
206
- Object.keys(MarkupMode2Konva).forEach((mode) =>
207
- this.konvaLayerFind(mode).forEach((ref) => ref.move({ x: dX, y: dY }))
208
- );
211
+ this.getObjects().forEach((obj) => obj.ref().move({ x: dX, y: dY }));
209
212
  };
210
213
 
211
214
  redirectToViewer = (event: any) => {
@@ -217,8 +220,8 @@ export class KonvaMarkup implements IMarkup {
217
220
  clearOverlay(): void {
218
221
  this.removeTextInput();
219
222
  this.removeImageInput();
220
- this._konvaTransformer.nodes([]);
221
- Object.keys(MarkupMode2Konva).forEach((mode) => this.konvaLayerFind(mode).forEach((ref) => ref.destroy()));
223
+ this.clearSelected();
224
+ this.getObjects().forEach((obj) => obj.ref().destroy());
222
225
  }
223
226
 
224
227
  getMarkupColor(): { r: number; g: number; b: number } {
@@ -229,25 +232,21 @@ export class KonvaMarkup implements IMarkup {
229
232
  this._markupColor.setColor(r, g, b);
230
233
  }
231
234
 
232
- colorizeAllMarkup(r = 255, g = 0, b = 0): void {
233
- const hex = new MarkupColor(r, g, b).HexColor;
234
- Object.keys(MarkupMode2Konva).forEach((mode) => {
235
- this.konvaLayerFind(mode).forEach((ref) => {
236
- const konvaShape = MarkupMode2Konva[mode];
237
- const konvaObj = konvaShape.initializer(ref);
238
- if (konvaObj.setColor) konvaObj.setColor(hex);
239
- });
235
+ colorizeAllMarkup(r: number, g: number, b: number): void {
236
+ const hexColor = new MarkupColor(r, g, b).HexColor;
237
+ this.getObjects().forEach((obj) => {
238
+ const colorable = obj as unknown as IMarkupColorable;
239
+ if (colorable && colorable.setColor) colorable.setColor(hexColor);
240
240
  });
241
241
 
242
242
  this._konvaLayer.draw();
243
243
  }
244
244
 
245
245
  colorizeSelectedMarkups(r: number, g: number, b: number): void {
246
+ const hexColor = new MarkupColor(r, g, b).HexColor;
246
247
  this.getSelectedObjects().forEach((obj) => {
247
248
  const colorable = obj as unknown as IMarkupColorable;
248
- if (colorable && colorable.setColor) {
249
- colorable.setColor(new MarkupColor(r, g, b).HexColor);
250
- }
249
+ if (colorable && colorable.setColor) colorable.setColor(hexColor);
251
250
  });
252
251
  }
253
252
 
@@ -295,115 +294,39 @@ export class KonvaMarkup implements IMarkup {
295
294
  }
296
295
 
297
296
  createObject(type: string, params: any): IMarkupObject {
298
- let object = null;
299
- let zIndex = this._zIndex;
300
-
301
- // TODO: factory?
302
- switch (type.toLocaleLowerCase()) {
303
- case "line":
304
- object = new KonvaLine(params);
305
- zIndex = 1;
306
- break;
307
- case "text":
308
- object = new KonvaText(params);
309
- break;
310
- case "rectangle":
311
- object = new KonvaRectangle(params);
312
- zIndex = 1;
313
- break;
314
- case "ellipse":
315
- object = new KonvaEllipse(params);
316
- zIndex = 1;
317
- break;
318
- case "arrow":
319
- object = new KonvaArrow(params);
320
- break;
321
- case "image":
322
- object = new KonvaImage(params);
323
- zIndex = 0;
324
- break;
325
- case "cloud":
326
- object = new KonvaCloud(params);
327
- zIndex = 1;
328
- break;
329
- default:
330
- throw new Error("Markup CreateObject - unsupported type has been detected.");
331
- }
297
+ const konvaShape = MarkupMode2Konva[type];
298
+ if (!konvaShape || !konvaShape.initializer)
299
+ throw new Error(`Markup CreateObject - unsupported markup type ${type}`);
332
300
 
301
+ const object = konvaShape.initializer(null, params);
333
302
  this.addObject(object);
334
303
 
335
- // Set zIndex only when shape has been added to Layer else we will get "Konva warning: Node has no parent. zIndex parameter is ignored."
336
- object.setZIndex(zIndex);
304
+ // Set zIndex only when shape has been added to Layer else we will get
305
+ // "Konva warning: Node has no parent. zIndex parameter is ignored."
306
+ object.setZIndex(konvaShape.zIndex ?? this._zIndex);
337
307
  this._zIndex++;
308
+
338
309
  return object;
339
310
  }
340
311
 
341
312
  getObjects(): IMarkupObject[] {
342
313
  const objects = [];
343
- this.konvaLayerFind("Line").forEach((line) => {
344
- objects.push(new KonvaLine(null, line));
345
- });
346
-
347
- this.konvaLayerFind("Text").forEach((text) => {
348
- objects.push(new KonvaText(null, text));
349
- });
350
-
351
- this.konvaLayerFind("Rectangle").forEach((rectangle) => {
352
- objects.push(new KonvaRectangle(null, rectangle));
353
- });
354
-
355
- this.konvaLayerFind("Ellipse").forEach((ellipse) => {
356
- objects.push(new KonvaEllipse(null, ellipse));
314
+ Object.keys(MarkupMode2Konva).forEach((type) => {
315
+ const konvaShape = MarkupMode2Konva[type];
316
+ this.konvaLayerFind(type).forEach((ref) => objects.push(konvaShape.initializer(ref)));
357
317
  });
358
-
359
- this.konvaLayerFind("Arrow").forEach((arrow) => {
360
- objects.push(new KonvaArrow(null, arrow));
361
- });
362
-
363
- this.konvaLayerFind("Image").forEach((image) => {
364
- objects.push(new KonvaImage(null, image));
365
- });
366
-
367
- this.konvaLayerFind("Cloud").forEach((cloud) => {
368
- objects.push(new KonvaCloud(null, cloud));
369
- });
370
-
371
318
  return objects;
372
319
  }
373
320
 
374
321
  getSelectedObjects(): IMarkupObject[] {
375
- const objects = [];
376
-
377
- this._konvaTransformer.nodes().forEach((obj) => {
378
- const konvaShapeName = obj.className;
379
- switch (konvaShapeName) {
380
- case "Line":
381
- objects.push(new KonvaLine(null, obj));
382
- break;
383
- case "Text":
384
- objects.push(new KonvaText(null, obj));
385
- break;
386
- case "Rect":
387
- objects.push(new KonvaRectangle(null, obj));
388
- break;
389
- case "Ellipse":
390
- objects.push(new KonvaEllipse(null, obj));
391
- break;
392
- case "Arrow":
393
- objects.push(new KonvaArrow(null, obj));
394
- break;
395
- case "Image":
396
- objects.push(new KonvaImage(null, obj));
397
- break;
398
- case "Cloud":
399
- objects.push(new KonvaCloud(null, obj));
400
- break;
401
- default:
402
- break;
403
- }
404
- });
405
-
406
- return objects;
322
+ return this._konvaTransformer
323
+ .nodes()
324
+ .map((ref) => {
325
+ const name = ref.className;
326
+ const konvaShape = Object.values(MarkupMode2Konva).find((shape) => shape.name === name);
327
+ return konvaShape ? konvaShape.initializer(ref) : null;
328
+ })
329
+ .filter((x) => x);
407
330
  }
408
331
 
409
332
  selectObjects(objects: IMarkupObject[]) {
@@ -474,8 +397,8 @@ export class KonvaMarkup implements IMarkup {
474
397
  this._konvaLayer.add(object.ref());
475
398
  }
476
399
 
477
- private konvaLayerFind(markupShape: string): any {
478
- const konvaShape = MarkupMode2Konva[markupShape];
400
+ private konvaLayerFind(type: string): any {
401
+ const konvaShape = MarkupMode2Konva[type];
479
402
  if (konvaShape && konvaShape.initializer) {
480
403
  // for "draggable" Konva uses Rectangles in Transformer. We need only Shapes from layer.
481
404
  return this._konvaLayer.find(konvaShape.name).filter((ref) => ref.parent === this._konvaLayer);