1+ import React from "react" ;
2+ import { ValidationProps } from "./useForm" ;
3+ import { InputWrapper , InputWrapperProps } from "./InputWrapper" ;
4+ import { CompletionContext , CompletionResult } from "@codemirror/autocomplete" ;
5+ import CodeMirror , { Extension } from "@uiw/react-codemirror" ;
6+ import { StreamLanguage , TagStyle } from "@codemirror/language" ;
7+ import { createTheme } from "@uiw/codemirror-themes" ;
8+ import { tags as t } from "@lezer/highlight" ;
9+ import { hashToColor , mergeComponentProps } from "../../utils" ;
10+ import "./EditorInput.style.scss"
11+
12+ export interface EditorInputProps extends Omit < InputWrapperProps , 'onChange' > , ValidationProps < any > {
13+ language ?: StreamLanguage < unknown >
14+ suggestions ?: ( context : CompletionContext ) => CompletionResult
15+ extensions ?: Extension [ ]
16+ disabled ?: boolean
17+ readonly ?: boolean
18+ tokenStyles ?: TagStyle [ ]
19+ onChange ?: ( value : string ) => void
20+ }
21+
22+ export const EditorInput : React . FC < EditorInputProps > = ( props ) => {
23+
24+ const { title, right, left, rightType, leftType, language, description, extensions = [ ] , tokenStyles = [ ] , formValidation, onChange, ...rest } = props
25+
26+ const internalExtensions : Extension [ ] = [ ...extensions , language ! ]
27+
28+ const myTheme = React . useMemo (
29+ ( ) => createTheme ( {
30+ theme : 'light' ,
31+ settings : {
32+ background : 'transparent' ,
33+ backgroundImage : '' ,
34+ foreground : 'rgba(255,255,255, 0.75)' ,
35+ caret : 'gray' ,
36+ selection : 'rgba(112,179,255,0.25)' ,
37+ selectionMatch : 'rgba(112,179,255,0.1)' ,
38+ fontSize : "0.8rem" ,
39+ gutterBackground : 'transparent' ,
40+ gutterForeground : 'rgba(255,255,255, 0.5)' ,
41+ gutterBorder : 'transparent' ,
42+ gutterActiveForeground : 'rgba(255,255,255, 1)' ,
43+ lineHighlight : 'rgba(255,255,255, 0.1)' ,
44+ } ,
45+ styles : [
46+ { tag : t . squareBracket , color : hashToColor ( "squareBracket" ) } ,
47+ { tag : t . bracket , color : hashToColor ( "bracket" ) } ,
48+ { tag : t . string , color : hashToColor ( "Text" ) } ,
49+ { tag : t . bool , color : hashToColor ( "Boolean" ) } ,
50+ { tag : t . number , color : hashToColor ( "Number" ) } ,
51+ ...tokenStyles
52+ ]
53+ } ) ,
54+ [ tokenStyles ]
55+ )
56+
57+ return < InputWrapper title = { title }
58+ description = { description }
59+ right = { right }
60+ left = { left }
61+ rightType = { rightType }
62+ leftType = { leftType }
63+ formValidation = { formValidation } >
64+
65+ < CodeMirror extensions = { internalExtensions } onChange = { value => {
66+ onChange ?.( value )
67+ formValidation ?. setValue ( value )
68+ } } theme = { myTheme } { ...mergeComponentProps ( "editor-input" , rest ) } basicSetup = { {
69+ lineNumbers : false ,
70+ foldGutter : false ,
71+ highlightActiveLine : false ,
72+ highlightActiveLineGutter : false ,
73+ } } />
74+ </ InputWrapper >
75+ }
0 commit comments