squarified 0.1.1 → 0.2.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.
package/dist/index.js CHANGED
@@ -1,1441 +1,1602 @@
1
1
  'use strict';
2
2
 
3
- var _computedKey;
4
- _computedKey = Symbol.iterator;
5
- class Iter {
6
- keys;
7
- data;
8
- constructor(data){
9
- this.data = data;
10
- this.keys = Object.keys(data);
11
- }
12
- // dprint-ignore
13
- *[_computedKey]() {
14
- for(let i = 0; i < this.keys.length; i++){
15
- yield {
16
- key: this.keys[i],
17
- value: this.data[this.keys[i]],
18
- index: i,
19
- peek: ()=>this.keys[i + 1]
20
- };
21
- }
22
- }
23
- }
24
- // For strings we only check the first character to determine if it's a number (I think it's enough)
25
- function perferNumeric(s) {
26
- if (typeof s === 'number') return true;
27
- return s.charCodeAt(0) >= 48 && s.charCodeAt(0) <= 57;
28
- }
29
-
3
+ var __defProp$a = Object.defineProperty;
4
+ var __defNormalProp$a = (obj, key, value) => key in obj ? __defProp$a(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
+ var __publicField$a = (obj, key, value) => __defNormalProp$a(obj, typeof key !== "symbol" ? key + "" : key, value);
30
6
  const DEG_TO_RAD = Math.PI / 180;
31
7
  class Matrix2D {
32
- a;
33
- b;
34
- c;
35
- d;
36
- e;
37
- f;
38
- constructor(loc = {}){
39
- this.a = loc.a || 1;
40
- this.b = loc.b || 0;
41
- this.c = loc.c || 0;
42
- this.d = loc.d || 1;
43
- this.e = loc.e || 0;
44
- this.f = loc.f || 0;
45
- }
46
- create(loc) {
47
- for (const { key, value } of new Iter(loc)){
48
- if (Object.hasOwnProperty.call(this, key)) {
49
- this[key] = value;
50
- }
51
- }
52
- return this;
53
- }
54
- transform(x, y, scaleX, scaleY, rotation, skewX, skewY) {
55
- this.scale(scaleX, scaleY).translation(x, y);
56
- if (skewX || skewY) {
57
- this.skew(skewX, skewY);
58
- } else {
59
- this.roate(rotation);
60
- }
61
- return this;
62
- }
63
- translation(x, y) {
64
- this.e += x;
65
- this.f += y;
66
- return this;
67
- }
68
- scale(a, d) {
69
- this.a *= a;
70
- this.d *= d;
71
- return this;
72
- }
73
- skew(x, y) {
74
- const tanX = Math.tan(x * DEG_TO_RAD);
75
- const tanY = Math.tan(y * DEG_TO_RAD);
76
- const a = this.a + this.b * tanX;
77
- const b = this.b + this.a * tanY;
78
- const c = this.c + this.d * tanX;
79
- const d = this.d + this.c * tanY;
80
- this.a = a;
81
- this.b = b;
82
- this.c = c;
83
- this.d = d;
84
- return this;
85
- }
86
- roate(rotation) {
87
- if (rotation > 0) {
88
- const rad = rotation * DEG_TO_RAD;
89
- const cosTheta = Math.cos(rad);
90
- const sinTheta = Math.sin(rad);
91
- const a = this.a * cosTheta - this.b * sinTheta;
92
- const b = this.a * sinTheta + this.b * cosTheta;
93
- const c = this.c * cosTheta - this.d * sinTheta;
94
- const d = this.c * sinTheta + this.d * cosTheta;
95
- this.a = a;
96
- this.b = b;
97
- this.c = c;
98
- this.d = d;
99
- }
100
- return this;
101
- }
8
+ constructor(loc = {}) {
9
+ __publicField$a(this, "a");
10
+ __publicField$a(this, "b");
11
+ __publicField$a(this, "c");
12
+ __publicField$a(this, "d");
13
+ __publicField$a(this, "e");
14
+ __publicField$a(this, "f");
15
+ this.a = loc.a || 1;
16
+ this.b = loc.b || 0;
17
+ this.c = loc.c || 0;
18
+ this.d = loc.d || 1;
19
+ this.e = loc.e || 0;
20
+ this.f = loc.f || 0;
21
+ }
22
+ create(loc) {
23
+ Object.assign(this, loc);
24
+ return this;
25
+ }
26
+ transform(x, y, scaleX, scaleY, rotation, skewX, skewY) {
27
+ this.scale(scaleX, scaleY).translation(x, y);
28
+ if (skewX || skewY) {
29
+ this.skew(skewX, skewY);
30
+ } else {
31
+ this.roate(rotation);
32
+ }
33
+ return this;
34
+ }
35
+ translation(x, y) {
36
+ this.e += x;
37
+ this.f += y;
38
+ return this;
39
+ }
40
+ scale(a, d) {
41
+ this.a *= a;
42
+ this.d *= d;
43
+ return this;
44
+ }
45
+ skew(x, y) {
46
+ const tanX = Math.tan(x * DEG_TO_RAD);
47
+ const tanY = Math.tan(y * DEG_TO_RAD);
48
+ const a = this.a + this.b * tanX;
49
+ const b = this.b + this.a * tanY;
50
+ const c = this.c + this.d * tanX;
51
+ const d = this.d + this.c * tanY;
52
+ this.a = a;
53
+ this.b = b;
54
+ this.c = c;
55
+ this.d = d;
56
+ return this;
57
+ }
58
+ roate(rotation) {
59
+ if (rotation > 0) {
60
+ const rad = rotation * DEG_TO_RAD;
61
+ const cosTheta = Math.cos(rad);
62
+ const sinTheta = Math.sin(rad);
63
+ const a = this.a * cosTheta - this.b * sinTheta;
64
+ const b = this.a * sinTheta + this.b * cosTheta;
65
+ const c = this.c * cosTheta - this.d * sinTheta;
66
+ const d = this.c * sinTheta + this.d * cosTheta;
67
+ this.a = a;
68
+ this.b = b;
69
+ this.c = c;
70
+ this.d = d;
71
+ }
72
+ return this;
73
+ }
102
74
  }
103
75
 
76
+ var __defProp$9 = Object.defineProperty;
77
+ var __defNormalProp$9 = (obj, key, value) => key in obj ? __defProp$9(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
78
+ var __publicField$9 = (obj, key, value) => __defNormalProp$9(obj, typeof key !== "symbol" ? key + "" : key, value);
104
79
  const SELF_ID = {
105
- id: 0,
106
- get () {
107
- return this.id++;
108
- }
80
+ id: 0,
81
+ get() {
82
+ return this.id++;
83
+ }
109
84
  };
85
+ var DisplayType = /* @__PURE__ */ ((DisplayType2) => {
86
+ DisplayType2["Graph"] = "Graph";
87
+ DisplayType2["Box"] = "Box";
88
+ DisplayType2["Rect"] = "Rect";
89
+ DisplayType2["Text"] = "Text";
90
+ DisplayType2["Layer"] = "Layer";
91
+ return DisplayType2;
92
+ })(DisplayType || {});
110
93
  class Display {
111
- parent;
112
- id;
113
- matrix;
114
- constructor(){
115
- this.parent = null;
116
- this.id = SELF_ID.get();
117
- this.matrix = new Matrix2D();
118
- }
119
- destory() {
120
- //
121
- }
94
+ constructor() {
95
+ __publicField$9(this, "parent");
96
+ __publicField$9(this, "id");
97
+ __publicField$9(this, "matrix");
98
+ this.parent = null;
99
+ this.id = SELF_ID.get();
100
+ this.matrix = new Matrix2D();
101
+ }
102
+ destory() {
103
+ }
122
104
  }
123
105
  const ASSIGN_MAPPINGS = {
124
- fillStyle: !0,
125
- strokeStyle: !0,
126
- font: !0,
127
- lineWidth: !0,
128
- textAlign: !0,
129
- textBaseline: !0
106
+ fillStyle: true,
107
+ strokeStyle: true,
108
+ font: true,
109
+ lineWidth: true,
110
+ textAlign: true,
111
+ textBaseline: true
130
112
  };
131
113
  function createInstruction() {
132
- return {
133
- mods: [],
134
- fillStyle (...args) {
135
- this.mods.push([
136
- 'fillStyle',
137
- args
138
- ]);
139
- },
140
- fillRect (...args) {
141
- this.mods.push([
142
- 'fillRect',
143
- args
144
- ]);
145
- },
146
- strokeStyle (...args) {
147
- this.mods.push([
148
- 'strokeStyle',
149
- args
150
- ]);
151
- },
152
- lineWidth (...args) {
153
- this.mods.push([
154
- 'lineWidth',
155
- args
156
- ]);
157
- },
158
- strokeRect (...args) {
159
- this.mods.push([
160
- 'strokeRect',
161
- args
162
- ]);
163
- },
164
- fillText (...args) {
165
- this.mods.push([
166
- 'fillText',
167
- args
168
- ]);
169
- },
170
- font (...args) {
171
- this.mods.push([
172
- 'font',
173
- args
174
- ]);
175
- },
176
- textBaseline (...args) {
177
- this.mods.push([
178
- 'textBaseline',
179
- args
180
- ]);
181
- },
182
- textAlign (...args) {
183
- this.mods.push([
184
- 'textAlign',
185
- args
186
- ]);
187
- }
188
- };
114
+ return {
115
+ mods: [],
116
+ fillStyle(...args) {
117
+ this.mods.push(["fillStyle", args]);
118
+ },
119
+ fillRect(...args) {
120
+ this.mods.push(["fillRect", args]);
121
+ },
122
+ strokeStyle(...args) {
123
+ this.mods.push(["strokeStyle", args]);
124
+ },
125
+ lineWidth(...args) {
126
+ this.mods.push(["lineWidth", args]);
127
+ },
128
+ strokeRect(...args) {
129
+ this.mods.push(["strokeRect", args]);
130
+ },
131
+ fillText(...args) {
132
+ this.mods.push(["fillText", args]);
133
+ },
134
+ font(...args) {
135
+ this.mods.push(["font", args]);
136
+ },
137
+ textBaseline(...args) {
138
+ this.mods.push(["textBaseline", args]);
139
+ },
140
+ textAlign(...args) {
141
+ this.mods.push(["textAlign", args]);
142
+ }
143
+ };
189
144
  }
190
145
  class S extends Display {
191
- width;
192
- height;
193
- x;
194
- y;
195
- scaleX;
196
- scaleY;
197
- rotation;
198
- skewX;
199
- skewY;
200
- constructor(options = {}){
201
- super();
202
- this.width = options.width || 0;
203
- this.height = options.height || 0;
204
- this.x = options.x || 0;
205
- this.y = options.y || 0;
206
- this.scaleX = options.scaleX || 1;
207
- this.scaleY = options.scaleY || 1;
208
- this.rotation = options.rotation || 0;
209
- this.skewX = options.skewX || 0;
210
- this.skewY = options.skewY || 0;
211
- }
146
+ constructor(options = {}) {
147
+ super();
148
+ __publicField$9(this, "width");
149
+ __publicField$9(this, "height");
150
+ __publicField$9(this, "x");
151
+ __publicField$9(this, "y");
152
+ __publicField$9(this, "scaleX");
153
+ __publicField$9(this, "scaleY");
154
+ __publicField$9(this, "rotation");
155
+ __publicField$9(this, "skewX");
156
+ __publicField$9(this, "skewY");
157
+ this.width = options.width || 0;
158
+ this.height = options.height || 0;
159
+ this.x = options.x || 0;
160
+ this.y = options.y || 0;
161
+ this.scaleX = options.scaleX || 1;
162
+ this.scaleY = options.scaleY || 1;
163
+ this.rotation = options.rotation || 0;
164
+ this.skewX = options.skewX || 0;
165
+ this.skewY = options.skewY || 0;
166
+ }
212
167
  }
213
168
  class Graph extends S {
214
- instruction;
215
- constructor(options = {}){
216
- super(options);
217
- this.instruction = createInstruction();
218
- }
219
- render(ctx) {
220
- this.create();
221
- this.instruction.mods.forEach((mod)=>{
222
- const direct = mod[0];
223
- if (direct in ASSIGN_MAPPINGS) {
224
- // @ts-expect-error
225
- ctx[direct] = mod[1];
226
- return;
227
- }
228
- // @ts-expect-error
229
- ctx[direct].apply(ctx, ...mod.slice(1));
230
- });
231
- }
169
+ constructor(options = {}) {
170
+ super(options);
171
+ __publicField$9(this, "instruction");
172
+ __publicField$9(this, "__options__");
173
+ this.instruction = createInstruction();
174
+ this.__options__ = options;
175
+ }
176
+ render(ctx) {
177
+ this.create();
178
+ const cap = this.instruction.mods.length;
179
+ for (let i = 0; i < cap; i++) {
180
+ const mod = this.instruction.mods[i];
181
+ const [direct, ...args] = mod;
182
+ if (direct in ASSIGN_MAPPINGS) {
183
+ ctx[direct] = args[0];
184
+ continue;
185
+ }
186
+ ctx[direct].apply(ctx, ...args);
187
+ }
188
+ }
189
+ get __instanceOf__() {
190
+ return "Graph" /* Graph */;
191
+ }
232
192
  }
233
193
 
234
- class Box extends Display {
235
- elements;
236
- constructor(){
237
- super();
238
- this.elements = [];
239
- }
240
- add(...elements) {
241
- const cap = elements.length;
242
- for(let i = 0; i < cap; i++){
243
- const element = elements[i];
244
- if (element.parent) ;
245
- this.elements.push(element);
246
- element.parent = this;
194
+ function isGraph(display) {
195
+ return display.__instanceOf__ === DisplayType.Graph;
196
+ }
197
+ function isBox(display) {
198
+ return display.__instanceOf__ === DisplayType.Box;
199
+ }
200
+ function isRect(display) {
201
+ return isGraph(display) && display.__shape__ === DisplayType.Rect;
202
+ }
203
+ function isText(display) {
204
+ return isGraph(display) && display.__shape__ === DisplayType.Text;
205
+ }
206
+ function isLayer(display) {
207
+ return display.__instanceOf__ === DisplayType.Layer;
208
+ }
209
+ const asserts = {
210
+ isGraph,
211
+ isBox,
212
+ isRect,
213
+ isText,
214
+ isLayer
215
+ };
216
+
217
+ var __defProp$8 = Object.defineProperty;
218
+ var __defNormalProp$8 = (obj, key, value) => key in obj ? __defProp$8(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
219
+ var __publicField$8 = (obj, key, value) => __defNormalProp$8(obj, key + "" , value);
220
+ class C extends Display {
221
+ constructor() {
222
+ super();
223
+ __publicField$8(this, "elements");
224
+ this.elements = [];
225
+ }
226
+ add(...elements) {
227
+ const cap = elements.length;
228
+ for (let i = 0; i < cap; i++) {
229
+ const element = elements[i];
230
+ if (element.parent) ;
231
+ this.elements.push(element);
232
+ element.parent = this;
233
+ }
234
+ }
235
+ remove(...elements) {
236
+ const cap = elements.length;
237
+ for (let i = 0; i < cap; i++) {
238
+ for (let j = this.elements.length - 1; j >= 0; j--) {
239
+ const element = this.elements[j];
240
+ if (element.id === elements[i].id) {
241
+ this.elements.splice(j, 1);
242
+ element.parent = null;
247
243
  }
244
+ }
248
245
  }
249
- remove(...elements) {
246
+ }
247
+ destory() {
248
+ this.elements.forEach((element) => element.parent = null);
249
+ this.elements.length = 0;
250
+ }
251
+ }
252
+ class Box extends C {
253
+ constructor() {
254
+ super();
255
+ __publicField$8(this, "elements");
256
+ this.elements = [];
257
+ }
258
+ add(...elements) {
259
+ const cap = elements.length;
260
+ for (let i = 0; i < cap; i++) {
261
+ const element = elements[i];
262
+ if (element.parent) ;
263
+ this.elements.push(element);
264
+ element.parent = this;
265
+ }
266
+ }
267
+ remove(...elements) {
268
+ const cap = elements.length;
269
+ for (let i = 0; i < cap; i++) {
270
+ for (let j = this.elements.length - 1; j >= 0; j--) {
271
+ const element = this.elements[j];
272
+ if (element.id === elements[i].id) {
273
+ this.elements.splice(j, 1);
274
+ element.parent = null;
275
+ }
276
+ }
277
+ }
278
+ }
279
+ destory() {
280
+ this.elements.forEach((element) => element.parent = null);
281
+ this.elements.length = 0;
282
+ }
283
+ get __instanceOf__() {
284
+ return DisplayType.Box;
285
+ }
286
+ clone() {
287
+ const box = new Box();
288
+ if (this.elements.length) {
289
+ const traverse = (elements, parent) => {
290
+ const els = [];
250
291
  const cap = elements.length;
251
- for(let i = 0; i < cap; i++){
252
- for(let j = this.elements.length - 1; j >= 0; j--){
253
- const element = this.elements[j];
254
- if (element.id === elements[i].id) {
255
- this.elements.splice(j, 1);
256
- element.parent = null;
257
- }
258
- }
292
+ for (let i = 0; i < cap; i++) {
293
+ const element = elements[i];
294
+ if (asserts.isBox(element)) {
295
+ const box2 = new Box();
296
+ box2.parent = parent;
297
+ box2.add(...traverse(element.elements, box2));
298
+ els.push(box2);
299
+ } else if (asserts.isGraph(element)) {
300
+ const el = element.clone();
301
+ el.parent = parent;
302
+ els.push(el);
303
+ }
259
304
  }
305
+ return els;
306
+ };
307
+ box.add(...traverse(this.elements, box));
260
308
  }
261
- destory() {
262
- this.elements.forEach((element)=>element.parent = null);
263
- this.elements.length = 0;
264
- }
309
+ return box;
310
+ }
311
+ }
312
+
313
+ var __defProp$7 = Object.defineProperty;
314
+ var __defNormalProp$7 = (obj, key, value) => key in obj ? __defProp$7(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
315
+ var __publicField$7 = (obj, key, value) => __defNormalProp$7(obj, typeof key !== "symbol" ? key + "" : key, value);
316
+ function writeBoundingRectForCanvas(c, w, h, dpr) {
317
+ c.width = w * dpr;
318
+ c.height = h * dpr;
319
+ c.style.cssText = `width: ${w}px; height: ${h}px`;
320
+ }
321
+ class Canvas {
322
+ constructor(options) {
323
+ __publicField$7(this, "canvas");
324
+ __publicField$7(this, "ctx");
325
+ this.canvas = createCanvasElement();
326
+ writeBoundingRectForCanvas(this.canvas, options.width, options.height, options.devicePixelRatio);
327
+ this.ctx = this.canvas.getContext("2d");
328
+ }
329
+ get c() {
330
+ return { canvas: this.canvas, ctx: this.ctx };
331
+ }
332
+ }
333
+ class Render {
334
+ constructor(to, options) {
335
+ __publicField$7(this, "c");
336
+ __publicField$7(this, "options");
337
+ this.c = new Canvas(options);
338
+ this.options = options;
339
+ this.initOptions(options);
340
+ !options.shaow && to.appendChild(this.canvas);
341
+ }
342
+ clear(width, height) {
343
+ this.ctx.clearRect(0, 0, width, height);
344
+ }
345
+ get canvas() {
346
+ return this.c.c.canvas;
347
+ }
348
+ get ctx() {
349
+ return this.c.c.ctx;
350
+ }
351
+ initOptions(userOptions = {}) {
352
+ Object.assign(this.options, userOptions);
353
+ writeBoundingRectForCanvas(this.canvas, this.options.width, this.options.height, this.options.devicePixelRatio);
354
+ }
355
+ update(schedule) {
356
+ this.clear(this.options.width, this.options.height);
357
+ schedule.execute(this);
358
+ }
359
+ destory() {
360
+ }
361
+ }
362
+
363
+ var __defProp$6 = Object.defineProperty;
364
+ var __defNormalProp$6 = (obj, key, value) => key in obj ? __defProp$6(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
365
+ var __publicField$6 = (obj, key, value) => __defNormalProp$6(obj, typeof key !== "symbol" ? key + "" : key, value);
366
+ class Layer extends C {
367
+ constructor(options = {}) {
368
+ super();
369
+ __publicField$6(this, "c");
370
+ __publicField$6(this, "__refresh__");
371
+ __publicField$6(this, "options");
372
+ __publicField$6(this, "width");
373
+ __publicField$6(this, "height");
374
+ __publicField$6(this, "x");
375
+ __publicField$6(this, "y");
376
+ __publicField$6(this, "scaleX");
377
+ __publicField$6(this, "scaleY");
378
+ __publicField$6(this, "rotation");
379
+ __publicField$6(this, "skewX");
380
+ __publicField$6(this, "skewY");
381
+ this.c = new Canvas({ width: 0, height: 0, devicePixelRatio: 1 });
382
+ this.__refresh__ = false;
383
+ this.options = /* @__PURE__ */ Object.create(null);
384
+ this.width = options.width || 0;
385
+ this.height = options.height || 0;
386
+ this.x = options.x || 0;
387
+ this.y = options.y || 0;
388
+ this.scaleX = options.scaleX || 1;
389
+ this.scaleY = options.scaleY || 1;
390
+ this.rotation = options.rotation || 0;
391
+ this.skewX = options.skewX || 0;
392
+ this.skewY = options.skewY || 0;
393
+ }
394
+ get __instanceOf__() {
395
+ return DisplayType.Layer;
396
+ }
397
+ setCanvasOptions(options = {}) {
398
+ Object.assign(this.options, options);
399
+ writeBoundingRectForCanvas(this.c.c.canvas, options.width || 0, options.height || 0, options.devicePixelRatio || 1);
400
+ }
401
+ setCacheSnapshot(c) {
402
+ const dpr = this.options.devicePixelRatio || 1;
403
+ const matrix = this.matrix.create({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
404
+ this.ctx.clearRect(0, 0, this.options.width, this.options.height);
405
+ matrix.transform(this.x, this.y, this.scaleX, this.scaleY, this.rotation, this.skewX, this.skewY);
406
+ applyCanvasTransform(this.ctx, matrix, dpr);
407
+ this.ctx.drawImage(c, 0, 0, this.options.width / dpr, this.options.height / dpr);
408
+ this.__refresh__ = true;
409
+ }
410
+ initLoc(options = {}) {
411
+ this.x = options.x || 0;
412
+ this.y = options.y || 0;
413
+ this.scaleX = options.scaleX || 1;
414
+ this.scaleY = options.scaleY || 1;
415
+ this.rotation = options.rotation || 0;
416
+ this.skewX = options.skewX || 0;
417
+ this.skewY = options.skewY || 0;
418
+ }
419
+ draw(ctx) {
420
+ const matrix = this.matrix.create({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
421
+ matrix.transform(this.x, this.y, this.scaleX, this.scaleY, this.rotation, this.skewX, this.skewY);
422
+ applyCanvasTransform(ctx, matrix, this.options.devicePixelRatio || 1);
423
+ ctx.drawImage(this.canvas, 0, 0);
424
+ }
425
+ get canvas() {
426
+ return this.c.c.canvas;
427
+ }
428
+ get ctx() {
429
+ return this.c.c.ctx;
430
+ }
265
431
  }
266
432
 
267
- // Runtime is designed for graph element
268
433
  function decodeHLS(meta) {
269
- const { h, l, s, a } = meta;
270
- if ('a' in meta) {
271
- return `hsla(${h}deg, ${s}%, ${l}%, ${a})`;
272
- }
273
- return `hsl(${h}deg, ${s}%, ${l}%)`;
434
+ const { h, l, s, a } = meta;
435
+ if ("a" in meta) {
436
+ return `hsla(${h}deg, ${s}%, ${l}%, ${a})`;
437
+ }
438
+ return `hsl(${h}deg, ${s}%, ${l}%)`;
274
439
  }
275
440
  function decodeRGB(meta) {
276
- const { r, g, b, a } = meta;
277
- if ('a' in meta) {
278
- return `rgba(${r}, ${g}, ${b}, ${a})`;
279
- }
280
- return `rgb(${r}, ${g}, ${b})`;
441
+ const { r, g, b, a } = meta;
442
+ if ("a" in meta) {
443
+ return `rgba(${r}, ${g}, ${b}, ${a})`;
444
+ }
445
+ return `rgb(${r}, ${g}, ${b})`;
281
446
  }
282
447
  function decodeColor(meta) {
283
- return meta.mode === 'rgb' ? decodeRGB(meta.desc) : decodeHLS(meta.desc);
448
+ return meta.mode === "rgb" ? decodeRGB(meta.desc) : decodeHLS(meta.desc);
284
449
  }
285
450
  function evaluateFillStyle(primitive, opacity = 1) {
286
- const descibe = {
287
- mode: primitive.mode,
288
- desc: {
289
- ...primitive.desc,
290
- a: opacity
291
- }
292
- };
293
- return decodeColor(descibe);
451
+ const descibe = { mode: primitive.mode, desc: { ...primitive.desc, a: opacity } };
452
+ return decodeColor(descibe);
294
453
  }
295
454
  const runtime = {
296
- evaluateFillStyle
455
+ evaluateFillStyle
297
456
  };
298
457
 
458
+ var __defProp$5 = Object.defineProperty;
459
+ var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
460
+ var __publicField$5 = (obj, key, value) => __defNormalProp$5(obj, key + "" , value);
299
461
  class Rect extends Graph {
300
- style;
301
- constructor(options = {}){
302
- super(options);
303
- this.style = options.style || Object.create(null);
304
- }
305
- create() {
306
- if (this.style.fill) {
307
- this.instruction.fillStyle(runtime.evaluateFillStyle(this.style.fill, this.style.opacity));
308
- this.instruction.fillRect(0, 0, this.width, this.height);
309
- }
310
- if (this.style.stroke) {
311
- this.instruction.strokeStyle(this.style.stroke);
312
- if (typeof this.style.lineWidth === 'number') {
313
- this.instruction.lineWidth(this.style.lineWidth);
314
- }
315
- this.instruction.strokeRect(0, 0, this.width, this.height);
316
- }
317
- }
462
+ constructor(options = {}) {
463
+ super(options);
464
+ __publicField$5(this, "style");
465
+ this.style = options.style || /* @__PURE__ */ Object.create(null);
466
+ }
467
+ get __shape__() {
468
+ return DisplayType.Rect;
469
+ }
470
+ create() {
471
+ if (this.style.fill) {
472
+ this.instruction.fillStyle(runtime.evaluateFillStyle(this.style.fill, this.style.opacity));
473
+ this.instruction.fillRect(0, 0, this.width, this.height);
474
+ }
475
+ if (this.style.stroke) {
476
+ this.instruction.strokeStyle(this.style.stroke);
477
+ if (typeof this.style.lineWidth === "number") {
478
+ this.instruction.lineWidth(this.style.lineWidth);
479
+ }
480
+ this.instruction.strokeRect(0, 0, this.width, this.height);
481
+ }
482
+ }
483
+ clone() {
484
+ return new Rect({ ...this.style, ...this.__options__ });
485
+ }
318
486
  }
319
487
 
488
+ var __defProp$4 = Object.defineProperty;
489
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
490
+ var __publicField$4 = (obj, key, value) => __defNormalProp$4(obj, typeof key !== "symbol" ? key + "" : key, value);
320
491
  class Text extends Graph {
321
- text;
322
- style;
323
- constructor(options = {}){
324
- super(options);
325
- this.text = options.text || '';
326
- this.style = options.style || Object.create(null);
327
- }
328
- create() {
329
- if (this.style.fill) {
330
- this.instruction.font(this.style.font);
331
- this.instruction.lineWidth(this.style.lineWidth);
332
- this.instruction.textBaseline(this.style.baseline);
333
- this.instruction.fillStyle(this.style.fill);
334
- this.instruction.fillText(this.text, 0, 0);
335
- }
336
- }
492
+ constructor(options = {}) {
493
+ super(options);
494
+ __publicField$4(this, "text");
495
+ __publicField$4(this, "style");
496
+ this.text = options.text || "";
497
+ this.style = options.style || /* @__PURE__ */ Object.create(null);
498
+ }
499
+ create() {
500
+ if (this.style.fill) {
501
+ this.instruction.font(this.style.font);
502
+ this.instruction.lineWidth(this.style.lineWidth);
503
+ this.instruction.textBaseline(this.style.baseline);
504
+ this.instruction.fillStyle(this.style.fill);
505
+ this.instruction.fillText(this.text, 0, 0);
506
+ }
507
+ }
508
+ clone() {
509
+ return new Text({ ...this.style, ...this.__options__ });
510
+ }
511
+ get __shape__() {
512
+ return DisplayType.Text;
513
+ }
337
514
  }
338
515
 
516
+ var __defProp$3 = Object.defineProperty;
517
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
518
+ var __publicField$3 = (obj, key, value) => __defNormalProp$3(obj, key + "" , value);
339
519
  class Event {
340
- eventCollections;
341
- constructor(){
342
- this.eventCollections = Object.create(null);
343
- }
344
- on(evt, handler, c) {
345
- if (!(evt in this.eventCollections)) {
346
- this.eventCollections[evt] = [];
347
- }
348
- const data = {
349
- name: evt,
350
- handler,
351
- ctx: c || this
352
- };
353
- this.eventCollections[evt].push(data);
354
- }
355
- off(evt, handler) {
356
- if (evt in this.eventCollections) {
357
- if (!handler) {
358
- this.eventCollections[evt] = [];
359
- return;
360
- }
361
- this.eventCollections[evt] = this.eventCollections[evt].filter((d)=>d.handler !== handler);
362
- }
363
- }
364
- emit(evt, ...args) {
365
- if (!this.eventCollections[evt]) return;
366
- const handlers = this.eventCollections[evt];
367
- if (handlers.length) {
368
- handlers.forEach((d)=>{
369
- d.handler.call(d.ctx, ...args);
370
- });
371
- }
372
- }
373
- bindWithContext(c) {
374
- return (evt, handler)=>this.on(evt, handler, c);
375
- }
520
+ constructor() {
521
+ __publicField$3(this, "eventCollections");
522
+ this.eventCollections = /* @__PURE__ */ Object.create(null);
523
+ }
524
+ on(evt, handler, c) {
525
+ if (!(evt in this.eventCollections)) {
526
+ this.eventCollections[evt] = [];
527
+ }
528
+ const data = {
529
+ name: evt,
530
+ handler,
531
+ ctx: c || this
532
+ };
533
+ this.eventCollections[evt].push(data);
534
+ }
535
+ off(evt, handler) {
536
+ if (evt in this.eventCollections) {
537
+ if (!handler) {
538
+ this.eventCollections[evt] = [];
539
+ return;
540
+ }
541
+ this.eventCollections[evt] = this.eventCollections[evt].filter((d) => d.handler !== handler);
542
+ }
543
+ }
544
+ emit(evt, ...args) {
545
+ if (!this.eventCollections[evt]) return;
546
+ const handlers = this.eventCollections[evt];
547
+ if (handlers.length) {
548
+ handlers.forEach((d) => {
549
+ d.handler.call(d.ctx, ...args);
550
+ });
551
+ }
552
+ }
553
+ bindWithContext(c) {
554
+ return (evt, handler) => this.on(evt, handler, c);
555
+ }
376
556
  }
377
557
 
378
- const NAME_SPACE = 'etoile';
558
+ const NAME_SPACE = "etoile";
379
559
  const log = {
380
- error: (message)=>{
381
- return `[${NAME_SPACE}] ${message}`;
382
- }
560
+ error: (message) => {
561
+ return `[${NAME_SPACE}] ${message}`;
562
+ }
383
563
  };
384
564
 
385
- class Render {
386
- canvas;
387
- ctx;
388
- options;
389
- constructor(to, options){
390
- this.canvas = document.createElement('canvas');
391
- this.ctx = this.canvas.getContext('2d');
392
- this.options = options;
393
- this.initOptions(options);
394
- to.appendChild(this.canvas);
395
- }
396
- clear(width, height) {
397
- this.ctx.clearRect(0, 0, width, height);
398
- }
399
- initOptions(userOptions = {}) {
400
- Object.assign(this.options, userOptions);
401
- const { options } = this;
402
- this.canvas.width = options.width * options.devicePixelRatio;
403
- this.canvas.height = options.height * options.devicePixelRatio;
404
- this.canvas.style.cssText = `width: ${options.width}px; height: ${options.height}px`;
405
- }
406
- update(schedule) {
407
- this.clear(this.options.width, this.options.height);
408
- schedule.execute(this);
409
- }
410
- destory() {}
565
+ var __defProp$2 = Object.defineProperty;
566
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
567
+ var __publicField$2 = (obj, key, value) => __defNormalProp$2(obj, typeof key !== "symbol" ? key + "" : key, value);
568
+ function drawGraphIntoCanvas(graph, opts, callback) {
569
+ const { ctx, dpr } = opts;
570
+ ctx.save();
571
+ if (asserts.isLayer(graph) && graph.__refresh__) {
572
+ callback(opts, graph);
573
+ return;
574
+ }
575
+ if (asserts.isLayer(graph) || asserts.isBox(graph)) {
576
+ const elements = graph.elements;
577
+ const cap = elements.length;
578
+ for (let i = 0; i < cap; i++) {
579
+ const element = elements[i];
580
+ drawGraphIntoCanvas(element, opts, callback);
581
+ }
582
+ callback(opts, graph);
583
+ }
584
+ if (asserts.isGraph(graph)) {
585
+ const matrix = graph.matrix.create({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
586
+ matrix.transform(graph.x, graph.y, graph.scaleX, graph.scaleY, graph.rotation, graph.skewX, graph.skewY);
587
+ applyCanvasTransform(ctx, matrix, dpr);
588
+ graph.render(ctx);
589
+ }
590
+ ctx.restore();
411
591
  }
412
-
413
- let Schedule$1 = class Schedule extends Box {
414
- render;
415
- to;
416
- event;
417
- constructor(to, renderOptions = {}){
418
- super();
419
- this.to = typeof to === 'string' ? document.querySelector(to) : to;
420
- if (!this.to) {
421
- throw new Error(log.error('The element to bind is not found.'));
422
- }
423
- const { width, height } = this.to.getBoundingClientRect();
424
- Object.assign(renderOptions, {
425
- width,
426
- height
427
- }, {
428
- devicePixelRatio: window.devicePixelRatio || 1
429
- });
430
- this.event = new Event();
431
- this.render = new Render(this.to, renderOptions);
432
- }
433
- applyTransform(matrix) {
434
- const pixel = this.render.options.devicePixelRatio;
435
- this.render.ctx.setTransform(matrix.a * pixel, matrix.b * pixel, matrix.c * pixel, matrix.d * pixel, matrix.e * pixel, matrix.f * pixel);
436
- }
437
- update() {
438
- this.render.update(this);
439
- const matrix = this.matrix.create({
440
- a: 1,
441
- b: 0,
442
- c: 0,
443
- d: 1,
444
- e: 0,
445
- f: 0
446
- });
447
- this.applyTransform(matrix);
448
- }
449
- // execute all graph elements
450
- execute(render, graph = this) {
451
- render.ctx.save();
452
- let matrix = graph.matrix;
453
- this.applyTransform(matrix);
454
- if (graph instanceof Box) {
455
- const cap = graph.elements.length;
456
- for(let i = 0; i < cap; i++){
457
- const element = graph.elements[i];
458
- matrix = element.matrix.create({
459
- a: 1,
460
- b: 0,
461
- c: 0,
462
- d: 1,
463
- e: 0,
464
- f: 0
465
- });
466
- if (element instanceof Graph) {
467
- matrix.transform(element.x, element.y, element.scaleX, element.scaleY, element.rotation, element.skewX, element.skewY);
468
- }
469
- this.execute(render, element);
470
- }
471
- }
472
- if (graph instanceof Graph) {
473
- graph.render(render.ctx);
592
+ class Schedule extends Box {
593
+ constructor(to, renderOptions = {}) {
594
+ super();
595
+ __publicField$2(this, "render");
596
+ __publicField$2(this, "to");
597
+ __publicField$2(this, "event");
598
+ this.to = typeof to === "string" ? document.querySelector(to) : to;
599
+ if (!this.to) {
600
+ throw new Error(log.error("The element to bind is not found."));
601
+ }
602
+ const { width, height } = this.to.getBoundingClientRect();
603
+ Object.assign(renderOptions, { width, height }, { devicePixelRatio: window.devicePixelRatio || 1 });
604
+ this.event = new Event();
605
+ this.render = new Render(this.to, renderOptions);
606
+ }
607
+ update() {
608
+ this.render.update(this);
609
+ const matrix = this.matrix.create({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
610
+ applyCanvasTransform(this.render.ctx, matrix, this.render.options.devicePixelRatio);
611
+ }
612
+ // execute all graph elements
613
+ execute(render, graph = this) {
614
+ drawGraphIntoCanvas(graph, { c: render.canvas, ctx: render.ctx, dpr: render.options.devicePixelRatio }, (opts, graph2) => {
615
+ if (asserts.isLayer(graph2)) {
616
+ if (graph2.__refresh__) {
617
+ graph2.draw(opts.ctx);
618
+ } else {
619
+ graph2.setCacheSnapshot(opts.c);
474
620
  }
475
- render.ctx.restore();
476
- }
477
- };
621
+ }
622
+ });
623
+ }
624
+ }
478
625
 
479
626
  function traverse(graphs, handler) {
480
- graphs.forEach((graph)=>{
481
- if (graph instanceof Box) {
482
- traverse(graph.elements, handler);
483
- } else if (graph instanceof Graph) {
484
- handler(graph);
485
- }
486
- });
627
+ const len = graphs.length;
628
+ for (let i = 0; i < len; i++) {
629
+ const graph = graphs[i];
630
+ if (asserts.isLayer(graph) && graph.__refresh__) {
631
+ handler(graph);
632
+ continue;
633
+ }
634
+ if (asserts.isGraph(graph)) {
635
+ handler(graph);
636
+ } else if (asserts.isBox(graph) || asserts.isLayer(graph)) {
637
+ traverse(graph.elements, handler);
638
+ }
639
+ }
487
640
  }
488
641
  const etoile = {
489
- Schedule: Schedule$1,
490
- traverse
642
+ Schedule,
643
+ traverse
491
644
  };
492
645
 
493
- // Currently, etoile is an internal module, so we won't need too much easing functions.
494
- // And the animation logic is implemented by user code.
495
646
  const easing = {
496
- linear: (k)=>k,
497
- quadraticIn: (k)=>k * k,
498
- quadraticOut: (k)=>k * (2 - k),
499
- quadraticInOut: (k)=>{
500
- if ((k *= 2) < 1) {
501
- return 0.5 * k * k;
502
- }
503
- return -0.5 * (--k * (k - 2) - 1);
504
- },
505
- cubicIn: (k)=>k * k * k,
506
- cubicOut: (k)=>{
507
- if ((k *= 2) < 1) {
508
- return 0.5 * k * k * k;
509
- }
510
- return 0.5 * ((k -= 2) * k * k + 2);
511
- },
512
- cubicInOut: (k)=>{
513
- if ((k *= 2) < 1) {
514
- return 0.5 * k * k * k;
515
- }
516
- return 0.5 * ((k -= 2) * k * k + 2);
517
- }
647
+ linear: (k) => k,
648
+ quadraticIn: (k) => k * k,
649
+ quadraticOut: (k) => k * (2 - k),
650
+ quadraticInOut: (k) => {
651
+ if ((k *= 2) < 1) {
652
+ return 0.5 * k * k;
653
+ }
654
+ return -0.5 * (--k * (k - 2) - 1);
655
+ },
656
+ cubicIn: (k) => k * k * k,
657
+ cubicOut: (k) => {
658
+ if ((k *= 2) < 1) {
659
+ return 0.5 * k * k * k;
660
+ }
661
+ return 0.5 * ((k -= 2) * k * k + 2);
662
+ },
663
+ cubicInOut: (k) => {
664
+ if ((k *= 2) < 1) {
665
+ return 0.5 * k * k * k;
666
+ }
667
+ return 0.5 * ((k -= 2) * k * k + 2);
668
+ }
518
669
  };
519
670
 
520
- function sortChildrenByKey(data, ...keys) {
521
- return data.sort((a, b)=>{
522
- for (const key of keys){
523
- const v = a[key];
524
- const v2 = b[key];
525
- if (perferNumeric(v) && perferNumeric(v2)) {
526
- if (v2 > v) return 1;
527
- if (v2 < v) return -1;
528
- continue;
529
- }
530
- // Not numeric, compare as string
531
- const comparison = ('' + v).localeCompare('' + v2);
532
- if (comparison !== 0) return comparison;
533
- }
534
- return 0;
671
+ function hashCode(str) {
672
+ let hash = 0;
673
+ for (let i = 0; i < str.length; i++) {
674
+ const code = str.charCodeAt(i);
675
+ hash = (hash << 5) - hash + code;
676
+ hash = hash & hash;
677
+ }
678
+ return hash;
679
+ }
680
+ function perferNumeric(s) {
681
+ if (typeof s === "number") return true;
682
+ return s.charCodeAt(0) >= 48 && s.charCodeAt(0) <= 57;
683
+ }
684
+ function createFillBlock(x, y, width, height, style) {
685
+ return new Rect({ width, height, x, y, style });
686
+ }
687
+ function createTitleText(text, x, y, font, color) {
688
+ return new Text({
689
+ text,
690
+ x,
691
+ y,
692
+ style: { fill: color, textAlign: "center", baseline: "middle", font, lineWidth: 1 }
693
+ });
694
+ }
695
+ const raf = window.requestAnimationFrame;
696
+ function createCanvasElement() {
697
+ return document.createElement("canvas");
698
+ }
699
+ function applyCanvasTransform(ctx, matrix, dpr) {
700
+ ctx.setTransform(matrix.a * dpr, matrix.b * dpr, matrix.c * dpr, matrix.d * dpr, matrix.e * dpr, matrix.f * dpr);
701
+ }
702
+ function mixin(app, methods) {
703
+ methods.forEach(({ name, fn }) => {
704
+ Object.defineProperty(app, name, {
705
+ value: fn(app),
706
+ writable: false
535
707
  });
708
+ });
709
+ }
710
+
711
+ function sortChildrenByKey(data, ...keys) {
712
+ return data.sort((a, b) => {
713
+ for (const key of keys) {
714
+ const v = a[key];
715
+ const v2 = b[key];
716
+ if (perferNumeric(v) && perferNumeric(v2)) {
717
+ if (v2 > v) return 1;
718
+ if (v2 < v) return -1;
719
+ continue;
720
+ }
721
+ const comparison = ("" + v).localeCompare("" + v2);
722
+ if (comparison !== 0) return comparison;
723
+ }
724
+ return 0;
725
+ });
536
726
  }
537
727
  function c2m(data, key, modifier) {
538
- if (Array.isArray(data.groups)) {
539
- data.groups = sortChildrenByKey(data.groups.map((d)=>c2m(d, key, modifier)), 'weight');
540
- }
541
- const obj = {
542
- ...data,
543
- weight: data[key]
544
- };
545
- if (modifier) return modifier(obj);
546
- return obj;
728
+ if (Array.isArray(data.groups)) {
729
+ data.groups = sortChildrenByKey(data.groups.map((d) => c2m(d, key, modifier)), "weight");
730
+ }
731
+ const obj = { ...data, weight: data[key] };
732
+ if (modifier) return modifier(obj);
733
+ return obj;
547
734
  }
548
735
  function flatten(data) {
549
- const result = [];
550
- for(let i = 0; i < data.length; i++){
551
- const { groups, ...rest } = data[i];
552
- result.push(rest);
553
- if (groups) {
554
- result.push(...flatten(groups));
555
- }
556
- }
557
- return result;
736
+ const result = [];
737
+ for (let i = 0; i < data.length; i++) {
738
+ const { groups, ...rest } = data[i];
739
+ result.push(rest);
740
+ if (groups) {
741
+ result.push(...flatten(groups));
742
+ }
743
+ }
744
+ return result;
558
745
  }
559
746
  function bindParentForModule(modules, parent) {
560
- return modules.map((module)=>{
561
- const next = {
562
- ...module
563
- };
564
- next.parent = parent;
565
- if (next.groups && Array.isArray(next.groups)) {
566
- next.groups = bindParentForModule(next.groups, next);
567
- }
568
- return next;
569
- });
747
+ return modules.map((module) => {
748
+ const next = { ...module };
749
+ next.parent = parent;
750
+ if (next.groups && Array.isArray(next.groups)) {
751
+ next.groups = bindParentForModule(next.groups, next);
752
+ }
753
+ return next;
754
+ });
570
755
  }
571
756
  function getNodeDepth(node) {
572
- let depth = 0;
573
- while(node.parent){
574
- node = node.parent;
575
- depth++;
576
- }
577
- return depth;
757
+ let depth = 0;
758
+ while (node.parent) {
759
+ node = node.parent;
760
+ depth++;
761
+ }
762
+ return depth;
578
763
  }
579
764
  function visit(data, fn) {
580
- if (!data) return null;
581
- for (const d of data){
582
- if (d.children) {
583
- const result = visit(d.children, fn);
584
- if (result) return result;
585
- }
586
- const stop = fn(d);
587
- if (stop) return d;
588
- }
589
- return null;
765
+ if (!data) return null;
766
+ for (const d of data) {
767
+ if (d.children) {
768
+ const result = visit(d.children, fn);
769
+ if (result) return result;
770
+ }
771
+ const stop = fn(d);
772
+ if (stop) return d;
773
+ }
774
+ return null;
590
775
  }
591
776
  function findRelativeNode(c, p, layoutNodes) {
592
- return visit(layoutNodes, (node)=>{
593
- const [x, y, w, h] = node.layout;
594
- if (p.x >= x && p.y >= y && p.x < x + w && p.y < y + h) {
595
- return true;
596
- }
597
- });
777
+ return visit(layoutNodes, (node) => {
778
+ const [x, y, w, h] = node.layout;
779
+ if (p.x >= x && p.y >= y && p.x < x + w && p.y < y + h) {
780
+ return true;
781
+ }
782
+ });
598
783
  }
599
784
  function findRelativeNodeById(id, layoutNodes) {
600
- return visit(layoutNodes, (node)=>{
601
- if (node.node.id === id) {
602
- return true;
603
- }
604
- });
785
+ return visit(layoutNodes, (node) => {
786
+ if (node.node.id === id) {
787
+ return true;
788
+ }
789
+ });
605
790
  }
606
791
 
607
792
  function squarify(data, rect, layoutDecorator) {
608
- const result = [];
609
- if (!data.length) return result;
610
- const worst = (start, end, shortestSide, totalWeight, aspectRatio)=>{
611
- const max = data[start].weight * aspectRatio;
612
- const min = data[end].weight * aspectRatio;
613
- return Math.max(shortestSide * shortestSide * max / (totalWeight * totalWeight), totalWeight * totalWeight / (shortestSide * shortestSide * min));
614
- };
615
- const recursion = (start, rect)=>{
616
- while(start < data.length){
617
- let totalWeight = 0;
618
- for(let i = start; i < data.length; i++){
619
- totalWeight += data[i].weight;
620
- }
621
- const shortestSide = Math.min(rect.w, rect.h);
622
- const aspectRatio = rect.w * rect.h / totalWeight;
623
- let end = start;
624
- let areaInRun = 0;
625
- let oldWorst = 0;
626
- // find the best split
627
- while(end < data.length){
628
- const area = data[end].weight * aspectRatio;
629
- const newWorst = worst(start, end, shortestSide, areaInRun + area, aspectRatio);
630
- if (end > start && oldWorst < newWorst) break;
631
- areaInRun += area;
632
- oldWorst = newWorst;
633
- end++;
634
- }
635
- const splited = Math.round(areaInRun / shortestSide);
636
- let areaInLayout = 0;
637
- for(let i = start; i < end; i++){
638
- const children = data[i];
639
- const area = children.weight * aspectRatio;
640
- const lower = Math.round(shortestSide * areaInLayout / areaInRun);
641
- const upper = Math.round(shortestSide * (areaInLayout + area) / areaInRun);
642
- const [x, y, w, h] = rect.w >= rect.h ? [
643
- rect.x,
644
- rect.y + lower,
645
- splited,
646
- upper - lower
647
- ] : [
648
- rect.x + lower,
649
- rect.y,
650
- upper - lower,
651
- splited
652
- ];
653
- const depth = getNodeDepth(children) || 1;
654
- const { titleAreaHeight, rectGap } = layoutDecorator;
655
- const diff = titleAreaHeight.max / depth;
656
- const hh = diff < titleAreaHeight.min ? titleAreaHeight.min : diff;
657
- result.push({
658
- layout: [
659
- x,
660
- y,
661
- w,
662
- h
663
- ],
664
- node: children,
665
- decorator: {
666
- ...layoutDecorator,
667
- titleHeight: hh
668
- },
669
- children: w > rectGap * 2 && h > hh + rectGap ? squarify(children.groups || [], {
670
- x: x + rectGap,
671
- y: y + hh,
672
- w: w - rectGap * 2,
673
- h: h - hh - rectGap
674
- }, layoutDecorator) : []
675
- });
676
- areaInLayout += area;
677
- }
678
- start = end;
679
- if (rect.w >= rect.h) {
680
- rect.x += splited;
681
- rect.w -= splited;
682
- } else {
683
- rect.y += splited;
684
- rect.h -= splited;
685
- }
686
- }
687
- };
688
- recursion(0, rect);
689
- return result;
793
+ const result = [];
794
+ if (!data.length) return result;
795
+ const worst = (start, end, shortestSide, totalWeight, aspectRatio) => {
796
+ const max = data[start].weight * aspectRatio;
797
+ const min = data[end].weight * aspectRatio;
798
+ return Math.max(
799
+ shortestSide * shortestSide * max / (totalWeight * totalWeight),
800
+ totalWeight * totalWeight / (shortestSide * shortestSide * min)
801
+ );
802
+ };
803
+ const recursion = (start, rect2) => {
804
+ while (start < data.length) {
805
+ let totalWeight = 0;
806
+ for (let i = start; i < data.length; i++) {
807
+ totalWeight += data[i].weight;
808
+ }
809
+ const shortestSide = Math.min(rect2.w, rect2.h);
810
+ const aspectRatio = rect2.w * rect2.h / totalWeight;
811
+ let end = start;
812
+ let areaInRun = 0;
813
+ let oldWorst = 0;
814
+ while (end < data.length) {
815
+ const area = data[end].weight * aspectRatio;
816
+ const newWorst = worst(start, end, shortestSide, areaInRun + area, aspectRatio);
817
+ if (end > start && oldWorst < newWorst) break;
818
+ areaInRun += area;
819
+ oldWorst = newWorst;
820
+ end++;
821
+ }
822
+ const splited = Math.round(areaInRun / shortestSide);
823
+ let areaInLayout = 0;
824
+ for (let i = start; i < end; i++) {
825
+ const children = data[i];
826
+ const area = children.weight * aspectRatio;
827
+ const lower = Math.round(shortestSide * areaInLayout / areaInRun);
828
+ const upper = Math.round(shortestSide * (areaInLayout + area) / areaInRun);
829
+ const [x, y, w, h] = rect2.w >= rect2.h ? [rect2.x, rect2.y + lower, splited, upper - lower] : [rect2.x + lower, rect2.y, upper - lower, splited];
830
+ const depth = getNodeDepth(children) || 1;
831
+ const { titleAreaHeight, rectGap } = layoutDecorator;
832
+ const diff = titleAreaHeight.max / depth;
833
+ const hh = diff < titleAreaHeight.min ? titleAreaHeight.min : diff;
834
+ result.push({
835
+ layout: [x, y, w, h],
836
+ node: children,
837
+ decorator: {
838
+ ...layoutDecorator,
839
+ titleHeight: hh
840
+ },
841
+ children: w > rectGap * 2 && h > hh + rectGap ? squarify(children.groups || [], {
842
+ x: x + rectGap,
843
+ y: y + hh,
844
+ w: w - rectGap * 2,
845
+ h: h - hh - rectGap
846
+ }, layoutDecorator) : []
847
+ });
848
+ areaInLayout += area;
849
+ }
850
+ start = end;
851
+ if (rect2.w >= rect2.h) {
852
+ rect2.x += splited;
853
+ rect2.w -= splited;
854
+ } else {
855
+ rect2.y += splited;
856
+ rect2.h -= splited;
857
+ }
858
+ }
859
+ };
860
+ recursion(0, rect);
861
+ return result;
690
862
  }
691
863
 
692
864
  function applyForOpacity(graph, lastState, nextState, easedProgress) {
693
- const alpha = lastState + (nextState - lastState) * easedProgress;
694
- if (graph instanceof Rect) {
695
- graph.style.opacity = alpha;
865
+ const alpha = lastState + (nextState - lastState) * easedProgress;
866
+ if (asserts.isRect(graph)) {
867
+ graph.style.opacity = alpha;
868
+ }
869
+ }
870
+ function createEffectRun(c) {
871
+ return (fn) => {
872
+ const effect = () => {
873
+ const done = fn();
874
+ if (!done) {
875
+ c.animationFrameID = raf(effect);
876
+ }
877
+ };
878
+ if (!c.animationFrameID) {
879
+ c.animationFrameID = raf(effect);
696
880
  }
881
+ };
882
+ }
883
+ function createEffectStop(c) {
884
+ return () => {
885
+ if (c.animationFrameID) {
886
+ window.cancelAnimationFrame(c.animationFrameID);
887
+ c.animationFrameID = null;
888
+ }
889
+ };
890
+ }
891
+ function createEffectScope() {
892
+ const c = {
893
+ animationFrameID: null
894
+ };
895
+ const run = createEffectRun(c);
896
+ const stop = createEffectStop(c);
897
+ return { run, stop };
697
898
  }
698
899
 
699
900
  class RegisterModule {
700
- static mixin(app, methods) {
701
- methods.forEach(({ name, fn })=>{
702
- Object.defineProperty(app, name, {
703
- value: fn(app),
704
- writable: false
705
- });
706
- });
707
- }
708
901
  }
709
902
  function registerModuleForSchedule(mod) {
710
- if (mod instanceof RegisterModule) {
711
- return (app, treemap, render)=>mod.init(app, treemap, render);
712
- }
713
- throw new Error(log.error('The module is not a valid RegisterScheduleModule.'));
903
+ if (mod instanceof RegisterModule) {
904
+ return (app, treemap, render) => mod.init(app, treemap, render);
905
+ }
906
+ throw new Error(log.error("The module is not a valid RegisterScheduleModule."));
714
907
  }
715
908
 
716
- // etoile is a simple 2D render engine for web and it don't take complex rendering into account.
717
- // So it's no need to implement a complex event algorithm or hit mode.
718
- // If one day etoile need to build as a useful library. Pls rewrite it!
719
- // All of implementation don't want to consider the compatibility of the browser.
720
- const primitiveEvents = [
721
- 'click',
722
- 'mousedown',
723
- 'mousemove',
724
- 'mouseup',
725
- 'mouseover',
726
- 'mouseout'
727
- ];
909
+ var __defProp$1 = Object.defineProperty;
910
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
911
+ var __publicField$1 = (obj, key, value) => __defNormalProp$1(obj, typeof key !== "symbol" ? key + "" : key, value);
912
+ const primitiveEvents = ["click", "mousedown", "mousemove", "mouseup", "mouseover", "mouseout"];
913
+ const fill = { desc: { r: 255, g: 255, b: 255 }, mode: "rgb" };
728
914
  function smoothDrawing(c) {
729
- const { self, treemap } = c;
730
- const currentNode = self.currentNode;
731
- if (currentNode) {
732
- const lloc = new Set();
733
- visit([
734
- currentNode
735
- ], (node)=>{
736
- const [x, y, w, h] = node.layout;
737
- const { rectGap, titleHeight } = node.decorator;
738
- lloc.add(x + '-' + y);
739
- lloc.add(x + '-' + (y + h - rectGap));
740
- lloc.add(x + '-' + (y + titleHeight));
741
- lloc.add(x + w - rectGap + '-' + (y + titleHeight));
742
- });
743
- const startTime = Date.now();
744
- const animationDuration = 300;
745
- const draw = ()=>{
746
- if (self.forceDestroy) {
747
- return;
748
- }
749
- const elapsed = Date.now() - startTime;
750
- const progress = Math.min(elapsed / animationDuration, 1);
751
- const easedProgress = easing.cubicIn(progress) || 0.1;
752
- let allTasksCompleted = true;
753
- treemap.reset();
754
- etoile.traverse([
755
- treemap.elements[0]
756
- ], (graph)=>{
757
- const key = `${graph.x}-${graph.y}`;
758
- if (lloc.has(key)) {
759
- applyForOpacity(graph, 1, 0.7, easedProgress);
760
- if (progress < 1) {
761
- allTasksCompleted = false;
762
- }
763
- }
764
- });
765
- applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
766
- treemap.update();
767
- if (!allTasksCompleted) {
768
- window.requestAnimationFrame(draw);
769
- }
770
- };
771
- if (!self.isAnimating) {
772
- self.isAnimating = true;
773
- window.requestAnimationFrame(draw);
774
- }
775
- } else {
776
- treemap.reset();
777
- applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
778
- treemap.update();
779
- }
915
+ const { self } = c;
916
+ const currentNode = self.currentNode;
917
+ if (currentNode) {
918
+ const effect = createEffectScope();
919
+ const startTime = Date.now();
920
+ const animationDuration = 300;
921
+ const [x, y, w, h] = currentNode.layout;
922
+ effect.run(() => {
923
+ const elapsed = Date.now() - startTime;
924
+ const progress = Math.min(elapsed / animationDuration, 1);
925
+ if (self.forceDestroy || progress >= 1) {
926
+ effect.stop();
927
+ self.highlight.reset();
928
+ self.highlight.setDisplayLayerForHighlight();
929
+ return true;
930
+ }
931
+ const easedProgress = easing.cubicInOut(progress);
932
+ self.highlight.reset();
933
+ const mask = createFillBlock(x, y, w, h, { fill, opacity: 0.4 });
934
+ self.highlight.highlight.add(mask);
935
+ self.highlight.setDisplayLayerForHighlight("1");
936
+ applyForOpacity(mask, 0.4, 0.4, easedProgress);
937
+ stackMatrixTransform(mask, self.translateX, self.translateY, self.scaleRatio);
938
+ self.highlight.highlight.update();
939
+ });
940
+ } else {
941
+ self.highlight.reset();
942
+ self.highlight.setDisplayLayerForHighlight();
943
+ }
780
944
  }
781
945
  function applyZoomEvent(ctx) {
782
- ctx.treemap.event.on('zoom', (node)=>{
783
- const root = null;
784
- if (ctx.self.isDragging) return;
785
- onZoom(ctx, node, root);
786
- });
946
+ ctx.treemap.event.on("zoom", (node) => {
947
+ const root = null;
948
+ if (ctx.self.isDragging) return;
949
+ onZoom(ctx, node, root);
950
+ });
787
951
  }
788
952
  function getOffset(el) {
789
- let e = 0;
790
- let f = 0;
791
- if (document.documentElement.getBoundingClientRect && el.getBoundingClientRect) {
792
- const { top, left } = el.getBoundingClientRect();
793
- e = top;
794
- f = left;
795
- } else {
796
- for(let elt = el; elt; elt = el.offsetParent){
797
- e += el.offsetLeft;
798
- f += el.offsetTop;
799
- }
800
- }
801
- return [
802
- e + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
803
- f + Math.max(document.documentElement.scrollTop, document.body.scrollTop)
804
- ];
953
+ let e = 0;
954
+ let f = 0;
955
+ if (document.documentElement.getBoundingClientRect && el.getBoundingClientRect) {
956
+ const { top, left } = el.getBoundingClientRect();
957
+ e = top;
958
+ f = left;
959
+ } else {
960
+ for (let elt = el; elt; elt = el.offsetParent) {
961
+ e += el.offsetLeft;
962
+ f += el.offsetTop;
963
+ }
964
+ }
965
+ return [
966
+ e + Math.max(document.documentElement.scrollLeft, document.body.scrollLeft),
967
+ f + Math.max(document.documentElement.scrollTop, document.body.scrollTop)
968
+ ];
805
969
  }
806
970
  function captureBoxXY(c, evt, a, d, translateX, translateY) {
807
- const boundingClientRect = c.getBoundingClientRect();
808
- if (evt instanceof MouseEvent) {
809
- const [e, f] = getOffset(c);
810
- return {
811
- x: (evt.clientX - boundingClientRect.left - e - translateX) / a,
812
- y: (evt.clientY - boundingClientRect.top - f - translateY) / d
813
- };
814
- }
971
+ const boundingClientRect = c.getBoundingClientRect();
972
+ if (evt instanceof MouseEvent) {
973
+ const [e, f] = getOffset(c);
815
974
  return {
816
- x: 0,
817
- y: 0
975
+ x: (evt.clientX - boundingClientRect.left - e - translateX) / a,
976
+ y: (evt.clientY - boundingClientRect.top - f - translateY) / d
818
977
  };
978
+ }
979
+ return { x: 0, y: 0 };
819
980
  }
820
- function bindPrimitiveEvent(ctx, evt, bus) {
821
- const { treemap, self } = ctx;
822
- const c = treemap.render.canvas;
823
- const handler = (e)=>{
824
- const { x, y } = captureBoxXY(c, e, self.scaleRatio, self.scaleRatio, self.translateX, self.translateY);
825
- const event = {
826
- native: e,
827
- module: findRelativeNode(c, {
828
- x,
829
- y
830
- }, treemap.layoutNodes)
831
- };
832
- // @ts-expect-error
833
- bus.emit(evt, event);
981
+ function bindPrimitiveEvent(c, ctx, evt, bus) {
982
+ const { treemap, self } = ctx;
983
+ const handler = (e) => {
984
+ const { x, y } = captureBoxXY(
985
+ c,
986
+ e,
987
+ self.scaleRatio,
988
+ self.scaleRatio,
989
+ self.translateX,
990
+ self.translateY
991
+ );
992
+ const event = {
993
+ native: e,
994
+ module: findRelativeNode(c, { x, y }, treemap.layoutNodes)
834
995
  };
835
- c.addEventListener(evt, handler);
836
- return handler;
996
+ bus.emit(evt, event);
997
+ };
998
+ c.addEventListener(evt, handler);
999
+ return handler;
837
1000
  }
838
1001
  class SelfEvent extends RegisterModule {
839
- currentNode;
840
- isAnimating;
841
- forceDestroy;
842
- scaleRatio;
843
- translateX;
844
- translateY;
845
- layoutWidth;
846
- layoutHeight;
847
- isDragging;
848
- draggingState;
849
- event;
850
- constructor(){
851
- super();
852
- this.currentNode = null;
853
- this.isAnimating = false;
854
- this.forceDestroy = false;
855
- this.isDragging = false;
856
- this.scaleRatio = 1;
857
- this.translateX = 0;
858
- this.translateY = 0;
859
- this.layoutWidth = 0;
860
- this.layoutHeight = 0;
861
- this.draggingState = {
862
- x: 0,
863
- y: 0
864
- };
865
- this.event = new Event();
866
- }
867
- ondragstart(metadata) {
868
- const { native } = metadata;
869
- if (isScrollWheelOrRightButtonOnMouseupAndDown(native)) {
870
- return;
871
- }
872
- const x = native.offsetX;
873
- const y = native.offsetY;
874
- this.self.isDragging = true;
875
- this.self.draggingState = {
876
- x,
877
- y
878
- };
879
- }
880
- ondragmove(metadata) {
881
- if (!this.self.isDragging) {
882
- if ('zoom' in this.treemap.event.eventCollections) {
883
- const condit = this.treemap.event.eventCollections.zoom.length > 0;
884
- if (!condit) {
885
- applyZoomEvent(this);
886
- }
887
- }
888
- return;
889
- }
890
- // @ts-expect-error
891
- this.self.event.off('mousemove', this.self.onmousemove);
892
- this.treemap.event.off('zoom');
893
- this.self.forceDestroy = true;
894
- const { native } = metadata;
895
- const x = native.offsetX;
896
- const y = native.offsetY;
897
- const { x: lastX, y: lastY } = this.self.draggingState;
898
- const drawX = x - lastX;
899
- const drawY = y - lastY;
900
- this.self.translateX += drawX;
901
- this.self.translateY += drawY;
902
- this.self.draggingState = {
903
- x,
904
- y
905
- };
906
- this.treemap.reset();
907
- applyGraphTransform(this.treemap.elements, this.self.translateX, this.self.translateY, this.self.scaleRatio);
908
- this.treemap.update();
909
- }
910
- ondragend(metadata) {
911
- if (!this.self.isDragging) {
912
- return;
913
- }
914
- this.self.isDragging = false;
915
- this.self.draggingState = {
916
- x: 0,
917
- y: 0
918
- };
919
- this.self.event.bindWithContext(this)('mousemove', this.self.onmousemove);
920
- }
921
- onmousemove(metadata) {
922
- const { self } = this;
923
- if (self.isDragging) {
924
- return;
1002
+ constructor() {
1003
+ super();
1004
+ __publicField$1(this, "currentNode");
1005
+ __publicField$1(this, "forceDestroy");
1006
+ __publicField$1(this, "scaleRatio");
1007
+ __publicField$1(this, "translateX");
1008
+ __publicField$1(this, "translateY");
1009
+ __publicField$1(this, "layoutWidth");
1010
+ __publicField$1(this, "layoutHeight");
1011
+ __publicField$1(this, "isDragging");
1012
+ __publicField$1(this, "draggingState");
1013
+ __publicField$1(this, "event");
1014
+ __publicField$1(this, "triggerZoom");
1015
+ // eslint-disable-next-line no-use-before-define
1016
+ __publicField$1(this, "highlight");
1017
+ this.currentNode = null;
1018
+ this.forceDestroy = false;
1019
+ this.isDragging = false;
1020
+ this.scaleRatio = 1;
1021
+ this.translateX = 0;
1022
+ this.translateY = 0;
1023
+ this.layoutWidth = 0;
1024
+ this.layoutHeight = 0;
1025
+ this.draggingState = { x: 0, y: 0 };
1026
+ this.event = new Event();
1027
+ this.triggerZoom = false;
1028
+ this.highlight = createHighlight();
1029
+ }
1030
+ ondragstart(metadata) {
1031
+ const { native } = metadata;
1032
+ if (isScrollWheelOrRightButtonOnMouseupAndDown(native)) {
1033
+ return;
1034
+ }
1035
+ const x = native.offsetX;
1036
+ const y = native.offsetY;
1037
+ this.self.isDragging = true;
1038
+ this.self.draggingState = { x, y };
1039
+ }
1040
+ ondragmove(metadata) {
1041
+ if (!this.self.isDragging) {
1042
+ if ("zoom" in this.treemap.event.eventCollections) {
1043
+ const condit = this.treemap.event.eventCollections.zoom.length > 0;
1044
+ if (!condit) {
1045
+ applyZoomEvent(this);
925
1046
  }
926
- const { module: node } = metadata;
927
- self.forceDestroy = false;
928
- if (self.currentNode !== node) {
929
- self.currentNode = node;
930
- self.isAnimating = false;
931
- }
932
- smoothDrawing(this);
933
- }
934
- onmouseout() {
935
- const { self } = this;
936
- self.currentNode = null;
937
- self.forceDestroy = true;
938
- self.isDragging = false;
939
- smoothDrawing(this);
940
- }
941
- onwheel(metadata) {
942
- const { self, treemap } = this;
943
- // @ts-expect-error
944
- const wheelDelta = metadata.native.wheelDelta;
945
- const absWheelDelta = Math.abs(wheelDelta);
946
- const offsetX = metadata.native.offsetX;
947
- const offsetY = metadata.native.offsetY;
948
- if (wheelDelta === 0) {
949
- return;
950
- }
951
- self.forceDestroy = true;
952
- self.isAnimating = true;
953
- treemap.reset();
954
- const factor = absWheelDelta > 3 ? 1.4 : absWheelDelta > 1 ? 1.2 : 1.1;
955
- const delta = wheelDelta > 0 ? factor : 1 / factor;
956
- self.scaleRatio *= delta;
957
- const translateX = offsetX - (offsetX - self.translateX) * delta;
958
- const translateY = offsetY - (offsetY - self.translateY) * delta;
959
- self.translateX = translateX;
960
- self.translateY = translateY;
961
- applyGraphTransform(treemap.elements, self.translateX, self.translateY, self.scaleRatio);
962
- treemap.update();
963
- self.forceDestroy = false;
964
- self.isAnimating = false;
965
- }
966
- init(app, treemap, render) {
967
- const event = this.event;
968
- const nativeEvents = [];
969
- const methods = [
970
- {
971
- name: 'on',
972
- fn: ()=>event.bindWithContext(treemap.api).bind(event)
973
- },
974
- {
975
- name: 'off',
976
- fn: ()=>event.off.bind(event)
977
- },
978
- {
979
- name: 'emit',
980
- fn: ()=>event.emit.bind(event)
981
- }
982
- ];
983
- RegisterModule.mixin(app, methods);
984
- const selfEvents = [
985
- ...primitiveEvents,
986
- 'wheel'
987
- ];
988
- selfEvents.forEach((evt)=>{
989
- nativeEvents.push(bindPrimitiveEvent({
990
- treemap,
991
- self: this
992
- }, evt, event));
993
- });
994
- const selfEvt = event.bindWithContext({
995
- treemap,
996
- self: this
997
- });
998
- selfEvt('mousedown', this.ondragstart);
999
- selfEvt('mousemove', this.ondragmove);
1000
- selfEvt('mouseup', this.ondragend);
1001
- // highlight
1002
- selfEvt('mousemove', this.onmousemove);
1003
- selfEvt('mouseout', this.onmouseout);
1004
- // wheel
1005
- selfEvt('wheel', this.onwheel);
1006
- applyZoomEvent({
1007
- treemap,
1008
- self: this
1009
- });
1010
- treemap.event.on('cleanup:selfevent', ()=>{
1011
- this.currentNode = null;
1012
- this.isAnimating = false;
1013
- this.scaleRatio = 1;
1014
- this.translateX = 0;
1015
- this.translateY = 0;
1016
- this.layoutWidth = treemap.render.canvas.width;
1017
- this.layoutHeight = treemap.render.canvas.height;
1018
- this.isDragging = false;
1019
- this.draggingState = {
1020
- x: 0,
1021
- y: 0
1022
- };
1023
- });
1047
+ }
1048
+ return;
1049
+ }
1050
+ this.self.highlight.reset();
1051
+ this.self.highlight.setDisplayLayerForHighlight();
1052
+ this.self.event.off("mousemove", this.self.onmousemove);
1053
+ this.treemap.event.off("zoom");
1054
+ this.self.forceDestroy = true;
1055
+ const { native } = metadata;
1056
+ const x = native.offsetX;
1057
+ const y = native.offsetY;
1058
+ const { x: lastX, y: lastY } = this.self.draggingState;
1059
+ const drawX = x - lastX;
1060
+ const drawY = y - lastY;
1061
+ this.self.translateX += drawX;
1062
+ this.self.translateY += drawY;
1063
+ this.self.draggingState = { x, y };
1064
+ if (this.self.triggerZoom) {
1065
+ refreshBackgroundLayer(this);
1066
+ }
1067
+ this.treemap.reset();
1068
+ stackMatrixTransform(this.treemap.backgroundLayer, 0, 0, 0);
1069
+ stackMatrixTransformWithGraphAndLayer(this.treemap.elements, this.self.translateX, this.self.translateY, this.self.scaleRatio);
1070
+ this.treemap.update();
1071
+ }
1072
+ ondragend() {
1073
+ if (!this.self.isDragging) {
1074
+ return;
1075
+ }
1076
+ this.self.isDragging = false;
1077
+ this.self.draggingState = { x: 0, y: 0 };
1078
+ this.self.highlight.reset();
1079
+ this.self.highlight.setDisplayLayerForHighlight();
1080
+ this.self.event.bindWithContext(this)("mousemove", this.self.onmousemove);
1081
+ }
1082
+ onmousemove(metadata) {
1083
+ const { self } = this;
1084
+ const { module: node } = metadata;
1085
+ self.forceDestroy = false;
1086
+ if (self.currentNode !== node) {
1087
+ self.currentNode = node;
1088
+ }
1089
+ smoothDrawing(this);
1090
+ }
1091
+ onmouseout() {
1092
+ const { self } = this;
1093
+ self.currentNode = null;
1094
+ self.forceDestroy = true;
1095
+ smoothDrawing(this);
1096
+ }
1097
+ onwheel(metadata) {
1098
+ const { self, treemap } = this;
1099
+ const wheelDelta = metadata.native.wheelDelta;
1100
+ const absWheelDelta = Math.abs(wheelDelta);
1101
+ const offsetX = metadata.native.offsetX;
1102
+ const offsetY = metadata.native.offsetY;
1103
+ if (wheelDelta === 0) {
1104
+ return;
1105
+ }
1106
+ self.forceDestroy = true;
1107
+ if (self.triggerZoom) {
1108
+ refreshBackgroundLayer(this);
1024
1109
  }
1110
+ treemap.reset();
1111
+ this.self.highlight.reset();
1112
+ this.self.highlight.setDisplayLayerForHighlight();
1113
+ stackMatrixTransform(this.treemap.backgroundLayer, 0, 0, 0);
1114
+ const factor = absWheelDelta > 3 ? 1.4 : absWheelDelta > 1 ? 1.2 : 1.1;
1115
+ const delta = wheelDelta > 0 ? factor : 1 / factor;
1116
+ self.scaleRatio *= delta;
1117
+ const translateX = offsetX - (offsetX - self.translateX) * delta;
1118
+ const translateY = offsetY - (offsetY - self.translateY) * delta;
1119
+ self.translateX = translateX;
1120
+ self.translateY = translateY;
1121
+ stackMatrixTransformWithGraphAndLayer(this.treemap.elements, this.self.translateX, this.self.translateY, this.self.scaleRatio);
1122
+ treemap.update();
1123
+ self.forceDestroy = false;
1124
+ }
1125
+ init(app, treemap) {
1126
+ const event = this.event;
1127
+ const nativeEvents = [];
1128
+ const methods = [
1129
+ {
1130
+ name: "on",
1131
+ fn: () => event.bindWithContext(treemap.api).bind(event)
1132
+ },
1133
+ {
1134
+ name: "off",
1135
+ fn: () => event.off.bind(event)
1136
+ },
1137
+ {
1138
+ name: "emit",
1139
+ fn: () => event.emit.bind(event)
1140
+ }
1141
+ ];
1142
+ mixin(app, methods);
1143
+ const selfEvents = [...primitiveEvents, "wheel"];
1144
+ selfEvents.forEach((evt) => {
1145
+ nativeEvents.push(bindPrimitiveEvent(treemap.render.canvas, { treemap, self: this }, evt, event));
1146
+ });
1147
+ const selfEvt = event.bindWithContext({ treemap, self: this });
1148
+ selfEvt("mousedown", this.ondragstart);
1149
+ selfEvt("mousemove", this.ondragmove);
1150
+ selfEvt("mouseup", this.ondragend);
1151
+ selfEvt("wheel", this.onwheel);
1152
+ applyZoomEvent({ treemap, self: this });
1153
+ let installHightlightEvent = false;
1154
+ treemap.event.on("onload:selfevent", ({ width, height, root }) => {
1155
+ this.highlight.init(width, height, root);
1156
+ if (!installHightlightEvent) {
1157
+ bindPrimitiveEvent(this.highlight.highlight.render.canvas, { treemap, self: this }, "mousemove", event);
1158
+ bindPrimitiveEvent(this.highlight.highlight.render.canvas, { treemap, self: this }, "mouseout", event);
1159
+ selfEvt("mousemove", this.onmousemove);
1160
+ selfEvt("mouseout", this.onmouseout);
1161
+ installHightlightEvent = true;
1162
+ this.highlight.setDisplayLayerForHighlight();
1163
+ }
1164
+ this.highlight.reset();
1165
+ });
1166
+ treemap.event.on("cleanup:selfevent", () => {
1167
+ this.currentNode = null;
1168
+ this.scaleRatio = 1;
1169
+ this.translateX = 0;
1170
+ this.translateY = 0;
1171
+ this.layoutWidth = treemap.render.canvas.width;
1172
+ this.layoutHeight = treemap.render.canvas.height;
1173
+ this.isDragging = false;
1174
+ this.triggerZoom = false;
1175
+ this.draggingState = { x: 0, y: 0 };
1176
+ });
1177
+ }
1025
1178
  }
1026
1179
  function estimateZoomingArea(node, root, w, h) {
1027
- const defaultSizes = [
1028
- w,
1029
- h,
1030
- 1
1031
- ];
1032
- if (root === node) {
1033
- return defaultSizes;
1034
- }
1035
- const viewArea = w * h;
1036
- let area = viewArea;
1037
- let parent = node.node.parent;
1038
- let totalWeight = node.node.weight;
1039
- while(parent){
1040
- const siblings = parent.groups || [];
1041
- let siblingWeightSum = 0;
1042
- for (const sibling of siblings){
1043
- siblingWeightSum += sibling.weight;
1044
- }
1045
- area *= siblingWeightSum / totalWeight;
1046
- totalWeight = parent.weight;
1047
- parent = parent.parent;
1048
- }
1049
- const maxScaleFactor = 2.5;
1050
- const minScaleFactor = 0.3;
1051
- const scaleFactor = Math.max(minScaleFactor, Math.min(maxScaleFactor, Math.sqrt(area / viewArea)));
1052
- return [
1053
- w * scaleFactor,
1054
- h * scaleFactor
1055
- ];
1180
+ const defaultSizes = [w, h, 1];
1181
+ if (root === node) {
1182
+ return defaultSizes;
1183
+ }
1184
+ const viewArea = w * h;
1185
+ let area = viewArea;
1186
+ let parent = node.node.parent;
1187
+ let totalWeight = node.node.weight;
1188
+ while (parent) {
1189
+ const siblings = parent.groups || [];
1190
+ let siblingWeightSum = 0;
1191
+ for (const sibling of siblings) {
1192
+ siblingWeightSum += sibling.weight;
1193
+ }
1194
+ area *= siblingWeightSum / totalWeight;
1195
+ totalWeight = parent.weight;
1196
+ parent = parent.parent;
1197
+ }
1198
+ const maxScaleFactor = 2.5;
1199
+ const minScaleFactor = 0.3;
1200
+ const scaleFactor = Math.max(minScaleFactor, Math.min(maxScaleFactor, Math.sqrt(area / viewArea)));
1201
+ return [w * scaleFactor, h * scaleFactor];
1056
1202
  }
1057
- function applyGraphTransform(graphs, translateX, translateY, scale) {
1058
- etoile.traverse(graphs, (graph)=>{
1059
- graph.x = graph.x * scale + translateX;
1060
- graph.y = graph.y * scale + translateY;
1061
- graph.scaleX = scale;
1062
- graph.scaleY = scale;
1063
- });
1203
+ function stackMatrixTransform(graph, e, f, scale) {
1204
+ graph.x = graph.x * scale + e;
1205
+ graph.y = graph.y * scale + f;
1206
+ graph.scaleX = scale;
1207
+ graph.scaleY = scale;
1208
+ }
1209
+ function stackMatrixTransformWithGraphAndLayer(graphs, e, f, scale) {
1210
+ etoile.traverse(graphs, (graph) => stackMatrixTransform(graph, e, f, scale));
1064
1211
  }
1065
1212
  function onZoom(ctx, node, root) {
1066
- if (!node) return;
1067
- const { treemap, self } = ctx;
1068
- let isAnimating = false;
1069
- const c = treemap.render.canvas;
1070
- const boundingClientRect = c.getBoundingClientRect();
1071
- const [w, h] = estimateZoomingArea(node, root, boundingClientRect.width, boundingClientRect.height);
1072
- resetLayout(treemap, w, h);
1073
- const module = findRelativeNodeById(node.node.id, treemap.layoutNodes);
1074
- if (module) {
1075
- const [mx, my, mw, mh] = module.layout;
1076
- const scale = Math.min(boundingClientRect.width / mw, boundingClientRect.height / mh);
1077
- const translateX = boundingClientRect.width / 2 - (mx + mw / 2) * scale;
1078
- const translateY = boundingClientRect.height / 2 - (my + mh / 2) * scale;
1079
- const initialScale = self.scaleRatio;
1080
- const initialTranslateX = self.translateX;
1081
- const initialTranslateY = self.translateY;
1082
- const startTime = Date.now();
1083
- const animationDuration = 300;
1084
- const draw = ()=>{
1085
- const elapsed = Date.now() - startTime;
1086
- const progress = Math.min(elapsed / animationDuration, 1);
1087
- const easedProgress = easing.cubicInOut(progress);
1088
- const scaleRatio = initialScale + (scale - initialScale) * easedProgress;
1089
- self.translateX = initialTranslateX + (translateX - initialTranslateX) * easedProgress;
1090
- self.translateY = initialTranslateY + (translateY - initialTranslateY) * easedProgress;
1091
- self.scaleRatio = scaleRatio;
1092
- treemap.reset();
1093
- applyGraphTransform(treemap.elements, self.translateX, self.translateY, scaleRatio);
1094
- treemap.update();
1095
- if (progress < 1) {
1096
- window.requestAnimationFrame(draw);
1097
- } else {
1098
- self.layoutWidth = w;
1099
- self.layoutHeight = h;
1100
- isAnimating = false;
1101
- }
1102
- };
1103
- if (!isAnimating) {
1104
- isAnimating = true;
1105
- window.requestAnimationFrame(draw);
1106
- }
1107
- }
1108
- root = node;
1213
+ if (!node) return;
1214
+ const { treemap, self } = ctx;
1215
+ self.forceDestroy = true;
1216
+ const c = treemap.render.canvas;
1217
+ const boundingClientRect = c.getBoundingClientRect();
1218
+ const [w, h] = estimateZoomingArea(node, root, boundingClientRect.width, boundingClientRect.height);
1219
+ resetLayout(treemap, w, h);
1220
+ const module = findRelativeNodeById(node.node.id, treemap.layoutNodes);
1221
+ if (module) {
1222
+ const [mx, my, mw, mh] = module.layout;
1223
+ const scale = Math.min(boundingClientRect.width / mw, boundingClientRect.height / mh);
1224
+ const translateX = boundingClientRect.width / 2 - (mx + mw / 2) * scale;
1225
+ const translateY = boundingClientRect.height / 2 - (my + mh / 2) * scale;
1226
+ const initialScale = self.scaleRatio;
1227
+ const initialTranslateX = self.translateX;
1228
+ const initialTranslateY = self.translateY;
1229
+ const startTime = Date.now();
1230
+ const animationDuration = 300;
1231
+ if (self.layoutHeight !== w || self.layoutHeight !== h) {
1232
+ delete treemap.fontsCaches[module.node.id];
1233
+ delete treemap.ellispsisWidthCache[module.node.id];
1234
+ }
1235
+ const { run, stop } = createEffectScope();
1236
+ run(() => {
1237
+ const elapsed = Date.now() - startTime;
1238
+ const progress = Math.min(elapsed / animationDuration, 1);
1239
+ treemap.backgroundLayer.__refresh__ = false;
1240
+ if (progress >= 1) {
1241
+ stop();
1242
+ self.layoutWidth = w;
1243
+ self.layoutHeight = h;
1244
+ self.forceDestroy = false;
1245
+ self.triggerZoom = true;
1246
+ return true;
1247
+ }
1248
+ const easedProgress = easing.cubicInOut(progress);
1249
+ const scaleRatio = initialScale + (scale - initialScale) * easedProgress;
1250
+ self.translateX = initialTranslateX + (translateX - initialTranslateX) * easedProgress;
1251
+ self.translateY = initialTranslateY + (translateY - initialTranslateY) * easedProgress;
1252
+ self.scaleRatio = scaleRatio;
1253
+ treemap.reset();
1254
+ stackMatrixTransformWithGraphAndLayer(treemap.elements, self.translateX, self.translateY, scaleRatio);
1255
+ treemap.update();
1256
+ });
1257
+ }
1258
+ root = node;
1109
1259
  }
1110
- // Only works for mouseup and mousedown events
1111
1260
  function isScrollWheelOrRightButtonOnMouseupAndDown(e) {
1112
- return e.which === 2 || e.which === 3;
1261
+ return e.which === 2 || e.which === 3;
1262
+ }
1263
+ function createHighlight() {
1264
+ let s = null;
1265
+ const setDisplayLayerForHighlight = (layer = "-1") => {
1266
+ if (!s) return;
1267
+ const c = s.render.canvas;
1268
+ c.style.zIndex = layer;
1269
+ };
1270
+ const init = (w, h, root) => {
1271
+ if (!s) {
1272
+ s = new Schedule(root, { width: w, height: h });
1273
+ }
1274
+ setDisplayLayerForHighlight();
1275
+ s.render.canvas.style.position = "absolute";
1276
+ s.render.canvas.style.pointerEvents = "none";
1277
+ };
1278
+ const reset = () => {
1279
+ if (!s) return;
1280
+ s.destory();
1281
+ s.update();
1282
+ };
1283
+ return {
1284
+ init,
1285
+ reset,
1286
+ setDisplayLayerForHighlight,
1287
+ get highlight() {
1288
+ return s;
1289
+ }
1290
+ };
1291
+ }
1292
+ function refreshBackgroundLayer(c) {
1293
+ const { treemap } = c;
1294
+ const { backgroundLayer } = treemap;
1295
+ backgroundLayer.__refresh__ = false;
1113
1296
  }
1114
1297
 
1298
+ var __defProp = Object.defineProperty;
1299
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1300
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
1115
1301
  const defaultRegistries = [
1116
- registerModuleForSchedule(new SelfEvent())
1302
+ registerModuleForSchedule(new SelfEvent())
1117
1303
  ];
1118
- function charCodeWidth(c, ch) {
1119
- return c.measureText(String.fromCharCode(ch)).width;
1120
- }
1121
- function evaluateOptimalFontSize(c, text, width, fontRange, fontFamily, height) {
1122
- height = Math.floor(height);
1123
- let optimalFontSize = fontRange.min;
1124
- for(let fontSize = fontRange.min; fontSize <= fontRange.max; fontSize++){
1125
- c.font = `${fontSize}px ${fontFamily}`;
1126
- let textWidth = 0;
1127
- const textHeight = fontSize;
1128
- let i = 0;
1129
- while(i < text.length){
1130
- const codePointWidth = charCodeWidth(c, text.charCodeAt(i));
1131
- textWidth += codePointWidth;
1132
- i++;
1133
- }
1134
- if (textWidth >= width) {
1135
- const overflow = textWidth - width;
1136
- const ratio = overflow / textWidth;
1137
- const newFontSize = Math.abs(Math.floor(fontSize - fontSize * ratio));
1138
- optimalFontSize = newFontSize || fontRange.min;
1139
- break;
1140
- }
1141
- if (textHeight >= height) {
1142
- const overflow = textHeight - height;
1143
- const ratio = overflow / textHeight;
1144
- const newFontSize = Math.abs(Math.floor(fontSize - fontSize * ratio));
1145
- optimalFontSize = newFontSize || fontRange.min;
1146
- break;
1147
- }
1148
- optimalFontSize = fontSize;
1149
- }
1150
- return optimalFontSize;
1304
+ function measureTextWidth(c, text) {
1305
+ return c.measureText(text).width;
1151
1306
  }
1152
- function getSafeText(c, text, width) {
1153
- const ellipsisWidth = c.measureText('...').width;
1154
- if (width < ellipsisWidth) {
1155
- return false;
1156
- }
1157
- let textWidth = 0;
1158
- let i = 0;
1159
- while(i < text.length){
1160
- const codePointWidth = charCodeWidth(c, text.charCodeAt(i));
1161
- textWidth += codePointWidth;
1162
- i++;
1163
- }
1164
- if (textWidth < width) {
1165
- return {
1166
- text,
1167
- width: textWidth
1168
- };
1307
+ function evaluateOptimalFontSize(c, text, font, desiredW, desiredH) {
1308
+ desiredW = Math.floor(desiredW);
1309
+ desiredH = Math.floor(desiredH);
1310
+ const { range, family } = font;
1311
+ let min = range.min;
1312
+ let max = range.max;
1313
+ const cache = /* @__PURE__ */ new Map();
1314
+ while (max - min >= 1) {
1315
+ const current = min + (max - min) / 2;
1316
+ if (!cache.has(current)) {
1317
+ c.font = `${current}px ${family}`;
1318
+ const metrics = c.measureText(text);
1319
+ const width2 = metrics.width;
1320
+ const height2 = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent;
1321
+ cache.set(current, { width: width2, height: height2 });
1322
+ }
1323
+ const { width, height } = cache.get(current);
1324
+ if (width > desiredW || height > desiredH) {
1325
+ max = current;
1326
+ } else {
1327
+ min = current;
1169
1328
  }
1170
- return {
1171
- text: '...',
1172
- width: ellipsisWidth
1173
- };
1329
+ }
1330
+ return Math.floor(min);
1174
1331
  }
1175
- function createFillBlock(color, x, y, width, height) {
1176
- return new Rect({
1177
- width,
1178
- height,
1179
- x,
1180
- y,
1181
- style: {
1182
- fill: color,
1183
- opacity: 1
1184
- }
1185
- });
1186
- }
1187
- function createTitleText(text, x, y, font, color) {
1188
- return new Text({
1189
- text,
1190
- x,
1191
- y,
1192
- style: {
1193
- fill: color,
1194
- textAlign: 'center',
1195
- baseline: 'middle',
1196
- font,
1197
- lineWidth: 1
1198
- }
1199
- });
1332
+ function getSafeText(c, text, width, cache) {
1333
+ let ellipsisWidth = 0;
1334
+ if (text in cache) {
1335
+ ellipsisWidth = cache[text];
1336
+ } else {
1337
+ ellipsisWidth = measureTextWidth(c, "...");
1338
+ cache[text] = ellipsisWidth;
1339
+ }
1340
+ if (width < ellipsisWidth) {
1341
+ return false;
1342
+ }
1343
+ const textWidth = measureTextWidth(c, text);
1344
+ if (textWidth < width) {
1345
+ return { text, width: textWidth };
1346
+ }
1347
+ return { text: "...", width: ellipsisWidth };
1200
1348
  }
1201
1349
  function resetLayout(treemap, w, h) {
1202
- treemap.layoutNodes = squarify(treemap.data, {
1203
- w,
1204
- h,
1205
- x: 0,
1206
- y: 0
1207
- }, treemap.decorator.layout);
1208
- treemap.reset();
1350
+ treemap.layoutNodes = squarify(treemap.data, { w, h, x: 0, y: 0 }, treemap.decorator.layout);
1351
+ treemap.reset(true);
1209
1352
  }
1210
- // https://www.typescriptlang.org/docs/handbook/mixins.html
1211
- class Schedule extends etoile.Schedule {
1212
- }
1213
- class TreemapLayout extends Schedule {
1214
- data;
1215
- layoutNodes;
1216
- decorator;
1217
- bgBox;
1218
- fgBox;
1219
- constructor(...args){
1220
- super(...args);
1221
- this.data = [];
1222
- this.layoutNodes = [];
1223
- this.bgBox = new Box();
1224
- this.fgBox = new Box();
1225
- this.decorator = Object.create(null);
1226
- }
1227
- drawBackgroundNode(node) {
1228
- for (const child of node.children){
1229
- this.drawBackgroundNode(child);
1230
- }
1231
- const [x, y, w, h] = node.layout;
1232
- const { rectGap, titleHeight } = node.decorator;
1233
- const fill = this.decorator.color.mappings[node.node.id];
1234
- if (node.children.length) {
1235
- const box = new Box();
1236
- box.add(createFillBlock(fill, x, y, w, titleHeight), createFillBlock(fill, x, y + h - rectGap, w, rectGap), createFillBlock(fill, x, y + titleHeight, rectGap, h - titleHeight - rectGap), createFillBlock(fill, x + w - rectGap, y + titleHeight, rectGap, h - titleHeight - rectGap));
1237
- this.bgBox.add(box);
1238
- } else {
1239
- this.bgBox.add(createFillBlock(fill, x, y, w, h));
1240
- }
1241
- }
1242
- drawForegroundNode(node) {
1243
- for (const child of node.children){
1244
- this.drawForegroundNode(child);
1245
- }
1246
- const [x, y, w, h] = node.layout;
1247
- const { rectBorderWidth, titleHeight, rectGap } = node.decorator;
1248
- const { fontSize, fontFamily, color } = this.decorator.font;
1249
- const rect = new Rect({
1250
- x: x + 0.5,
1251
- y: y + 0.5,
1252
- width: w,
1253
- height: h,
1254
- style: {
1255
- stroke: '#222',
1256
- lineWidth: rectBorderWidth
1257
- }
1258
- });
1259
- this.fgBox.add(rect);
1260
- this.render.ctx.textBaseline = 'middle';
1261
- const optimalFontSize = evaluateOptimalFontSize(this.render.ctx, node.node.label, w - rectGap * 2, fontSize, fontFamily, node.children.length ? Math.round(titleHeight / 2) + rectGap : h);
1262
- this.render.ctx.font = `${optimalFontSize}px ${fontFamily}`;
1263
- if (h > titleHeight) {
1264
- const result = getSafeText(this.render.ctx, node.node.label, w - rectGap * 2);
1265
- if (!result) return;
1266
- const { text, width } = result;
1267
- const textX = x + Math.round((w - width) / 2);
1268
- let textY = y + Math.round(h / 2);
1269
- if (node.children.length) {
1270
- textY = y + Math.round(titleHeight / 2);
1271
- }
1272
- this.fgBox.add(createTitleText(text, textX, textY, `${optimalFontSize}px ${fontFamily}`, color));
1273
- } else {
1274
- const ellipsisWidth = 3 * charCodeWidth(this.render.ctx, 46);
1275
- const textX = x + Math.round((w - ellipsisWidth) / 2);
1276
- const textY = y + Math.round(h / 2);
1277
- this.fgBox.add(createTitleText('...', textX, textY, `${optimalFontSize}px ${fontFamily}`, color));
1278
- }
1279
- }
1280
- reset() {
1281
- this.bgBox.destory();
1282
- this.fgBox.destory();
1283
- this.remove(this.bgBox, this.fgBox);
1284
- for (const node of this.layoutNodes){
1285
- this.drawBackgroundNode(node);
1286
- this.drawForegroundNode(node);
1287
- }
1288
- this.add(this.bgBox, this.fgBox);
1289
- }
1290
- get api() {
1291
- return {
1292
- zoom: (node)=>{
1293
- this.event.emit('zoom', node);
1294
- }
1295
- };
1353
+ class TreemapLayout extends etoile.Schedule {
1354
+ constructor(...args) {
1355
+ super(...args);
1356
+ __publicField(this, "data");
1357
+ __publicField(this, "layoutNodes");
1358
+ __publicField(this, "decorator");
1359
+ __publicField(this, "bgLayer");
1360
+ __publicField(this, "fgBox");
1361
+ __publicField(this, "fontsCaches");
1362
+ __publicField(this, "ellispsisWidthCache");
1363
+ this.data = [];
1364
+ this.layoutNodes = [];
1365
+ this.bgLayer = new Layer();
1366
+ this.fgBox = new Box();
1367
+ this.decorator = /* @__PURE__ */ Object.create(null);
1368
+ this.fontsCaches = /* @__PURE__ */ Object.create(null);
1369
+ this.ellispsisWidthCache = /* @__PURE__ */ Object.create(null);
1370
+ this.bgLayer.setCanvasOptions(this.render.options);
1371
+ }
1372
+ drawBackgroundNode(node) {
1373
+ const [x, y, w, h] = node.layout;
1374
+ const fill = this.decorator.color.mappings[node.node.id];
1375
+ const s = createFillBlock(x, y, w, h, { fill });
1376
+ this.bgLayer.add(s);
1377
+ for (const child of node.children) {
1378
+ this.drawBackgroundNode(child);
1379
+ }
1380
+ }
1381
+ drawForegroundNode(node) {
1382
+ const [x, y, w, h] = node.layout;
1383
+ if (!w || !h) return;
1384
+ const { rectBorderWidth, titleHeight, rectGap } = node.decorator;
1385
+ const { fontSize, fontFamily, color } = this.decorator.font;
1386
+ this.fgBox.add(createFillBlock(x + 0.5, y + 0.5, w, h, { stroke: "#222", lineWidth: rectBorderWidth }));
1387
+ let optimalFontSize;
1388
+ if (node.node.id in this.fontsCaches) {
1389
+ optimalFontSize = this.fontsCaches[node.node.id];
1390
+ } else {
1391
+ optimalFontSize = evaluateOptimalFontSize(
1392
+ this.render.ctx,
1393
+ node.node.label,
1394
+ {
1395
+ range: fontSize,
1396
+ family: fontFamily
1397
+ },
1398
+ w - rectGap * 2,
1399
+ node.children.length ? Math.round(titleHeight / 2) + rectGap : h
1400
+ );
1401
+ this.fontsCaches[node.node.id] = optimalFontSize;
1402
+ }
1403
+ this.render.ctx.font = `${optimalFontSize}px ${fontFamily}`;
1404
+ const result = getSafeText(this.render.ctx, node.node.label, w - rectGap * 2, this.ellispsisWidthCache);
1405
+ if (!result) return;
1406
+ if (result.width >= w || optimalFontSize >= h) return;
1407
+ const { text, width } = result;
1408
+ const textX = x + Math.round((w - width) / 2);
1409
+ const textY = y + (node.children.length ? Math.round(titleHeight / 2) : Math.round(h / 2));
1410
+ this.fgBox.add(createTitleText(text, textX, textY, `${optimalFontSize}px ${fontFamily}`, color));
1411
+ for (const child of node.children) {
1412
+ this.drawForegroundNode(child);
1413
+ }
1414
+ }
1415
+ reset(refresh = false) {
1416
+ this.remove(this.bgLayer, this.fgBox);
1417
+ if (!this.bgLayer.__refresh__) {
1418
+ this.bgLayer.destory();
1419
+ for (const node of this.layoutNodes) {
1420
+ this.drawBackgroundNode(node);
1421
+ }
1422
+ }
1423
+ if (!this.fgBox.elements.length || refresh) {
1424
+ this.render.ctx.textBaseline = "middle";
1425
+ this.fgBox.destory();
1426
+ for (const node of this.layoutNodes) {
1427
+ this.drawForegroundNode(node);
1428
+ }
1429
+ } else {
1430
+ this.fgBox = this.fgBox.clone();
1296
1431
  }
1432
+ this.add(this.bgLayer, this.fgBox);
1433
+ }
1434
+ get api() {
1435
+ return {
1436
+ zoom: (node) => {
1437
+ this.event.emit("zoom", node);
1438
+ }
1439
+ };
1440
+ }
1441
+ get backgroundLayer() {
1442
+ return this.bgLayer;
1443
+ }
1297
1444
  }
1298
1445
  function createTreemap() {
1299
- let treemap = null;
1300
- let root = null;
1301
- let installed = false;
1302
- const uses = [];
1303
- const context = {
1304
- init,
1305
- dispose,
1306
- setOptions,
1307
- resize,
1308
- use,
1309
- zoom
1310
- };
1311
- function init(el) {
1312
- treemap = new TreemapLayout(el);
1313
- root = el;
1314
- }
1315
- function dispose() {
1316
- if (root && treemap) {
1317
- treemap.destory();
1318
- root.removeChild(root.firstChild);
1319
- root = null;
1320
- treemap = null;
1321
- }
1322
- }
1323
- function resize() {
1324
- if (!treemap || !root) return;
1325
- const { width, height } = root.getBoundingClientRect();
1326
- treemap.render.initOptions({
1327
- height,
1328
- width,
1329
- devicePixelRatio: window.devicePixelRatio
1330
- });
1331
- treemap.event.emit('cleanup:selfevent');
1332
- resetLayout(treemap, width, height);
1333
- treemap.update();
1334
- }
1335
- function setOptions(options) {
1336
- if (!treemap) {
1337
- throw new Error('Treemap not initialized');
1338
- }
1339
- treemap.data = bindParentForModule(options.data || []);
1340
- if (!installed) {
1341
- for (const registry of defaultRegistries){
1342
- registry(context, treemap, treemap.render);
1343
- }
1344
- installed = true;
1345
- }
1346
- for (const use of uses){
1347
- use(treemap);
1348
- }
1349
- resize();
1350
- }
1351
- function use(key, register) {
1352
- switch(key){
1353
- case 'decorator':
1354
- uses.push((treemap)=>register(treemap));
1355
- break;
1356
- }
1357
- }
1358
- function zoom(id) {
1359
- if (!treemap) {
1360
- throw new Error("treemap don't init.");
1361
- }
1362
- const node = findRelativeNodeById(id, treemap.layoutNodes);
1363
- node && treemap.api.zoom(node);
1364
- }
1365
- return context;
1446
+ let treemap = null;
1447
+ let root = null;
1448
+ let installed = false;
1449
+ const uses = [];
1450
+ const context = {
1451
+ init,
1452
+ dispose,
1453
+ setOptions,
1454
+ resize,
1455
+ use,
1456
+ zoom
1457
+ };
1458
+ function init(el) {
1459
+ treemap = new TreemapLayout(el);
1460
+ root = el;
1461
+ root.style.position = "relative";
1462
+ if (!installed) {
1463
+ for (const registry of defaultRegistries) {
1464
+ registry(context, treemap, treemap.render);
1465
+ }
1466
+ installed = true;
1467
+ }
1468
+ }
1469
+ function dispose() {
1470
+ if (root && treemap) {
1471
+ treemap.destory();
1472
+ root.removeChild(root.firstChild);
1473
+ root = null;
1474
+ treemap = null;
1475
+ }
1476
+ }
1477
+ function resize() {
1478
+ if (!treemap || !root) return;
1479
+ const { width, height } = root.getBoundingClientRect();
1480
+ treemap.backgroundLayer.__refresh__ = false;
1481
+ treemap.render.initOptions({ height, width, devicePixelRatio: window.devicePixelRatio });
1482
+ treemap.render.canvas.style.position = "absolute";
1483
+ treemap.backgroundLayer.setCanvasOptions(treemap.render.options);
1484
+ treemap.backgroundLayer.initLoc();
1485
+ treemap.backgroundLayer.matrix = treemap.backgroundLayer.matrix.create({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 });
1486
+ treemap.fontsCaches = /* @__PURE__ */ Object.create(null);
1487
+ treemap.event.emit("cleanup:selfevent");
1488
+ treemap.event.emit("onload:selfevent", { width, height, root });
1489
+ resetLayout(treemap, width, height);
1490
+ treemap.update();
1491
+ }
1492
+ function setOptions(options) {
1493
+ if (!treemap) {
1494
+ throw new Error("Treemap not initialized");
1495
+ }
1496
+ treemap.data = bindParentForModule(options.data || []);
1497
+ for (const use2 of uses) {
1498
+ use2(treemap);
1499
+ }
1500
+ resize();
1501
+ }
1502
+ function use(key, register) {
1503
+ switch (key) {
1504
+ case "decorator":
1505
+ uses.push((treemap2) => register(treemap2));
1506
+ break;
1507
+ }
1508
+ }
1509
+ function zoom(id) {
1510
+ if (!treemap) {
1511
+ throw new Error("treemap don't init.");
1512
+ }
1513
+ const node = findRelativeNodeById(id, treemap.layoutNodes);
1514
+ node && treemap.api.zoom(node);
1515
+ }
1516
+ return context;
1366
1517
  }
1367
1518
 
1368
1519
  const defaultLayoutOptions = {
1369
- titleAreaHeight: {
1370
- max: 80,
1371
- min: 20
1372
- },
1373
- rectGap: 5,
1374
- rectBorderRadius: 0.5,
1375
- rectBorderWidth: 1.5
1520
+ titleAreaHeight: {
1521
+ max: 80,
1522
+ min: 20
1523
+ },
1524
+ rectGap: 5,
1525
+ rectBorderRadius: 0.5,
1526
+ rectBorderWidth: 1.5
1376
1527
  };
1377
1528
  const defaultFontOptions = {
1378
- color: '#000',
1379
- fontSize: {
1380
- max: 38,
1381
- min: 7
1382
- },
1383
- fontFamily: 'sans-serif'
1529
+ color: "#000",
1530
+ fontSize: {
1531
+ max: 38,
1532
+ min: 7
1533
+ },
1534
+ fontFamily: "sans-serif"
1384
1535
  };
1385
1536
  function presetDecorator(app) {
1386
- Object.assign(app.decorator, {
1387
- layout: defaultLayoutOptions,
1388
- font: defaultFontOptions,
1389
- color: colorMappings(app)
1390
- });
1537
+ Object.assign(app.decorator, {
1538
+ layout: defaultLayoutOptions,
1539
+ font: defaultFontOptions,
1540
+ color: { mappings: evaluateColorMappings(app.data) }
1541
+ });
1391
1542
  }
1392
- function colorDecorator(node, state) {
1393
- const depth = getNodeDepth(node);
1394
- let baseHue = 0;
1395
- let sweepAngle = Math.PI * 2;
1396
- const totalHueRange = Math.PI;
1397
- if (node.parent) {
1398
- sweepAngle = node.weight / node.parent.weight * sweepAngle;
1399
- baseHue = state.hue + sweepAngle / Math.PI * 180;
1400
- }
1401
- baseHue += sweepAngle;
1402
- const depthHueOffset = depth + totalHueRange / 10;
1403
- const finalHue = baseHue + depthHueOffset / 2;
1404
- const saturation = 0.6 + 0.4 * Math.max(0, Math.cos(finalHue));
1405
- const lightness = 0.5 + 0.2 * Math.max(0, Math.cos(finalHue + Math.PI * 2 / 3));
1406
- state.hue = baseHue;
1407
- return {
1408
- mode: 'hsl',
1409
- desc: {
1410
- h: finalHue,
1411
- s: Math.round(saturation * 100),
1412
- l: Math.round(lightness * 100)
1413
- }
1543
+ function evaluateColorMappings(data) {
1544
+ const colorMappings = {};
1545
+ const hashToHue = (id) => {
1546
+ const hash = Math.abs(hashCode(id));
1547
+ return hash % 360;
1548
+ };
1549
+ const lightScale = (depth) => 70 - depth * 5;
1550
+ const baseSaturation = 40;
1551
+ const siblingHueShift = 20;
1552
+ const rc = 0.2126;
1553
+ const gc = 0.7152;
1554
+ const bc = 0.0722;
1555
+ const hslToRgb = (h, s, l) => {
1556
+ const a = s * Math.min(l, 1 - l);
1557
+ const f = (n) => {
1558
+ const k = (n + h / 30) % 12;
1559
+ return l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
1414
1560
  };
1415
- }
1416
- function evaluateColorMappingByNode(node, state) {
1417
- const colorMappings = {};
1418
- if (node.groups && Array.isArray(node.groups)) {
1419
- for (const child of node.groups){
1420
- Object.assign(colorMappings, evaluateColorMappingByNode(child, state));
1421
- }
1422
- }
1423
- if (node.id) {
1424
- colorMappings[node.id] = colorDecorator(node, state);
1425
- }
1426
- return colorMappings;
1427
- }
1428
- function colorMappings(app) {
1429
- const colorMappings = {};
1430
- const state = {
1431
- hue: 0
1561
+ return { r: f(0), g: f(8), b: f(4) };
1562
+ };
1563
+ const calculateLuminance = (r, g, b) => {
1564
+ return rc * r + gc * g + bc * b;
1565
+ };
1566
+ const calculateColor = (module, depth, parentHue, siblingIndex, totalSiblings) => {
1567
+ const nodeHue = hashToHue(module.id);
1568
+ const hue = parentHue !== null ? (parentHue + siblingHueShift * siblingIndex / totalSiblings) % 360 : nodeHue;
1569
+ const lightness = lightScale(depth);
1570
+ const hslColor = {
1571
+ h: hue,
1572
+ s: baseSaturation,
1573
+ l: lightness / 100
1432
1574
  };
1433
- for (const node of app.data){
1434
- Object.assign(colorMappings, evaluateColorMappingByNode(node, state));
1435
- }
1436
- return {
1437
- mappings: colorMappings
1575
+ const { r, g, b } = hslToRgb(hslColor.h, hslColor.s / 100, hslColor.l);
1576
+ const luminance = calculateLuminance(r, g, b);
1577
+ if (luminance < 0.6) {
1578
+ hslColor.l += 0.2;
1579
+ } else if (luminance > 0.8) {
1580
+ hslColor.l -= 0.1;
1581
+ }
1582
+ hslColor.l *= 100;
1583
+ colorMappings[module.id] = {
1584
+ mode: "hsl",
1585
+ desc: hslColor
1438
1586
  };
1587
+ if (module.groups && Array.isArray(module.groups)) {
1588
+ const totalChildren = module.groups.length;
1589
+ for (let i = 0; i < totalChildren; i++) {
1590
+ const child = module.groups[i];
1591
+ calculateColor(child, depth + 1, hue, i, totalChildren);
1592
+ }
1593
+ }
1594
+ };
1595
+ for (let i = 0; i < data.length; i++) {
1596
+ const module = data[i];
1597
+ calculateColor(module, 0, null, i, data.length);
1598
+ }
1599
+ return colorMappings;
1439
1600
  }
1440
1601
 
1441
1602
  exports.TreemapLayout = TreemapLayout;