squarified 0.3.7 → 0.4.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.
@@ -0,0 +1,394 @@
1
+ import { DEFAULT_MATRIX_LOC, PI_2, Schedule, createRoundBlock, definePlugin, easing, hashCode, isScrollWheelOrRightButtonOnMouseupAndDown, isWheelEvent, mixinWithParams, smoothFrame, stackMatrixTransform, stackMatrixTransformWithGraphAndLayer } from "./dom-event-DQ8OFrZa.mjs";
2
+
3
+ //#region src/plugins/highlight.ts
4
+ var Highlight = class extends Schedule {
5
+ reset() {
6
+ this.destory();
7
+ this.update();
8
+ }
9
+ get canvas() {
10
+ return this.render.canvas;
11
+ }
12
+ setZIndexForHighlight(zIndex = "-1") {
13
+ this.canvas.style.zIndex = zIndex;
14
+ }
15
+ init() {
16
+ this.setZIndexForHighlight();
17
+ this.canvas.style.position = "absolute";
18
+ this.canvas.style.pointerEvents = "none";
19
+ }
20
+ };
21
+ const ANIMATION_DURATION = 300;
22
+ const HIGH_LIGHT_OPACITY = .3;
23
+ const fill = {
24
+ desc: {
25
+ r: 255,
26
+ g: 255,
27
+ b: 255
28
+ },
29
+ mode: "rgb"
30
+ };
31
+ const presetHighlightPlugin = definePlugin({
32
+ name: "treemap:preset-highlight",
33
+ onLoad() {
34
+ const meta = this.getPluginMetadata("treemap:preset-highlight");
35
+ if (!meta) return;
36
+ if (!meta.highlight) meta.highlight = new Highlight(this.instance.to);
37
+ },
38
+ onDOMEventTriggered(name, _, module, { stateManager: state, matrix }) {
39
+ if (name === "mousemove") {
40
+ if (state.canTransition("MOVE")) {
41
+ const meta = this.getPluginMetadata("treemap:preset-highlight");
42
+ if (!module) {
43
+ meta.highlight?.reset();
44
+ meta.highlight?.update();
45
+ meta.highlight?.setZIndexForHighlight();
46
+ return;
47
+ }
48
+ const [x, y, w, h] = module.layout;
49
+ const effectiveRadius = Math.min(module.config.rectRadius, w / 4, h / 4);
50
+ smoothFrame((_$1, cleanup) => {
51
+ cleanup();
52
+ meta.highlight?.reset();
53
+ const mask = createRoundBlock(x, y, w, h, {
54
+ fill,
55
+ opacity: HIGH_LIGHT_OPACITY,
56
+ radius: effectiveRadius,
57
+ padding: 0
58
+ });
59
+ meta.highlight?.add(mask);
60
+ meta.highlight?.setZIndexForHighlight("1");
61
+ stackMatrixTransform(mask, matrix.e, matrix.f, matrix.a);
62
+ meta.highlight?.update();
63
+ }, { duration: ANIMATION_DURATION });
64
+ }
65
+ }
66
+ },
67
+ onResize() {
68
+ const meta = this.getPluginMetadata("treemap:preset-highlight");
69
+ if (!meta) return;
70
+ meta.highlight?.render.initOptions({ ...this.instance.render.options });
71
+ meta.highlight?.reset();
72
+ meta.highlight?.init();
73
+ },
74
+ onDispose() {
75
+ const meta = this.getPluginMetadata("treemap:preset-highlight");
76
+ if (meta && meta.highlight) {
77
+ meta.highlight.destory();
78
+ meta.highlight = null;
79
+ }
80
+ },
81
+ meta: { highlight: null }
82
+ });
83
+
84
+ //#endregion
85
+ //#region src/plugins/drag.ts
86
+ const presetDragElementPlugin = definePlugin({
87
+ name: "treemap:preset-drag-element",
88
+ onDOMEventTriggered(name, event, module, domEvent) {
89
+ const { stateManager: state, matrix, component } = domEvent;
90
+ switch (name) {
91
+ case "mousemove": {
92
+ if (state.isInState("DRAGGING")) domEvent.silent("click");
93
+ else domEvent.active("click");
94
+ const meta = getDragOptions.call(this);
95
+ if (!meta) return;
96
+ if (meta.dragOptions.x === 0 && meta.dragOptions.y === 0) {
97
+ state.transition("IDLE");
98
+ return;
99
+ }
100
+ state.transition("DRAGGING");
101
+ if (state.isInState("DRAGGING")) {
102
+ const highlight = getHighlightInstance.call(this);
103
+ smoothFrame((_, cleanup) => {
104
+ cleanup();
105
+ const { offsetX, offsetY } = event.native;
106
+ const drawX = offsetX - meta.dragOptions.x;
107
+ const drawY = offsetY - meta.dragOptions.y;
108
+ const lastX = meta.dragOptions.x;
109
+ const lastY = meta.dragOptions.y;
110
+ if (highlight?.highlight) {
111
+ highlight.highlight.reset();
112
+ highlight.highlight.setZIndexForHighlight();
113
+ }
114
+ matrix.translation(drawX, drawY);
115
+ meta.dragOptions.x = offsetX;
116
+ meta.dragOptions.y = offsetY;
117
+ meta.dragOptions.lastX = lastX;
118
+ meta.dragOptions.lastY = lastY;
119
+ component.cleanup();
120
+ component.draw(false, false);
121
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, matrix.a);
122
+ component.update();
123
+ return true;
124
+ }, {
125
+ duration: ANIMATION_DURATION,
126
+ deps: [() => state.isInState("IDLE")]
127
+ });
128
+ }
129
+ break;
130
+ }
131
+ case "mouseup": {
132
+ if (state.isInState("PRESSED")) {
133
+ const meta = getDragOptions.call(this);
134
+ if (meta && meta.dragOptions) {
135
+ if (meta.dragOptions.x === meta.dragOptions.lastX && meta.dragOptions.y === meta.dragOptions.lastY) {
136
+ state.transition("IDLE");
137
+ return;
138
+ }
139
+ }
140
+ }
141
+ if (state.isInState("DRAGGING") && state.canTransition("IDLE")) {
142
+ const highlight = getHighlightInstance.call(this);
143
+ if (highlight && highlight.highlight) {
144
+ highlight.highlight.reset();
145
+ highlight.highlight.setZIndexForHighlight();
146
+ }
147
+ const meta = getDragOptions.call(this);
148
+ if (meta && meta.dragOptions) {
149
+ meta.dragOptions.x = 0;
150
+ meta.dragOptions.y = 0;
151
+ meta.dragOptions.lastX = 0;
152
+ meta.dragOptions.lastY = 0;
153
+ state.transition("IDLE");
154
+ }
155
+ }
156
+ break;
157
+ }
158
+ case "mousedown": {
159
+ if (isScrollWheelOrRightButtonOnMouseupAndDown(event.native)) return;
160
+ const meta = getDragOptions.call(this);
161
+ if (!meta) return;
162
+ meta.dragOptions.x = event.native.offsetX;
163
+ meta.dragOptions.y = event.native.offsetY;
164
+ meta.dragOptions.lastX = event.native.offsetX;
165
+ meta.dragOptions.lastY = event.native.offsetY;
166
+ state.transition("PRESSED");
167
+ break;
168
+ }
169
+ }
170
+ },
171
+ meta: { dragOptions: {
172
+ x: 0,
173
+ y: 0,
174
+ lastX: 0,
175
+ lastY: 0
176
+ } },
177
+ onResize({ matrix, stateManager: state }) {
178
+ matrix.create(DEFAULT_MATRIX_LOC);
179
+ state.reset();
180
+ }
181
+ });
182
+ function getHighlightInstance() {
183
+ return this.getPluginMetadata("treemap:preset-highlight");
184
+ }
185
+ function getDragOptions() {
186
+ const meta = this.getPluginMetadata("treemap:preset-drag-element");
187
+ return meta;
188
+ }
189
+
190
+ //#endregion
191
+ //#region src/plugins/preset-color.ts
192
+ const presetColorPlugin = definePlugin({
193
+ name: "treemap:preset-color",
194
+ onModuleInit(modules) {
195
+ const colorMappings = {};
196
+ for (let i = 0; i < modules.length; i++) {
197
+ const module = modules[i];
198
+ assignColorMappings(colorMappings, module, Math.abs(hashCode(module.node.id)) % PI_2, 0);
199
+ }
200
+ return { colorMappings };
201
+ }
202
+ });
203
+ function assignColorMappings(colorMappings, module, ancestorHue, depth) {
204
+ const hueOffset = Math.abs(hashCode(module.node.id)) % 60 - 30;
205
+ const hue = (ancestorHue + hueOffset) % 360;
206
+ const saturation = Math.max(75 - depth * 5, 40);
207
+ const baseLightness = 55 - depth * 3;
208
+ const color = adjustColorToComfortableForHumanEye(hue, saturation, baseLightness);
209
+ colorMappings[module.node.id] = color;
210
+ if (module.node.isCombinedNode && module.node.originalNodes) for (const combined of module.node.originalNodes) colorMappings[combined.id] = color;
211
+ if (module.children && module.children.length) {
212
+ const childCount = module.children.length;
213
+ for (let i = 0; i < childCount; i++) {
214
+ const childHueOffset = 40 * i / childCount;
215
+ const childHue = (hue + childHueOffset) % 360;
216
+ assignColorMappings(colorMappings, module.children[i], childHue, depth + 1);
217
+ }
218
+ }
219
+ }
220
+ function adjustColorToComfortableForHumanEye(hue, saturation, lightness) {
221
+ hue = (hue % 360 + 360) % 360;
222
+ saturation = Math.min(Math.max(saturation, 40), 85);
223
+ lightness = Math.min(Math.max(lightness, 35), 75);
224
+ if (hue >= 60 && hue <= 180) {
225
+ saturation = Math.max(saturation - 10, 40);
226
+ lightness = Math.min(lightness + 5, 75);
227
+ } else if (hue >= 200 && hue <= 280) lightness = Math.min(lightness + 8, 75);
228
+ else if (hue >= 0 && hue <= 30) saturation = Math.max(saturation - 5, 40);
229
+ return {
230
+ mode: "hsl",
231
+ desc: {
232
+ h: hue,
233
+ s: saturation,
234
+ l: lightness
235
+ }
236
+ };
237
+ }
238
+
239
+ //#endregion
240
+ //#region src/plugins/wheel.ts
241
+ function presetScalePlugin(options) {
242
+ return definePlugin({
243
+ name: "treemap:preset-scale",
244
+ onDOMEventTriggered(_, event, module, evt) {
245
+ if (isWheelEvent(event)) onWheel(this, event, evt);
246
+ },
247
+ meta: { scaleOptions: {
248
+ scale: 1,
249
+ minScale: options?.min || .1,
250
+ maxScale: options?.max || Infinity,
251
+ scaleFactor: .05
252
+ } },
253
+ onResize({ matrix, stateManager: state }) {
254
+ const meta = getScaleOptions.call(this);
255
+ if (meta) meta.scaleOptions.scale = 1;
256
+ matrix.create(DEFAULT_MATRIX_LOC);
257
+ state.reset();
258
+ }
259
+ });
260
+ }
261
+ function getScaleOptions() {
262
+ const meta = this.getPluginMetadata("treemap:preset-scale");
263
+ return meta;
264
+ }
265
+ function onWheel(pluginContext, event, { stateManager: state, component, matrix }) {
266
+ event.native.preventDefault();
267
+ const meta = getScaleOptions.call(pluginContext);
268
+ if (!meta) return;
269
+ const { scale, minScale, maxScale, scaleFactor } = meta.scaleOptions;
270
+ const delta = event.native.deltaY < 0 ? scaleFactor : -scaleFactor;
271
+ const newScale = Math.max(minScale, Math.min(maxScale, scale + delta));
272
+ if (newScale === scale) return;
273
+ state.transition("SCALING");
274
+ const mouseX = event.native.offsetX;
275
+ const mouseY = event.native.offsetY;
276
+ const scaleDiff = newScale / scale;
277
+ meta.scaleOptions.scale = newScale;
278
+ const highlight = getHighlightInstance.apply(pluginContext);
279
+ smoothFrame((_, cleanup) => {
280
+ cleanup();
281
+ if (highlight && highlight.highlight) {
282
+ highlight.highlight.reset();
283
+ highlight.highlight.setZIndexForHighlight();
284
+ }
285
+ matrix.scale(scaleDiff, scaleDiff);
286
+ matrix.e = mouseX - (mouseX - matrix.e) * scaleDiff;
287
+ matrix.f = mouseY - (mouseY - matrix.f) * scaleDiff;
288
+ component.cleanup();
289
+ const { width, height } = component.render.options;
290
+ component.layoutNodes = component.calculateLayoutNodes(component.data, {
291
+ w: width,
292
+ h: height,
293
+ x: 0,
294
+ y: 0
295
+ }, matrix.a);
296
+ component.draw(true, false);
297
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, newScale);
298
+ component.update();
299
+ if (state.canTransition("IDLE")) state.transition("IDLE");
300
+ return true;
301
+ }, { duration: ANIMATION_DURATION });
302
+ }
303
+
304
+ //#endregion
305
+ //#region src/plugins/zoomable.ts
306
+ const MAX_SCALE_MULTIPLIER = 2;
307
+ const presetZoomablePlugin = definePlugin({
308
+ name: "treemap:preset-zoomable",
309
+ onLoad(treemap, { stateManager: state, matrix }) {
310
+ return mixinWithParams(treemap, [{
311
+ name: "zoom",
312
+ fn: () => (id) => {
313
+ const meta = this.getPluginMetadata("treemap:preset-zoomable");
314
+ if (!meta || state.isInState("ZOOMING")) return;
315
+ const targetModule = this.resolveModuleById(id);
316
+ if (!targetModule) return;
317
+ meta.previousMatrixState = {
318
+ e: matrix.e,
319
+ f: matrix.f,
320
+ a: matrix.a,
321
+ d: matrix.d
322
+ };
323
+ const component = this.instance;
324
+ state.transition("ZOOMING");
325
+ const [nodeX, nodeY, nodeW, nodeH] = targetModule.layout;
326
+ const { width, height } = component.render.options;
327
+ const currentScale = matrix.a;
328
+ const scaleX = width / nodeW;
329
+ const scaleY = height / nodeH;
330
+ const fullScale = Math.min(scaleX, scaleY) * .9;
331
+ const targetScale = Math.min(fullScale, currentScale * MAX_SCALE_MULTIPLIER);
332
+ const scale = targetScale;
333
+ const nodeCenterX = nodeX + nodeW / 2;
334
+ const nodeCenterY = nodeY + nodeH / 2;
335
+ const viewCenterX = width / 2;
336
+ const viewCenterY = height / 2;
337
+ const targetE = viewCenterX - nodeCenterX * scale;
338
+ const targetF = viewCenterY - nodeCenterY * scale;
339
+ const scaleMeta = getScaleOptions.call(this);
340
+ if (scaleMeta) scaleMeta.scaleOptions.scale = scale;
341
+ const highlight = getHighlightInstance.call(this);
342
+ const dragMeta = getDragOptions.call(this);
343
+ if (dragMeta) {
344
+ dragMeta.dragOptions.x = 0;
345
+ dragMeta.dragOptions.y = 0;
346
+ dragMeta.dragOptions.lastX = 0;
347
+ dragMeta.dragOptions.lastY = 0;
348
+ }
349
+ const startMatrix = {
350
+ e: matrix.e,
351
+ f: matrix.f,
352
+ a: matrix.a,
353
+ d: matrix.d
354
+ };
355
+ smoothFrame((progress) => {
356
+ const easedProgress = easing.cubicInOut(progress);
357
+ const currentE = startMatrix.e + (targetE - startMatrix.e) * easedProgress;
358
+ const currentF = startMatrix.f + (targetF - startMatrix.f) * easedProgress;
359
+ const currentA = startMatrix.a + (scale - startMatrix.a) * easedProgress;
360
+ const currentD = startMatrix.d + (scale - startMatrix.d) * easedProgress;
361
+ matrix.create(DEFAULT_MATRIX_LOC);
362
+ matrix.e = currentE;
363
+ matrix.f = currentF;
364
+ matrix.a = currentA;
365
+ matrix.d = currentD;
366
+ if (highlight?.highlight) {
367
+ highlight.highlight.reset();
368
+ highlight.highlight.setZIndexForHighlight();
369
+ }
370
+ component.cleanup();
371
+ const { width: width$1, height: height$1 } = component.render.options;
372
+ component.layoutNodes = component.calculateLayoutNodes(component.data, {
373
+ w: width$1,
374
+ h: height$1,
375
+ x: 0,
376
+ y: 0
377
+ }, matrix.a);
378
+ component.draw(true, false);
379
+ stackMatrixTransformWithGraphAndLayer(component.elements, matrix.e, matrix.f, matrix.a);
380
+ component.update();
381
+ }, {
382
+ duration: ANIMATION_DURATION,
383
+ onStop: () => {
384
+ state.reset();
385
+ }
386
+ });
387
+ }
388
+ }]);
389
+ },
390
+ meta: { isZooming: false }
391
+ });
392
+
393
+ //#endregion
394
+ 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.0",
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,7 @@
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",
32
38
  "@types/markdown-it": "^14.1.2",
33
39
  "@types/node": "^22.7.4",
34
40
  "chokidar": "^4.0.3",
@@ -36,23 +42,31 @@
36
42
  "esbuild": "^0.24.0",
37
43
  "eslint": "^9.16.0",
38
44
  "eslint-config-kagura": "^3.0.1",
45
+ "gray-matter": "^4.0.3",
39
46
  "highlight.js": "^11.11.1",
40
- "js-yaml": "^4.1.0",
41
47
  "markdown-it": "^14.1.0",
42
- "rollup": "^4.40.0",
43
- "rollup-plugin-dts": "^6.2.1",
44
- "rollup-plugin-swc3": "^0.12.1",
48
+ "markdown-it-anchor": "^9.2.0",
49
+ "markdown-it-highlightjs": "^4.2.0",
50
+ "mermaid": "^11.6.0",
51
+ "rolldown": "1.0.0-beta.8",
45
52
  "tinyexec": "^0.3.2",
53
+ "tinyglobby": "^0.2.13",
46
54
  "tsx": "^4.19.2",
47
55
  "typescript": "^5.7.3",
48
- "vite-bundle-analyzer": "^0.16.2"
56
+ "vite-bundle-analyzer": "^0.20.1",
57
+ "puppeteer": "^24.9.0"
49
58
  },
50
59
  "pnpm": {
51
60
  "overrides": {
52
61
  "esbuild": "0.24.0",
53
62
  "is-core-module": "npm:@nolyfill/is-core-module@^1",
54
63
  "safer-buffer": "npm:@nolyfill/safer-buffer@^1"
55
- }
64
+ },
65
+ "onlyBuiltDependencies": [
66
+ "esbuild",
67
+ "dprint",
68
+ "rolldown"
69
+ ]
56
70
  },
57
- "packageManager": "pnpm@9.4.0"
71
+ "packageManager": "pnpm@10.10.0+sha512.d615db246fe70f25dcfea6d8d73dee782ce23e2245e3c4f6f888249fb568149318637dca73c2c5c8ef2a4ca0d5657fb9567188bfab47f566d1ee6ce987815c39"
58
72
  }