Skip to content

Commit 0eda145

Browse files
committed
update use object keys as key instead of display name
1 parent 6b7ee76 commit 0eda145

File tree

10 files changed

+110
-92
lines changed

10 files changed

+110
-92
lines changed

packages/northstar/src/blocks/component/block.ts

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ export class ComponentBlock extends RectBlock {
7878
}
7979

8080
socketUpdater(useSocket: UseSocket): void {
81-
const contents = Object.values(this.info.contents);
82-
const events = Object.values(this.info.events);
83-
const inputs = Object.values(this.info.inputs);
84-
const outputs = Object.values(this.info.outputs);
85-
const methods = Object.values(this.info.methods);
81+
const contents = Object.entries(this.info.contents);
82+
const events = Object.entries(this.info.events);
83+
const inputs = Object.entries(this.info.inputs);
84+
const outputs = Object.entries(this.info.outputs);
85+
const methods = Object.entries(this.info.methods);
8686

8787
useSocket("parent", SingleInSocket, {
8888
type: "L",
@@ -102,73 +102,81 @@ export class ComponentBlock extends RectBlock {
102102
}
103103
}
104104

105-
const shouldHideSocket = (socketInfo: {
106-
mode: string;
107-
displayName: string;
108-
}) => {
109-
const prop = this.props[`[${socketInfo.displayName}]`];
105+
const shouldHideSocket = (
106+
k: string,
107+
socketInfo: {
108+
mode: string;
109+
displayName: string;
110+
},
111+
) => {
112+
const prop = this.props[`[${k}]`];
110113
return (
111114
(socketInfo.mode === "as-hidden-socket" && prop !== true) ||
112115
(socketInfo.mode === "as-hidable-socket" && prop === false) ||
113116
(socketInfo.mode === "as-primary-and-socket" && this.primaryFilled)
114117
);
115118
};
116119

117-
for (const content of contents) {
118-
if (content.mode === "as-primary" || shouldHideSocket(content)) continue;
119-
const socket = useSocket(content.displayName, MultiOutSocket, {
120+
for (const [k, v] of contents) {
121+
if (v.mode === "as-primary" || shouldHideSocket(k, v)) continue;
122+
const socket = useSocket(k, MultiOutSocket, {
123+
label: v.displayName,
120124
type: "L",
121125
path: PATH_OUT_RECT,
122126
hideLabel: contents.length === 1,
123-
direction: content.position ?? Direction.RIGHT,
127+
direction: v.position ?? Direction.RIGHT,
124128
});
125129

126-
if (content.mode === "as-primary-and-socket") {
130+
if (v.mode === "as-primary-and-socket") {
127131
this.getPrimaryDisabled = () => {
128132
return socket.allConnectedLines.length > 0;
129133
};
130134
}
131135
}
132136

133-
for (const event of events) {
134-
if (shouldHideSocket(event)) continue;
135-
useSocket(event.displayName, SingleOutSocket, {
137+
for (const [k, v] of events) {
138+
if (shouldHideSocket(k, v)) continue;
139+
useSocket(k, SingleOutSocket, {
140+
label: v.displayName,
136141
type: "E",
137142
path: PATH_OUT_TRIANGLE,
138-
direction: event.position ?? Direction.BOTTOM,
143+
direction: v.position ?? Direction.BOTTOM,
139144
});
140145
}
141146

142-
for (const input of inputs) {
143-
if (input.mode === "as-primary" || shouldHideSocket(input)) continue;
144-
const socket = useSocket(input.displayName, SingleInSocket, {
147+
for (const [k, v] of inputs) {
148+
if (v.mode === "as-primary" || shouldHideSocket(k, v)) continue;
149+
const socket = useSocket(k, SingleInSocket, {
150+
label: v.displayName,
145151
type: "D",
146152
path: PATH_IN_ELIPSE,
147-
direction: input.position ?? Direction.UP,
153+
direction: v.position ?? Direction.UP,
148154
});
149155

150-
if (input.mode === "as-primary-and-socket") {
156+
if (v.mode === "as-primary-and-socket") {
151157
this.getPrimaryDisabled = () => {
152158
return socket.allConnectedLines.length > 0;
153159
};
154160
}
155161
}
156162

157-
for (const output of outputs) {
158-
if (shouldHideSocket(output)) continue;
159-
useSocket(output.displayName, MultiOutSocket, {
163+
for (const [k, v] of outputs) {
164+
if (shouldHideSocket(k, v)) continue;
165+
useSocket(k, MultiOutSocket, {
166+
label: v.displayName,
160167
type: "D",
161168
path: PATH_OUT_ELIPSE,
162-
direction: output.position ?? Direction.BOTTOM,
169+
direction: v.position ?? Direction.BOTTOM,
163170
});
164171
}
165172

166-
for (const method of methods) {
167-
if (shouldHideSocket(method)) continue;
168-
useSocket(method.displayName, MultiInSocket, {
173+
for (const [k, v] of methods) {
174+
if (shouldHideSocket(k, v)) continue;
175+
useSocket(k, MultiInSocket, {
176+
label: v.displayName,
169177
type: "E",
170178
path: PATH_IN_TRIANGLE,
171-
direction: method.position ?? Direction.TOP,
179+
direction: v.position ?? Direction.TOP,
172180
});
173181
}
174182
}

packages/northstar/src/blocks/component/getProps.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export function getProps(block: ComponentBlock): PropsData {
99
const primaryInputInfo = block.primaryInputInfo;
1010
if (primaryInputInfo) {
1111
slotPos.push({
12-
name: "slots pos",
12+
key: "slots-pos",
13+
displayName: "slots pos",
1314
type: "dropdown",
1415
options: ["TOP", "BOTTOM"],
1516
getVal: () => {
@@ -23,44 +24,44 @@ export function getProps(block: ComponentBlock): PropsData {
2324

2425
return [
2526
...slotPos,
26-
...Object.values(info.props).map(
27-
(v) =>
27+
...Object.entries(info.props).map(
28+
([k, v]) =>
2829
({
2930
...v,
31+
key: k,
3032
getVal:
3133
v.type === "readonly"
3234
? () => v.value
3335
: () => {
34-
return block.props[v.name] ?? v.defaultVal;
36+
return block.props[k] ?? v.defaultVal;
3537
},
3638
setVal: (val: any) => {
37-
block.props[v.name] = val;
39+
block.props[k] = val;
3840
},
3941
}) as PropData,
4042
),
41-
...Object.values({
43+
...Object.entries({
4244
...info.contents,
4345
...info.inputs,
4446
...info.outputs,
4547
...info.events,
4648
...info.methods,
4749
})
4850
.filter(
49-
(v) => v.mode === "as-hidable-socket" || v.mode === "as-hidden-socket",
51+
([k, v]) =>
52+
v.mode === "as-hidable-socket" || v.mode === "as-hidden-socket",
5053
)
5154
.map(
52-
(v) =>
55+
([k, v]) =>
5356
({
54-
name: `[${v.displayName}]`,
57+
key: `[${k}]`,
58+
displayName: `[${v.displayName}]`,
5559
type: "switch",
5660
getVal: () => {
57-
return (
58-
block.props[`[${v.displayName}]`] ??
59-
v.mode === "as-hidable-socket"
60-
);
61+
return block.props[`[${k}]`] ?? v.mode === "as-hidable-socket";
6162
},
6263
setVal: (val: any) => {
63-
block.props[`[${v.displayName}]`] = val;
64+
block.props[`[${k}]`] = val;
6465
},
6566
}) as PropData,
6667
),

packages/northstar/src/blocks/component/toBlockOutput.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,63 +16,62 @@ import { ValidatorBlock } from "../special/validator";
1616
import { ComponentBlock } from "./block";
1717

1818
export function toBlockOutput(block: ComponentBlock) {
19-
const blockContents = Object.values(block.info.contents);
20-
const blockEvents = Object.values(block.info.events);
21-
const blockInputs = Object.values(block.info.inputs);
22-
const blockProps = Object.values(block.info.props);
23-
const blockPlugins = Object.values(block.info.plugins);
19+
const blockContents = Object.entries(block.info.contents);
20+
const blockEvents = Object.entries(block.info.events);
21+
const blockInputs = Object.entries(block.info.inputs);
22+
const blockProps = Object.entries(block.info.props);
23+
const blockPlugins = Object.entries(block.info.plugins);
2424

2525
const callbacks = {} as ComponentBlockCallbacks;
26-
for (const event of blockEvents) {
27-
callbacks[event.displayName] = singleOutSocketToOutput(
28-
block.getSocketByName(event.displayName) as SingleOutSocket,
26+
for (const [k, v] of blockEvents) {
27+
callbacks[k] = singleOutSocketToOutput(
28+
block.getSocketByName(k) as SingleOutSocket,
2929
);
3030
}
3131

3232
const props = {} as ComponentBlockProps;
33-
for (const v of blockProps) {
33+
for (const [k, v] of blockProps) {
3434
if (v.type !== "readonly") {
35-
props[v.name] = block.props[v.name] ?? v.defaultVal;
35+
props[k] = block.props[k] ?? v.defaultVal;
3636
}
3737
}
38-
for (const input of blockInputs) {
38+
for (const [k, v] of blockInputs) {
3939
if (
40-
input.mode === "as-primary" ||
41-
(input.mode === "as-primary-and-socket" && block.primaryFilled)
40+
v.mode === "as-primary" ||
41+
(v.mode === "as-primary-and-socket" && block.primaryFilled)
4242
)
4343
continue;
44-
const socket = block.getSocketByName(input.displayName)
45-
?.allConnectedLines[0]?.a;
46-
props[input.displayName] = {
44+
const socket = block.getSocketByName(k)?.allConnectedLines[0]?.a;
45+
props[k] = {
4746
blockId: socket?.block.id ?? NaN,
4847
socketName: socket?.label ?? "",
4948
};
5049
}
5150

5251
let children = {} as ComponentBlockChildren;
53-
for (const content of blockContents) {
52+
for (const [k, v] of blockContents) {
5453
if (
55-
content.mode === "as-primary" ||
56-
(content.mode === "as-primary-and-socket" && block.primaryFilled)
54+
v.mode === "as-primary" ||
55+
(v.mode === "as-primary-and-socket" && block.primaryFilled)
5756
)
5857
continue;
59-
children[content.displayName] =
58+
children[k] =
6059
block
61-
.getSocketByName(content.displayName)
60+
.getSocketByName(k)
6261
?.allConnectedLines.map((l) => (l.b as Socket).block)
6362
.sort((a, b) => a.boardY - b.boardY)
6463
.map((b) => b.id) ?? [];
6564
}
6665

6766
let plugins = {} as ComponentBlockPlugins;
68-
for (const plugin of blockPlugins) {
69-
if (plugin.kind !== "input-plugin") continue;
67+
for (const [k, v] of blockPlugins) {
68+
if (v.kind !== "input-plugin") continue;
7069

7170
const validators: ValidatorBlock[] = [];
7271
let currentPluginBlock: Block | undefined = block;
7372
while (true) {
7473
currentPluginBlock = currentPluginBlock.dockedByBlocks.find(
75-
([d, b]) => d === plugin.direction,
74+
([d, b]) => d === v.direction,
7675
)?.[1];
7776
if (!currentPluginBlock) {
7877
break;
@@ -84,7 +83,7 @@ export function toBlockOutput(block: ComponentBlock) {
8483
}
8584
}
8685

87-
plugins[plugin.displayName] = `$ => {
86+
plugins[k] = `$ => {
8887
${validators
8988
.map((v) => {
9089
if (v.inputValue.value.length === 0)

packages/northstar/src/blocks/special/FuncBlockBase.r.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ export abstract class FuncBlockBase extends RectBlock implements SpecialBlock {
189189
getProps(): PropsData {
190190
return [
191191
{
192-
name: "slots pos",
192+
key: "slots-pos",
193+
displayName: "slots pos",
193194
type: "dropdown",
194195
options: ["TOP", "BOTTOM"],
195196
getVal: () => {
@@ -200,7 +201,8 @@ export abstract class FuncBlockBase extends RectBlock implements SpecialBlock {
200201
},
201202
} satisfies PropData,
202203
{
203-
name: "output pos",
204+
key: "output-pos",
205+
displayName: "output pos",
204206
type: "dropdown",
205207
options: ["BOTTOM", "TOP"],
206208
getVal: () => {

packages/northstar/src/blocks/special/do.r.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ export class DoBlock extends RectBlock {
6565
getProps(): PropsData {
6666
return [
6767
{
68-
name: "number",
68+
key: "number",
69+
displayName: "number",
6970
type: "number",
7071
min: 2,
7172
max: 5,
@@ -77,7 +78,8 @@ export class DoBlock extends RectBlock {
7778
},
7879
},
7980
{
80-
name: "rotate",
81+
key: "rotate",
82+
displayName: "rotate",
8183
type: "switch",
8284
getVal: () => {
8385
return this.rotate;

packages/northstar/src/blocks/special/if.r.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export class IfElseBlock extends RectBlock implements SpecialBlock {
7979
getProps(): PropsData {
8080
return [
8181
{
82-
name: "[else]",
82+
key: "[else]",
83+
displayName: "[else]",
8384
type: "switch",
8485
getVal: () => {
8586
return this.hasElse;

packages/northstar/src/blocks/special/imp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ export class ImpBlock extends FuncBlockBase {
7070
getProps(): PropsData {
7171
return [
7272
{
73-
name: "[then]",
73+
key: "[then]",
74+
displayName: "[then]",
7475
type: "switch",
7576
getVal: () => {
7677
return this.hasThen;

packages/northstar/src/utils/props.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { currentGraph } from "../store";
22

33
export interface PropDataBase {
4-
name: string;
4+
key: string;
5+
displayName: string;
56
}
67

78
export interface TextPropData extends PropDataBase {
@@ -49,7 +50,7 @@ export function mergeProps([props0, ...propsRest]: PropsData[]) {
4950
for (const v of props0) {
5051
if (
5152
propsRest.every((ps) =>
52-
ps.some((p) => p.name === v.name && p.type === v.type),
53+
ps.some((p) => p.displayName === v.displayName && p.type === v.type),
5354
)
5455
) {
5556
mergedProps.push(v);

packages/northstar/src/views/properties.r.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { getSelectedProps } from "../utils/props";
55

66
export default view(_ => {
77
const props = getSelectedProps();
8-
_.for(props, "name", p => {
8+
_.for(props, "key", p => {
99
const r = ref() as MainElRef;
1010
_.$cls`col-span-1 flex justify-center items-center border-b border-gray-500 h-8`;
1111
_._div(
@@ -14,7 +14,7 @@ export default view(_ => {
1414
(r.current?.$mainEl?.firstChild as HTMLElement | null | undefined)?.focus();
1515
},
1616
},
17-
p.name,
17+
p.displayName,
1818
);
1919
_.$cls`col-span-2 h-8`;
2020
if (p.type === "text") {

0 commit comments

Comments
 (0)