-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e3ca774
commit 0f5ab70
Showing
10 changed files
with
422 additions
and
87 deletions.
There are no files selected for viewing
141 changes: 141 additions & 0 deletions
141
packages/graphiql-react/src/editor/components/increments-editors.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,141 @@ | ||
import { useCallback, useEffect, useRef, useState } from 'react'; | ||
|
||
import { ChevronDownIcon, ChevronUpIcon } from '../../icons'; | ||
import { UnStyledButton } from '../../ui'; | ||
import { | ||
commonKeys, | ||
DEFAULT_EDITOR_THEME, | ||
DEFAULT_KEY_MAP, | ||
importCodeMirror, | ||
} from '../common'; | ||
import { useSynchronizeOption } from '../hooks'; | ||
import { IncrementalPayload } from '../tabs'; | ||
import { CodeMirrorEditor, CommonEditorProps } from '../types'; | ||
|
||
import '../style/codemirror.css'; | ||
import '../style/fold.css'; | ||
import '../style/lint.css'; | ||
import '../style/hint.css'; | ||
import '../style/info.css'; | ||
import '../style/jump.css'; | ||
import '../style/auto-insertion.css'; | ||
import '../style/editor.css'; | ||
import '../style/increments-editors.css'; | ||
|
||
type UseIncrementsEditorArgs = CommonEditorProps & { | ||
increment: IncrementalPayload; | ||
}; | ||
|
||
function useIncrementsEditor({ | ||
editorTheme = DEFAULT_EDITOR_THEME, | ||
keyMap = DEFAULT_KEY_MAP, | ||
increment, | ||
}: UseIncrementsEditorArgs) { | ||
const [editor, setEditor] = useState<CodeMirrorEditor | null>(null); | ||
|
||
const ref = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
let isActive = true; | ||
void importCodeMirror( | ||
[ | ||
import('codemirror/addon/fold/foldgutter'), | ||
import('codemirror/addon/fold/brace-fold'), | ||
import('codemirror/addon/dialog/dialog'), | ||
import('codemirror/addon/search/search'), | ||
import('codemirror/addon/search/searchcursor'), | ||
import('codemirror/addon/search/jump-to-line'), | ||
// @ts-expect-error | ||
import('codemirror/keymap/sublime'), | ||
import('codemirror-graphql/esm/results/mode'), | ||
import('codemirror-graphql/esm/utils/info-addon'), | ||
], | ||
{ useCommonAddons: false }, | ||
).then(CodeMirror => { | ||
// Don't continue if the effect has already been cleaned up | ||
if (!isActive) { | ||
return; | ||
} | ||
|
||
const container = ref.current; | ||
if (!container) { | ||
return; | ||
} | ||
|
||
const newEditor = CodeMirror(container, { | ||
value: JSON.stringify(increment.payload, null, 2), | ||
lineWrapping: true, | ||
readOnly: true, | ||
theme: editorTheme, | ||
mode: 'graphql-results', | ||
foldGutter: true, | ||
gutters: ['CodeMirror-foldgutter'], | ||
// @ts-expect-error | ||
info: true, | ||
extraKeys: commonKeys, | ||
}); | ||
|
||
setEditor(newEditor); | ||
}); | ||
|
||
return () => { | ||
isActive = false; | ||
}; | ||
}, [editorTheme]); | ||
|
||
useSynchronizeOption(editor, 'keyMap', keyMap); | ||
|
||
return ref; | ||
} | ||
|
||
function IncrementEditor( | ||
props: UseIncrementsEditorArgs & { isInitial: boolean }, | ||
) { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const incrementEditor = useIncrementsEditor(props); | ||
|
||
const toggleEditor = useCallback(() => setIsOpen(current => !current), []); | ||
|
||
return ( | ||
<div | ||
className="graphiql-increment-editor" | ||
style={isOpen ? { height: '30vh' } : {}} | ||
> | ||
<UnStyledButton | ||
className="graphiql-increment-editor-toggle" | ||
onClick={toggleEditor} | ||
> | ||
{props.isInitial ? 'Initial payload' : 'Increment'} (after{' '} | ||
{props.increment.timing / 1000}s) | ||
{isOpen ? ( | ||
<ChevronUpIcon className="graphiql-increment-editor-chevron" /> | ||
) : ( | ||
<ChevronDownIcon className="graphiql-increment-editor-chevron" /> | ||
)} | ||
</UnStyledButton> | ||
<div | ||
ref={incrementEditor} | ||
className={`graphiql-editor ${isOpen ? '' : 'hidden'}`} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
export type IncrementsEditorsProps = CommonEditorProps & { | ||
incrementalPayloads: IncrementalPayload[]; | ||
}; | ||
|
||
export function IncrementsEditors(props: IncrementsEditorsProps) { | ||
return ( | ||
<div className="graphiql-increments-editors"> | ||
{props.incrementalPayloads.map((increment, index) => ( | ||
<IncrementEditor | ||
key={increment.timing} | ||
isInitial={index === 0} | ||
increment={increment} | ||
{...props} | ||
/> | ||
))} | ||
</div> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
export { HeaderEditor } from './header-editor'; | ||
export { ImagePreview } from './image-preview'; | ||
export { IncrementsEditors } from './increments-editors'; | ||
export { QueryEditor } from './query-editor'; | ||
export { ResponseEditor } from './response-editor'; | ||
export { VariableEditor } from './variable-editor'; | ||
|
||
export type { IncrementsEditorsProps } from './increments-editors'; |
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
31 changes: 31 additions & 0 deletions
31
packages/graphiql-react/src/editor/style/increments-editors.css
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,31 @@ | ||
.graphiql-increments-editors { | ||
display: flex; | ||
flex-direction: column; | ||
padding: var(--px-16); | ||
} | ||
|
||
.graphiql-increment-editor { | ||
padding: var(--px-4) 0; | ||
display: flex; | ||
flex-direction: column; | ||
position: relative; | ||
} | ||
|
||
.graphiql-increment-editor + .graphiql-increment-editor { | ||
border-top: 1px solid | ||
hsla(var(--color-neutral), var(--alpha-background-heavy)); | ||
} | ||
|
||
.graphiql-increment-editor-toggle, | ||
button.graphiql-increment-editor-toggle { | ||
padding: var(--px-2) var(--px-4); | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
} | ||
|
||
.graphiql-increment-editor-chevron { | ||
height: var(--px-12); | ||
width: var(--px-12); | ||
margin-left: var(--px-4); | ||
} |
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
Oops, something went wrong.