@yanqirenshi/d3.pad 0.3.1 → 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.
Files changed (135) hide show
  1. package/README.md +106 -12
  2. package/dist/index.d.ts +5 -4
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +9 -6
  5. package/dist/pad/Pad.d.ts +2 -3
  6. package/dist/pad/Pad.d.ts.map +1 -1
  7. package/dist/pad/Pad.js +22 -28
  8. package/dist/pad/PadDiagram.js +1 -1
  9. package/dist/pad/builtins.d.ts +2 -0
  10. package/dist/pad/builtins.d.ts.map +1 -0
  11. package/dist/pad/builtins.js +24 -0
  12. package/dist/pad/{datamodels → elements}/Assignment.d.ts +3 -1
  13. package/dist/pad/elements/Assignment.d.ts.map +1 -0
  14. package/dist/pad/{painters → elements}/Assignment.js +51 -5
  15. package/dist/pad/{datamodels → elements}/Call.d.ts +7 -1
  16. package/dist/pad/elements/Call.d.ts.map +1 -0
  17. package/dist/pad/{painters → elements}/Call.js +115 -18
  18. package/dist/pad/{datamodels → elements}/Group.d.ts +9 -2
  19. package/dist/pad/elements/Group.d.ts.map +1 -0
  20. package/dist/pad/elements/Group.js +113 -0
  21. package/dist/pad/{datamodels → elements}/Loop.d.ts +9 -2
  22. package/dist/pad/elements/Loop.d.ts.map +1 -0
  23. package/dist/pad/{painters → elements}/Loop.js +73 -54
  24. package/dist/pad/elements/PadContainerBase.d.ts +56 -0
  25. package/dist/pad/elements/PadContainerBase.d.ts.map +1 -0
  26. package/dist/pad/elements/PadContainerBase.js +110 -0
  27. package/dist/pad/elements/PadElementBase.d.ts +12 -0
  28. package/dist/pad/elements/PadElementBase.d.ts.map +1 -0
  29. package/dist/pad/elements/PadElementBase.js +11 -0
  30. package/dist/pad/{datamodels → elements}/Parallel.d.ts +4 -1
  31. package/dist/pad/elements/Parallel.d.ts.map +1 -0
  32. package/dist/pad/elements/Parallel.js +125 -0
  33. package/dist/pad/elements/Predefined.d.ts +51 -0
  34. package/dist/pad/elements/Predefined.d.ts.map +1 -0
  35. package/dist/pad/elements/Predefined.js +48 -0
  36. package/dist/pad/{datamodels → elements}/Process.d.ts +4 -1
  37. package/dist/pad/elements/Process.d.ts.map +1 -0
  38. package/dist/pad/{painters → elements}/Process.js +75 -7
  39. package/dist/pad/{datamodels → elements}/Selection.d.ts +9 -2
  40. package/dist/pad/elements/Selection.d.ts.map +1 -0
  41. package/dist/pad/{painters → elements}/Selection.js +73 -47
  42. package/dist/pad/{datamodels → elements}/Terminal.d.ts +3 -1
  43. package/dist/pad/elements/Terminal.d.ts.map +1 -0
  44. package/dist/pad/{datamodels → elements}/Terminal.js +19 -6
  45. package/dist/pad/parts/connector.d.ts +3 -0
  46. package/dist/pad/parts/connector.d.ts.map +1 -0
  47. package/dist/pad/parts/connector.js +17 -0
  48. package/dist/pad/parts/interactive.d.ts.map +1 -0
  49. package/dist/pad/{painters → parts}/interactive.js +1 -1
  50. package/dist/pad/parts/underline.d.ts +9 -0
  51. package/dist/pad/parts/underline.d.ts.map +1 -0
  52. package/dist/pad/parts/underline.js +30 -0
  53. package/dist/pad/registry.d.ts +7 -5
  54. package/dist/pad/registry.d.ts.map +1 -1
  55. package/dist/pad/registry.js +19 -8
  56. package/dist/pad/types.d.ts +3 -3
  57. package/package.json +2 -2
  58. package/src/index.ts +11 -8
  59. package/src/pad/Pad.ts +22 -28
  60. package/src/pad/PadDiagram.tsx +1 -1
  61. package/src/pad/builtins.ts +26 -0
  62. package/src/pad/{painters → elements}/Assignment.ts +56 -5
  63. package/src/pad/{painters → elements}/Call.ts +128 -17
  64. package/src/pad/elements/Group.ts +130 -0
  65. package/src/pad/{painters → elements}/Loop.ts +81 -62
  66. package/src/pad/elements/PadContainerBase.ts +145 -0
  67. package/src/pad/elements/PadElementBase.ts +22 -0
  68. package/src/pad/elements/Parallel.ts +140 -0
  69. package/src/pad/elements/Predefined.ts +55 -0
  70. package/src/pad/{painters → elements}/Process.ts +79 -7
  71. package/src/pad/{painters → elements}/Selection.ts +81 -54
  72. package/src/pad/{datamodels → elements}/Terminal.ts +24 -6
  73. package/src/pad/parts/connector.ts +19 -0
  74. package/src/pad/{painters → parts}/interactive.ts +1 -1
  75. package/src/pad/parts/underline.ts +33 -0
  76. package/src/pad/registry.ts +33 -14
  77. package/src/pad/types.ts +4 -4
  78. package/dist/pad/datamodels/Assignment.d.ts.map +0 -1
  79. package/dist/pad/datamodels/Assignment.js +0 -51
  80. package/dist/pad/datamodels/Call.d.ts.map +0 -1
  81. package/dist/pad/datamodels/Call.js +0 -83
  82. package/dist/pad/datamodels/Connector.d.ts +0 -35
  83. package/dist/pad/datamodels/Connector.d.ts.map +0 -1
  84. package/dist/pad/datamodels/Connector.js +0 -23
  85. package/dist/pad/datamodels/Group.d.ts.map +0 -1
  86. package/dist/pad/datamodels/Group.js +0 -71
  87. package/dist/pad/datamodels/Loop.d.ts.map +0 -1
  88. package/dist/pad/datamodels/Loop.js +0 -77
  89. package/dist/pad/datamodels/Parallel.d.ts.map +0 -1
  90. package/dist/pad/datamodels/Parallel.js +0 -80
  91. package/dist/pad/datamodels/Process.d.ts.map +0 -1
  92. package/dist/pad/datamodels/Process.js +0 -59
  93. package/dist/pad/datamodels/Selection.d.ts.map +0 -1
  94. package/dist/pad/datamodels/Selection.js +0 -81
  95. package/dist/pad/datamodels/Terminal.d.ts.map +0 -1
  96. package/dist/pad/elements.d.ts +0 -2
  97. package/dist/pad/elements.d.ts.map +0 -1
  98. package/dist/pad/elements.js +0 -32
  99. package/dist/pad/painters/Assignment.d.ts +0 -4
  100. package/dist/pad/painters/Assignment.d.ts.map +0 -1
  101. package/dist/pad/painters/Call.d.ts +0 -4
  102. package/dist/pad/painters/Call.d.ts.map +0 -1
  103. package/dist/pad/painters/Connector.d.ts +0 -4
  104. package/dist/pad/painters/Connector.d.ts.map +0 -1
  105. package/dist/pad/painters/Connector.js +0 -15
  106. package/dist/pad/painters/Group.d.ts +0 -4
  107. package/dist/pad/painters/Group.d.ts.map +0 -1
  108. package/dist/pad/painters/Group.js +0 -99
  109. package/dist/pad/painters/Loop.d.ts +0 -4
  110. package/dist/pad/painters/Loop.d.ts.map +0 -1
  111. package/dist/pad/painters/Parallel.d.ts +0 -4
  112. package/dist/pad/painters/Parallel.d.ts.map +0 -1
  113. package/dist/pad/painters/Parallel.js +0 -102
  114. package/dist/pad/painters/Process.d.ts +0 -5
  115. package/dist/pad/painters/Process.d.ts.map +0 -1
  116. package/dist/pad/painters/Selection.d.ts +0 -4
  117. package/dist/pad/painters/Selection.d.ts.map +0 -1
  118. package/dist/pad/painters/Terminal.d.ts +0 -4
  119. package/dist/pad/painters/Terminal.d.ts.map +0 -1
  120. package/dist/pad/painters/Terminal.js +0 -19
  121. package/dist/pad/painters/interactive.d.ts.map +0 -1
  122. package/src/pad/datamodels/Assignment.ts +0 -55
  123. package/src/pad/datamodels/Call.ts +0 -91
  124. package/src/pad/datamodels/Connector.ts +0 -25
  125. package/src/pad/datamodels/Group.ts +0 -76
  126. package/src/pad/datamodels/Loop.ts +0 -82
  127. package/src/pad/datamodels/Parallel.ts +0 -85
  128. package/src/pad/datamodels/Process.ts +0 -63
  129. package/src/pad/datamodels/Selection.ts +0 -87
  130. package/src/pad/elements.ts +0 -34
  131. package/src/pad/painters/Connector.ts +0 -15
  132. package/src/pad/painters/Group.ts +0 -116
  133. package/src/pad/painters/Parallel.ts +0 -115
  134. package/src/pad/painters/Terminal.ts +0 -23
  135. /package/dist/pad/{painters → parts}/interactive.d.ts +0 -0
@@ -0,0 +1,145 @@
1
+ // 入れ子を持つ要素(GROUP・LOOP・SELECTION・PARALLEL)の親クラス(#324)。PadElementBase の下に置く。
2
+ //
3
+ // この 4 つは、中に「行」(rows・when の body・task の rows)を持ち、行を、登録表(registry.ts)から引いた要素で、
4
+ // 上から下へ並べる。その処理が、4 つのファイルに、それぞれ 2 つずつ(高さを測る側と、描く側)あった。ここに 1 つずつ持たせる。
5
+ // measureRows: 行の高さを測る(normalize の側。DOM に触らない)
6
+ // drawRows: 行を描く(draw の側。行の間を drawConnector でつなぐ)
7
+ // finish: draw の末尾(transform・size・anchors・クリック)。4 つとも同じ形
8
+ // 最低の高さ(LABEL_HEIGHT・CONDITION_HEIGHT・ROW_HEIGHT)を当てはめるのは、要素の側(要素ごとに違うため)。
9
+ //
10
+ // 数えるのは、実際に描く行だけ(登録されていない _class の行は、測るときも描くときも飛ばす。#307)。
11
+ // _class を省いた行は、PROCESS として扱う。
12
+ // 高さの式を、normalize と draw で 1 つにすることは、していない(行を並べる繰り返しを 1 か所にしただけ)。
13
+ //
14
+ // 「上の余白」(inset.top。#330): 要素は、自分の上端から、最初の箱(条件・ラベルなど)が始まる位置までの距離を、
15
+ // normalize が返す data.inset.top で申告できる(SELECTION は、ブラケットのはみ出しを高さに含めるので BRACKET_MARGIN。
16
+ // 申告しない要素は 0)。外の要素は、最初の行の申告を見て、左の箱の中央と最初の行の最初の箱の中央をそろえる。
17
+ // a. 左の箱を下げる(GROUP・LOOP): 左の箱を inset のぶん下に描く。自分の inset も、最初の行から引き継ぐ(さらに外の要素が見て合わせる)
18
+ // b. 行を上げる(SELECTION の when の本体): drawRows の alignTop で、最初の行を inset のぶん上に置く
19
+ // PARALLEL は、左に箱が無く(ブラケットだけ)合わせる相手が無いので、何もしない(inset は申告しない)。
20
+ import { ELEMENTS } from '../registry.js';
21
+ import { drawConnector } from '../parts/connector.js';
22
+ import { attachClick } from '../parts/interactive.js';
23
+ import PadElementBase from './PadElementBase.js';
24
+
25
+ /* eslint-disable @typescript-eslint/no-explicit-any */
26
+ /** drawRows に渡すもの */
27
+ export interface DrawRowsOptions {
28
+ /** 行を置く左端 */
29
+ x: number;
30
+ /** 最初の行の上端 */
31
+ y: number;
32
+ /** 行の間の余白 */
33
+ rowGap: number;
34
+ /**
35
+ * true のとき、最初の行を、その行の inset.top のぶん上に置く(y が、最初の行の「最初の箱」の上端になる。#330)。
36
+ * 左の箱を y に置く要素(SELECTION の when)で使う。左の箱を下げる側(GROUP・LOOP)は使わない。
37
+ */
38
+ alignTop?: boolean;
39
+ /** 入れ子の中の開閉でも、いちばん外の Pad を描き直す関数(行に渡す) */
40
+ redraw: any;
41
+ /** 要素のクリックの知らせ先(行に渡す) */
42
+ onElementClick: any;
43
+ }
44
+
45
+ export default abstract class PadContainerBase extends PadElementBase {
46
+ /** 行(row)に対応する要素のクラス。_class を省いた行は PROCESS。登録されていなければ undefined(その行は、飛ばす) */
47
+ private rowElement (row: any) {
48
+ return ELEMENTS[row._class || 'PROCESS'];
49
+ }
50
+
51
+ /**
52
+ * 行を並べたときの高さを測る。各行を normalize して、高さだけを積む(DOM に触らない。幅の実測は、描くときだけ要る)。
53
+ * drawn: 描ける行の数。height: 描ける行の、最初の上端から最後の下端まで(描ける行が無ければ 0)。
54
+ * inset: 最初の描ける行の inset.top(その行の上端から、最初の箱までの距離。申告が無ければ 0。#330)。
55
+ */
56
+ protected measureRows (rows: any[], rowGap: number): { drawn: number; height: number; inset: number } {
57
+ let y = 0;
58
+ let drawn = 0;
59
+ let inset = 0;
60
+
61
+ for (const row of rows) {
62
+ const Element = this.rowElement(row);
63
+ if (!Element) continue;
64
+
65
+ const rowData = new Element().normalize(row, { x: 0, y, width: 0, center: false });
66
+ if (!drawn) inset = this.insetTop(rowData);
67
+ y = rowData.anchors.bottom.y + rowGap;
68
+ drawn++;
69
+ }
70
+
71
+ return { drawn, height: drawn ? y - rowGap : 0, inset };
72
+ }
73
+
74
+ /**
75
+ * normalize が返した data の「上の余白」(inset.top)。読むのは、ここ 1 か所(測る側も描く側も、これを通す)。
76
+ * 使うのは、有限の、0 より大きい数(typeof が number)だけ。申告が無い・文字列・負・NaN・Infinity・null などは 0。
77
+ * 文字列の '8' は、数に直さず 0 とする。直さないと、`inset + LABEL_HEIGHT` が文字列の連結('844')になって高さが壊れ、
78
+ * 負の値では左の箱が自分の上端より上に描かれる(独自の要素が返す値の守り。最初からある要素は、数しか返さない。#330)
79
+ */
80
+ private insetTop (data: any): number {
81
+ const top = data && data.inset && data.inset.top;
82
+
83
+ return typeof top === 'number' && Number.isFinite(top) && top > 0 ? top : 0;
84
+ }
85
+
86
+ /**
87
+ * 行を、g に、上から下へ描く。行の間は、drawConnector でつなぐ。
88
+ * drawn: 描いた行の数。end: 最後の行の下端の y(描いた行が無ければ、最初の y)。width: 描いた行の幅の最大。
89
+ */
90
+ protected drawRows (g: any, rows: any[], options: DrawRowsOptions): { drawn: number; end: number; width: number } {
91
+ const { x, y, rowGap, redraw, onElementClick, alignTop } = options;
92
+ let rowY = y;
93
+ let width = 0;
94
+ let drawn = 0;
95
+ let prevRowData: any = null;
96
+
97
+ for (const row of rows) {
98
+ const Element = this.rowElement(row);
99
+ if (!Element) continue;
100
+
101
+ const element = new Element();
102
+ // 最初の行を、inset のぶん上に置く(#330)。余白は行の位置(y)によらないので、y=0 で測る
103
+ if (alignTop && !drawn)
104
+ rowY -= this.insetTop(element.normalize(row, { x, y: 0, width: 0, center: true }));
105
+ // center:trueにして、行のanchorを自身の中心ではなく左端(=x)にする。「連続性のある処理のベース」の規則
106
+ // (要素の左端を線上に配置する)に合わせ、行同士を結ぶConnectorがまっすぐな縦線になるようにする。
107
+ const rowData = element.normalize(row, { x, y: rowY, width: 0, center: true });
108
+ rowData.redraw = redraw;
109
+ rowData.onElementClick = onElementClick;
110
+ element.draw(g, rowData);
111
+
112
+ if (prevRowData)
113
+ drawConnector(g, prevRowData.anchors.bottom, rowData.anchors.top);
114
+ prevRowData = rowData;
115
+
116
+ width = Math.max(width, rowData.size ? rowData.size.w : 0);
117
+ rowY = rowData.anchors.bottom.y + rowGap;
118
+ drawn++;
119
+ }
120
+
121
+ return { drawn, end: drawn ? rowY - rowGap : y, width };
122
+ }
123
+
124
+ /**
125
+ * draw の末尾。g の位置を決め、報告する大きさ(data.size)と anchors を確定し、クリックを付ける。
126
+ * anchor_at_spine=true(PAD 全体を 1 本の縦線でつなぐ用途)のときは、縦線の位置(=左端)をそのまま anchor にする。
127
+ * false のときは、自分自身の中央を anchor にする(単体表示ページ用)。
128
+ */
129
+ protected finish (g: any, data: any, totalWidth: number, totalHeight: number) {
130
+ const x = data.position.x;
131
+ g.attr('transform', `translate(${x},${data.position.y})`);
132
+
133
+ data.size = { w: totalWidth, h: totalHeight };
134
+
135
+ const anchorX = data.anchor_at_spine ? x : x + totalWidth / 2;
136
+ data.anchors = {
137
+ top: { x: anchorX, y: data.position.y },
138
+ bottom: { x: anchorX, y: data.position.y + totalHeight },
139
+ };
140
+
141
+ attachClick(g, data);
142
+
143
+ return g;
144
+ }
145
+ }
@@ -0,0 +1,22 @@
1
+ // 要素(PAD の記号)の親クラス(#322)。要素は、1 つの種類(_class)につき、1 つのクラス。
2
+ //
3
+ // 1 つのクラスが、入力データの正規化(dataTemplate・normalize。位置と高さ)と、SVG への描画(draw)の両方を持つ。
4
+ // 以前は、要素ごとに datamodels/<要素>.ts と painters/<要素>.ts の 2 つのクラスに分かれていた(#322 でまとめた)。
5
+ // クラスは状態(フィールド)を持たない。呼び出しごとの値は、normalize が返す data に入る。
6
+ //
7
+ // 独自の要素を足す側(registerElement)は、このクラスを継承してもよいし、継承せずに、normalize と draw を持つクラスを渡してもよい
8
+ // (registerElement が見るのは、この 2 つのメソッドがあるかどうかだけ)。
9
+ /* eslint-disable @typescript-eslint/no-explicit-any */
10
+ export default abstract class PadElementBase {
11
+ /** 正規化したデータの雛形(既定値つき) */
12
+ abstract dataTemplate (): any;
13
+
14
+ /**
15
+ * 入力のノード(core)と、置く場所(box: { x, y, width, center })から、data(位置・大きさ・anchors)を作る。
16
+ * 高さは、DOM を測らず、データだけから確定できる(幅は、文字の実測が要るので、draw が確定する)。
17
+ */
18
+ abstract normalize (core: any, box: any): any;
19
+
20
+ /** data を、d3 の selection(place)に描く。幅など、実測が要るものは、ここで data に書き戻す */
21
+ abstract draw (place: any, data: any): any;
22
+ }
@@ -0,0 +1,140 @@
1
+ // Parallel(PARALLEL { TASK{...} TASK{...} })の要素のクラス(#322: 以前の datamodels/Parallel.ts と painters/Parallel.ts を 1 つにまとめた。中身は移しただけ)。
2
+ // 元のPAD(Problem Analysis Diagram)には無い記号だが、複数の処理が並行して
3
+ // 走ることを図示するためにこのプロジェクト独自に追加する。
4
+ // 各TASKは、従来通りPROCESS要素そのもの({cells, dividers})1行として
5
+ // 描画するか、あるいは複数の文が連続するTASK(例: 同じ処理を続けて2回
6
+ // 呼ぶ等)を表すために{rows: [...]}として複数行を持てる
7
+ // (rowsはLOOP/SELECTIONの本体と同じくregistry.ts経由で_classごとに描画する。
8
+ // _class省略時はPROCESS扱い)。
9
+ // draw は、normalize が作った data を、d3 のセレクション(place)に描く。
10
+ //
11
+ // 各TASKは、{rows: [...]}を持つ場合はLOOP/SELECTIONの本体と同じくregistry.ts
12
+ // 経由で行ごとに描画し(複数の文が連続するTASKを表す)、持たない場合は
13
+ // 従来通りProcess.tsのProcessクラスをそのまま流用して1行の矩形として
14
+ // 描画する(要素間・行間はrow_gapぶんの余白を空けて縦に並べる)。
15
+ // 左端には、並行実行であることを表す矩形のブラケット
16
+ // (元のPADには無い、このプロジェクト独自の拡張記号)を添える。
17
+ // {rows: [...]}を持つTASKで複数行を描画するときは、Pad.ts本体の要素間と
18
+ // 同じConnector要素で行間をつなぎ、処理の連続性を表す。
19
+
20
+ import Theme from '../Theme.js';
21
+ import Process from './Process.js';
22
+ import PadContainerBase from './PadContainerBase.js';
23
+
24
+ const ROW_HEIGHT = 44;
25
+ const ROW_GAP = 11; // TASK同士(要素間)・TASK内の行同士の余白
26
+ const BRACKET_WIDTH = 12; // 左のブラケット(矩形)の幅
27
+ const BRACKET_MARGIN = 0; // ブラケットと本体(行)の間隔
28
+
29
+ const processPainter = new Process();
30
+
31
+ export default class Parallel extends PadContainerBase {
32
+ // 1つのTASKの高さを求める。{rows: [...]}を持つTASKは本体(LOOP/SELECTIONのbodyと同じ考え方)として
33
+ // 各行の高さを積み上げる(PadContainerBase の measureRows)。持たないTASK(従来通りの{cells, dividers})は固定のROW_HEIGHTとする。
34
+ // {rows: [...]}を持っても、描ける行が無い(空・知らない_classだけ)TASKは、draw と同じくROW_HEIGHTとする(数えるのは、実際に描く行だけ。#307)。
35
+ private taskHeight (task: any, rowGap: number) {
36
+ if (!task.rows) return ROW_HEIGHT;
37
+
38
+ const { drawn, height } = this.measureRows(task.rows, rowGap);
39
+
40
+ return drawn ? height : ROW_HEIGHT;
41
+ }
42
+
43
+ dataTemplate () {
44
+ return {
45
+ // tasks: ({ cells: string[], dividers: (...)[] } | { rows: ({_class?, ...})[] })[]
46
+ tasks: [],
47
+ font: { family: '"Courier New", monospace', size: 14, color: Theme.text },
48
+ stroke: { color: Theme.stroke, width: 1 },
49
+ row_height: ROW_HEIGHT,
50
+ row_gap: ROW_GAP,
51
+ bracket: { width: BRACKET_WIDTH, margin: BRACKET_MARGIN },
52
+ chevron_depth: 12,
53
+ size: { w: 0, h: 0 }, // 幅・高さともに描画時(実測後)に確定する
54
+ position: { x: 0, y: 0 },
55
+ // trueのときConnector用のanchorをbox.x(PADの縦線位置)にする。
56
+ // 見た目(描画位置)は常にbox.xからの左詰め=変わらない。
57
+ anchor_at_spine: false,
58
+ // NOTE: 幅がテキスト実測依存のため、x座標は暫定値(yのみ確定)。
59
+ // draw が実測後に確定値へ更新する。
60
+ anchors: { top: { x: 0, y: 0 }, bottom: { x: 0, y: 0 } },
61
+ _id: null,
62
+ _core: null,
63
+ _class: 'PARALLEL',
64
+ };
65
+ }
66
+
67
+ normalize (core: any, box: any) {
68
+ if (!core) return null;
69
+
70
+ const data = this.dataTemplate();
71
+ data._core = core;
72
+ data._id = core.id;
73
+ data.tasks = core.tasks || [];
74
+ data.position = { x: box.x, y: box.y };
75
+ data.anchor_at_spine = !!box.center;
76
+
77
+ const n = data.tasks.length;
78
+ // 行の間の余白は、定数 ROW_GAP ではなく data.row_gap(draw と同じもの。値は同じ 11。#324)
79
+ const h = data.tasks.reduce((sum: number, task: any)=> sum + this.taskHeight(task, data.row_gap) + data.row_gap, 0) - (n ? data.row_gap : 0);
80
+ data.anchors = {
81
+ top: { x: box.x, y: box.y },
82
+ bottom: { x: box.x, y: box.y + h },
83
+ };
84
+
85
+ return data;
86
+ }
87
+
88
+ draw (place: any, data: any) {
89
+ // 幅がテキスト実測に依存するため、先にローカル座標(0,0起点)で内容を
90
+ // 描いてから、実測幅を使ってg全体の配置(x)とanchorsを確定する。
91
+ const g = place.append('g').attr('class', 'pad-parallel');
92
+
93
+ const bodyX = data.bracket.width + data.bracket.margin;
94
+ const rowHeight = data.row_height;
95
+ const rowGap = data.row_gap;
96
+
97
+ let y = 0;
98
+ let maxWidth = 0;
99
+
100
+ data.tasks.forEach((task: any)=> {
101
+ if (task.rows) {
102
+ // 行を描く(PadContainerBase の drawRows)。行の後ろに足したrowGapは、行間としては正しいが末尾の行の後ろ分は
103
+ // TASK自身の高さには含めない(直後の共通のy += rowGapと二重になり、次のTASK/要素と重なってしまうため。
104
+ // normalize の taskHeight と同じ考え方)。
105
+ // 描いた行が無いTASK(rows: []・知らない_classだけ)は、rowHeightぶん進める(normalize と同じ。#307)
106
+ const body = this.drawRows(g, task.rows, { x: bodyX, y, rowGap, redraw: data.redraw, onElementClick: data.onElementClick });
107
+ maxWidth = Math.max(maxWidth, body.width);
108
+ y = body.drawn ? body.end : y + rowHeight;
109
+ } else {
110
+ const rowData = {
111
+ cells: task.cells,
112
+ dividers: task.dividers,
113
+ font: data.font,
114
+ stroke: data.stroke,
115
+ chevron_depth: data.chevron_depth,
116
+ position: { x: bodyX, y },
117
+ size: { w: 0, h: rowHeight },
118
+ onElementClick: data.onElementClick,
119
+ };
120
+ processPainter.draw(g, rowData);
121
+ maxWidth = Math.max(maxWidth, rowData.size.w);
122
+ y += rowHeight;
123
+ }
124
+ y += rowGap;
125
+ });
126
+
127
+ const totalHeight = data.tasks.length ? y - rowGap : 0;
128
+ const totalWidth = bodyX + maxWidth;
129
+
130
+ // 左端のブラケット(並行実行を表す矩形。先頭に挿入し、下に来るようにする)
131
+ g.insert('rect', ':first-child')
132
+ .attr('x', 0).attr('y', 0)
133
+ .attr('width', data.bracket.width).attr('height', totalHeight)
134
+ .attr('fill', 'none')
135
+ .attr('stroke', data.stroke.color)
136
+ .attr('stroke-width', data.stroke.width);
137
+
138
+ return this.finish(g, data, totalWidth, totalHeight);
139
+ }
140
+ }
@@ -0,0 +1,55 @@
1
+ // PREDEFINED(定義済みの処理の呼び出し)(#318)。流れ図の「定義済み処理」と同じ形の箱。
2
+ // 読む項目・開閉のラベルとクリック・呼び出し先の置き方・高さは、CALL と同じ(Call を継承し、_class と箱の描き方だけを差し替える)。
3
+ //
4
+ // 箱: 外枠の左右の内側に、上端から下端までの縦線を 1 本ずつ引く。縦線と外枠の間は、何も書かない帯(BAND)。
5
+ // セル(cells)は、2 本の縦線の間に描く(帯の分だけ右へずらす)。
6
+ // 報告する幅(data.size.w)と、呼び出し先につなぐ横線・ラベルの位置は、帯を含めた箱の右端から数える
7
+ // (Call の drawBox が返す幅が、そのまま使われる)。
8
+ import Call from './Call.js';
9
+ import { drawCells } from './Process.js';
10
+
11
+ /** 外枠と縦線の間の帯の幅 */
12
+ export const BAND = 8;
13
+
14
+ export default class Predefined extends Call {
15
+ dataTemplate () {
16
+ return { ...super.dataTemplate(), _class: 'PREDEFINED' };
17
+ }
18
+
19
+ boxClass (): string {
20
+ return 'pad-predefined';
21
+ }
22
+
23
+ drawBox (g: any, data: any): number {
24
+ const cells = g.append('g').attr('transform', `translate(${BAND}, 0)`);
25
+ const cellsWidth = drawCells(cells, {
26
+ cells: data.cells,
27
+ dividers: data.dividers,
28
+ links: data.links,
29
+ font: data.font,
30
+ stroke: data.stroke,
31
+ chevron_depth: data.chevron_depth,
32
+ height: data.size.h,
33
+ });
34
+ const totalWidth = cellsWidth + BAND * 2;
35
+
36
+ // 外枠(セルより先に挿入し、下に来るようにする)
37
+ g.insert('rect', ':first-child')
38
+ .attr('width', totalWidth)
39
+ .attr('height', data.size.h)
40
+ .attr('fill', '#fff')
41
+ .attr('stroke', data.stroke.color)
42
+ .attr('stroke-width', data.stroke.width);
43
+
44
+ // 左右の内側の縦線(上端から下端まで)
45
+ for (const x of [BAND, totalWidth - BAND]) {
46
+ g.append('line')
47
+ .attr('x1', x).attr('y1', 0)
48
+ .attr('x2', x).attr('y2', data.size.h)
49
+ .attr('stroke', data.stroke.color)
50
+ .attr('stroke-width', data.stroke.width);
51
+ }
52
+
53
+ return totalWidth;
54
+ }
55
+ }
@@ -1,15 +1,24 @@
1
- // PROCESS(処理)の描画クラス。
2
- // 正規化済みデータ(datamodels/Process.ts の出力)を、d3 のセレクション
3
- // (place) に対して描画する。データの正規化には関知しない。
1
+ // PROCESS(処理)の要素のクラス(#322: 以前の datamodels/Process.ts と painters/Process.ts を 1 つにまとめた。中身は移しただけ)。
2
+ // 1つの処理を、四角い箱の中の「ラベル+引数」の並び(cells)として表す。
3
+ // セルの区切り(dividers)は点線・実線・山形、セルごとにリンク(links)も付けられる。
4
+ // 何をセルに入れるかは、使う側が決める(要素は文言を固定しない)。
5
+ // draw は、normalize が作った data を、d3 のセレクション(place)に描く。
4
6
  //
5
7
  // 各セルの幅はテキスト量に合わせて可変にするため、まず全セルのテキストを
6
8
  // 描画してから実測(getBBox)し、その幅を元にセル位置・区切り位置・
7
9
  // 全体幅を決める(Assignmentと同じ考え方をセル数分に一般化したもの)。
8
10
  //
9
11
  // セルの描画ロジック(drawCells)は外枠(rect)を持たないため、
10
- // painters/Parallel.ts から複数行をひとつの外枠にまとめる用途でも
12
+ // Parallel.ts から複数行をひとつの外枠にまとめる用途でも
11
13
  // そのまま再利用できる。
12
- import { attachClick } from './interactive.js';
14
+
15
+ import Theme from '../Theme.js';
16
+ import { attachClick } from '../parts/interactive.js';
17
+ import { drawUnderline } from '../parts/underline.js';
18
+ import PadElementBase from './PadElementBase.js';
19
+
20
+ const HEIGHT = 44;
21
+ const CHEVRON_DEPTH = 12;
13
22
 
14
23
  const PADDING_LEFT = 12;
15
24
  const PADDING_AFTER_TEXT = 14;
@@ -26,16 +35,23 @@ export function drawCells (g: any, spec: any) {
26
35
  const chevronDepth = spec.chevron_depth;
27
36
  const links = spec.links || [];
28
37
 
38
+ const containers: any[] = []; // セルごとの、text の親(リンクなら <a>)。下線を同じ親に引く
29
39
  const textEls = spec.cells.map((text: any, i: number)=> {
30
40
  const href = links[i];
31
41
  const container = href ? g.append('a').attr('xlink:href', href).attr('href', href) : g;
32
42
 
43
+ // リンクのクリックは、リンク先へ移るだけにする(箱へ伝えない)。伝わると、箱の g に付けた attachClick が働いて、
44
+ // 移る前に、箱のクリック〔onElementClick。インスペクタの選択など〕も呼ばれる(#336。CALL の開閉のラベルと同じ考え。#309)。
45
+ // stopPropagation だけで、preventDefault は呼ばない: リンク先へ移る動き(修飾キー付き・中ボタンも)は、そのまま。
46
+ // 文字も下線(同じ <a> の中の line。underline.ts)も、<a> に付けたこの 1 つで足りる。
47
+ if (href) container.on('click', (event: any)=> { if (event && event.stopPropagation) event.stopPropagation(); });
48
+ containers.push(container);
49
+
33
50
  return container.append('text')
34
51
  .attr('y', h / 2)
35
52
  .attr('dominant-baseline', 'central')
36
53
  .style('font-family', spec.font.family)
37
54
  .style('font-size', spec.font.size)
38
- .style('text-decoration', href ? 'underline' : null)
39
55
  .style('cursor', href ? 'pointer' : null)
40
56
  .attr('fill', spec.font.color)
41
57
  .text(text);
@@ -71,6 +87,11 @@ export function drawCells (g: any, spec: any) {
71
87
 
72
88
  textEls.forEach((t: any, i: number)=> t.attr('x', cellStartX[i]));
73
89
 
90
+ // リンクのセルの下線は、text-decoration ではなく、自分で line を引く(underline.ts。#320)。文字の x が決まったあとに引く
91
+ textEls.forEach((t: any, i: number)=> {
92
+ if (links[i]) drawUnderline(containers[i], t, spec.font.color);
93
+ });
94
+
74
95
  dividers.forEach(d=> {
75
96
  if ('chevron'===d.type) {
76
97
  const apexX = 'right'===d.direction ? d.x + d.depth : d.x;
@@ -96,7 +117,58 @@ export function drawCells (g: any, spec: any) {
96
117
  return totalWidth;
97
118
  }
98
119
 
99
- export default class Process {
120
+ export default class Process extends PadElementBase {
121
+ dataTemplate () {
122
+ return {
123
+ // cells: string[](先頭はラベル。以降は表示する引数)
124
+ // dividers: ('dotted'|'solid'|{type:'chevron', direction:'left'|'right'})[]
125
+ // 要素数は cells.length - 1。dotted=ラベルと最初の引数の区切り、
126
+ // solid=引数同士の区切り、chevron=前の値が次の値へ流れ込む
127
+ // (Assignmentの「<」と同じ役割)ことを表す。
128
+ cells: [],
129
+ dividers: [],
130
+ // cellsと同じ長さの配列(省略可)。リンクさせたいセルの位置に
131
+ // URL文字列、それ以外はnullを入れる(例: dwmem_load/dwtab_rebuild
132
+ // が対象とするテーブル名セルをWBSページへのリンクにする)。
133
+ links: [],
134
+ description: null, // 箱の右に添える説明。無ければnull
135
+ font: { family: '"Courier New", monospace', size: 14, color: Theme.text },
136
+ stroke: { color: Theme.stroke, width: 1 },
137
+ chevron_depth: CHEVRON_DEPTH,
138
+ size: { w: 0, h: HEIGHT }, // 幅はテキスト実測後に draw が確定する
139
+ position: { x: 0, y: 0 },
140
+ // trueのときConnector用のanchorをbox.x(PADの縦線位置)にする。
141
+ // 見た目(描画位置)は常にbox.xからの左詰め=変わらない。
142
+ anchor_at_spine: false,
143
+ // NOTE: 幅がテキスト実測依存のため、x座標は暫定値(yのみ確定)。
144
+ // draw が実測後に確定値へ更新する。
145
+ anchors: { top: { x: 0, y: 0 }, bottom: { x: 0, y: HEIGHT } },
146
+ _id: null,
147
+ _core: null,
148
+ _class: 'PROCESS',
149
+ };
150
+ }
151
+
152
+ normalize (core: any, box: any) {
153
+ if (!core) return null;
154
+
155
+ const data = this.dataTemplate();
156
+ data._core = core;
157
+ data._id = core.id;
158
+ data.cells = core.cells || [];
159
+ data.dividers = core.dividers || [];
160
+ data.links = core.links || [];
161
+ data.description = core.description || null;
162
+ data.position = { x: box.x, y: box.y };
163
+ data.anchor_at_spine = !!box.center;
164
+ data.anchors = {
165
+ top: { x: box.x, y: box.y },
166
+ bottom: { x: box.x, y: box.y + HEIGHT },
167
+ };
168
+
169
+ return data;
170
+ }
171
+
100
172
  draw (place: any, data: any) {
101
173
  // 幅がテキスト実測に依存するため、先にローカル座標(0,0起点)で内容を
102
174
  // 描いてから、実測幅を使ってanchorsを確定する。描画位置(x)は常に
@@ -1,5 +1,15 @@
1
- // SELECTION(選択)の描画クラス。
2
- // 正規化済みデータ(datamodels/Selection.ts の出力)を、d3 のセレクション
1
+ // SELECTION(選択)の要素のクラス(#322: 以前の datamodels/Selection.ts と painters/Selection.ts を 1 つにまとめた。中身は移しただけ)。
2
+ // 元のPAD(Problem Analysis Diagram)が持つ「選択」記号に相当する
3
+ // (PARALLEL等とは異なり、拡張ではなく本来のPAD要素)。
4
+ // SELECTION { WHEN(条件){本体} WHEN(条件){本体} ... ELSE{本体} } のように、条件
5
+ // (WHEN)を複数持てる形で表す(構文上は複数許容される可能性があるため)。
6
+ // 末尾にELSE(どのWHENにも合致しない場合の処理)を1つ持てる。whens配列内で
7
+ // { else: true, body } と表す(条件式のかわりに固定ラベル「ELSE」を表示する
8
+ // だけで、構造上はWHENと同じ1行として扱う)。
9
+ // 各行の本体(body)は { _class?: string, ... }[] を持ち、Pad本体と同じ
10
+ // レジストリ(registry.ts)で対応するデータクラスに振り分ける(LOOPの
11
+ // rowsと同じ考え方。_class省略時はPROCESS扱い)。
12
+ // 正規化済みデータ(Selection.ts の normalize の出力)を、d3 のセレクション
3
13
  // (place) に対して描画する。
4
14
  //
5
15
  // 左の「条件」列(WHENごとに1行、単純な1セルの箱)を積み重ね、条件列全体を
@@ -11,25 +21,81 @@
11
21
  // NOTE: SELECTION自身の開始-終了の縦線は引かない。トップレベルで使われる場合は
12
22
  // Pad.tsが最後に描く開始-終了の縦線(Pad.ts参照)で足り、LOOP等に入れ子に
13
23
  // なる場合はブラケットより下に浮いた線になってしまうだけで不要なため。
14
- import { ELEMENTS } from '../registry.js';
15
- import { attachClick } from './interactive.js';
16
24
 
17
- const CONDITION_HEIGHT = 44; // 条件ボックスの高さ(本体の実際の高さに関わらず固定)
25
+ import Theme from '../Theme.js';
26
+ import PadContainerBase from './PadContainerBase.js';
27
+
28
+ const CONDITION_HEIGHT = 44;
29
+ const ROW_GAP = 11;
30
+ // ブラケットが条件ボックスの上下にはみ出す量(normalize と draw の両方が使う)。
31
+ // はみ出しを自分の高さに含めるので、上下に BRACKET_MARGIN ずつの余白を持つ(#309)。
32
+ const BRACKET_MARGIN = 8;
33
+
18
34
  const CONDITION_PADDING = 12; // 条件ボックス内の左右余白
19
35
  const BRACKET_GAP = 12; // ブラケットと条件列の間隔
20
36
  const BRACKET_OVERLAP = 20; // ブラケット(ベースの四角)が条件ボックスへ被る量
21
- const BRACKET_MARGIN = 8; // ブラケットの上下が条件ボックスよりわずかに広がる量
22
37
  const CONNECTOR_GAP = 14; // 条件ボックス右端から本体左端までの横線の長さ
23
38
 
24
- export default class Selection {
39
+ export default class Selection extends PadContainerBase {
40
+ dataTemplate () {
41
+ return {
42
+ whens: [], // ({ condition: string } | { else: true })[] & { body: ({_class?, ...})[] }[]
43
+ font: { family: '"Courier New", monospace', size: 14, color: Theme.text },
44
+ stroke: { color: Theme.stroke, width: 1 },
45
+ row_gap: ROW_GAP,
46
+ chevron_depth: 12,
47
+ size: { w: 0, h: 0 }, // 幅・高さともに描画時(実測後)に確定する
48
+ position: { x: 0, y: 0 },
49
+ // 自分の上端から、最初の条件の箱が始まる位置まで(ブラケットのはみ出しのぶん。外の要素が見て、左の箱の高さを合わせる。#330)
50
+ inset: { top: BRACKET_MARGIN },
51
+ // trueのときConnector用のanchorをbox.x(PADの縦線位置)にする。
52
+ // 見た目(描画位置)は常にbox.xからの左詰め=変わらない。
53
+ anchor_at_spine: false,
54
+ // NOTE: 幅がテキスト実測依存のため、x座標は暫定値(yのみ確定)。
55
+ // draw が実測後に確定値へ更新する。
56
+ anchors: { top: { x: 0, y: 0 }, bottom: { x: 0, y: 0 } },
57
+ _id: null,
58
+ _core: null,
59
+ _class: 'SELECTION',
60
+ };
61
+ }
62
+
63
+ // core: { id, whens: { condition: string, body: ({_class?, ...})[] }[] }
64
+ normalize (core: any, box: any) {
65
+ if (!core) return null;
66
+
67
+ const data = this.dataTemplate();
68
+ data._core = core;
69
+ data._id = core.id;
70
+ data.whens = core.whens || [];
71
+ data.position = { x: box.x, y: box.y };
72
+ data.anchor_at_spine = !!box.center;
73
+
74
+ let y = BRACKET_MARGIN;
75
+ for (const when of data.whens) {
76
+ // 行の高さ(PadContainerBase の measureRows。描ける行だけ数える)。条件ボックスの高さ(CONDITION_HEIGHT)が最低の高さ
77
+ // 最初の行は、その行の余白(inset)のぶん上に置く(draw の alignTop)ので、そのぶん低くなる(#330)
78
+ const measured = this.measureRows((when as any).body || [], data.row_gap);
79
+ const bodyHeight = measured.height - measured.inset;
80
+ const rowHeight = Math.max(CONDITION_HEIGHT, bodyHeight);
81
+ y += rowHeight + data.row_gap;
82
+ }
83
+ const totalHeight = data.whens.length ? y - data.row_gap + BRACKET_MARGIN : 0;
84
+
85
+ data.anchors = {
86
+ top: { x: box.x, y: box.y },
87
+ bottom: { x: box.x, y: box.y + totalHeight },
88
+ };
89
+
90
+ return data;
91
+ }
92
+
25
93
  draw (place: any, data: any) {
26
94
  const g = place.append('g').attr('class', 'pad-selection');
27
95
 
28
96
  const rowGap = data.row_gap;
29
97
  const conditionX = BRACKET_GAP;
30
- const connectorModel = new ELEMENTS.CONNECTOR.datamodel();
31
- const connectorPainter = new ELEMENTS.CONNECTOR.painter();
32
-
98
+
33
99
  // ブラケット(ベースの四角)は常に条件ボックスより下のレイヤーに置く
34
100
  // (条件ボックスの白背景で被った部分を隠すため)。寸法(高さ)は
35
101
  // 全行分の配置が確定した後に決まるため、ここでは仮の属性で追加して
@@ -76,36 +142,10 @@ export default class Selection {
76
142
  .attr('stroke', data.stroke.color)
77
143
  .attr('stroke-width', data.stroke.width);
78
144
 
79
- let rowY = y;
80
- let rowMaxWidth = 0;
81
- let prevRowData: any = null;
82
- (when.body || []).forEach((bodyRow: any)=> {
83
- const rowClass = bodyRow._class || 'PROCESS';
84
- const entry = ELEMENTS[rowClass];
85
- if (!entry) return;
86
-
87
- const model = new entry.datamodel();
88
- const painter = new entry.painter();
89
- // center:trueにして、行のanchorを自身の中心ではなく左端
90
- // (=bodyX)にする。「連続性のある処理のベース」の規則
91
- // (要素の左端を線上に配置する)に合わせ、行同士を結ぶ
92
- // Connectorがまっすぐな縦線になるようにする。
93
- const rowData = model.normalize(bodyRow, { x: bodyX, y: rowY, width: 0, center: true });
94
- rowData.redraw = data.redraw;
95
- rowData.onElementClick = data.onElementClick;
96
- painter.draw(g, rowData);
97
-
98
- if (prevRowData) {
99
- const connector = connectorModel.normalize(prevRowData.anchors.bottom, rowData.anchors.top);
100
- connectorPainter.draw(g, connector);
101
- }
102
- prevRowData = rowData;
103
-
104
- rowMaxWidth = Math.max(rowMaxWidth, rowData.size ? rowData.size.w : 0);
105
- rowY = rowData.anchors.bottom.y + rowGap;
106
- });
107
- const bodyHeight = (when.body || []).length ? rowY - rowGap - y : 0;
108
- maxBodyWidth = Math.max(maxBodyWidth, rowMaxWidth);
145
+ // 本体(when ごとの行。PadContainerBase の drawRows)。描いた行が無ければ、高さは 0(条件ボックスの高さが最低の高さ)
146
+ const body = this.drawRows(g, when.body || [], { x: bodyX, y, rowGap, redraw: data.redraw, onElementClick: data.onElementClick, alignTop: true });
147
+ const bodyHeight = body.drawn ? body.end - y : 0;
148
+ maxBodyWidth = Math.max(maxBodyWidth, body.width);
109
149
 
110
150
  // 条件ボックスと本体をつなぐ横線(条件ボックスの高さの中央)
111
151
  g.append('line')
@@ -132,19 +172,6 @@ export default class Selection {
132
172
  .attr('x', 0).attr('y', bracketTop)
133
173
  .attr('width', BRACKET_GAP + BRACKET_OVERLAP).attr('height', bracketBottom - bracketTop);
134
174
 
135
- const x = data.position.x;
136
- g.attr('transform', `translate(${x},${data.position.y})`);
137
-
138
- data.size = { w: totalWidth, h: totalHeight };
139
-
140
- const anchorX = data.anchor_at_spine ? x : x + totalWidth / 2;
141
- data.anchors = {
142
- top: { x: anchorX, y: data.position.y },
143
- bottom: { x: anchorX, y: data.position.y + totalHeight },
144
- };
145
-
146
- attachClick(g, data);
147
-
148
- return g;
175
+ return this.finish(g, data, totalWidth, totalHeight);
149
176
  }
150
177
  }