Skip to content

Support multiline value when setting a environment variable [INS-5038] #8414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/insomnia/src/ui/components/codemirror/one-line-editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface OneLineEditorProps {
onPaste?: (text: string) => void;
onBlur?: (e: FocusEvent) => void;
eventListeners?: EditorEventListener<keyof EditorEventMap>[];
dynamicHeight?: boolean;
}

export interface EditorEventListener<T extends keyof EditorEventMap> {
Expand All @@ -53,6 +54,7 @@ export const OneLineEditor = forwardRef<OneLineEditorHandle, OneLineEditorProps>
onPaste,
onBlur,
eventListeners,
dynamicHeight = false,
}, ref) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const codeMirror = useRef<CodeMirror.EditorFromTextArea | null>(null);
Expand Down Expand Up @@ -98,7 +100,7 @@ export const OneLineEditor = forwardRef<OneLineEditorHandle, OneLineEditorProps>
lint: false,
matchBrackets: false,
autoCloseBrackets: false,
viewportMargin: 30,
viewportMargin: dynamicHeight ? Infinity : 30,
readOnly: !!readOnly,
tabindex: 0,
selectionPointer: 'default',
Expand Down Expand Up @@ -127,11 +129,16 @@ export const OneLineEditor = forwardRef<OneLineEditorHandle, OneLineEditorProps>
};
codeMirror.current = CodeMirror.fromTextArea(textAreaRef.current, initialOptions);
codeMirror.current.on('beforeChange', (_: CodeMirror.Editor, change: CodeMirror.EditorChangeCancellable) => {
const isPaste = change.text && change.text.length > 1;
if (isPaste) {
if (dynamicHeight) {
return;
}
// change.txt is ['', ''] when the user presses enter
const isPastingMultilineOrEnteringNewLine = change.text && change.text.length > 1;
if (isPastingMultilineOrEnteringNewLine) {
const startsWithCurl = change.text[0].startsWith('curl');
const isWhitespace = change.text.join('').trim();
if (startsWithCurl || !isWhitespace) {
const isWhitespace = !(change.text.join('').trim());
// why cancel when pasting curl here?
if (startsWithCurl || isWhitespace) {
change.cancel();
return;
}
Expand Down Expand Up @@ -211,7 +218,7 @@ export const OneLineEditor = forwardRef<OneLineEditorHandle, OneLineEditorProps>
id,
);
}
}, [defaultValue, getAutocompleteConstants, handleGetRenderContext, handleRender, onBlur, onKeyDown, onPaste, placeholder, readOnly, settings.autocompleteDelay, getKeyMap, settings.hotKeyRegistry, settings.nunjucksPowerUserMode, settings.showVariableSourceAndValue, eventListeners, id]);
}, [defaultValue, getAutocompleteConstants, handleGetRenderContext, handleRender, onBlur, onKeyDown, onPaste, placeholder, readOnly, settings.autocompleteDelay, getKeyMap, settings.hotKeyRegistry, settings.nunjucksPowerUserMode, settings.showVariableSourceAndValue, eventListeners, id, dynamicHeight]);

const cleanUpEditor = useCallback(() => {
codeMirror.current?.toTextArea();
Expand Down Expand Up @@ -324,6 +331,7 @@ export const OneLineEditor = forwardRef<OneLineEditorHandle, OneLineEditorProps>
className={classnames('editor--single-line', {
editor: true,
'editor--readonly': readOnly,
'editor--dynamic-height': dynamicHeight,
})}
data-editor-type={type || 'text'}
data-testid="OneLineEditor"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ interface EditorProps {
vaultKey?: string;
isPrivate?: boolean;
}
const cellCommonStyle = 'h-full px-2 flex items-center';

const createNewPair = (enabled: boolean = true): EnvironmentKvPairData => ({
id: generateId('envPair'),
Expand Down Expand Up @@ -183,10 +182,10 @@ export const EnvironmentKVEditor = ({ data, onChange, vaultKey = '', isPrivate =
const isValidJSONString = checkValidJSONString(value);
return (
<>
<div slot="drag" className={`${cellCommonStyle} w-6 flex flex-shrink-0 items-center justify-end border-l border-r-0`} style={{ padding: 0 }}>
<div slot="drag" className="h-[--line-height-sm] px-2 flex items-center w-6 flex-shrink-0 justify-end border-l border-r-0" style={{ padding: 0 }}>
<Icon icon="grip-vertical" className="cursor-grab mr-1" />
</div>
<div className={`${cellCommonStyle} relative h-full w-[30%] flex flex-grow pl-1`}>
<div className="px-2 flex items-center relative h-[--line-height-sm] w-[30%] flex-grow pl-1">
<OneLineEditor
id={`environment-kv-editor-name-${id}`}
placeholder={'Input Name'}
Expand Down Expand Up @@ -220,14 +219,15 @@ export const EnvironmentKVEditor = ({ data, onChange, vaultKey = '', isPrivate =
</Tooltip>
}
</div>
<div className={`${cellCommonStyle} w-[50%] relative`}>
<div className="min-h-[--line-height-sm] px-2 flex items-center w-[50%] relative">
{type === EnvironmentKvPairDataType.STRING &&
<OneLineEditor
id={`environment-kv-editor-value-${id}`}
placeholder={'Input Value'}
defaultValue={value.toString()}
readOnly={!enabled}
onChange={newValue => handleItemChange(id, 'value', newValue)}
dynamicHeight
/>
}
{type === EnvironmentKvPairDataType.JSON &&
Expand Down Expand Up @@ -279,7 +279,7 @@ export const EnvironmentKVEditor = ({ data, onChange, vaultKey = '', isPrivate =
/>
}
</div>
<div className={`${cellCommonStyle} w-32`} >
<div className="h-[--line-height-sm] px-2 flex items-center w-32" >
<MenuTrigger>
<ItemButton className="py-1 px-[--padding-sm] w-full font-bold flex flex-1 items-center justify-between aria-pressed:bg-[--hl-sm] rounded-sm text-[--color-font] hover:bg-[--hl-xs] text-sm" tabIndex={-1} aria-label='Type Selection'>
<span className="flex truncate items-center justify-center gap-2" >{kvPairItemTypes.find(t => t.id === type)?.name}</span>
Expand Down Expand Up @@ -320,7 +320,7 @@ export const EnvironmentKVEditor = ({ data, onChange, vaultKey = '', isPrivate =
</Popover>
</MenuTrigger>
</div>
<div className={`${cellCommonStyle} w-20`} >
<div className="h-[--line-height-sm] px-2 flex items-center w-20" >
<ItemButton
className="flex items-center justify-center h-7 aspect-square rounded-sm text-[--color-font] hover:bg-[--hl-xs] focus:ring-inset ring-1 ring-transparent focus:ring-[--hl-md] transition-all text-sm"
tabIndex={-1}
Expand Down Expand Up @@ -385,7 +385,7 @@ export const EnvironmentKVEditor = ({ data, onChange, vaultKey = '', isPrivate =
id={id}
textValue={`environment-item-${name || id}`}
style={{ opacity: enabled ? '1' : '0.4' }}
className={'w-full flex focus:outline-none h-[--line-height-sm]'}
className={'w-full flex focus:outline-none min-h-[--line-height-sm]'}
>
{renderPairItem(kvPair)}
</ListBoxItem>
Expand Down
7 changes: 7 additions & 0 deletions packages/insomnia/src/ui/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,13 @@ html {
.editor.editor--dynamic-height .CodeMirror-lines {
padding-bottom: 0.5em;
}
.editor.editor--single-line.editor--dynamic-height .CodeMirror-scroll {
align-items: flex-start;
}
.editor.editor--single-line.editor--dynamic-height .CodeMirror-lines {
padding-top: 0.7em;
}

/*
We need to restyle CodeMirrow for raw editor
*/
Expand Down
Loading