Skip to content

Commit 93334da

Browse files
authored
feat: rebind masked variables (#4849)
Here improved creating variables to override deeper bindings when new variable overrides ancestor variable similar to css variables behavior. Here's a video with example https://github.com/user-attachments/assets/41f93f5b-fd2d-4a3b-97d1-076f0ded5f01
1 parent b68d29b commit 93334da

File tree

5 files changed

+225
-107
lines changed

5 files changed

+225
-107
lines changed

apps/builder/app/builder/features/settings-panel/resource-panel.tsx

+4-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ import {
5959
$selectedPage,
6060
} from "~/shared/awareness";
6161
import { updateWebstudioData } from "~/shared/instance-utils";
62-
import { restoreTreeVariablesMutable } from "~/shared/data-variables";
62+
import { rebindTreeVariablesMutable } from "~/shared/data-variables";
6363

6464
const validateUrl = (value: string, scope: Record<string, unknown>) => {
6565
const evaluatedValue = evaluateExpressionWithinScope(value, scope);
@@ -608,7 +608,7 @@ export const ResourceForm = forwardRef<
608608
updateWebstudioData((data) => {
609609
data.dataSources.set(newVariable.id, newVariable);
610610
data.resources.set(newResource.id, newResource);
611-
restoreTreeVariablesMutable({ instancePath, ...data });
611+
rebindTreeVariablesMutable({ instancePath, ...data });
612612
});
613613
},
614614
}));
@@ -740,7 +740,7 @@ export const SystemResourceForm = forwardRef<
740740
updateWebstudioData((data) => {
741741
data.dataSources.set(newVariable.id, newVariable);
742742
data.resources.set(newResource.id, newResource);
743-
restoreTreeVariablesMutable({ instancePath, ...data });
743+
rebindTreeVariablesMutable({ instancePath, ...data });
744744
});
745745
},
746746
}));
@@ -856,7 +856,7 @@ export const GraphqlResourceForm = forwardRef<
856856
updateWebstudioData((data) => {
857857
data.dataSources.set(newVariable.id, newVariable);
858858
data.resources.set(newResource.id, newResource);
859-
restoreTreeVariablesMutable({ instancePath, ...data });
859+
rebindTreeVariablesMutable({ instancePath, ...data });
860860
});
861861
},
862862
}));

apps/builder/app/builder/features/settings-panel/variable-popover.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ import { generateCurl } from "./curl";
7575
import { updateWebstudioData } from "~/shared/instance-utils";
7676
import {
7777
findUnsetVariableNames,
78-
restoreTreeVariablesMutable,
78+
rebindTreeVariablesMutable,
7979
} from "~/shared/data-variables";
8080

8181
const $variablesByName = computed(
@@ -299,7 +299,7 @@ const ParameterForm = forwardRef<
299299
const name = z.string().parse(formData.get("name"));
300300
updateWebstudioData((data) => {
301301
data.dataSources.set(variable.id, { ...variable, name });
302-
restoreTreeVariablesMutable({ instancePath, ...data });
302+
rebindTreeVariablesMutable({ instancePath, ...data });
303303
});
304304
},
305305
}));
@@ -339,7 +339,7 @@ const useValuePanelRef = ({
339339
type: "variable",
340340
value: variableValue,
341341
});
342-
restoreTreeVariablesMutable({ instancePath, ...data });
342+
rebindTreeVariablesMutable({ instancePath, ...data });
343343
});
344344
},
345345
}));

apps/builder/app/builder/shared/binding-popover.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import {
3939
lintExpression,
4040
} from "@webstudio-is/sdk";
4141
import { $dataSourceVariables, $isDesignMode } from "~/shared/nano-states";
42-
import { computeExpression } from "~/shared/data-variables";
42+
import {
43+
computeExpression,
44+
encodeDataVariableName,
45+
} from "~/shared/data-variables";
4346
import {
4447
ExpressionEditor,
4548
formatValuePreview,
@@ -146,7 +149,10 @@ const BindingPanel = ({
146149
active={usedIdentifiers.has(identifier)}
147150
// convert variable to expression
148151
onClick={() => {
149-
editorApiRef.current?.replaceSelection(identifier);
152+
if (name) {
153+
const nameIdentifier = encodeDataVariableName(name);
154+
editorApiRef.current?.replaceSelection(nameIdentifier);
155+
}
150156
}}
151157
// expression editor blur is fired after pointer down even
152158
// preventing it allows to not trigger validation

0 commit comments

Comments
 (0)