-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(editor): use a fancy editor for fallback hop editing
- Loading branch information
1 parent
aef18f2
commit 83ee303
Showing
7 changed files
with
113 additions
and
52 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import React, { useEffect, useId, useRef, useState } from 'react'; | ||
|
||
import { useDebounce } from '@uidotdev/usehooks'; | ||
|
||
import { Editor } from '@monaco-editor/react'; | ||
import type * as monaco from 'monaco-editor'; | ||
|
||
export const CodeEditor: React.FC<{ | ||
value: string; | ||
changed(val: string, hasErrors: boolean): void; | ||
language: 'json' | 'groovy' | 'jexl'; | ||
}> = ({ value: outsideValue, changed, language }) => { | ||
const editorRef = useRef<monaco.editor.IStandaloneCodeEditor | null>(null); | ||
|
||
const hasError = useRef(false); | ||
const [value, setValue] = useState(outsideValue); | ||
const prevValue = useRef<string>(outsideValue); | ||
const debouncedValue = useDebounce(prevValue.current, 600); | ||
|
||
// Call the change listener when the debounced value changes | ||
useEffect(() => { | ||
if (outsideValue !== debouncedValue) { | ||
changed(debouncedValue, hasError.current); | ||
} | ||
}, [debouncedValue, hasError.current]); | ||
|
||
// Allow value updates from outside | ||
useEffect(() => { | ||
if (editorRef.current && outsideValue !== prevValue.current) { | ||
prevValue.current = outsideValue; | ||
editorRef.current.setValue(outsideValue); | ||
} | ||
}, [outsideValue]); | ||
|
||
return ( | ||
<Editor | ||
onMount={ed => (editorRef.current = ed)} | ||
defaultValue={value} | ||
defaultLanguage={language} | ||
defaultPath={`inmemory://model/${useId()}.${language}`} | ||
theme="light" | ||
options={{ | ||
formatOnType: true, | ||
scrollBeyondLastLine: false, | ||
minimap: { | ||
enabled: false, | ||
}, | ||
}} | ||
height={250} | ||
onValidate={markers => { | ||
hasError.current = markers.length > 0; | ||
}} | ||
onChange={val => { | ||
prevValue.current = val ?? ''; | ||
setValue(val ?? ''); | ||
}} | ||
/> | ||
); | ||
}; |
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