Skip to content

Commit bc5d295

Browse files
authored
feat: support expressions copy paste between instances (#4790)
Ref #4768 Here improved copy paste experience between expressions. All expressions while editing have are no longer encoded with ids. For example `system.search.name` is the same. Though invalid js characters are encoded with code point like this `Collection Item.title` becomes `Collection$32$Item.title` when copy into textual editor. And this less obscure name can be copied between different lists with the same `Collection Item` name.
1 parent c776166 commit bc5d295

File tree

6 files changed

+283
-129
lines changed

6 files changed

+283
-129
lines changed

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ const NameField = ({
9797
const variablesByName = useStore($variablesByName);
9898
const validateName = useCallback(
9999
(value: string) => {
100-
if (variablesByName.get(value) !== variableId) {
100+
if (
101+
variablesByName.has(value) &&
102+
variablesByName.get(value) !== variableId
103+
) {
101104
return "Name is already used by another variable on this instance";
102105
}
103106
if (value.trim().length === 0) {

apps/builder/app/builder/features/settings-panel/variables-section.tsx

+24-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { computed } from "nanostores";
33
import { useStore } from "@nanostores/react";
44
import {
55
Button,
6+
css,
67
CssValueListArrowFocus,
78
CssValueListItem,
89
DropdownMenu,
@@ -61,6 +62,8 @@ const $availableVariables = computed(
6162
if (instancePath === undefined) {
6263
return [];
6364
}
65+
const [{ instanceSelector }] = instancePath;
66+
const [selectedInstanceId] = instanceSelector;
6467
const availableVariables = new Map<DataSource["name"], DataSource>();
6568
// order from ancestor to descendant
6669
// so descendants can override ancestor variables
@@ -71,7 +74,12 @@ const $availableVariables = computed(
7174
}
7275
}
7376
}
74-
return Array.from(availableVariables.values());
77+
// order local variables first
78+
return Array.from(availableVariables.values()).sort((left, right) => {
79+
const leftRank = left.scopeInstanceId === selectedInstanceId ? 0 : 1;
80+
const rightRank = right.scopeInstanceId === selectedInstanceId ? 0 : 1;
81+
return leftRank - rightRank;
82+
});
7583
}
7684
);
7785

@@ -184,6 +192,13 @@ const EmptyVariables = () => {
184192
);
185193
};
186194

195+
const variableLabelStyle = css({
196+
whiteSpace: "nowrap",
197+
overflow: "hidden",
198+
textOverflow: "ellipsis",
199+
maxWidth: "100%",
200+
});
201+
187202
const VariablesItem = ({
188203
variable,
189204
source,
@@ -197,8 +212,6 @@ const VariablesItem = ({
197212
value: unknown;
198213
usageCount: number;
199214
}) => {
200-
const labelValue =
201-
value === undefined ? "" : `: ${formatValuePreview(value)}`;
202215
const [inspectDialogOpen, setInspectDialogOpen] = useState(false);
203216
const [isMenuOpen, setIsMenuOpen] = useState(false);
204217
return (
@@ -207,9 +220,14 @@ const VariablesItem = ({
207220
id={variable.id}
208221
index={index}
209222
label={
210-
<Flex align="center">
223+
<Flex align="center" css={{}}>
211224
<Label color={source}>{variable.name}</Label>
212-
{labelValue}
225+
{value !== undefined && (
226+
<span className={variableLabelStyle.toString()}>
227+
&nbsp;
228+
{formatValuePreview(value)}
229+
</span>
230+
)}
213231
</Flex>
214232
}
215233
disabled={source === "remote"}
@@ -276,6 +294,7 @@ const VariablesList = () => {
276294

277295
return (
278296
<CssValueListArrowFocus>
297+
{/* local variables should be ordered first to not block tab to first item */}
279298
{availableVariables.map((variable, index) => (
280299
<VariablesItem
281300
key={variable.id}

0 commit comments

Comments
 (0)