Skip to content

Commit e22cfda

Browse files
committed
fix: multi line issue in variable-editor
1 parent 0412084 commit e22cfda

File tree

1 file changed

+39
-12
lines changed

1 file changed

+39
-12
lines changed

src/components/VariableEditor.tsx

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,19 @@ const VariableEditor = ({
4141
defaultValue = {},
4242
databaseVariables = {},
4343
}: VariableEditorProps) => {
44-
const initialCollection = Object.keys(defaultValue).map(key => ({ id: key, key, value: defaultValue[key] }));
45-
const [collection, setCollection] = useState<{ id: string, key: string, value: string }[]>(initialCollection);
44+
type Item = {
45+
id: string,
46+
key: string,
47+
value: string,
48+
multiLine: boolean,
49+
};
50+
const initialCollection = Object.keys(defaultValue).map<Item>(key => ({
51+
id: key,
52+
key,
53+
value: defaultValue[key],
54+
multiLine: defaultValue[key].includes("\n"),
55+
}));
56+
const [collection, setCollection] = useState<Item[]>(initialCollection);
4657

4758
const finalValue = useMemo(() => {
4859
const record = Object.fromEntries(collection.map(item => (item.key ? [item.key, item.value] : [])));
@@ -54,6 +65,7 @@ const VariableEditor = ({
5465
id: uuidv4(),
5566
key: "",
5667
value: "",
68+
multiLine: false,
5769
}]);
5870
};
5971

@@ -73,6 +85,7 @@ const VariableEditor = ({
7385
const isAppleDevice = (/Mac|iPhone|iPad|iPod/).test(navigator.userAgent);
7486
const isPressingCtrl = isAppleDevice ? e.metaKey : e.ctrlKey;
7587
if (isPressingCtrl && e.key === "v") {
88+
e.preventDefault();
7689
const text = await (async () => {
7790
try {
7891
return await navigator.clipboard.readText();
@@ -82,12 +95,14 @@ const VariableEditor = ({
8295
})();
8396
if (text) {
8497
if (text.includes("=")) {
85-
const newItems = Object.entries(parseEnvVariables(text)).map(line => {
98+
const newItems = Object.entries(parseEnvVariables(text)).map<Item>(line => {
8699
const [key, value] = line;
100+
const multiLine = value.startsWith('"') && value.endsWith('"');
87101
return {
88102
id: crypto.randomUUID(),
89103
key,
90-
value,
104+
value: multiLine ? JSON.parse(value) : value,
105+
multiLine,
91106
};
92107
});
93108
setCollection(c => {
@@ -181,14 +196,26 @@ const VariableEditor = ({
181196
onKeyDown={handleKeyDown}
182197
required
183198
/>
184-
<FormControl
185-
type={type}
186-
value={item.value}
187-
onChange={e => updateItemValue(item.id, e.target.value)}
188-
placeholder={valuePlaceholder}
189-
onKeyDown={handleKeyDown}
190-
required
191-
/>
199+
{item.multiLine && type === "text" ? (
200+
<FormControl
201+
as="textarea"
202+
rows={3}
203+
value={item.value}
204+
onChange={e => updateItemValue(item.id, e.target.value)}
205+
placeholder={valuePlaceholder}
206+
onKeyDown={handleKeyDown}
207+
required
208+
/>
209+
) : (
210+
<FormControl
211+
type={type}
212+
value={item.value}
213+
onChange={e => updateItemValue(item.id, e.target.value)}
214+
placeholder={valuePlaceholder}
215+
onKeyDown={handleKeyDown}
216+
required
217+
/>
218+
)}
192219
<Button variant="danger" onClick={() => removeItem(item.id)}>
193220
<FontAwesomeIcon icon={faXmark} size="lg" className="fa-fw" />
194221
</Button>

0 commit comments

Comments
 (0)