squarified 0.3.7 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,472 @@
1
+ import { h as definePlugin, H as smoothFrame, r as createRoundBlock, F as stackMatrixTransform, S as Schedule, K as DEFAULT_MATRIX_LOC, I as isScrollWheelOrRightButtonOnMouseupAndDown, G as stackMatrixTransformWithGraphAndLayer, p as hashCode, P as PI_2, o as isWheelEvent, y as mixinWithParams } from './dom-event-BLJt9knO.mjs';
2
+
3
+ // Currently, etoile is an internal module, so we won't need too much easing functions.
4
+ // And the animation logic is implemented by user code.
5
+ const easing = {
6
+ linear: (k)=>k,
7
+ quadraticIn: (k)=>k * k,
8
+ quadraticOut: (k)=>k * (2 - k),
9
+ quadraticInOut: (k)=>{
10
+ if ((k *= 2) < 1) {
11
+ return 0.5 * k * k;
12
+ }
13
+ return -0.5 * (--k * (k - 2) - 1);
14
+ },
15
+ cubicIn: (k)=>k * k * k,
16
+ cubicOut: (k)=>{
17
+ if ((k *= 2) < 1) {
18
+ return 0.5 * k * k * k;
19
+ }
20
+ return 0.5 * ((k -= 2) * k * k + 2);
21
+ },
22
+ cubicInOut: (k)=>{
23
+ if ((k *= 2) < 1) {
24
+ return 0.5 * k * k * k;
25
+ }
26
+ return 0.5 * ((k -= 2) * k * k + 2);
27
+ }
28
+ };
29
+
30
+ class Highlight extends Schedule {
31
+ reset() {
32
+ this.destory();
33
+ this.update();
34
+ }
35
+ get canvas() {
36
+ return this.render.canvas;
37
+ }
38
+ setZIndexForHighlight(zIndex = '-1') {
39
+ this.canvas.style.zIndex = zIndex;
40
+ }
41
+ init() {
42
+ this.setZIndexForHighlight();
43
+ this.canvas.style.position = 'absolute';
44
+ this.canvas.style.pointerEvents = 'none';
45
+ }
46
+ }
47
+ const ANIMATION_DURATION = 300;
48
+ const HIGH_LIGHT_OPACITY = 0.3;
49
+ const fill = {
50
+ desc: {
51
+ r: 255,
52
+ g: 255,
53
+ b: 255
54
+ },
55
+ mode: 'rgb'
56
+ };
57
+ const presetHighlightPlugin = definePlugin({
58
+ name: 'treemap:preset-highlight',
59
+ onLoad () {
60
+ const meta = this.getPluginMetadata('treemap:preset-highlight');
61
+ if (!meta) {
62
+ return;
63
+ }
64
+ if (!meta.highlight) {
65
+ meta.highlight = new Highlight(this.instance.to);
66
+ }
67
+ },
68
+ onDOMEventTriggered (name, _, module, { stateManager: state, matrix }) {
69
+ if (name === 'mousemove') {
70
+ if (state.canTransition('MOVE')) {
71
+ const meta = this.getPluginMetadata('treemap:preset-highlight');
72
+ if (!module) {
73
+ meta.highlight?.reset();
74
+ meta.highlight?.update();
75
+ meta.highlight?.setZIndexForHighlight();
76
+ return;
77
+ }
78
+ const [x, y, w, h] = module.layout;
79
+ const effectiveRadius = Math.min(module.config.rectRadius, w / 4, h / 4);
80
+ smoothFrame((_, cleanup)=>{
81
+ cleanup();
82
+ meta.highlight?.reset();
83
+ const mask = createRoundBlock(x, y, w, h, {
84
+ fill,
85
+ opacity: HIGH_LIGHT_OPACITY,
86
+ radius: effectiveRadius,
87
+ padding: 0
88
+ });
89
+ meta.highlight?.add(mask);
90
+ meta.highlight?.setZIndexForHighlight('1');
91
+ stackMatrixTransform(mask, matrix.e, matrix.f, matrix.a);
92
+ meta.highlight?.update();
93
+ }, {
94
+ duration: ANIMATION_DURATION
95
+ });
96
+ }
97
+ }
98
+ },
99
+ onResize () {
100
+ const meta = this.getPluginMetadata('treemap:preset-highlight');
101
+ if (!meta) {
102
+ return;
103
+ }
104
+ meta.highlight?.render.initOptions({
105
+ ...this.instance.render.options
106
+ });
107
+ meta.highlight?.reset();
108
+ meta.highlight?.init();
109
+ },
110
+ onDispose () {
111
+ const meta = this.getPluginMetadata('treemap:preset-highlight');
112
+ if (meta && meta.highlight) {
113
+ meta.highlight.destory();
114
+ meta.highlight = null;
115
+ }
116
+ },
117
+ meta: {
118
+ highlight: null
119
+ }
120
+ });
121
+
122
+ const presetDragElementPlugin = definePlugin({
123
+ name: 'treemap:preset-drag-element',
124
+ onDOMEventTriggered (name, event, module, domEvent) {
125
+ const { stateManager: state, matrix, component } = domEvent;
126
+ switch(name){
127
+ case 'mousemove':
128
+ {
129
+ if (state.isInState('DRAGGING')) {
130
+ domEvent.silent('click');
131
+ } else {
132
+ domEvent.active('click');
133
+ }
134
+ const meta = getDragOptions.call(this);
135
+ if (!meta) {
136
+ return;
137
+ }
138
+ if (meta.dragOptions.x === 0 && meta.dragOptions.y === 0) {
139
+ state.transition('IDLE');
140
+ return;
141
+ }
142
+ state.transition('DRAGGING');
143
+ if (state.isInState('DRAGGING')) {
144
+ const highlight = getHighlightInstance.call(this);
145
+ smoothFrame((_, cleanup)=>{
146
+ cleanup();
147
+ const { offsetX, offsetY } = event.native;
148
+ const drawX = offsetX - meta.dragOptions.x;
149
+ const drawY = offsetY - meta.dragOptions.y;
150
+ const lastX = meta.dragOptions.x;
151
+ const lastY = meta.dragOptions.y;
152
+ if (highlight?.highlight) {
153
+ highlight.highlight.reset();
154
+ highlight.highlight.setZIndexForHighlight();
155
+ }
156
+ matrix.translation(drawX, drawY);
157
+ meta.dragOptions.x = offsetX;
158
+ meta.dragOptions.y = offsetY;
159
+ meta.dragOptions.lastX = lastX;
160
+ meta.dragOptions.lastY = lastY;
161
+ component.cleanup();
162
+ component.draw(false, false);
163
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, matrix.a);
164
+ component.update();
165
+ return true;
166
+ }, {
167
+ duration: ANIMATION_DURATION,
168
+ deps: [
169
+ ()=>state.isInState('IDLE')
170
+ ]
171
+ });
172
+ }
173
+ break;
174
+ }
175
+ case 'mouseup':
176
+ {
177
+ if (state.isInState('PRESSED')) {
178
+ const meta = getDragOptions.call(this);
179
+ if (meta && meta.dragOptions) {
180
+ if (meta.dragOptions.x === meta.dragOptions.lastX && meta.dragOptions.y === meta.dragOptions.lastY) {
181
+ state.transition('IDLE');
182
+ return;
183
+ }
184
+ }
185
+ }
186
+ if (state.isInState('DRAGGING') && state.canTransition('IDLE')) {
187
+ const highlight = getHighlightInstance.call(this);
188
+ if (highlight && highlight.highlight) {
189
+ highlight.highlight.reset();
190
+ highlight.highlight.setZIndexForHighlight();
191
+ }
192
+ const meta = getDragOptions.call(this);
193
+ if (meta && meta.dragOptions) {
194
+ meta.dragOptions.x = 0;
195
+ meta.dragOptions.y = 0;
196
+ meta.dragOptions.lastX = 0;
197
+ meta.dragOptions.lastY = 0;
198
+ state.transition('IDLE');
199
+ }
200
+ }
201
+ break;
202
+ }
203
+ case 'mousedown':
204
+ {
205
+ if (isScrollWheelOrRightButtonOnMouseupAndDown(event.native)) {
206
+ return;
207
+ }
208
+ const meta = getDragOptions.call(this);
209
+ if (!meta) {
210
+ return;
211
+ }
212
+ meta.dragOptions.x = event.native.offsetX;
213
+ meta.dragOptions.y = event.native.offsetY;
214
+ meta.dragOptions.lastX = event.native.offsetX;
215
+ meta.dragOptions.lastY = event.native.offsetY;
216
+ state.transition('PRESSED');
217
+ break;
218
+ }
219
+ }
220
+ },
221
+ meta: {
222
+ dragOptions: {
223
+ x: 0,
224
+ y: 0,
225
+ lastX: 0,
226
+ lastY: 0
227
+ }
228
+ },
229
+ onResize ({ matrix, stateManager: state }) {
230
+ matrix.create(DEFAULT_MATRIX_LOC);
231
+ state.reset();
232
+ }
233
+ });
234
+ function getHighlightInstance() {
235
+ return this.getPluginMetadata('treemap:preset-highlight');
236
+ }
237
+ function getDragOptions() {
238
+ const meta = this.getPluginMetadata('treemap:preset-drag-element');
239
+ return meta;
240
+ }
241
+
242
+ const presetColorPlugin = definePlugin({
243
+ name: 'treemap:preset-color',
244
+ onModuleInit (modules) {
245
+ const colorMappings = {};
246
+ for(let i = 0; i < modules.length; i++){
247
+ const module = modules[i];
248
+ assignColorMappings(colorMappings, module, Math.abs(hashCode(module.node.id)) % PI_2, 0);
249
+ }
250
+ return {
251
+ colorMappings
252
+ };
253
+ }
254
+ });
255
+ function assignColorMappings(colorMappings, module, ancestorHue, depth) {
256
+ const hueOffset = Math.abs(hashCode(module.node.id)) % 60 - 30;
257
+ const hue = (ancestorHue + hueOffset) % 360;
258
+ const saturation = Math.max(75 - depth * 5, 40);
259
+ const baseLightness = 55 - depth * 3;
260
+ const color = adjustColorToComfortableForHumanEye(hue, saturation, baseLightness);
261
+ colorMappings[module.node.id] = color;
262
+ if (module.node.isCombinedNode && module.node.originalNodes) {
263
+ for (const combined of module.node.originalNodes){
264
+ colorMappings[combined.id] = color;
265
+ }
266
+ }
267
+ if (module.children && module.children.length) {
268
+ const childCount = module.children.length;
269
+ for(let i = 0; i < childCount; i++){
270
+ const childHueOffset = 40 * i / childCount;
271
+ const childHue = (hue + childHueOffset) % 360;
272
+ assignColorMappings(colorMappings, module.children[i], childHue, depth + 1);
273
+ }
274
+ }
275
+ }
276
+ function adjustColorToComfortableForHumanEye(hue, saturation, lightness) {
277
+ hue = (hue % 360 + 360) % 360;
278
+ saturation = Math.min(Math.max(saturation, 40), 85);
279
+ lightness = Math.min(Math.max(lightness, 35), 75);
280
+ if (hue >= 60 && hue <= 180) {
281
+ saturation = Math.max(saturation - 10, 40);
282
+ lightness = Math.min(lightness + 5, 75);
283
+ } else if (hue >= 200 && hue <= 280) {
284
+ lightness = Math.min(lightness + 8, 75);
285
+ } else if (hue >= 0 && hue <= 30) {
286
+ saturation = Math.max(saturation - 5, 40);
287
+ }
288
+ return {
289
+ mode: 'hsl',
290
+ desc: {
291
+ h: hue,
292
+ s: saturation,
293
+ l: lightness
294
+ }
295
+ };
296
+ }
297
+
298
+ function presetScalePlugin(options) {
299
+ return definePlugin({
300
+ name: 'treemap:preset-scale',
301
+ onDOMEventTriggered (_, event, module, evt) {
302
+ if (isWheelEvent(event)) {
303
+ onWheel(this, event, evt);
304
+ }
305
+ },
306
+ meta: {
307
+ scaleOptions: {
308
+ scale: 1,
309
+ minScale: options?.min || 0.1,
310
+ maxScale: options?.max || Infinity,
311
+ scaleFactor: 0.05
312
+ }
313
+ },
314
+ onResize ({ matrix, stateManager: state }) {
315
+ const meta = getScaleOptions.call(this);
316
+ if (meta) {
317
+ meta.scaleOptions.scale = 1;
318
+ }
319
+ matrix.create(DEFAULT_MATRIX_LOC);
320
+ state.reset();
321
+ }
322
+ });
323
+ }
324
+ function getScaleOptions() {
325
+ const meta = this.getPluginMetadata('treemap:preset-scale');
326
+ return meta;
327
+ }
328
+ function onWheel(pluginContext, event, { stateManager: state, component, matrix }) {
329
+ event.native.preventDefault();
330
+ const meta = getScaleOptions.call(pluginContext);
331
+ if (!meta) {
332
+ return;
333
+ }
334
+ const { scale, minScale, maxScale, scaleFactor } = meta.scaleOptions;
335
+ const delta = event.native.deltaY < 0 ? scaleFactor : -scaleFactor;
336
+ const newScale = Math.max(minScale, Math.min(maxScale, scale + delta));
337
+ if (newScale === scale) {
338
+ return;
339
+ }
340
+ state.transition('SCALING');
341
+ const mouseX = event.native.offsetX;
342
+ const mouseY = event.native.offsetY;
343
+ const scaleDiff = newScale / scale;
344
+ meta.scaleOptions.scale = newScale;
345
+ const highlight = getHighlightInstance.apply(pluginContext);
346
+ smoothFrame((_, cleanup)=>{
347
+ cleanup();
348
+ if (highlight && highlight.highlight) {
349
+ highlight.highlight.reset();
350
+ highlight.highlight.setZIndexForHighlight();
351
+ }
352
+ matrix.scale(scaleDiff, scaleDiff);
353
+ matrix.e = mouseX - (mouseX - matrix.e) * scaleDiff;
354
+ matrix.f = mouseY - (mouseY - matrix.f) * scaleDiff;
355
+ component.cleanup();
356
+ const { width, height } = component.render.options;
357
+ component.layoutNodes = component.calculateLayoutNodes(component.data, {
358
+ w: width,
359
+ h: height,
360
+ x: 0,
361
+ y: 0
362
+ }, matrix.a);
363
+ component.draw(true, false);
364
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, newScale);
365
+ component.update();
366
+ if (state.canTransition('IDLE')) {
367
+ state.transition('IDLE');
368
+ }
369
+ return true;
370
+ }, {
371
+ duration: ANIMATION_DURATION
372
+ });
373
+ }
374
+
375
+ const MAX_SCALE_MULTIPLIER = 2.0;
376
+ const presetZoomablePlugin = definePlugin({
377
+ name: 'treemap:preset-zoomable',
378
+ onLoad (treemap, { stateManager: state, matrix }) {
379
+ return mixinWithParams(treemap, [
380
+ {
381
+ name: 'zoom',
382
+ fn: ()=>(id)=>{
383
+ const meta = this.getPluginMetadata('treemap:preset-zoomable');
384
+ if (!meta || state.isInState('ZOOMING')) {
385
+ return;
386
+ }
387
+ const targetModule = this.resolveModuleById(id);
388
+ if (!targetModule) {
389
+ return;
390
+ }
391
+ meta.previousMatrixState = {
392
+ e: matrix.e,
393
+ f: matrix.f,
394
+ a: matrix.a,
395
+ d: matrix.d
396
+ };
397
+ const component = this.instance;
398
+ state.transition('ZOOMING');
399
+ const [nodeX, nodeY, nodeW, nodeH] = targetModule.layout;
400
+ const { width, height } = component.render.options;
401
+ const currentScale = matrix.a;
402
+ const scaleX = width / nodeW;
403
+ const scaleY = height / nodeH;
404
+ const fullScale = Math.min(scaleX, scaleY) * 0.9;
405
+ const targetScale = Math.min(fullScale, currentScale * MAX_SCALE_MULTIPLIER);
406
+ const scale = targetScale;
407
+ const nodeCenterX = nodeX + nodeW / 2;
408
+ const nodeCenterY = nodeY + nodeH / 2;
409
+ const viewCenterX = width / 2;
410
+ const viewCenterY = height / 2;
411
+ const targetE = viewCenterX - nodeCenterX * scale;
412
+ const targetF = viewCenterY - nodeCenterY * scale;
413
+ const scaleMeta = getScaleOptions.call(this);
414
+ if (scaleMeta) {
415
+ scaleMeta.scaleOptions.scale = scale;
416
+ }
417
+ const highlight = getHighlightInstance.call(this);
418
+ const dragMeta = getDragOptions.call(this);
419
+ if (dragMeta) {
420
+ dragMeta.dragOptions.x = 0;
421
+ dragMeta.dragOptions.y = 0;
422
+ dragMeta.dragOptions.lastX = 0;
423
+ dragMeta.dragOptions.lastY = 0;
424
+ }
425
+ const startMatrix = {
426
+ e: matrix.e,
427
+ f: matrix.f,
428
+ a: matrix.a,
429
+ d: matrix.d
430
+ };
431
+ smoothFrame((progress)=>{
432
+ const easedProgress = easing.cubicInOut(progress);
433
+ const currentE = startMatrix.e + (targetE - startMatrix.e) * easedProgress;
434
+ const currentF = startMatrix.f + (targetF - startMatrix.f) * easedProgress;
435
+ const currentA = startMatrix.a + (scale - startMatrix.a) * easedProgress;
436
+ const currentD = startMatrix.d + (scale - startMatrix.d) * easedProgress;
437
+ matrix.create(DEFAULT_MATRIX_LOC);
438
+ matrix.e = currentE;
439
+ matrix.f = currentF;
440
+ matrix.a = currentA;
441
+ matrix.d = currentD;
442
+ if (highlight?.highlight) {
443
+ highlight.highlight.reset();
444
+ highlight.highlight.setZIndexForHighlight();
445
+ }
446
+ component.cleanup();
447
+ const { width, height } = component.render.options;
448
+ component.layoutNodes = component.calculateLayoutNodes(component.data, {
449
+ w: width,
450
+ h: height,
451
+ x: 0,
452
+ y: 0
453
+ }, matrix.a);
454
+ component.draw(true, false);
455
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, matrix.a);
456
+ component.update();
457
+ }, {
458
+ duration: ANIMATION_DURATION,
459
+ onStop: ()=>{
460
+ state.reset();
461
+ }
462
+ });
463
+ }
464
+ }
465
+ ]);
466
+ },
467
+ meta: {
468
+ isZooming: false
469
+ }
470
+ });
471
+
472
+ export { presetColorPlugin, presetDragElementPlugin, presetHighlightPlugin, presetScalePlugin, presetZoomablePlugin };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squarified",
3
- "version": "0.3.7",
3
+ "version": "0.4.1",
4
4
  "description": "squarified tree map",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -10,6 +10,12 @@
10
10
  "types": "./dist/index.d.ts",
11
11
  "import": "./dist/index.mjs",
12
12
  "require": "./dist/index.js"
13
+ },
14
+ "./package.json": "./package.json",
15
+ "./plugin": {
16
+ "types": "./dist/plugin.d.ts",
17
+ "import": "./dist/plugin.mjs",
18
+ "require": "./dist/plugin.js"
13
19
  }
14
20
  },
15
21
  "keywords": [
@@ -28,7 +34,8 @@
28
34
  "author": "Kanno",
29
35
  "license": "MIT",
30
36
  "devDependencies": {
31
- "@types/js-yaml": "^4.0.9",
37
+ "@microsoft/api-extractor": "^7.52.3",
38
+ "@swc/core": "^1.11.29",
32
39
  "@types/markdown-it": "^14.1.2",
33
40
  "@types/node": "^22.7.4",
34
41
  "chokidar": "^4.0.3",
@@ -36,23 +43,33 @@
36
43
  "esbuild": "^0.24.0",
37
44
  "eslint": "^9.16.0",
38
45
  "eslint-config-kagura": "^3.0.1",
46
+ "gray-matter": "^4.0.3",
39
47
  "highlight.js": "^11.11.1",
40
- "js-yaml": "^4.1.0",
41
48
  "markdown-it": "^14.1.0",
42
- "rollup": "^4.40.0",
49
+ "markdown-it-anchor": "^9.2.0",
50
+ "markdown-it-highlightjs": "^4.2.0",
51
+ "mermaid": "^11.6.0",
52
+ "puppeteer": "^24.9.0",
53
+ "rollup": "^4.41.1",
43
54
  "rollup-plugin-dts": "^6.2.1",
44
55
  "rollup-plugin-swc3": "^0.12.1",
45
56
  "tinyexec": "^0.3.2",
57
+ "tinyglobby": "^0.2.13",
46
58
  "tsx": "^4.19.2",
47
59
  "typescript": "^5.7.3",
48
- "vite-bundle-analyzer": "^0.16.2"
60
+ "vite-bundle-analyzer": "^0.21.0"
49
61
  },
50
62
  "pnpm": {
51
63
  "overrides": {
52
64
  "esbuild": "0.24.0",
53
65
  "is-core-module": "npm:@nolyfill/is-core-module@^1",
54
66
  "safer-buffer": "npm:@nolyfill/safer-buffer@^1"
55
- }
67
+ },
68
+ "onlyBuiltDependencies": [
69
+ "@swc/core",
70
+ "dprint",
71
+ "esbuild"
72
+ ]
56
73
  },
57
- "packageManager": "pnpm@9.4.0"
74
+ "packageManager": "pnpm@10.10.0+sha512.d615db246fe70f25dcfea6d8d73dee782ce23e2245e3c4f6f888249fb568149318637dca73c2c5c8ef2a4ca0d5657fb9567188bfab47f566d1ee6ce987815c39"
58
75
  }