Skip to content

Commit facd072

Browse files
yangzhang75claude
andcommitted
fix(workflow-form): give the Form View a code editor host, so its step panel's code button opens the editor
The code editor renders into an outlet the operator canvas published once (`<ng-template #codeEditor>` in WorkspaceComponent, handed to CodeEditorService as `vc`) and never let go of. The Form View has no outlet of its own, yet its step panel shows the operator's real property frame and, in edit mode, the ordinary "Edit code content" button. Pressing it opened the editor into whatever `vc` held: nothing, from a direct open of the Form View, or the destroyed canvas's outlet after the routed switch between the views (apache#8581), where Monaco rendered into a detached view and nothing appeared on screen. Verified on main: the button is there, the click does nothing, no error. Each view that shows operator properties now carries its own outlet, marked `texeraCodeEditorHost` on an `<ng-template>`: the directive attaches the outlet to CodeEditorService when the view's template is built and detaches it -- only its own -- when the view is destroyed, which also closes an editor still open in it. The views are sibling routes, so there is one host at a time. The button opens into whichever host is up and guards the case of none (every view that mounts a codearea hosts the editor now, so that is a guard rather than a state the button shows): a co-editor's open reaching a codearea with no host neither throws nor marks that operator's editor open, and reaches only the codearea on that operator. The editor is told its operator by its opener instead of reading the highlight in its constructor, and may be dragged within the Form View's page as well as the canvas. Deliberately not in this change: exposing a code property as a form input. The form's rules keep the code editor out of what an author may expose (NON_FORM_FIELD_TYPES) and fall back from it on an old workflow that carries one (CANVAS_ONLY_FORMLY_TYPES); editing code is not filling in a value, and a form reader should not be writing code. The step panel is the author's own editing surface, which is what this fixes. Closes apache#8760 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01FVvP3ttj22f9LB4p9u2anY
1 parent a46ddc6 commit facd072

18 files changed

Lines changed: 441 additions & 77 deletions
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
import { Component, OnDestroy } from "@angular/core";
21+
import { TestBed } from "@angular/core/testing";
22+
import { CodeEditorHostDirective } from "./code-editor-host.directive";
23+
import { CodeEditorService } from "../../service/code-editor/code-editor.service";
24+
25+
/** A view that hosts the editor, as the operator canvas and the Form View do. */
26+
@Component({
27+
template: "<ng-template texeraCodeEditorHost></ng-template>",
28+
imports: [CodeEditorHostDirective],
29+
})
30+
class HostingViewComponent {}
31+
32+
/** Stands in for the editor: what the host holds, and what must be torn down with the view. */
33+
@Component({ template: "editor" })
34+
class HostedComponent implements OnDestroy {
35+
static destroyed = 0;
36+
ngOnDestroy(): void {
37+
HostedComponent.destroyed++;
38+
}
39+
}
40+
41+
describe("CodeEditorHostDirective", () => {
42+
let service: CodeEditorService;
43+
44+
beforeEach(async () => {
45+
HostedComponent.destroyed = 0;
46+
await TestBed.configureTestingModule({ imports: [HostingViewComponent, HostedComponent] }).compileComponents();
47+
service = TestBed.inject(CodeEditorService);
48+
});
49+
50+
it("attaches its outlet as the editor's host when the view is built", () => {
51+
expect(service.host).toBeUndefined();
52+
53+
const fixture = TestBed.createComponent(HostingViewComponent);
54+
fixture.detectChanges();
55+
56+
expect(service.host).toBeDefined();
57+
// The real outlet: the editor is created into it.
58+
const ref = service.host!.createComponent(HostedComponent);
59+
expect(ref.location.nativeElement.textContent).toBe("editor");
60+
});
61+
62+
it("closes what it hosts and lets go of the host when the view is destroyed", () => {
63+
const fixture = TestBed.createComponent(HostingViewComponent);
64+
fixture.detectChanges();
65+
service.host!.createComponent(HostedComponent);
66+
67+
fixture.destroy();
68+
69+
// The editor's own teardown ran, by the view's (it writes awareness and saves its geometry
70+
// there) ...
71+
expect(HostedComponent.destroyed).toBe(1);
72+
// ... and nothing claims to host an editor any more.
73+
expect(service.host).toBeUndefined();
74+
});
75+
76+
it("lets go only of its own outlet, not of a host that has since replaced it", () => {
77+
const first = TestBed.createComponent(HostingViewComponent);
78+
first.detectChanges();
79+
const firstHost = service.host;
80+
const second = TestBed.createComponent(HostingViewComponent);
81+
second.detectChanges();
82+
expect(service.host).not.toBe(firstHost);
83+
84+
first.destroy();
85+
86+
expect(service.host).toBeDefined();
87+
expect(service.host).not.toBe(firstHost);
88+
});
89+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
import { Directive, OnDestroy, ViewContainerRef } from "@angular/core";
21+
import { CodeEditorService } from "../../service/code-editor/code-editor.service";
22+
23+
/**
24+
* Marks the outlet the code editor opens into, on an `<ng-template>` in a view's template. Each view
25+
* that shows operator properties -- the operator canvas and the Form View -- carries one, and the
26+
* view in front is the editor's host: its outlet is attached to CodeEditorService when the view's
27+
* template is built, and detached when the view is destroyed -- which closes an editor still open in
28+
* it too, since the view's teardown destroys what its outlet holds, before this hook runs. The views
29+
* are sibling routes and never overlap, so there is one host at a time; a late destroy still lets go
30+
* only of its own.
31+
*/
32+
@Directive({
33+
selector: "[texeraCodeEditorHost]",
34+
standalone: true,
35+
})
36+
export class CodeEditorHostDirective implements OnDestroy {
37+
constructor(
38+
private readonly outlet: ViewContainerRef,
39+
private readonly codeEditorService: CodeEditorService
40+
) {
41+
this.codeEditorService.attachHost(this.outlet);
42+
}
43+
44+
ngOnDestroy(): void {
45+
this.codeEditorService.detachHost(this.outlet);
46+
}
47+
}

‎frontend/src/app/workspace/component/code-editor-dialog/code-editor.component.browser.spec.ts‎

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,41 @@ describe("CodeEditorComponent (browser)", () => {
232232
vi.restoreAllMocks();
233233
});
234234

235-
// Builds a fixture for the highlighted operator, but defers the
236-
// detectChanges/ngAfterViewInit step so the caller can stage `code` /
237-
// `formControl` (and the EditorApp's fake editor) before the subscribe body
238-
// fires. Returns the fixture, the fake editor, and the component instance.
239-
function makeFixtureWithFakes() {
240-
const predicate = { ...mockJavaUDFPredicate };
235+
// Builds a fixture for a Java UDF operator, told its operator the way the
236+
// codearea button tells it, but defers the detectChanges step (ngOnInit +
237+
// ngAfterViewInit) so the caller can stage `formControl`, the shared text
238+
// and the EditorApp's fake editor before the subscribe body fires. The
239+
// shared text is staged on the graph, where ngOnInit reads it from: a `code`
240+
// property on the predicate becomes a Y.Text on the shared
241+
// operator-properties map, and that is what the subscribe body binds to (and
242+
// crosses the `if (!this.code) return;` gate with). Returns the fixture, the
243+
// fake editor, the component instance and that shared text (undefined when
244+
// staged without one).
245+
function makeFixtureWithFakes(options: { withSharedCode?: boolean } = {}) {
246+
const { withSharedCode = true } = options;
247+
const predicate = { ...mockJavaUDFPredicate, operatorProperties: withSharedCode ? { code: "" } : {} };
241248
workflowActionService.addOperator(predicate, mockPoint);
242249
workflowActionService.getJointGraphWrapper().highlightOperators(predicate.operatorID);
250+
const sharedCode = withSharedCode
251+
? ((
252+
workflowActionService
253+
.getTexeraGraph()
254+
.getSharedOperatorType(predicate.operatorID)
255+
.get("operatorProperties") as any
256+
).get("code") as Y.Text)
257+
: undefined;
243258

244259
const fixture = TestBed.createComponent(CodeEditorComponent);
260+
// The opener names the operator; see makeFixture in the jsdom spec.
261+
fixture.componentRef.setInput("currentOperatorId", predicate.operatorID);
245262
const editor = makeFakeEditor();
246263
// The component pulls the editor back out of `EditorApp.getEditor()` inside
247264
// its start path (and again from `adjustEditorSize`), so the mocked
248265
// EditorApp must hand back this same fake.
249266
editorAppMock.getEditor.mockReturnValue(editor);
250267
const c = fixture.componentInstance as any;
251268
c.formControl = new FormControl({ value: "", disabled: false });
252-
// A YText must live inside a Y.Doc to be useful; the binding stub doesn't
253-
// care, but we stage it as if it came from the shared model so the
254-
// subscribe body crosses the `if (!this.code) return;` gate.
255-
c.code = new Y.Doc().getText("code");
256-
return { fixture, editor, c: c as CodeEditorComponent };
269+
return { fixture, editor, c: c as CodeEditorComponent, sharedCode };
257270
}
258271

259272
// The component's subscribe path runs `from(startEditor()).pipe(...)`,
@@ -367,8 +380,7 @@ describe("CodeEditorComponent (browser)", () => {
367380
});
368381

369382
it("initializeMonacoEditor: leaves the binding unwired when the operator has no shared code", async () => {
370-
const { fixture, editor, c } = makeFixtureWithFakes();
371-
(c as any).code = undefined;
383+
const { fixture, editor } = makeFixtureWithFakes({ withSharedCode: false });
372384

373385
fixture.detectChanges();
374386
await flush();
@@ -412,10 +424,8 @@ describe("CodeEditorComponent (browser)", () => {
412424
});
413425

414426
it("initializeDiffEditor: diffs this operator's latest-version code against the shared text", async () => {
415-
const { fixture, c } = makeFixtureWithFakes();
416-
const sharedText = new Y.Doc().getText("code");
417-
sharedText.insert(0, "shared editing text");
418-
(c as any).code = sharedText;
427+
const { fixture, sharedCode } = makeFixtureWithFakes();
428+
sharedCode!.insert(0, "shared editing text");
419429
// The decoy operator is listed FIRST, so picking anything other than the
420430
// entry matching `currentOperatorId` surfaces the wrong version.
421431
vi.spyOn(workflowActionService, "getTempWorkflow").mockReturnValue({

‎frontend/src/app/workspace/component/code-editor-dialog/code-editor.component.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
id="code-editor-container"
2323
class="box"
2424
cdkDrag
25-
cdkDragBoundary="texera-workspace"
25+
cdkDragBoundary="texera-workspace, texera-workflow-form"
2626
(focusin)="onFocus()">
2727
<h5
2828
id="title"

‎frontend/src/app/workspace/component/code-editor-dialog/code-editor.component.spec.ts‎

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,38 @@ describe("CodeEditorComponent", () => {
244244
workflowActionService.addOperator(predicate, mockPoint);
245245
workflowActionService.getJointGraphWrapper().highlightOperators(predicate.operatorID);
246246
const fixture = TestBed.createComponent(CodeEditorComponent);
247+
// The opener names the operator (the panel's button reads the highlight, the form's its
248+
// binding); the highlight above is for what still reads it, such as onFocus.
249+
fixture.componentRef.setInput("currentOperatorId", predicate.operatorID);
250+
// The opener hands the editor the field's control before the first check; the AI actions read
251+
// its disabled state. Editable here; the read-only test stages its own.
252+
fixture.componentInstance.formControl = new FormControl("");
247253
fixture.detectChanges();
248254
return fixture;
249255
}
250256

251-
it("creates with the highlighted operator", () => {
257+
it("binds to the operator it is given", () => {
252258
const fixture = makeFixture(mockJavaUDFPredicate);
253259
expect(fixture.componentInstance).toBeTruthy();
254260
expect(fixture.componentInstance.currentOperatorId).toBe(mockJavaUDFPredicate.operatorID);
255261
});
256262

263+
it("keeps the operator it is given when another one is highlighted (a Form View editor)", () => {
264+
// The form's field names its operator; the highlight, if any, is a step the reader is looking at.
265+
const given = { ...buildPredicateWithCode("given-op", "PythonUDFV2", "x = 1"), customDisplayName: "Given" };
266+
const highlighted = buildPredicate("highlighted-op", "PythonUDFV2");
267+
workflowActionService.addOperator(given, mockPoint);
268+
workflowActionService.addOperator(highlighted, mockPoint);
269+
workflowActionService.getJointGraphWrapper().highlightOperators(highlighted.operatorID);
270+
const fixture = TestBed.createComponent(CodeEditorComponent);
271+
fixture.componentRef.setInput("currentOperatorId", given.operatorID);
272+
273+
fixture.detectChanges();
274+
275+
expect(fixture.componentInstance.currentOperatorId).toBe(given.operatorID);
276+
expect(fixture.componentInstance.title).toBe("Given");
277+
});
278+
257279
it("shows UI parameter parser errors for the edited operator", () => {
258280
const predicate = buildPredicate("python-with-invalid-parameters", "PythonUDFV2");
259281
makeFixture(predicate);
@@ -278,9 +300,9 @@ describe("CodeEditorComponent", () => {
278300
expect(notificationServiceMock.error).not.toHaveBeenCalled();
279301
});
280302

281-
// Language detection — the constructor maps `RUDFSource` / `RUDF` to `r`,
303+
// Language detection — ngOnInit maps `RUDFSource` / `RUDF` to `r`,
282304
// the three V2-era Python operator types to `python`, and anything else
283-
// to `java`. The exact branch lives in the constructor; the public
305+
// to `java`. The exact branch lives in ngOnInit; the public
284306
// `language` field is what the rest of the editor (LSP wiring, file-
285307
// suffix selection) keys off.
286308

@@ -381,6 +403,7 @@ describe("CodeEditorComponent", () => {
381403
fixture.componentInstance.onFocus();
382404
expect(highlightSpy).toHaveBeenCalledWith(mockJavaUDFPredicate.operatorID);
383405
});
406+
384407
});
385408

386409
describe("rejectCurrentAnnotation", () => {
@@ -454,6 +477,21 @@ describe("CodeEditorComponent", () => {
454477
});
455478

456479
describe("AI assistant action wiring", () => {
480+
// Monaco's readOnly stops typing, not the context-menu actions, and accepting a type annotation
481+
// inserts into the shared code; a read-only viewer's editor must not offer them (#8760).
482+
it("registers no AI actions for a read-only editor, and both for an editable one", () => {
483+
const fixture = makeFixture(mockJavaUDFPredicate);
484+
const readOnly = makeFakeEditor();
485+
fixture.componentInstance.formControl = new FormControl({ value: "", disabled: true });
486+
(fixture.componentInstance as any).setupAIAssistantActions(asEditor(readOnly));
487+
expect(readOnly.addAction).not.toHaveBeenCalled();
488+
489+
const editable = makeFakeEditor();
490+
fixture.componentInstance.formControl = new FormControl("");
491+
(fixture.componentInstance as any).setupAIAssistantActions(asEditor(editable));
492+
expect(editable.addAction).toHaveBeenCalledTimes(2);
493+
});
494+
457495
// setupAIAssistantActions checks an AI-provider flag (OpenAI vs others)
458496
// before deciding whether to register the per-selection 'Add Type
459497
// Annotation' action. We can't drive the action body without a real
@@ -477,6 +515,7 @@ describe("CodeEditorComponent", () => {
477515
wfActions.addOperator(mockJavaUDFPredicate, mockPoint);
478516
wfActions.getJointGraphWrapper().highlightOperators(mockJavaUDFPredicate.operatorID);
479517
const fixture = TestBed.createComponent(CodeEditorComponent);
518+
fixture.componentRef.setInput("currentOperatorId", mockJavaUDFPredicate.operatorID);
480519
fixture.detectChanges();
481520
const checked = await TestBed.inject(AIAssistantService).checkAIAssistantEnabled().toPromise();
482521
expect(checked).toBe("OpenAI");
@@ -1313,6 +1352,7 @@ describe("CodeEditorComponent", () => {
13131352
if (tempOperators) stageTempWorkflow(tempOperators);
13141353
TestBed.inject(WorkflowVersionService).setDisplayParticularVersion(true);
13151354
const fixture = TestBed.createComponent(CodeEditorComponent);
1355+
fixture.componentRef.setInput("currentOperatorId", predicate.operatorID);
13161356
fixture.detectChanges();
13171357
await waitFor(() => Boolean(diffModelOf(fixture.componentInstance)), "the mounted diff editor");
13181358
return fixture.componentInstance;

‎frontend/src/app/workspace/component/code-editor-dialog/code-editor.component.ts‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import {
2323
ComponentRef,
2424
ElementRef,
2525
HostListener,
26+
Input,
2627
OnDestroy,
28+
OnInit,
2729
Type,
2830
ViewChild,
2931
} from "@angular/core";
@@ -93,14 +95,18 @@ export const LANGUAGE_SERVER_CONNECTION_TIMEOUT_MS = 1000;
9395
AnnotationSuggestionComponent,
9496
],
9597
})
96-
export class CodeEditorComponent implements AfterViewInit, SafeStyle, OnDestroy {
98+
export class CodeEditorComponent implements OnInit, AfterViewInit, SafeStyle, OnDestroy {
9799
@ViewChild("editor", { static: true }) editorElement!: ElementRef;
98100
@ViewChild("container", { static: true }) containerElement!: ElementRef;
99101
@ViewChild(AnnotationSuggestionComponent) annotationSuggestion!: AnnotationSuggestionComponent;
100102
@ViewChild(BreakpointConditionInputComponent) breakpointConditionInput!: BreakpointConditionInputComponent;
101103
private code?: YText;
102104
private workflowVersionStreamSubject: Subject<void> = new Subject<void>();
103-
public currentOperatorId!: string;
105+
/**
106+
* The operator whose code this edits, given by the opener: the operator property panel's button
107+
* reads the highlighted operator, the Form View's the operator its field is bound to.
108+
*/
109+
@Input() currentOperatorId!: string;
104110

105111
public title: string | undefined;
106112
public formControl!: FormControl;
@@ -150,8 +156,10 @@ export class CodeEditorComponent implements AfterViewInit, SafeStyle, OnDestroy
150156
private config: GuiConfigService,
151157
private uiUdfParametersSyncService: UiUdfParametersSyncService,
152158
private notificationService: NotificationService
153-
) {
154-
this.currentOperatorId = this.workflowActionService.getJointGraphWrapper().getCurrentHighlightedOperatorIDs()[0];
159+
) {}
160+
161+
ngOnInit(): void {
162+
// Everything here keys on currentOperatorId, an input set after construction.
155163
const operatorType = this.workflowActionService.getTexeraGraph().getOperator(this.currentOperatorId).operatorType;
156164
if (CodeEditorComponent.PYTHON_OPERATOR_TYPES.has(operatorType)) {
157165
this.language = "python";
@@ -468,6 +476,11 @@ export class CodeEditorComponent implements AfterViewInit, SafeStyle, OnDestroy
468476
}
469477

470478
private setupAIAssistantActions(editor: MonacoEditor) {
479+
// Monaco's readOnly stops typing, not the context-menu actions, and accepting a type annotation
480+
// inserts into the shared code: a read-only viewer's editor offers none (#8760).
481+
if (this.formControl.disabled) {
482+
return;
483+
}
471484
// Check if the AI provider is "openai"
472485
this.aiAssistantService
473486
.checkAIAssistantEnabled()
@@ -679,6 +692,8 @@ export class CodeEditorComponent implements AfterViewInit, SafeStyle, OnDestroy
679692
this.editorApp?.getEditor()?.layout();
680693
}
681694
onFocus() {
695+
// The wrapper's own highlight, as it always was, not WorkflowActionService's: that one also
696+
// announces the highlight to co-editors and switches multi-select mode, neither wanted on focus.
682697
this.workflowActionService.getJointGraphWrapper().highlightOperators(this.currentOperatorId);
683698
}
684699
}

0 commit comments

Comments
 (0)