yangzhang75 commented on code in PR #8304:
URL: https://github.com/apache/texera/pull/8304#discussion_r3908649051


##########
frontend/src/app/workspace/service/form-binding/form-binding.service.ts:
##########
@@ -0,0 +1,308 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { Injectable } from "@angular/core";
+import { BehaviorSubject, Observable } from "rxjs";
+import { CustomJSONSchema7 } from "../../types/custom-json-schema.interface";
+import { OperatorPredicate } from "../../types/workflow-common.interface";
+import { FormFieldBinding, FormFieldOverride, FormBindingConfig } from 
"../../../common/type/workflow";
+import { OperatorMetadataService } from 
"../operator-metadata/operator-metadata.service";
+import { DynamicSchemaService } from 
"../dynamic-schema/dynamic-schema.service";
+import { WorkflowActionService } from 
"../workflow-graph/model/workflow-action.service";
+
+/** A binding paired with the operator field that renders it. */
+export interface ResolvedField {
+  binding: FormFieldBinding;
+  /** The operator's current value -- what the form shows and what a run uses. 
*/
+  value: unknown;
+  /** Label for the operator this input belongs to, for the author's 
reference. */
+  operatorLabel: string;
+  schema?: CustomJSONSchema7;
+  /**
+   * Set when the binding no longer points at a real property, because the 
operator
+   * was deleted on the regular canvas or the raw key was mistyped. Broken 
inputs are
+   * shown to the author with the reason and hidden from everyone else, since 
filling
+   * one in could not do anything.
+   */
+  brokenReason?: string;
+}
+
+/**
+ * Reads and writes the Form View definition. The one rule it enforces: a 
definition never
+ * affects a run -- filling an input is the same `setOperatorProperty` edit 
the canvas makes,
+ * and everything else (names, help text, ordering, instruction) is 
presentation.
+ */
+@Injectable({ providedIn: "root" })
+export class FormBindingService {
+  /** Whether the author is choosing which properties the form offers. Sticky 
(choosing spans
+   *  several operators), so it stays on until the author turns it off. */
+  private choosingSubject = new BehaviorSubject<boolean>(false);
+  public readonly choosing$: Observable<boolean> = 
this.choosingSubject.asObservable();
+
+  constructor(
+    private workflowActionService: WorkflowActionService,
+    private operatorMetadataService: OperatorMetadataService,
+    private dynamicSchemaService: DynamicSchemaService
+  ) {}
+
+  public isChoosing(): boolean {
+    return this.choosingSubject.value;
+  }
+
+  public setChoosing(choosing: boolean): void {
+    if (this.choosingSubject.value !== choosing) {
+      this.choosingSubject.next(choosing);
+    }
+  }
+
+  public getConfig(): FormBindingConfig {
+    return this.workflowActionService.getFormBinding();
+  }
+
+  /** Apply a partial edit to the definition, announcing it so it gets saved. 
*/
+  public updateConfig(patch: Partial<FormBindingConfig>): void {
+    this.workflowActionService.setFormBinding({ ...this.getConfig(), ...patch 
});
+  }
+
+  public setFields(fields: FormFieldBinding[]): void {
+    this.updateConfig({ fields });
+  }
+
+  public updateBinding(id: string, patch: Partial<FormFieldBinding>): void {
+    this.setFields(this.getConfig().fields.map(p => (p.id === id ? { ...p, 
...patch } : p)));
+  }
+
+  public removeBinding(id: string): void {
+    this.setFields(this.getConfig().fields.filter(p => p.id !== id));
+  }
+
+  /** The binding for one operator property, if it is currently exposed on the 
form. */
+  private findBinding(operatorID: string, propertyKey: string): 
FormFieldBinding | undefined {
+    return this.getConfig().fields.find(p => p.operatorID === operatorID && 
p.propertyKey === propertyKey);
+  }
+
+  /**
+   * Expose an operator property on the form. A no-op if it is already 
exposed; the display
+   * name starts from the schema's title so the author begins with a readable 
label.
+   */
+  public addBinding(operatorID: string, propertyKey: string): void {
+    if (this.findBinding(operatorID, propertyKey)) {
+      return;
+    }
+    const schema = this.propertySchema(operatorID, propertyKey);
+    this.setFields([
+      ...this.getConfig().fields,
+      {
+        id: `field-${crypto.randomUUID()}`,
+        operatorID,
+        propertyKey,
+        displayName: (schema?.title as string) || propertyKey,
+        // Deliberately not seeded from the schema's description. That text 
describes the
+        // operator to whoever wired it up -- "Multiple string key/value 
pairs" -- and
+        // appearing unbidden under a reader's input it is worse than nothing: 
it reads
+        // as guidance the author never wrote and cannot be told apart from 
guidance
+        // they did. Empty until the author has something to say.
+        helpText: undefined,
+      },
+    ]);
+  }
+
+  /**
+   * Apply an override to one field. An entry that no longer says anything is 
deleted
+   * rather than left as an empty object, so the saved definition stays a 
record of the
+   * author's decisions instead of accumulating every field they happened to 
look at.
+   */
+  public setFieldOverride(bindingId: string, path: string, patch: 
Partial<FormFieldOverride>): void {
+    const binding = this.getConfig().fields.find(p => p.id === bindingId);
+    if (!binding) {
+      return;
+    }
+    const merged: FormFieldOverride = { ...(binding.overrides?.[path] ?? {}), 
...patch };
+    if (merged.displayName !== undefined && merged.displayName.trim() === "") {
+      delete merged.displayName;
+    }
+    if (merged.hidden === false) {
+      delete merged.hidden;
+    }
+    const overrides = { ...(binding.overrides ?? {}) };
+    if (Object.keys(merged).length === 0) {
+      delete overrides[path];
+    } else {
+      overrides[path] = merged;
+    }
+    this.updateBinding(bindingId, { overrides: Object.keys(overrides).length > 
0 ? overrides : undefined });
+  }
+
+  public isExposed(operatorID: string, propertyKey: string): boolean {
+    return this.findBinding(operatorID, propertyKey) !== undefined;
+  }
+
+  /** Add or remove an input from the form, driven by the property editor's 
tick box. */
+  public setExposed(operatorID: string, propertyKey: string, exposed: 
boolean): void {
+    if (exposed) {
+      this.addBinding(operatorID, propertyKey);
+      return;
+    }
+    const existing = this.findBinding(operatorID, propertyKey);
+    if (existing) {
+      this.removeBinding(existing.id);
+    }
+  }
+
+  /** Move an input to a new position; array order is what the form renders. */
+  public reorder(from: number, to: number): void {
+    const fields = [...this.getConfig().fields];
+    if (from === to || from < 0 || to < 0 || from >= fields.length || to >= 
fields.length) {
+      return;
+    }
+    fields.splice(to, 0, fields.splice(from, 1)[0]);
+    this.setFields(fields);
+  }
+
+  /**
+   * Choose whether an operator's output is shown on the form after a run.
+   *
+   * This also marks the operator for result viewing on the graph, because the 
engine
+   * only materialises results for operators in `opsToViewResult` -- picking 
one here
+   * without that produced a run that completed but showed nothing.
+   */
+  public toggleResultOperator(operatorID: string): void {
+    const shown = this.getConfig().resultOperatorIds;
+    const next = shown.includes(operatorID) ? shown.filter(id => id !== 
operatorID) : [...shown, operatorID];
+    this.updateConfig({ resultOperatorIds: next });
+    this.syncViewResultOperators(next);
+  }
+
+  /**
+   * Keep the graph's view-result set in step with what the form promises to 
show.
+   * Operators the canvas already views for its own reasons are left alone.
+   */
+  public syncViewResultOperators(shown: string[] = 
this.getConfig().resultOperatorIds): void {
+    const graph = this.workflowActionService.getTexeraGraph();
+    const existing = new Set(graph.getOperatorsToViewResult());
+    shown.filter(id => graph.hasOperator(id)).forEach(id => existing.add(id));
+    this.workflowActionService.setViewOperatorResults([...existing]);

Review Comment:
    Addressed by removing syncViewResultOperators entirely. The Form View no 
longer writes the canvas's view-result flags: it records only its own selection 
(resultOperatorIds), and its picker offers exactly the operators the workflow 
already views. Those results are already materialised, so nothing needs to 
touch the graph, there is no set to fall out of sync, and a canvas user's own 
view-result choices are never changed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to