Skip to content

Commit b602a03

Browse files
committed
fix(ui): autosave acks no longer wipe open add-entry drafts and in-progress JSON
A drained autosave ack (and the config.changed refresh path) clones the whole config form, changing every object/array identity while content is byte-identical. The collection add-entry draft closed on identity change and the JSON textarea reset its uncommitted text — the operator's typing vanished ~1s after any earlier toggle. Both guards now check content equality when the identity churns; a genuinely external content change still closes/resets as before.
1 parent 92eed63 commit b602a03

4 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎ui/src/components/config-form-array-integrity.browser.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,29 @@ describe("config form array integrity", () => {
181181
expectElement(findAddButton(draftHost), "unique array draft commit").click();
182182
expect(onPatch).toHaveBeenCalledWith(["codes"], ["first"]);
183183

184+
// An autosave ack clones the form: identity churn with identical content
185+
// must not close an open draft mid-typing.
186+
expectElement(findAddButton(container), "reopened unique array add").click();
187+
await draftHost.updateComplete;
188+
const typedDraft = expectElement(
189+
draftHost.querySelector<HTMLInputElement>("[data-collection-draft-value]"),
190+
"typed unique array draft",
191+
);
192+
typedDraft.value = "in-progress";
193+
typedDraft.dispatchEvent(new Event("input", { bubbles: true }));
194+
await draftHost.updateComplete;
195+
renderSchema({
196+
type: "array",
197+
minItems: 2,
198+
uniqueItems: true,
199+
items: { type: "string" },
200+
});
201+
await draftHost.updateComplete;
202+
const survivingDraft = draftHost.querySelector<HTMLInputElement>(
203+
"[data-collection-draft-value]",
204+
);
205+
expect(survivingDraft?.value).toBe("in-progress");
206+
184207
onPatch.mockClear();
185208
renderArrayFixture(container, {
186209
schema: {

‎ui/src/components/config-form-collection-draft.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export class ConfigFormCollectionDraft extends OpenClawLightDomElement {
3838
previous &&
3939
(!next ||
4040
previous.identity !== next.identity ||
41-
!Object.is(previous.sourceIdentity, next.sourceIdentity))
41+
// Content equality, not Object.is: a drained autosave ack (or a
42+
// config.changed refresh) clones the whole form, changing every
43+
// identity while the bytes are unchanged — closing here would wipe
44+
// the operator's in-progress add-entry draft mid-typing.
45+
(!Object.is(previous.sourceIdentity, next.sourceIdentity) &&
46+
!configValuesEqual(previous.sourceIdentity, next.sourceIdentity)))
4247
) {
4348
this.closeDraft();
4449
}

‎ui/src/components/config-form-integrity.browser.test.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,19 @@ describe("config form integrity", () => {
558558
await host.updateComplete;
559559
expect(commits).toHaveBeenCalledTimes(1);
560560

561+
// Identity churn with identical content (autosave ack clone) keeps the
562+
// draft open; only a real external array change closes it.
561563
host.props = {
562564
...host.props,
563565
sourceIdentity: [],
564566
};
565567
await host.updateComplete;
568+
expect(host.querySelector(".cfg-collection-draft")).not.toBeNull();
569+
host.props = {
570+
...host.props,
571+
sourceIdentity: ["externally-added"],
572+
};
573+
await host.updateComplete;
566574
expect(host.querySelector(".cfg-collection-draft")).toBeNull();
567575
host.remove();
568576
});
@@ -995,12 +1003,14 @@ describe("config form integrity", () => {
9951003
expect(siblingUpdateTextarea.validationMessage).not.toBe("");
9961004
expect(error.hidden).toBe(false);
9971005

1006+
// A genuinely external change (different content) still resets in-progress
1007+
// text; identity churn with identical bytes (autosave ack) must not.
9981008
render(
9991009
renderConfigForm({
10001010
schema: analysis.schema,
10011011
uiHints: {},
10021012
unsupportedPaths: analysis.unsupportedPaths,
1003-
value: { accounts: { primary: { enabled: true } } },
1013+
value: { accounts: { primary: { enabled: false } } },
10041014
showAdvanced: true,
10051015
onShowAdvanced: () => {},
10061016
onPatch,
@@ -1011,7 +1021,7 @@ describe("config form integrity", () => {
10111021
container.querySelector<HTMLTextAreaElement>(".cfg-map textarea"),
10121022
"externally reset JSON map value",
10131023
);
1014-
expect(resetTextarea.value).toContain('"enabled": true');
1024+
expect(resetTextarea.value).toContain('"enabled": false');
10151025
expect(resetTextarea.getAttribute("aria-invalid")).toBe("false");
10161026
expect(resetTextarea.validationMessage).toBe("");
10171027
expect(error.hidden).toBe(true);

‎ui/src/components/config-form.node.shared.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { icons } from "../components/icons.ts";
77
import "../components/tooltip.ts";
88
import { t } from "../i18n/index.ts";
99
import { formatUnknownText } from "../lib/format.ts";
10-
import { isSupportedConfigValueValid } from "./config-form.constraints.ts";
10+
import { configValuesEqual, isSupportedConfigValueValid } from "./config-form.constraints.ts";
1111
import type { ConfigSearchCriteria } from "./config-form.search.ts";
1212
import {
1313
configFieldId,
@@ -450,7 +450,11 @@ export function renderJsonTextareaControl(params: {
450450
const previous = jsonTextareaState.get(element);
451451
if (
452452
previous &&
453-
(!Object.is(previous.sourceValue, params.sourceValue) ||
453+
// Content equality for the value: an autosave ack clones the form,
454+
// so identity churn with identical bytes must not erase in-progress
455+
// (possibly not-yet-valid) JSON the operator is typing.
456+
((!Object.is(previous.sourceValue, params.sourceValue) &&
457+
!configValuesEqual(previous.sourceValue, params.sourceValue)) ||
454458
!Object.is(previous.rowIdentity, params.rowIdentity) ||
455459
previous.fallback !== renderedFallback ||
456460
previous.pathKey !== pathKey)

0 commit comments

Comments
 (0)