-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make body field a Code editor field (#1221)
body inout
- Loading branch information
1 parent
3308fae
commit c3250b2
Showing
13 changed files
with
310 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
web/src/components/CreateTestPlugins/Rest/steps/RequestDetails/BodyField/BodyField.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import CodeMirror from '@uiw/react-codemirror'; | ||
import {Form, Radio} from 'antd'; | ||
import {BodyFieldContainer} from './BodyFieldContainer'; | ||
import {SingleLabel} from './SingleLabel'; | ||
import {useBodyMode} from './useBodyMode'; | ||
import {useLanguageExtensionsMemo} from './useLanguageExtensionsMemo'; | ||
|
||
interface IProps { | ||
isEditing?: boolean; | ||
body?: string; | ||
} | ||
|
||
export const BodyField = ({isEditing = false, body}: IProps): React.ReactElement => { | ||
const [bodyMode, setBodyMode] = useBodyMode(isEditing, body); | ||
const extensions = useLanguageExtensionsMemo(bodyMode); | ||
const hasNoBody = bodyMode === 'none'; | ||
return ( | ||
<> | ||
<span> | ||
<SingleLabel label="Request body">{null}</SingleLabel> | ||
<Radio.Group value={bodyMode} onChange={e => setBodyMode(e.target.value)}> | ||
<Radio value="none" data-cy="bodyMode-none"> | ||
None | ||
</Radio> | ||
<Radio value="raw" data-cy="bodyMode-raw"> | ||
Raw | ||
</Radio> | ||
<Radio value="json" data-cy="bodyMode-json"> | ||
JSON | ||
</Radio> | ||
<Radio value="xml" data-cy="bodyMode-xml"> | ||
XML | ||
</Radio> | ||
</Radio.Group> | ||
</span> | ||
{hasNoBody && ( | ||
<div> | ||
<h4>This request has no body {bodyMode}</h4> | ||
</div> | ||
)} | ||
<BodyFieldContainer $isDisplaying={hasNoBody}> | ||
<Form.Item name="body"> | ||
<CodeMirror | ||
data-cy="body" | ||
basicSetup={{lineNumbers: true, indentOnInput: true}} | ||
extensions={extensions} | ||
spellCheck={false} | ||
placeholder={`Enter request body text `} | ||
/> | ||
</Form.Item> | ||
</BodyFieldContainer> | ||
</> | ||
); | ||
}; |
25 changes: 25 additions & 0 deletions
25
...c/components/CreateTestPlugins/Rest/steps/RequestDetails/BodyField/BodyFieldContainer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const BodyFieldContainer = styled.div<{$isDisplaying: boolean}>` | ||
width: 100%; | ||
display: ${({$isDisplaying}) => ($isDisplaying ? 'none' : 'unset')}; | ||
&& { | ||
.cm-editor { | ||
overflow: hidden; | ||
display: flex; | ||
border-radius: 2px; | ||
font-size: ${({theme}) => theme.size.md}; | ||
outline: 1px solid grey; | ||
font-family: SFPro, serif; | ||
} | ||
.cm-line { | ||
padding: 0; | ||
span { | ||
font-family: SFPro, serif; | ||
} | ||
} | ||
} | ||
`; |
14 changes: 14 additions & 0 deletions
14
web/src/components/CreateTestPlugins/Rest/steps/RequestDetails/BodyField/SingleLabel.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {Form} from 'antd'; | ||
import styled from 'styled-components'; | ||
|
||
export const SingleLabel = styled(Form.Item)` | ||
&& { | ||
.ant-form-item-control-input { | ||
display: none; | ||
} | ||
.ant-form-item-label { | ||
padding: 0 0 8px; | ||
} | ||
} | ||
`; |
24 changes: 24 additions & 0 deletions
24
web/src/components/CreateTestPlugins/Rest/steps/RequestDetails/BodyField/useBodyMode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import {Dispatch, SetStateAction, useEffect, useState} from 'react'; | ||
import Validator from 'utils/Validator'; | ||
|
||
export type BodyMode = 'json' | 'xml' | 'raw' | 'none'; | ||
|
||
function useGuessBodyModeEffect( | ||
isEditing: undefined | boolean, | ||
setBodyMode: Dispatch<SetStateAction<BodyMode>>, | ||
body?: string | ||
) { | ||
const [initialized, setInitialized] = useState(false); | ||
useEffect(() => { | ||
if (!initialized && isEditing && body) { | ||
setInitialized(true); | ||
setBodyMode(Validator.getBodyType(body)); | ||
} | ||
}, [isEditing, setBodyMode, body, initialized, setInitialized]); | ||
} | ||
|
||
export function useBodyMode(isEditing?: boolean, body?: string): [BodyMode, Dispatch<SetStateAction<BodyMode>>] { | ||
const [bodyMode, setBodyMode] = useState<BodyMode>('none'); | ||
useGuessBodyModeEffect(isEditing, setBodyMode, body); | ||
return [bodyMode, setBodyMode]; | ||
} |
46 changes: 46 additions & 0 deletions
46
...nents/CreateTestPlugins/Rest/steps/RequestDetails/BodyField/useLanguageExtensionsMemo.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {json, jsonLanguage, jsonParseLinter} from '@codemirror/lang-json'; | ||
import {xml, xmlLanguage} from '@codemirror/lang-xml'; | ||
import {LanguageSupport} from '@codemirror/language'; | ||
import {Diagnostic, linter, lintGutter} from '@codemirror/lint'; | ||
import {EditorView} from '@codemirror/view'; | ||
import {XMLValidator} from 'fast-xml-parser'; | ||
import {useMemo} from 'react'; | ||
import {BodyMode} from './useBodyMode'; | ||
|
||
export function useLanguageExtensionsMemo(bodyMode: BodyMode): any[] { | ||
return useMemo(() => { | ||
switch (bodyMode) { | ||
case 'xml': | ||
return [ | ||
xml(), | ||
linter((view: EditorView): Diagnostic[] => { | ||
const result = XMLValidator.validate(view.state.doc.sliceString(0)); | ||
if (result === true) return []; | ||
return [ | ||
{ | ||
actions: [], | ||
severity: 'error', | ||
source: result.err.code, | ||
message: result.err.msg, | ||
from: result.err.line, | ||
to: result.err.col, | ||
}, | ||
]; | ||
}), | ||
new LanguageSupport(xmlLanguage), | ||
lintGutter({}), | ||
]; | ||
case 'json': | ||
return [ | ||
json(), | ||
linter((view: EditorView): Diagnostic[] => jsonParseLinter()(view)), | ||
new LanguageSupport(jsonLanguage), | ||
lintGutter({}), | ||
]; | ||
case 'raw': | ||
return []; | ||
default: | ||
return [lintGutter({})]; | ||
} | ||
}, [bodyMode]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters