@reeve-iot/workflow-mui 1.1.0 → 1.1.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.
@@ -4,7 +4,7 @@ import { ERROR_OUTPUT_NAME, ERROR_OUTPUT_TYPE, WORKFLOW_INPUT_BLOCK_TYPE_ID, WOR
4
4
  import { useEditorColors } from '../theme/editorColors.js';
5
5
  import { NODE_HEADER_HEIGHT, NODE_ITEM_HEIGHT, NODE_WIDTH } from './geometry.js';
6
6
  import { PORT_RADIUS, PORT_STROKE_WIDTH } from './portGeometry.js';
7
- import { PORT_LABEL_FONT_SIZE, PORT_LABEL_INSET, layoutPortLabelRow } from './portLabelLayout.js';
7
+ import { PORT_LABEL_FONT_SIZE, PORT_LABEL_INSET, layoutFittedPortLabelRow } from './portLabelLayout.js';
8
8
  import { useNodePorts } from './nodePorts.js';
9
9
  import { useGraphTopology } from './graphTopology.js';
10
10
  import { unavailableBlockText } from './unavailableBlock.js';
@@ -141,9 +141,16 @@ export const BlockNode = memo(function BlockNode({ node, isSelected, isConnectin
141
141
  const connectable = !readOnly && unavailable === undefined;
142
142
  const inputTerms = inputs.map((port) => termOf('inputs', port));
143
143
  const outputTerms = outputs.map((port) => termOf('outputs', port));
144
- const inputLabels = inputs.map((port, index) => `${port.displayName}${port.isRequired ? '*' : ''}${typeSuffix(inputTerms[index])}`);
145
- const outputLabels = outputs.map((port, index) => `${port.displayName}${typeSuffix(outputTerms[index])}`);
146
- const labelRows = Array.from({ length: Math.max(inputs.length, outputs.length) }, (_, index) => layoutPortLabelRow(inputLabels[index], outputLabels[index], NODE_WIDTH));
144
+ const inputNames = inputs.map((port) => `${port.displayName}${port.isRequired ? '*' : ''}`);
145
+ const outputNames = outputs.map((port) => port.displayName);
146
+ const inputLabels = inputNames.map((name, index) => `${name}${typeSuffix(inputTerms[index])}`);
147
+ const outputLabels = outputNames.map((name, index) => `${name}${typeSuffix(outputTerms[index])}`);
148
+ const labelText = (labels, names, index) => {
149
+ const full = labels[index];
150
+ const name = names[index];
151
+ return full === undefined || name === undefined ? undefined : { full, name };
152
+ };
153
+ const labelRows = Array.from({ length: Math.max(inputs.length, outputs.length) }, (_, index) => layoutFittedPortLabelRow(labelText(inputLabels, inputNames, index), labelText(outputLabels, outputNames, index), NODE_WIDTH));
147
154
  const displayName = isEntry ? entryNodeLabel : isExit ? exitNodeLabel : (unavailable?.title ?? blockType?.displayName ?? '');
148
155
  const subCanvasNames = isSynthetic ? [] : (blockType?.subCanvasNames ?? []);
149
156
  const hasSubCanvases = subCanvasNames.length > 0;
@@ -192,7 +199,7 @@ export const BlockNode = memo(function BlockNode({ node, isSelected, isConnectin
192
199
  return;
193
200
  event.stopPropagation();
194
201
  onConnectionEnd(node.id);
195
- }, style: { cursor: 'crosshair' } }), _jsxs("g", { ...bodyPointer, style: { cursor: 'grab' }, children: [_jsx("title", { children: labelHint(inputLabels[index], inputTerms[index]) }), _jsx("text", { x: PORT_LABEL_INSET, y: portY(index) + 5, clipPath: `url(#port-in-clip-${clipId}-${index})`, "data-testid": `workflow-port-label-${node.id}-input-${input.name}`, ...termAttributes(inputTerms[index]), fill: inputTerms[index]?.knowledge === 'refused' ? c.danger : c.textDim, style: { fontSize: PORT_LABEL_FONT_SIZE, ...(inputTerms[index]?.stale === true ? { fontStyle: 'italic', opacity: 0.7 } : {}) }, children: inputLabels[index] })] })] }, input.name))), outputs.map((output, index) => {
202
+ }, style: { cursor: 'crosshair' } }), _jsxs("g", { ...bodyPointer, style: { cursor: 'grab' }, children: [_jsx("title", { children: labelHint(inputLabels[index], inputTerms[index]) }), _jsx("text", { x: PORT_LABEL_INSET, y: portY(index) + 5, clipPath: `url(#port-in-clip-${clipId}-${index})`, "data-testid": `workflow-port-label-${node.id}-input-${input.name}`, ...termAttributes(inputTerms[index]), fill: inputTerms[index]?.knowledge === 'refused' ? c.danger : c.textDim, style: { fontSize: PORT_LABEL_FONT_SIZE, ...(inputTerms[index]?.stale === true ? { fontStyle: 'italic', opacity: 0.7 } : {}) }, children: labelRows[index]?.input })] })] }, input.name))), outputs.map((output, index) => {
196
203
  const isErrorPort = output.name === ERROR_PORT_NAME;
197
204
  return (_jsxs("g", { children: [_jsx("circle", { cx: NODE_WIDTH, cy: portY(index), r: PORT_RADIUS, "data-testid": `workflow-port-${node.id}-output-${output.name}`, "aria-label": `${displayName} output ${output.displayName}`, fill: c.nodeFill, stroke: isErrorPort ? c.danger : isEntry ? c.headerInput : c.nodeStroke, strokeWidth: PORT_STROKE_WIDTH, onMouseDown: (event) => {
198
205
  if (!connectable || event.button !== 0)
@@ -200,7 +207,7 @@ export const BlockNode = memo(function BlockNode({ node, isSelected, isConnectin
200
207
  event.stopPropagation();
201
208
  event.preventDefault();
202
209
  onConnectionStart(node.id, node.position.x + NODE_WIDTH, node.position.y + portY(index));
203
- }, style: { cursor: 'crosshair' } }), _jsxs("g", { ...bodyPointer, style: { cursor: 'grab' }, children: [_jsx("title", { children: labelHint(outputLabels[index], outputTerms[index]) }), _jsx("text", { x: NODE_WIDTH - PORT_LABEL_INSET, y: portY(index) + 5, textAnchor: "end", clipPath: `url(#port-out-clip-${clipId}-${index})`, "data-testid": `workflow-port-label-${node.id}-output-${output.name}`, ...termAttributes(outputTerms[index]), fill: isErrorPort || outputTerms[index]?.knowledge === 'refused' ? c.danger : c.textDim, style: { fontSize: PORT_LABEL_FONT_SIZE, ...(outputTerms[index]?.stale === true ? { fontStyle: 'italic', opacity: 0.7 } : {}) }, children: outputLabels[index] })] })] }, output.name));
210
+ }, style: { cursor: 'crosshair' } }), _jsxs("g", { ...bodyPointer, style: { cursor: 'grab' }, children: [_jsx("title", { children: labelHint(outputLabels[index], outputTerms[index]) }), _jsx("text", { x: NODE_WIDTH - PORT_LABEL_INSET, y: portY(index) + 5, textAnchor: "end", clipPath: `url(#port-out-clip-${clipId}-${index})`, "data-testid": `workflow-port-label-${node.id}-output-${output.name}`, ...termAttributes(outputTerms[index]), fill: isErrorPort || outputTerms[index]?.knowledge === 'refused' ? c.danger : c.textDim, style: { fontSize: PORT_LABEL_FONT_SIZE, ...(outputTerms[index]?.stale === true ? { fontStyle: 'italic', opacity: 0.7 } : {}) }, children: labelRows[index]?.output })] })] }, output.name));
204
211
  }), hasSubCanvases && (_jsxs(_Fragment, { children: [_jsx("line", { x1: 8, y1: portsEndY, x2: NODE_WIDTH - 8, y2: portsEndY, stroke: c.nodeStroke, strokeWidth: 1, style: { pointerEvents: 'none' } }), subCanvasNames.map((canvasName, index) => {
205
212
  const buttonY = portsEndY + BUTTONS_PADDING_TOP + index * (BUTTON_HEIGHT + BUTTON_GAP);
206
213
  return (_jsxs("g", { style: { cursor: 'pointer' }, children: [_jsx("rect", { x: 8, y: buttonY, width: NODE_WIDTH - 16, height: BUTTON_HEIGHT, rx: 4, ry: 4, fill: c.infoBg, stroke: c.infoBorder, strokeWidth: 1, style: { pointerEvents: 'none' } }), _jsxs("text", { x: NODE_WIDTH / 2, y: buttonY + BUTTON_HEIGHT / 2 + 5, textAnchor: "middle", fill: c.info, style: { fontSize: 11, fontWeight: 500, pointerEvents: 'none' }, children: ["\u25B6 Enter: ", canvasName] }), _jsx("rect", { x: 8, y: buttonY, width: NODE_WIDTH - 16, height: BUTTON_HEIGHT, rx: 4, ry: 4, fill: "transparent", "data-subcanvas-entry": `${node.id}:${canvasName}`, onClick: (event) => {
@@ -7,3 +7,19 @@ export interface PortLabelRowLayout {
7
7
  outputStart: number;
8
8
  }
9
9
  export declare function layoutPortLabelRow(input: string | undefined, output: string | undefined, nodeWidth: number): PortLabelRowLayout;
10
+ /** A port label in its two drawable forms: the whole label, and the port's name without the type. */
11
+ export interface PortLabelText {
12
+ full: string;
13
+ name: string;
14
+ }
15
+ export interface FittedPortLabelRow extends PortLabelRowLayout {
16
+ input: string | undefined;
17
+ output: string | undefined;
18
+ }
19
+ /** The longest form of a label that fits `room`: the whole label, else the name alone, else the name
20
+ * shortened to an ellipsis, keeping a required input's mark. A label is never cut mid-glyph by the node edge. */
21
+ export declare function fitPortLabel(label: PortLabelText, room: number): string;
22
+ /** Lays out one port row and chooses the text each side draws. A side whose whole label does not fit
23
+ * its share falls back to the port name; the row is then shared out again, and a side gets its whole
24
+ * label back when the room the other side freed is enough for it. */
25
+ export declare function layoutFittedPortLabelRow(input: PortLabelText | undefined, output: PortLabelText | undefined, nodeWidth: number): FittedPortLabelRow;
@@ -17,10 +17,14 @@ const EM_WIDTHS = [
17
17
  ['%', 0.889],
18
18
  ['W', 0.944],
19
19
  ['@', 1.015],
20
+ ['\u2026', 1],
20
21
  ];
21
22
  const UNLISTED_EM_WIDTH = 0.6;
22
23
  const WIDE_SCRIPT_START = 0x2e80;
23
24
  const ESTIMATE_MARGIN = 1.1;
25
+ const ELLIPSIS = '\u2026';
26
+ const REQUIRED_MARK = '*';
27
+ const ROUNDING_TOLERANCE = 1e-6;
24
28
  const emWidthOf = new Map(EM_WIDTHS.flatMap(([chars, width]) => [...chars].map((ch) => [ch, width])));
25
29
  export function estimatePortLabelWidth(text, fontSize = PORT_LABEL_FONT_SIZE) {
26
30
  let em = 0;
@@ -45,3 +49,49 @@ export function layoutPortLabelRow(input, output, nodeWidth) {
45
49
  const inputEnd = PORT_LABEL_INSET + inputShare;
46
50
  return { inputEnd, outputStart: inputEnd + PORT_LABEL_GAP };
47
51
  }
52
+ const fits = (text, room) => estimatePortLabelWidth(text) * ESTIMATE_MARGIN <= room + ROUNDING_TOLERANCE;
53
+ /** The longest form of a label that fits `room`: the whole label, else the name alone, else the name
54
+ * shortened to an ellipsis, keeping a required input's mark. A label is never cut mid-glyph by the node edge. */
55
+ export function fitPortLabel(label, room) {
56
+ if (fits(label.full, room))
57
+ return label.full;
58
+ if (fits(label.name, room))
59
+ return label.name;
60
+ const mark = label.name.endsWith(REQUIRED_MARK) ? REQUIRED_MARK : '';
61
+ const glyphs = [...label.name.slice(0, label.name.length - mark.length)];
62
+ for (let keep = glyphs.length - 1; keep > 1; keep--) {
63
+ const shortened = glyphs.slice(0, keep).join('').trimEnd() + ELLIPSIS + mark;
64
+ if (fits(shortened, room))
65
+ return shortened;
66
+ }
67
+ return glyphs.slice(0, 1).join('') + ELLIPSIS + mark;
68
+ }
69
+ /** Lays out one port row and chooses the text each side draws. A side whose whole label does not fit
70
+ * its share falls back to the port name; the row is then shared out again, and a side gets its whole
71
+ * label back when the room the other side freed is enough for it. */
72
+ export function layoutFittedPortLabelRow(input, output, nodeWidth) {
73
+ const rowEnd = nodeWidth - PORT_LABEL_INSET;
74
+ const layout = (inputText, outputText) => {
75
+ const row = layoutPortLabelRow(inputText, outputText, nodeWidth);
76
+ return { row, inputRoom: row.inputEnd - PORT_LABEL_INSET, outputRoom: rowEnd - row.outputStart };
77
+ };
78
+ const whole = layout(input?.full, output?.full);
79
+ let inputText = input === undefined || fits(input.full, whole.inputRoom) ? input?.full : input.name;
80
+ let outputText = output === undefined || fits(output.full, whole.outputRoom) ? output?.full : output.name;
81
+ if (input !== undefined && inputText !== input.full) {
82
+ const tried = layout(input.full, outputText);
83
+ if (fits(input.full, tried.inputRoom) && (outputText === undefined || fits(outputText, tried.outputRoom)))
84
+ inputText = input.full;
85
+ }
86
+ if (output !== undefined && outputText !== output.full) {
87
+ const tried = layout(inputText, output.full);
88
+ if (fits(output.full, tried.outputRoom) && (inputText === undefined || fits(inputText, tried.inputRoom)))
89
+ outputText = output.full;
90
+ }
91
+ const { row, inputRoom, outputRoom } = layout(inputText, outputText);
92
+ return {
93
+ ...row,
94
+ input: input === undefined ? undefined : fitPortLabel({ full: inputText ?? input.full, name: input.name }, inputRoom),
95
+ output: output === undefined ? undefined : fitPortLabel({ full: outputText ?? output.full, name: output.name }, outputRoom),
96
+ };
97
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reeve-iot/workflow-mui",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "Material UI presentation layer for @reeve-iot/workflow-core: canvas, block nodes, palette and config panels.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -41,7 +41,7 @@
41
41
  "typecheck": "tsc -b"
42
42
  },
43
43
  "dependencies": {
44
- "@reeve-iot/workflow-core": "1.1.0"
44
+ "@reeve-iot/workflow-core": "1.1.1"
45
45
  },
46
46
  "peerDependencies": {
47
47
  "@emotion/react": "^11.0.0",