|
| 1 | +import React, { useContext, useEffect, useRef, useState } from 'react' |
| 2 | +import * as monacoEditor from 'monaco-editor/esm/vs/editor/editor.api' |
| 3 | +import MonacoEditor, { monaco } from 'react-monaco-editor' |
| 4 | +import cx from 'classnames' |
| 5 | +import { darkTheme, lightTheme, MonacoThemes } from 'uiSrc/constants/monaco/cypher' |
| 6 | + |
| 7 | +import { Nullable } from 'uiSrc/utils' |
| 8 | +import { IEditorMount } from 'uiSrc/pages/workbench/interfaces' |
| 9 | +import { Theme } from 'uiSrc/constants' |
| 10 | +import { ThemeContext } from 'uiSrc/contexts/themeContext' |
| 11 | +import styles from './styles.modules.scss' |
| 12 | + |
| 13 | +export interface Props { |
| 14 | + value: string |
| 15 | + onChange: (value: string) => void |
| 16 | + disabled?: boolean |
| 17 | + wrapperClassName?: string |
| 18 | + 'data-testid'?: string |
| 19 | +} |
| 20 | +const MonacoJson = (props: Props) => { |
| 21 | + const { |
| 22 | + value: valueProp, |
| 23 | + onChange, |
| 24 | + disabled, |
| 25 | + wrapperClassName, |
| 26 | + 'data-testid': dataTestId |
| 27 | + } = props |
| 28 | + const [value, setValue] = useState<string>(valueProp) |
| 29 | + const monacoObjects = useRef<Nullable<IEditorMount>>(null) |
| 30 | + |
| 31 | + const { theme } = useContext(ThemeContext) |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + monacoObjects.current?.editor.updateOptions({ readOnly: disabled }) |
| 35 | + }, [disabled]) |
| 36 | + |
| 37 | + const handleChange = (val: string) => { |
| 38 | + setValue(val) |
| 39 | + onChange(val) |
| 40 | + } |
| 41 | + |
| 42 | + const editorDidMount = ( |
| 43 | + editor: monacoEditor.editor.IStandaloneCodeEditor, |
| 44 | + monaco: typeof monacoEditor, |
| 45 | + ) => { |
| 46 | + monacoObjects.current = { editor, monaco } |
| 47 | + monaco.languages.json.jsonDefaults.setDiagnosticsOptions({ |
| 48 | + validate: true, |
| 49 | + schemaValidation: 'error', |
| 50 | + schemaRequest: 'error', |
| 51 | + trailingCommas: 'error' |
| 52 | + }) |
| 53 | + } |
| 54 | + |
| 55 | + if (monaco?.editor) { |
| 56 | + monaco.editor.defineTheme(MonacoThemes.Dark, darkTheme) |
| 57 | + monaco.editor.defineTheme(MonacoThemes.Light, lightTheme) |
| 58 | + } |
| 59 | + |
| 60 | + const options: monacoEditor.editor.IStandaloneEditorConstructionOptions = { |
| 61 | + wordWrap: 'on', |
| 62 | + automaticLayout: true, |
| 63 | + formatOnPaste: false, |
| 64 | + padding: { top: 10 }, |
| 65 | + suggest: { |
| 66 | + preview: false, |
| 67 | + showStatusBar: false, |
| 68 | + showIcons: false, |
| 69 | + }, |
| 70 | + quickSuggestions: false, |
| 71 | + minimap: { |
| 72 | + enabled: false, |
| 73 | + }, |
| 74 | + overviewRulerLanes: 0, |
| 75 | + hideCursorInOverviewRuler: true, |
| 76 | + overviewRulerBorder: false, |
| 77 | + lineNumbersMinChars: 4, |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className={cx(styles.wrapper, wrapperClassName, { disabled })}> |
| 82 | + <MonacoEditor |
| 83 | + language="json" |
| 84 | + theme={theme === Theme.Dark ? 'dark' : 'light'} |
| 85 | + value={value} |
| 86 | + onChange={handleChange} |
| 87 | + options={options} |
| 88 | + className="json-monaco-editor" |
| 89 | + editorDidMount={editorDidMount} |
| 90 | + data-testid={dataTestId} |
| 91 | + /> |
| 92 | + </div> |
| 93 | + ) |
| 94 | +} |
| 95 | + |
| 96 | +export default MonacoJson |
0 commit comments