Switched back to jump to setting instead

This commit is contained in:
Lam Wei Lun
2026-09-09 12:26:09 +08:00
parent cae23a933e
commit c3a21fa0d4
6 changed files with 11 additions and 1109 deletions
@@ -159,73 +159,4 @@ assert.equal(ctx.revealTarget(100, -5, 50), 50, "negative start is clamped to th
assert.equal(ctx.revealTarget(200, 50, 100), 150, "a scroll viewpoint reveals a window past the current rows");
assert.equal(ctx.revealTarget(10, 0, 50), 10, "a list shorter than one window stays fully materialized");
// ---- inline setting editor helpers (settingControlRows / Value / CollectedValue) ----
const scalarBoolDesc = {
editable: true, control: "toggle", cardinality: "scalar", value: true,
min: undefined, max: undefined, is_int: false, unit: ""
};
const scalarRows = ctx.settingControlRows(scalarBoolDesc);
assert.equal(scalarRows.length, 1, "a scalar setting yields exactly one control row");
assert.equal(scalarRows[0].kind, "toggle", "the control kind is carried through");
assert.equal(scalarRows[0].index, 0, "the single row is indexed 0");
assert.deepEqual(ctx.settingControlRows({ editable: false }), [], "a non-editable setting yields no control rows");
const vectorDesc = {
editable: true, control: "number", cardinality: "vector", values: [0.2, 0.4],
index_labels: ["1", "2"], min: 0, max: 1, is_int: false, unit: "mm"
};
const vectorRows = ctx.settingControlRows(vectorDesc);
assert.equal(vectorRows.length, 2, "a vector setting yields one row per value");
assert.deepEqual(vectorRows.map(function (r) { return r.value; }), [0.2, 0.4], "each row carries its current value");
assert.deepEqual(vectorRows.map(function (r) { return r.label; }), ["1", "2"], "vector rows use the per-index labels");
assert.equal(vectorRows[0].unit, "mm", "the unit is carried to each row");
// settingControlValue reads a control element (checked/value) as the shipped value.
assert.equal(ctx.settingControlValue({ kind: "toggle" }, { checked: true }), true, "a toggle submits its checked state");
assert.equal(ctx.settingControlValue({ kind: "toggle" }, { checked: false }), false, "an off toggle submits false");
assert.equal(ctx.settingControlValue({ kind: "number", is_int: false, min: 0, max: 1 }, { value: "0.5" }), 0.5, "a float number parses");
assert.equal(ctx.settingControlValue({ kind: "number", is_int: true, min: 0, max: 10 }, { value: "5" }), 5, "an int number parses");
assert.equal(ctx.settingControlValue({ kind: "number", is_int: false, min: 0, max: 1 }, { value: "2" }), undefined, "an out-of-range number is rejected");
assert.equal(ctx.settingControlValue({ kind: "number", is_int: false, min: 0, max: 1 }, { value: "" }), undefined, "an empty number is rejected");
assert.equal(ctx.settingControlValue({ kind: "dropdown" }, { dataset: { value: "3" } }), 3, "an enum dropdown submits its stored int value");
assert.equal(ctx.settingControlValue({ kind: "dropdown" }, { dataset: { value: "nope" } }), undefined, "a non-numeric dropdown value is rejected");
assert.equal(ctx.settingControlValue({ kind: "dropdown" }, {}), undefined, "a dropdown with no value is rejected");
// dropdownLabel maps the current int value to its human label, falling back to the value.
const seamOptions = [
{ value: 0, key: "nearest", label: "Nearest" },
{ value: 1, key: "aligned", label: "Aligned" },
{ value: 2, key: "random", label: "Random" }
];
assert.equal(ctx.dropdownLabel(seamOptions, 1), "Aligned", "the dropdown label for a known value is its label");
assert.equal(ctx.dropdownLabel(seamOptions, 9), "9", "an unknown value falls back to the raw value");
assert.equal(ctx.dropdownLabel([], 3), "3", "empty options fall back to the raw value");
assert.equal(ctx.dropdownLabel(
[{ value: 1, key: "aligned", label: "" }], 1), "aligned", "a blank label falls back to the key");
// dropdownIcon maps the current int value to its pattern pictogram, empty when there is none.
const patternOptions = [
{ value: 0, key: "rectilinear", label: "Rectilinear" },
{ value: 3, key: "gyroid", label: "Gyroid", icon: "data:image/svg+xml;base64,AAA" },
{ value: 5, key: "grid", label: "Grid", icon: "data:image/svg+xml;base64,BBB" }
];
assert.equal(ctx.dropdownIcon(patternOptions, 3), "data:image/svg+xml;base64,AAA", "a known value returns its icon");
assert.equal(ctx.dropdownIcon(patternOptions, 0), "", "a value with no icon returns empty");
assert.equal(ctx.dropdownIcon(patternOptions, 9), "", "an unknown value returns empty");
assert.equal(ctx.dropdownIcon([], 3), "", "empty options return empty");
assert.equal(ctx.settingControlValue({ kind: "text" }, { value: "hello" }), "hello", "text submits as a string");
// percent (coFloatOrPercent / coFloatsOrPercents): submits the raw typed string; empty is invalid.
assert.equal(ctx.settingControlValue({ kind: "percent" }, { value: "10%" }), "10%", "a percent value submits as its string");
assert.equal(ctx.settingControlValue({ kind: "percent" }, { value: "0.5" }), "0.5", "an mm value submits as a plain string");
assert.equal(ctx.settingControlValue({ kind: "percent" }, { value: " " }), undefined, "a blank percent value is rejected");
assert.equal(ctx.settingControlValue({ kind: "percent" }, {}), undefined, "a percent field with no value is rejected");
// A percent scalar-to-scalar payload carries the raw string through unchanged.
assert.equal(ctx.settingCollectedValue(
{ editable: true, control: "percent", cardinality: "scalar", value: "10%" },
[{ kind: "percent", value: "10%" }], [{ value: "10%" }]), "10%", "a percent scalar assembles its string");
// settingCollectedValue assembles the payload (scalar vs vector) for the set_setting message.
assert.equal(ctx.settingCollectedValue({ editable: false }, [], []), undefined, "a non-editable setting yields no payload");
assert.equal(ctx.settingCollectedValue(scalarBoolDesc, scalarRows, [{ checked: true }]), true, "a scalar assembles a single value");
assert.deepEqual(ctx.settingCollectedValue(vectorDesc, vectorRows, [{ value: "0.3" }, { value: "0.7" }]), [0.3, 0.7], "a vector assembles an array");
assert.equal(ctx.settingCollectedValue(vectorDesc, vectorRows, [{ value: "0.3" }, { value: "2" }]), undefined, "an invalid vector element aborts the whole payload");
console.log("ok");