@nmmty/lazycanvas 0.3.4 → 0.3.5

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.
@@ -7,9 +7,9 @@ export declare class LayersManager implements ILayersManager {
7
7
  constructor(debug?: boolean);
8
8
  /**
9
9
  * Add a layer to the map
10
- * @param layers {AnyLayer[] | Group[]} - The `layer` or `group` to add to the map
10
+ * @param layers {AnyLayer[]} - The `layer` or `group` to add to the map
11
11
  */
12
- add(...layers: AnyLayer[] | Group[]): this;
12
+ add(...layers: AnyLayer[]): this;
13
13
  /**
14
14
  * Remove a layer from the map
15
15
  * @param ids {string[]} - The `id` of the layer or group to remove
@@ -22,8 +22,9 @@ export declare class LayersManager implements ILayersManager {
22
22
  /**
23
23
  * Get a layer from the map
24
24
  * @param id {string} - The `id` of the layer or group to get
25
+ * @param cross {boolean} - Whether to search in groups or not
25
26
  */
26
- get(id: string): AnyLayer | Group | undefined;
27
+ get(id: string, cross?: boolean): AnyLayer | undefined;
27
28
  /**
28
29
  * Check if a layer exists in the map
29
30
  * @param id {string} - The `id` of the layer or group to check
@@ -36,7 +37,7 @@ export declare class LayersManager implements ILayersManager {
36
37
  /**
37
38
  * Get the values of the map
38
39
  */
39
- values(): IterableIterator<AnyLayer | Group>;
40
+ values(): IterableIterator<AnyLayer>;
40
41
  /**
41
42
  * Get the keys of the map
42
43
  */
@@ -44,12 +45,12 @@ export declare class LayersManager implements ILayersManager {
44
45
  /**
45
46
  * Get the entries of the map
46
47
  */
47
- entries(): IterableIterator<[string, AnyLayer | Group]>;
48
+ entries(): IterableIterator<[string, AnyLayer]>;
48
49
  /**
49
50
  * For each layer in the map
50
51
  * @param callbackfn {Function} - The `callback` function to execute
51
52
  */
52
- forEach(callbackfn: (value: AnyLayer | Group, key: string, map: Map<string, AnyLayer | Group>) => void): this;
53
+ forEach(callbackfn: (value: AnyLayer, key: string, map: Map<string, AnyLayer>) => void): this;
53
54
  /**
54
55
  * Convert the map to a JSON object
55
56
  */
@@ -62,11 +63,12 @@ export declare class LayersManager implements ILayersManager {
62
63
  /**
63
64
  * Convert the map to an array
64
65
  */
65
- toArray(): Array<AnyLayer | Group>;
66
+ toArray(): Array<AnyLayer>;
66
67
  /**
67
68
  * Convert an array to the map
68
- * @param array {Array<AnyLayer | Group>} - The `array` to convert
69
+ * @param array {Array<AnyLayer>} - The `array` to convert
69
70
  */
70
- fromArray(array: Array<AnyLayer | Group>): this;
71
+ fromArray(array: Array<AnyLayer>): this;
71
72
  sort(): void;
73
+ private crossSearch;
72
74
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.LayersManager = void 0;
4
+ const Group_1 = require("../components/Group");
4
5
  const LazyUtil_1 = require("../../utils/LazyUtil");
5
6
  class LayersManager {
6
7
  map;
@@ -11,7 +12,7 @@ class LayersManager {
11
12
  }
12
13
  /**
13
14
  * Add a layer to the map
14
- * @param layers {AnyLayer[] | Group[]} - The `layer` or `group` to add to the map
15
+ * @param layers {AnyLayer[]} - The `layer` or `group` to add to the map
15
16
  */
16
17
  add(...layers) {
17
18
  if (this.debug)
@@ -48,9 +49,13 @@ class LayersManager {
48
49
  /**
49
50
  * Get a layer from the map
50
51
  * @param id {string} - The `id` of the layer or group to get
52
+ * @param cross {boolean} - Whether to search in groups or not
51
53
  */
52
- get(id) {
53
- return this.map.get(id);
54
+ get(id, cross = false) {
55
+ if (cross)
56
+ return this.crossSearch(id);
57
+ else
58
+ return this.map.get(id);
54
59
  }
55
60
  /**
56
61
  * Check if a layer exists in the map
@@ -113,7 +118,7 @@ class LayersManager {
113
118
  }
114
119
  /**
115
120
  * Convert an array to the map
116
- * @param array {Array<AnyLayer | Group>} - The `array` to convert
121
+ * @param array {Array<AnyLayer>} - The `array` to convert
117
122
  */
118
123
  fromArray(array) {
119
124
  this.map = new Map(array.map(l => [l.id, l]));
@@ -122,5 +127,17 @@ class LayersManager {
122
127
  sort() {
123
128
  this.fromArray(this.toArray().sort((a, b) => a.zIndex - b.zIndex));
124
129
  }
130
+ crossSearch(id) {
131
+ for (const layer of this.map.values()) {
132
+ if (layer.id === id)
133
+ return layer;
134
+ if (layer instanceof Group_1.Group) {
135
+ const result = layer.components.find(l => l.id === id);
136
+ if (result)
137
+ return result;
138
+ }
139
+ }
140
+ return undefined;
141
+ }
125
142
  }
126
143
  exports.LayersManager = LayersManager;
@@ -35,6 +35,7 @@ const Group_1 = require("../structures/components/Group");
35
35
  const LineLayer_1 = require("../structures/components/LineLayer");
36
36
  const BezierLayer_1 = require("../structures/components/BezierLayer");
37
37
  const QuadraticLayer_1 = require("../structures/components/QuadraticLayer");
38
+ const TextLayer_1 = require("../structures/components/TextLayer");
38
39
  function generateID(type) {
39
40
  return `${type}-${Math.random().toString(36).substr(2, 9)}`;
40
41
  }
@@ -149,7 +150,7 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
149
150
  let match = v.match(linkReg);
150
151
  if (!manager)
151
152
  return 0;
152
- let anyLayer = manager.get(match[2]);
153
+ let anyLayer = manager.get(match[2], true);
153
154
  const parcer = parser(ctx, canvas, manager);
154
155
  switch (match[1]) {
155
156
  case 'link-w':
@@ -157,6 +158,9 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
157
158
  if (anyLayer instanceof LineLayer_1.LineLayer || anyLayer instanceof BezierLayer_1.BezierLayer || anyLayer instanceof QuadraticLayer_1.QuadraticLayer) {
158
159
  return anyLayer.getBoundingBox(ctx, canvas, manager).width + (parseInt(match[3]) || 0);
159
160
  }
161
+ else if (anyLayer instanceof TextLayer_1.TextLayer) {
162
+ return anyLayer.measureText(ctx, canvas).width + (parseInt(match[3]) || 0);
163
+ }
160
164
  else {
161
165
  return (parcer.parse(anyLayer.props.size.width) || 0) + (parseInt(match[3]) || 0);
162
166
  }
@@ -170,6 +174,9 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
170
174
  if (anyLayer instanceof LineLayer_1.LineLayer || anyLayer instanceof BezierLayer_1.BezierLayer || anyLayer instanceof QuadraticLayer_1.QuadraticLayer) {
171
175
  return anyLayer.getBoundingBox(ctx, canvas, manager).height + (parseInt(match[3]) || 0);
172
176
  }
177
+ else if (anyLayer instanceof TextLayer_1.TextLayer) {
178
+ return anyLayer.measureText(ctx, canvas).height + (parseInt(match[3]) || 0);
179
+ }
173
180
  else {
174
181
  return (parcer.parse(anyLayer.props.size.height, LazyUtil_1.defaultArg.wh(parcer.parse(anyLayer.props.size.width)), LazyUtil_1.defaultArg.vl(true)) || 0) + (parseInt(match[3]) || 0);
175
182
  }
@@ -200,7 +207,7 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
200
207
  else if (v instanceof Link_1.Link) {
201
208
  if (!manager)
202
209
  return 0;
203
- let anyLayer = manager.get(v.source);
210
+ let anyLayer = manager.get(v.source, true);
204
211
  const parcer = parser(ctx, canvas, manager);
205
212
  switch (v.type) {
206
213
  case enum_1.LinkType.Width:
@@ -209,6 +216,9 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
209
216
  if (anyLayer instanceof LineLayer_1.LineLayer || anyLayer instanceof BezierLayer_1.BezierLayer || anyLayer instanceof QuadraticLayer_1.QuadraticLayer) {
210
217
  return anyLayer.getBoundingBox(ctx, canvas, manager).width + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
211
218
  }
219
+ else if (anyLayer instanceof TextLayer_1.TextLayer) {
220
+ return anyLayer.measureText(ctx, canvas).width + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
221
+ }
212
222
  else {
213
223
  return (parcer.parse(anyLayer.props.size.width) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
214
224
  }
@@ -223,6 +233,9 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
223
233
  if (anyLayer instanceof LineLayer_1.LineLayer || anyLayer instanceof BezierLayer_1.BezierLayer || anyLayer instanceof QuadraticLayer_1.QuadraticLayer) {
224
234
  return anyLayer.getBoundingBox(ctx, canvas, manager).height + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
225
235
  }
236
+ else if (anyLayer instanceof TextLayer_1.TextLayer) {
237
+ return anyLayer.measureText(ctx, canvas).height + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
238
+ }
226
239
  else {
227
240
  return (parcer.parse(anyLayer.props.size.height, LazyUtil_1.defaultArg.wh(parcer.parse(anyLayer.props.size.width)), LazyUtil_1.defaultArg.vl(true)) || 0) + (parcer.parse(v.additionalSpacing, LazyUtil_1.defaultArg.wh(layer.width, layer.height), LazyUtil_1.defaultArg.vl(options.vertical, options.layer)) || 0);
228
241
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmmty/lazycanvas",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "A simple way to interact with @napi-rs/canvas in an advanced way!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",