Skip to content

Commit 0cc3213

Browse files
yangzhang75claude
andcommitted
fix(workflow-form): let ui-udf-parameters show the form's renames and hides, and add a parameter to the card's operator
The ui-udf-parameters widget hides formly's labels and draws fixed column headers (Value / Name / Type), so the author's per-sub-field overrides from the Form View changed nothing a reader saw: a renamed column kept its old name and a hidden one kept its header over an empty cell. On a Form View card the widget now decides which columns are in view from the first row's sub-fields (a hidden column, or one whose group is hidden, is left out, header and cells alike), read live, since the form's hide lands on a row after formly has built it; the header shows the author's name for the column, else the panel's fixed label. The Form View names the operator on every field it renders (props.operatorID, FIELD_OPERATOR_ID_PROP / fieldOperatorID), since nothing is highlighted on the form; the same mark tells a shared widget which host it is on. Add parameter uses it: it inserts a declaration into an operator's code, and it took that operator from the canvas highlight, so on the form it edited the wrong operator or failed. It now adds to the field's operator on a card, and to the highlighted one on the panel. The declared parameters then reach the operator: the panel's frame writes them only for the operator it shows while it acts as the editor, so the form writes them when the frame does not (a writer only, the operator still on the graph). A reader's value cells stay locked: the Form View marks a reader's field props.disabled, and the widget's own "the value is editable" used to re-enable the control after formly had disabled it. And the widget's own lock on the Name and Type cells (inferred from the code; a name typed there no longer matches a declaration) now holds under any host: the panel enables its whole form group when it turns interactive, and formly mirrors a control's state into props.disabled, so a one-time disable did not survive on the canvas while the form showed the cells locked. The widget holds a locked cell through the control's disabled-change hook, which disables it again whenever a host enables it; a cell configured editable is released. Closes apache#8763 Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent cfca8e1 commit 0cc3213

14 files changed

Lines changed: 1285 additions & 32 deletions
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { describe, expect, it } from "vitest";
20+
import { SUB_FIELD_VIEW_PROP, subFieldLabel, subFieldsInView, subFieldViews } from "./sub-field-view";
21+
22+
describe("sub-field view", () => {
23+
const parts = [
24+
{ path: "value", label: "Value" },
25+
{ path: "attribute.attributeName", label: "Name" },
26+
{ path: "attribute.attributeType", label: "Type" },
27+
] as const;
28+
29+
const host = (view?: Record<string, { label: string; shown: boolean }>) => ({
30+
props: view === undefined ? {} : { [SUB_FIELD_VIEW_PROP]: view },
31+
});
32+
33+
describe("a host that publishes nothing", () => {
34+
// The operator property panel: no author, no overrides, so a widget's own headers are the answer.
35+
it("leaves every part in view, under the widget's own name", () => {
36+
const field = host();
37+
expect(subFieldViews(field)).toBeUndefined();
38+
expect(subFieldsInView(field, parts)).toBe(parts);
39+
expect(subFieldLabel(field, "attribute.attributeName", "Name")).toBe("Name");
40+
});
41+
});
42+
43+
describe("a host that publishes", () => {
44+
it("drops what the author hid and keeps the rest in the widget's order", () => {
45+
const field = host({
46+
value: { label: "Value", shown: true },
47+
"attribute.attributeName": { label: "Parameter", shown: true },
48+
"attribute.attributeType": { label: "Type", shown: false },
49+
});
50+
51+
expect(subFieldsInView(field, parts).map(p => p.path)).toEqual(["value", "attribute.attributeName"]);
52+
});
53+
54+
it("gives the author's name, and the widget's own where the author kept it", () => {
55+
const field = host({
56+
"attribute.attributeName": { label: "Parameter", shown: true },
57+
"attribute.attributeType": { label: "Type", shown: true },
58+
});
59+
60+
expect(subFieldLabel(field, "attribute.attributeName", "Name")).toBe("Parameter");
61+
expect(subFieldLabel(field, "attribute.attributeType", "Type")).toBe("Type");
62+
});
63+
64+
// A part the host said nothing about is one the author was never offered, not one they hid.
65+
it("keeps a part the host did not mention", () => {
66+
const field = host({ "attribute.attributeType": { label: "Type", shown: false } });
67+
68+
expect(subFieldsInView(field, parts).map(p => p.path)).toEqual(["value", "attribute.attributeName"]);
69+
});
70+
71+
it("falls back to the widget's name for a part the host did not mention", () => {
72+
expect(subFieldLabel(host({}), "value", "Value")).toBe("Value");
73+
});
74+
});
75+
76+
// A widget reads this from its template, so it runs on every check; a new array each time would
77+
// have an ngFor tear its header down and rebuild it, losing the focus in a cell being used.
78+
describe("identity", () => {
79+
it("returns the same array while the answer has not changed", () => {
80+
const field = host({ "attribute.attributeType": { label: "Type", shown: false } });
81+
82+
const first = subFieldsInView(field, parts);
83+
expect(subFieldsInView(field, parts)).toBe(first);
84+
});
85+
86+
it("returns a new array once the answer changes", () => {
87+
const field = host({ "attribute.attributeType": { label: "Type", shown: false } });
88+
const first = subFieldsInView(field, parts);
89+
90+
field.props = {
91+
[SUB_FIELD_VIEW_PROP]: {
92+
"attribute.attributeType": { label: "Type", shown: true },
93+
},
94+
};
95+
96+
const second = subFieldsInView(field, parts);
97+
expect(second).not.toBe(first);
98+
expect(second.map(p => p.path)).toEqual(parts.map(p => p.path));
99+
});
100+
101+
it("keeps each host's answer apart", () => {
102+
const hidden = host({ "attribute.attributeType": { label: "Type", shown: false } });
103+
const whole = host({});
104+
105+
expect(subFieldsInView(hidden, parts)).toHaveLength(2);
106+
expect(subFieldsInView(whole, parts)).toHaveLength(3);
107+
expect(subFieldsInView(hidden, parts)).toHaveLength(2);
108+
});
109+
});
110+
111+
it("ignores a published value that is not a map", () => {
112+
expect(subFieldViews({ props: { [SUB_FIELD_VIEW_PROP]: "nonsense" } })).toBeUndefined();
113+
expect(subFieldViews({ props: {} })).toBeUndefined();
114+
expect(subFieldViews({})).toBeUndefined();
115+
});
116+
});
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
/**
21+
* What the Form View's author decided about each sub-field of an exposed input, resolved for the
22+
* reader in front of it, for a widget that draws its own chrome.
23+
*
24+
* Formly expresses a rename as the field's label and a hide as `hide`, and renders both through the
25+
* field's wrapper. A widget that renders its sub-fields itself -- a table with its own column
26+
* headers, a legend, a summary line -- never goes through that wrapper, so it shows neither: a
27+
* renamed column keeps its old heading, and a hidden one keeps its heading over an empty cell
28+
* (#8763). Reading the state back out of the rendered fields is not the answer either: it is only
29+
* there once formly has built a row, so a table whose operator declares nothing yet has nothing to
30+
* read, and a group's hide is marked on the group while its children are merely not rendered.
31+
*
32+
* So the page that holds the author's decisions publishes them, once, on the field it hands the
33+
* widget ({@link SUB_FIELD_VIEW_PROP}), and the widget asks here. Nothing published -- the operator
34+
* property panel, where there is no author and no overrides -- means every sub-field is in view
35+
* under its own name, which is what a widget's fixed headers already say.
36+
*/
37+
export interface SubFieldView {
38+
/**
39+
* The name the author gave this sub-field, or "" where they kept the one it came with: a widget
40+
* has a name of its own for the part it draws (a table's column heading) and keeps it then.
41+
*/
42+
readonly label: string;
43+
/** Whether this sub-field is in view at all. False only for a reader looking at a hidden one. */
44+
readonly shown: boolean;
45+
}
46+
47+
/** Where a host publishes {@link SubFieldView}s, keyed by sub-field path, in the field's `props`. */
48+
export const SUB_FIELD_VIEW_PROP = "subFieldView";
49+
50+
/** What the host published for this field, if it is a host that publishes. */
51+
export function subFieldViews(field: {
52+
props?: Record<string, unknown>;
53+
}): Readonly<Record<string, SubFieldView>> | undefined {
54+
const published = field.props?.[SUB_FIELD_VIEW_PROP];
55+
return typeof published === "object" && published !== null
56+
? (published as Readonly<Record<string, SubFieldView>>)
57+
: undefined;
58+
}
59+
60+
/** A sub-field's name: the author's where they gave one, else the one the widget draws itself. */
61+
export function subFieldLabel(field: { props?: Record<string, unknown> }, path: string, fallback: string): string {
62+
const label = subFieldViews(field)?.[path]?.label;
63+
return label ? label : fallback;
64+
}
65+
66+
/**
67+
* The parts of a widget's own layout the reader should see, in the widget's order: everything, bar
68+
* what the author hid. A part the host said nothing about stays -- it is one the author was never
69+
* offered, not one they hid.
70+
*
71+
* The same array instance comes back while the answer has not changed, so a widget may call this
72+
* from its template (the state arrives after the first check, so it has to be read live) without
73+
* an `ngFor` tearing its rows down on every pass.
74+
*/
75+
export function subFieldsInView<T extends { readonly path: string }>(
76+
field: { props?: Record<string, unknown> },
77+
parts: readonly T[]
78+
): readonly T[] {
79+
const views = subFieldViews(field);
80+
const next = views === undefined ? parts : parts.filter(part => shown(views, part.path));
81+
const previous = lastInView.get(field) as readonly T[] | undefined;
82+
if (previous !== undefined && previous.length === next.length && previous.every((p, i) => p === next[i])) {
83+
return previous;
84+
}
85+
lastInView.set(field, next);
86+
return next;
87+
}
88+
89+
/**
90+
* Whether a path is in view: its own entry, and every group it sits in. Hiding a group hides what
91+
* is inside it, and the author hid the group, so that is the entry the host published.
92+
*/
93+
function shown(views: Readonly<Record<string, SubFieldView>>, path: string): boolean {
94+
const segments = path.split(".");
95+
return segments.every((_, i) => views[segments.slice(0, i + 1).join(".")]?.shown !== false);
96+
}
97+
98+
/** Per field, the array {@link subFieldsInView} last returned, so an unchanged answer keeps its identity. */
99+
const lastInView = new WeakMap<object, readonly unknown[]>();

‎frontend/src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.formly.spec.ts‎

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { FieldType, FieldTypeConfig, FormlyFieldConfig, FormlyForm, FormlyModule
2424
import { NzInputModule } from "ng-zorro-antd/input";
2525
import { NotificationService } from "../../../common/service/notification/notification.service";
2626
import { UiUdfParametersSyncService } from "../../service/code-editor/ui-udf-parameters-sync.service";
27+
import { ExecuteWorkflowService } from "../../service/execute-workflow/execute-workflow.service";
2728
import { WorkflowActionService } from "../../service/workflow-graph/model/workflow-action.service";
2829
import { UiUdfParametersComponent } from "./ui-udf-parameters.component";
2930

@@ -73,13 +74,23 @@ class HostComponent {
7374
}
7475

7576
/**
76-
* Real formly, no mocks of it: the operator property panel enables and disables its whole form
77-
* group (setInteractivity), and formly mirrors a control's state into props.disabled, so the
78-
* widget's lock on the Name and Type columns has to survive a wholesale enable(). The widget's own
79-
* spec drives populate on hand-made rows; this one proves the lock under formly's own build.
77+
* Real formly, no mocks of it. The widget's own spec drives populate on hand-made rows, which says
78+
* nothing about what formly builds from a row template, what it does with a field the Form View
79+
* marked for a reader, or what survives a host that enables the whole group. This one stands the
80+
* table up the way the app does -- a form, the builder row template formly-jsonschema produces,
81+
* nz-input cells -- so the rows are formly's own: the reader's lock is the one formly applied, and
82+
* the widget's lock on the Name and Type columns has to survive the panel's wholesale enable()
83+
* (setInteractivity), which formly mirrors into props.disabled.
8084
*/
8185
describe("UiUdfParametersComponent under real formly", () => {
86+
/** The canvas's modification flag, which the Form View holds off outside edit mode. */
87+
let modificationEnabled = true;
88+
/** Whether a run is in flight, the half of the canvas's rule the flag above no longer carries. */
89+
let executionInFlight = false;
90+
8291
beforeEach(async () => {
92+
modificationEnabled = true;
93+
executionInFlight = false;
8394
await TestBed.configureTestingModule({
8495
imports: [
8596
HostComponent,
@@ -94,18 +105,29 @@ describe("UiUdfParametersComponent under real formly", () => {
94105
{
95106
provide: WorkflowActionService,
96107
useValue: {
97-
checkWorkflowModificationEnabled: () => true,
108+
checkWorkflowModificationEnabled: () => modificationEnabled,
98109
getJointGraphWrapper: () => ({ getCurrentHighlightedOperatorIDs: () => ["op-1"] }),
99110
},
100111
},
101112
{ provide: UiUdfParametersSyncService, useValue: { addParameter: vi.fn() } },
102113
{ provide: NotificationService, useValue: { error: vi.fn() } },
114+
{ provide: ExecuteWorkflowService, useValue: { isExecutionInFlight: () => executionInFlight } },
103115
],
104116
}).compileComponents();
105117
});
106118

107-
function render() {
119+
/**
120+
* Renders the host; `topProps` go on the table's field (the Form View puts its marks there), `rows`
121+
* replace the two parameters the model declares by default.
122+
*/
123+
function render(topProps?: Record<string, unknown>, rows?: unknown[]) {
108124
const fixture = TestBed.createComponent(HostComponent);
125+
if (topProps) {
126+
fixture.componentInstance.fields[0].props = topProps;
127+
}
128+
if (rows) {
129+
fixture.componentInstance.model = { uiParameters: rows as HostComponent["model"]["uiParameters"] };
130+
}
109131
fixture.detectChanges();
110132
const form = fixture.componentInstance.form;
111133
const cell = (row: number, path: string) => form.get(`uiParameters.${row}.${path}`)!;
@@ -124,6 +146,44 @@ describe("UiUdfParametersComponent under real formly", () => {
124146
expect(locks()).toEqual([false, true, true, true]);
125147
});
126148

149+
const addButton = (fixture: { nativeElement: HTMLElement }) =>
150+
fixture.nativeElement.querySelector(".add-parameter-button");
151+
152+
it("offers Add parameter on a writer's Form View card without the canvas's flag, and not on a reader's, whose field is disabled", () => {
153+
modificationEnabled = false;
154+
155+
const writer = render({ operatorID: "op-form" });
156+
expect(addButton(writer.fixture)).not.toBeNull();
157+
158+
const reader = render({ operatorID: "op-form", disabled: true });
159+
expect(reader.locks()).toEqual([true, true, true, true]);
160+
expect(addButton(reader.fixture)).toBeNull();
161+
});
162+
163+
// formly disables the controls it builds under a disabled field, not the array itself, and an
164+
// array with no rows reads as enabled; the form's mark on the field is what says "a reader's".
165+
it("offers Add parameter to a writer whose UDF declares no parameters yet, and not to a reader, though the empty array control is enabled", () => {
166+
modificationEnabled = false;
167+
168+
const writer = render({ operatorID: "op-form" }, []);
169+
expect(writer.form.get("uiParameters")!.enabled).toBe(true);
170+
expect(addButton(writer.fixture)).not.toBeNull();
171+
172+
const reader = render({ operatorID: "op-form", disabled: true }, []);
173+
expect(reader.form.get("uiParameters")!.enabled).toBe(true);
174+
expect(addButton(reader.fixture)).toBeNull();
175+
});
176+
177+
// A declaration goes into the UDF's code, which a run must not have rewritten under it; the
178+
// values on the card stay editable through a run, as they always were.
179+
it("takes Add parameter off a writer's card while a run is in flight, leaving the values editable", () => {
180+
executionInFlight = true;
181+
182+
const writer = render({ operatorID: "op-form" });
183+
184+
expect(addButton(writer.fixture)).toBeNull();
185+
expect(writer.locks()).toEqual([false, true, true, true]);
186+
});
127187
// The lock goes back on in the microtask after the host's enable() (see keepLocked), so the
128188
// assertions wait one; what the reader sees is read from the inputs, not only the controls.
129189
const settled = async (fixture: { detectChanges(): void }) => {

‎frontend/src/app/workspace/component/ui-udf-parameters/ui-udf-parameters.component.html‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<div class="ui-udf-parameters-field">
2020
<button
2121
class="add-parameter-button"
22-
*ngIf="workflowModificationEnabled && !draftVisible"
22+
*ngIf="editable && !draftVisible"
2323
nz-button
2424
nzType="dashed"
2525
nzBlock
@@ -37,8 +37,8 @@
3737
<div class="ui-udf-parameter-row header">
3838
<div
3939
class="field-cell"
40-
*ngFor="let column of fieldColumns">
41-
<span class="col-title">{{ column.label }}</span>
40+
*ngFor="let column of columns">
41+
<span class="col-title">{{ columnLabel(column) }}</span>
4242
</div>
4343
</div>
4444

@@ -95,7 +95,7 @@
9595
<ng-container *ngIf="field.fieldGroup?.[i] as rowField">
9696
<div
9797
class="field-cell"
98-
*ngFor="let column of fieldColumns">
98+
*ngFor="let column of columns">
9999
<formly-field
100100
*ngIf="getColumnField(rowField, column) as columnField"
101101
[field]="columnField"></formly-field>

0 commit comments

Comments
 (0)