Файли
openidea/src/features/run/ExpandableTextarea.tsx
T
Isaac Gallartтаigallart 8f6200c3fa feat(run): add an expanded editor for Python arguments (#7307)
The Python run configuration's Arguments field becomes a one-line field with an expand button that opens the editor with one argument per line. Accepting joins them into one command line with single spaces, keeping quotes and escapes (new splitArgsText in platform/commandLine).

Closes #7306

Co-authored-by: igallart <[email protected]>
2026-12-02 07:27:18 +08:00

154 рядки
4.5 KiB
TypeScript

import { useRef, useState, type ChangeEvent, type KeyboardEvent, type RefObject } from "react";
import clsx from "clsx";
import { splitArgsText } from "@/platform/commandLine";
import { useTx } from "@/platform/i18n";
import { isImeKey } from "@/platform/ime";
import { Button, IconButton, Input, Popup } from "@/ui";
import styles from "./Run.module.css";
/**
* A run configuration's text field with an Expand button (⇧⏎) that
* edits it in a larger editor where each line stays on one visual line and scrolls sideways,
* so one long argument isn't mistaken for several.
*/
export function ExpandableTextarea({
label,
value,
placeholder,
rows = 4,
invalid,
commandLine = false,
onChange,
}: {
label: string;
value: string;
placeholder?: string;
rows?: number;
invalid?: boolean;
/** Show a command line inline and one argument per line in the expanded editor. */
commandLine?: boolean;
onChange: (value: string) => void;
}) {
const tx = useTx();
const fieldRef = useRef<HTMLDivElement>(null);
const [expanded, setExpanded] = useState(false);
// Configurations can already contain multiline arguments. Keep the compact
// view on one line without stripping spaces as the user types.
const inlineValue = commandLine && /[\r\n]/.test(value) ? splitArgsText(value).join(" ") : value;
const fieldProps = {
value: inlineValue,
onChange: (e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => onChange(e.target.value),
onKeyDown: (e: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
if (e.key === "Enter" && e.shiftKey && !e.altKey && !e.metaKey && !e.ctrlKey && !isImeKey(e.nativeEvent)) {
e.preventDefault();
setExpanded(true);
}
},
};
return (
<div
ref={fieldRef}
className={commandLine ? clsx(styles.inputWithButton, styles.compactTextarea) : styles.textareaWithButton}
>
{commandLine && !/[\r\n]/.test(inlineValue) ? (
<Input mono aria-label={label} placeholder={placeholder} invalid={invalid} {...fieldProps} />
) : (
<textarea
className={clsx(styles.textarea, invalid && styles.textareaInvalid)}
aria-label={tx(label)}
aria-invalid={invalid || undefined}
spellCheck={false}
placeholder={tx(placeholder)}
rows={commandLine ? 1 : rows}
wrap={commandLine ? "off" : undefined}
{...fieldProps}
/>
)}
<IconButton
icon={commandLine ? "list" : "expand-all"}
size="small"
iconSize={14}
title="Expand (⇧⏎)"
aria-haspopup="dialog"
aria-expanded={expanded}
onClick={() => setExpanded(!expanded)}
/>
{expanded && (
<ExpandedTextPopup
anchor={fieldRef}
label={tx(label)}
value={value}
commandLine={commandLine}
onOk={onChange}
onClose={() => setExpanded(false)}
/>
)}
</div>
);
}
function ExpandedTextPopup({
anchor,
label,
value,
commandLine,
onOk,
onClose,
}: {
anchor: RefObject<HTMLDivElement | null>;
label: string;
value: string;
commandLine: boolean;
onOk: (value: string) => void;
onClose: () => void;
}) {
const [text, setText] = useState(() => (commandLine ? splitArgsText(value).join("\n") : value));
const apply = () => {
const next = commandLine ? splitArgsText(text).join(" ") : text;
if (next !== value) onOk(next);
onClose();
};
return (
<Popup
open
anchor={anchor}
aria-label={label}
className={styles.expandedEditor}
onClose={onClose}
trapFocus
matchWidth
closeOnDeactivate={false}
resizable="both"
minWidth={240}
minHeight={160}
>
<div
className={styles.expandedEditorBody}
onKeyDown={(e) => {
if (e.key !== "Enter" || !(e.ctrlKey || e.metaKey) || e.altKey || e.shiftKey || isImeKey(e.nativeEvent))
return;
e.preventDefault();
e.stopPropagation();
apply();
}}
>
<textarea
className={clsx(styles.textarea, styles.expandedTextarea)}
aria-label={label}
spellCheck={false}
wrap="off"
value={text}
data-autofocus
onChange={(e) => setText(e.target.value)}
/>
<div className={styles.expandedEditorActions}>
<Button onClick={onClose}>Cancel</Button>
<Button variant="primary" onClick={apply}>
OK
</Button>
</div>
</div>
</Popup>
);
}