@yanqirenshi/d3.deployment 0.8.5 → 0.9.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.
- package/LICENSE +21 -0
- package/README.markdown +108 -15
- package/babel.config.js +14 -14
- package/dist/components/D3Deployment.js +3 -4
- package/dist/js/Rectum.js +74 -0
- package/package.json +7 -6
- package/tests/Rectum.test.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2026 Satoshi Iwasaki (yanqirenshi@gmail.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.markdown
CHANGED
|
@@ -1,15 +1,108 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
# @yanqirenshi/d3.deployment
|
|
2
|
+
|
|
3
|
+
デプロイメント図(ノード・エッジ・ポート)を SVG で描画する React コンポーネントです。
|
|
4
|
+
|
|
5
|
+
- ノードは `children` で入れ子にできます。宣言サイズより子が大きい場合は親が自動で広がります。
|
|
6
|
+
- ノードはドラッグで移動でき、**親を動かすとサブツリーごと追従**します。
|
|
7
|
+
- エッジの取り付け位置はポートの角度で指定します(0=真下・時計回り。90=左 / 180=真上 / 270=右)。
|
|
8
|
+
- **このライブラリは描画のみを行います。** クリックはコールバックで通知するだけで、
|
|
9
|
+
編集 UI は利用側が用意します(ショーケースではインスペクタ `@yanqirenshi/colonoscope` を繋いでいます)。
|
|
10
|
+
|
|
11
|
+
## インストール
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @yanqirenshi/d3.deployment
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
React 18 / 19 に対応しています(`peerDependencies`)。
|
|
18
|
+
|
|
19
|
+
## 使い方
|
|
20
|
+
|
|
21
|
+
描画の管理オブジェクト `Rectum` を作って React コンポーネント `D3Deployment` に渡し、
|
|
22
|
+
`data()` でデータを流し込みます。
|
|
23
|
+
|
|
24
|
+
```jsx
|
|
25
|
+
import React from 'react';
|
|
26
|
+
import D3Deployment, { Rectum } from '@yanqirenshi/d3.deployment';
|
|
27
|
+
|
|
28
|
+
export default function Graph({ data }) {
|
|
29
|
+
const [rectum, setRectum] = React.useState(null);
|
|
30
|
+
|
|
31
|
+
React.useEffect(() => {
|
|
32
|
+
const r = new Rectum({
|
|
33
|
+
transform: { k: 0.6, x: 120, y: 120 }, // 初期のズーム / パン
|
|
34
|
+
callbacks: {
|
|
35
|
+
// 描画は行わず通知のみ。core は入力データそのもの
|
|
36
|
+
node: { click: (core, node) => {} },
|
|
37
|
+
edge: { click: (core, edge) => {} },
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
r.data(data);
|
|
41
|
+
setRectum(r);
|
|
42
|
+
}, [data]);
|
|
43
|
+
|
|
44
|
+
if (!rectum) return null;
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<div style={{ width: '100%', height: '100%' }}>
|
|
48
|
+
<D3Deployment rectum={rectum} />
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### 描画後にポートの角度を変える
|
|
55
|
+
|
|
56
|
+
```js
|
|
57
|
+
rectum.setEdgePorts(edge_id, { from: 0, to: 180 }); // 省略した側は変更しない
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
入力データ側(`_core`)の角度を書き換えてから再フィットするので、
|
|
61
|
+
以後の再描画やコンポーネントの remount でも維持されます。
|
|
62
|
+
|
|
63
|
+
## 入力データ
|
|
64
|
+
|
|
65
|
+
`{ nodes, edges }` の連想配列です。
|
|
66
|
+
|
|
67
|
+
```js
|
|
68
|
+
{
|
|
69
|
+
nodes: [
|
|
70
|
+
{
|
|
71
|
+
type: 'NODE', id: 1,
|
|
72
|
+
label: { text: 'NODE 01', position: { x: 20, y: 20 }, font: { size: 16, color: '#333333' } },
|
|
73
|
+
description: 'ホバー時のツールチップに出る(任意)',
|
|
74
|
+
link: { url: 'https://example.com/' }, // 任意。ボックス左下に表示
|
|
75
|
+
padding: 40, // 任意。数値・全辺共通
|
|
76
|
+
size: { w: 150, h: 150 }, position: { x: 0, y: 0 },
|
|
77
|
+
children: [ /* 入れ子のノード */ ],
|
|
78
|
+
},
|
|
79
|
+
],
|
|
80
|
+
edges: [
|
|
81
|
+
{ id: 100, from: { id: 1, position: 0 }, to: { id: 2, position: 180 } },
|
|
82
|
+
],
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
`edges[].from.id` / `to.id` は `nodes[].id` を参照します(入れ子の内側のノードも指定できます)。
|
|
87
|
+
|
|
88
|
+
フィールドの詳細はショーケースの d3.deployment のページ(「データ」タブ)を参照してください。
|
|
89
|
+
サンプルの実データは `showcase/src/demos/data/deployment-sample.js` です。
|
|
90
|
+
|
|
91
|
+
## 開発
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
npm run transpile -w @yanqirenshi/d3.deployment # src -> dist(dist はコミット対象)
|
|
95
|
+
npm test -w @yanqirenshi/d3.deployment # jest
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
## Author
|
|
99
|
+
|
|
100
|
+
Satoshi Iwasaki (yanqirenshi@gmail.com)
|
|
101
|
+
|
|
102
|
+
## Copyright
|
|
103
|
+
|
|
104
|
+
Copyright (c) 2018-2026 Satoshi Iwasaki (yanqirenshi@gmail.com)
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
Licensed under the MIT License.
|
package/babel.config.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
presets: [
|
|
3
|
-
[
|
|
4
|
-
"@babel/preset-env"
|
|
5
|
-
]
|
|
6
|
-
],
|
|
7
|
-
env: {
|
|
8
|
-
test: {
|
|
9
|
-
plugins: [
|
|
10
|
-
"babel-plugin-transform-es2015-modules-commonjs",
|
|
11
|
-
],
|
|
12
|
-
},
|
|
13
|
-
},
|
|
14
|
-
};
|
|
1
|
+
module.exports = {
|
|
2
|
+
presets: [
|
|
3
|
+
[
|
|
4
|
+
"@babel/preset-env"
|
|
5
|
+
]
|
|
6
|
+
],
|
|
7
|
+
env: {
|
|
8
|
+
test: {
|
|
9
|
+
plugins: [
|
|
10
|
+
"babel-plugin-transform-es2015-modules-commonjs",
|
|
11
|
+
],
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
};
|
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
7
6
|
exports["default"] = D3Deployment;
|
|
8
|
-
var _react =
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
9
8
|
var _assh0le = _interopRequireDefault(require("@yanqirenshi/assh0le"));
|
|
10
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
|
|
11
|
-
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
12
10
|
function D3Deployment(props) {
|
|
13
11
|
var rectum = props.rectum;
|
|
12
|
+
var id = props.id || "d3-deployment-graph";
|
|
14
13
|
return /*#__PURE__*/_react["default"].createElement(_assh0le["default"], {
|
|
15
|
-
id:
|
|
14
|
+
id: id,
|
|
16
15
|
rectum: rectum
|
|
17
16
|
});
|
|
18
17
|
}
|
package/dist/js/Rectum.js
CHANGED
|
@@ -64,9 +64,13 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
|
|
|
64
64
|
value: function initCallbacks(params) {
|
|
65
65
|
var callbacks = params && params.callbacks || {};
|
|
66
66
|
var node = callbacks.node || {};
|
|
67
|
+
var edge = callbacks.edge || {};
|
|
67
68
|
return {
|
|
68
69
|
node: {
|
|
69
70
|
click: node.click || null
|
|
71
|
+
},
|
|
72
|
+
edge: {
|
|
73
|
+
click: edge.click || null
|
|
70
74
|
}
|
|
71
75
|
};
|
|
72
76
|
}
|
|
@@ -411,6 +415,7 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
|
|
|
411
415
|
}
|
|
412
416
|
this.attachNodeDrag(place, data);
|
|
413
417
|
this.attachNodeClick(place);
|
|
418
|
+
this.attachEdgeClick(place);
|
|
414
419
|
}
|
|
415
420
|
/**
|
|
416
421
|
* ノードのクリック通知(callbacks.node.click)。
|
|
@@ -435,6 +440,75 @@ var Rectum = exports["default"] = /*#__PURE__*/function (_Colon) {
|
|
|
435
440
|
callback(d._core, d);
|
|
436
441
|
});
|
|
437
442
|
}
|
|
443
|
+
/**
|
|
444
|
+
* エッジのクリック通知(callbacks.edge.click)。
|
|
445
|
+
* エッジ path のほか、両端のポート円からも同じエッジを通知する
|
|
446
|
+
* (細い線より円のほうが掴みやすい)。(core, edge) を渡す契約は
|
|
447
|
+
* node.click と同じ(core = 入力のエッジオブジェクトそのまま)。
|
|
448
|
+
*/
|
|
449
|
+
}, {
|
|
450
|
+
key: "attachEdgeClick",
|
|
451
|
+
value: function attachEdgeClick(place) {
|
|
452
|
+
var _this5 = this;
|
|
453
|
+
var callback = this._callbacks.edge.click;
|
|
454
|
+
if (!callback) return;
|
|
455
|
+
place.selectAll('path.edge').style('cursor', 'pointer').on('click', function (event) {
|
|
456
|
+
// path の datum は線分データのため data-edge-id からエッジを引く
|
|
457
|
+
var el = event.currentTarget;
|
|
458
|
+
var id = el && el.getAttribute && el.getAttribute('data-edge-id');
|
|
459
|
+
var edge = _this5.findEdge(id);
|
|
460
|
+
if (!edge) return;
|
|
461
|
+
event.stopPropagation();
|
|
462
|
+
callback(edge._core, edge);
|
|
463
|
+
});
|
|
464
|
+
place.selectAll('circle.port').style('cursor', 'pointer').on('click', function (event, d) {
|
|
465
|
+
event.stopPropagation();
|
|
466
|
+
callback(d.edge._core, d.edge);
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
/** id(入力 id。DOM の data-edge-id は文字列)からエッジを引く。 */
|
|
470
|
+
}, {
|
|
471
|
+
key: "findEdge",
|
|
472
|
+
value: function findEdge(id) {
|
|
473
|
+
var data = this.data();
|
|
474
|
+
if (!data || id === null || id === undefined) return null;
|
|
475
|
+
return data.edges.ht[id] || data.edges.ht[Number(id)] || null;
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* エッジのポート位置(角度)を変更して再フィットし、DOM があれば
|
|
479
|
+
* その場で再描画する(remount 不要)。角度は入力と同じ規約
|
|
480
|
+
* (ノード中心から見て 0=真下・時計回り。90=左, 180=真上, 270=右)。
|
|
481
|
+
*
|
|
482
|
+
* @param {*} edge_id 対象エッジの id(入力の id)
|
|
483
|
+
* @param {object} positions { from?: 角度, to?: 角度 } 省略側は変更しない
|
|
484
|
+
*/
|
|
485
|
+
}, {
|
|
486
|
+
key: "setEdgePorts",
|
|
487
|
+
value: function setEdgePorts(edge_id) {
|
|
488
|
+
var positions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
489
|
+
var edge = this.findEdge(edge_id);
|
|
490
|
+
if (!edge) return this;
|
|
491
|
+
|
|
492
|
+
// 入力(_core)の角度を更新する。fittingPort は _core を読むので
|
|
493
|
+
// そのまま再フィットでき、以後の再描画・remount でも維持される。
|
|
494
|
+
if (positions.from !== undefined && positions.from !== null) edge._core.from.position = positions.from;
|
|
495
|
+
if (positions.to !== undefined && positions.to !== null) edge._core.to.position = positions.to;
|
|
496
|
+
this.fittingPort(edge.from.port);
|
|
497
|
+
this.fittingPort(edge.to.port);
|
|
498
|
+
this.fittingEdge(edge);
|
|
499
|
+
var place = this.d3Element();
|
|
500
|
+
if (place) {
|
|
501
|
+
place.selectAll('circle.port').filter(function (d) {
|
|
502
|
+
return d === edge.from.port || d === edge.to.port;
|
|
503
|
+
}).attr('cx', function (d) {
|
|
504
|
+
return d.position.x;
|
|
505
|
+
}).attr('cy', function (d) {
|
|
506
|
+
return d.position.y;
|
|
507
|
+
});
|
|
508
|
+
this._painter.EDGE.redraw(place, edge);
|
|
509
|
+
}
|
|
510
|
+
return this;
|
|
511
|
+
}
|
|
438
512
|
///// ////////////////////////////////////////////////////////////////
|
|
439
513
|
///// Drag (grab + drag でノードを移動)
|
|
440
514
|
///// ////////////////////////////////////////////////////////////////
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yanqirenshi/d3.deployment",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.9.1",
|
|
4
|
+
"description": "Deployment diagram renderer (nestable nodes / edges / ports) — dragging a parent moves its whole subtree",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"doc": "docs"
|
|
@@ -26,19 +26,20 @@
|
|
|
26
26
|
},
|
|
27
27
|
"scripts": {
|
|
28
28
|
"test": "jest",
|
|
29
|
-
"transpile": "babel src -d dist --copy-files",
|
|
29
|
+
"transpile": "babel src -d dist --copy-files --delete-dir-on-start",
|
|
30
30
|
"prepublishOnly": "npm run transpile"
|
|
31
31
|
},
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
34
|
-
"url": "git+https://github.com/
|
|
34
|
+
"url": "git+https://github.com/Foolsgolds/Assholes.git",
|
|
35
|
+
"directory": "packages/d3.deployment"
|
|
35
36
|
},
|
|
36
37
|
"author": "yanqirenshi@gmail.com",
|
|
37
38
|
"license": "MIT",
|
|
38
39
|
"bugs": {
|
|
39
|
-
"url": "https://github.com/
|
|
40
|
+
"url": "https://github.com/Foolsgolds/Assholes/issues"
|
|
40
41
|
},
|
|
41
|
-
"homepage": "https://github.com/
|
|
42
|
+
"homepage": "https://github.com/Foolsgolds/Assholes/tree/main/packages/d3.deployment#readme",
|
|
42
43
|
"publishConfig": {
|
|
43
44
|
"access": "public"
|
|
44
45
|
}
|
package/tests/Rectum.test.js
CHANGED
|
@@ -182,3 +182,43 @@ test('id 0 is a valid node reference in edges (same class as sitemap issue #11)'
|
|
|
182
182
|
expect(typeof edge.from.position.x).toBe('number');
|
|
183
183
|
expect(typeof edge.from.position.y).toBe('number');
|
|
184
184
|
});
|
|
185
|
+
|
|
186
|
+
// issue #13: エッジのポート位置(角度)を描画後に変更する公開 API。
|
|
187
|
+
// setEdgePorts は _core の角度を更新してポートを再フィットする(DOM 不要でテスト可能)。
|
|
188
|
+
test('setEdgePorts refits ports to the new angles (issue #13)', () => {
|
|
189
|
+
const rectum = new Rectum({});
|
|
190
|
+
rectum.data(NESTED_SAMPLE());
|
|
191
|
+
const data = rectum.data();
|
|
192
|
+
|
|
193
|
+
const edge = data.edges.ht[100];
|
|
194
|
+
const node1 = data.nodes.ht[1];
|
|
195
|
+
const node2 = data.nodes.ht[2];
|
|
196
|
+
|
|
197
|
+
// 初期: from は角度 0(真下) → node 1 の下辺中央(マージン 8)
|
|
198
|
+
expect(edge.from.port.position.x).toBeCloseTo(node1._position.x + node1._size.w / 2, 0);
|
|
199
|
+
expect(edge.from.port.position.y).toBeCloseTo(node1._position.y + node1._size.h + 8, 0);
|
|
200
|
+
|
|
201
|
+
rectum.setEdgePorts(100, { from: 180, to: 90 });
|
|
202
|
+
|
|
203
|
+
// _core が更新され、from ポートは上辺中央へ、to ポートは左辺中央へ再フィット
|
|
204
|
+
expect(edge._core.from.position).toBe(180);
|
|
205
|
+
expect(edge._core.to.position).toBe(90);
|
|
206
|
+
|
|
207
|
+
expect(edge.from.port.position.x).toBeCloseTo(node1._position.x + node1._size.w / 2, 0);
|
|
208
|
+
expect(edge.from.port.position.y).toBeCloseTo(node1._position.y - 8, 0);
|
|
209
|
+
|
|
210
|
+
expect(edge.to.port.position.x).toBeCloseTo(node2._position.x - 8, 0);
|
|
211
|
+
expect(edge.to.port.position.y).toBeCloseTo(node2._position.y + node2._size.h / 2, 0);
|
|
212
|
+
|
|
213
|
+
// エッジ端点もポートに追従
|
|
214
|
+
expect(edge.from.position).toEqual(edge.from.port.position);
|
|
215
|
+
expect(edge.to.position).toEqual(edge.to.port.position);
|
|
216
|
+
|
|
217
|
+
// 片側のみの変更(from は 180 のまま)
|
|
218
|
+
rectum.setEdgePorts(100, { to: 270 });
|
|
219
|
+
expect(edge._core.from.position).toBe(180);
|
|
220
|
+
expect(edge.to.port.position.x).toBeCloseTo(node2._position.x + node2._size.w + 8, 0);
|
|
221
|
+
|
|
222
|
+
// 未知のエッジ id は無視される(例外なし)
|
|
223
|
+
expect(() => rectum.setEdgePorts(999, { from: 0 })).not.toThrow();
|
|
224
|
+
});
|