diff --git a/src/components/ui/editor/JsonEditor.tsx b/src/components/ui/editor/JsonEditor.tsx index 320a06c..0dee58b 100644 --- a/src/components/ui/editor/JsonEditor.tsx +++ b/src/components/ui/editor/JsonEditor.tsx @@ -1,8 +1,8 @@ -import CodeMirror, { BasicSetupOptions } from '@uiw/react-codemirror'; +import CodeMirror, { BasicSetupOptions, ReactCodeMirrorRef } from '@uiw/react-codemirror'; import fullConfig from '../../../tailwindResolver'; import { json } from '@codemirror/lang-json'; import { SettingsContext, ThemeContext } from '../../../App'; -import { CSSProperties, useContext } from 'react'; +import { CSSProperties, ForwardedRef, forwardRef, useContext } from 'react'; import { Option, Properties } from '../../../types/components/Editor'; import { duotoneLight } from '@uiw/codemirror-theme-duotone'; @@ -20,7 +20,8 @@ interface Props{ contenteditable: boolean; } -export default function JsonEditor({ input, onChange, className, ...rest }: Props) { +export default forwardRef(function JsonEditor(props: Props, ref: ForwardedRef) { + const { input, onChange, className, ...rest } = props; const theme = useContext(ThemeContext); const settings = useContext(SettingsContext); @@ -52,6 +53,7 @@ export default function JsonEditor({ input, onChange, className, ...rest }: Prop <> ); -} +}); diff --git a/src/pages/Editors.tsx b/src/pages/Editors.tsx index 087729e..18824b5 100644 --- a/src/pages/Editors.tsx +++ b/src/pages/Editors.tsx @@ -1,4 +1,5 @@ -import { useEffect, useState, useRef } from 'react'; +import { Ref, useEffect, useRef, useState } from 'react'; +import { openSearchPanel } from '@codemirror/search'; import JsonEditor from '../components/ui/editor/JsonEditor'; import CleanUp from '../core/cleanUp'; import ResultMenu from '../components/ui/menu/ResultMenu'; @@ -6,6 +7,7 @@ import JsonMenu from '../components/ui/menu/JsonMenu'; import EditorContainer from '../components/ui/editor/EditorContainer'; import { EditorsPageProps } from '../types/pages'; import Loading from '../components/ui/Loading'; +import { ReactCodeMirrorRef } from '@uiw/react-codemirror'; const cleanUp = new CleanUp(); const defaultSpacing = '2'; @@ -54,6 +56,8 @@ export default function Editors({ onPersist, currentJson }: EditorsPageProps) { const [result, setResult] = useState(''); const [error, setError] = useState(''); const [spacing, setSpacing] = useState(defaultSpacing); + const jsonReferenceEditor = useRef(); + const resultReferenceEditor: any = useRef(); useEffect(() => { worker.current = new Worker(URL.createObjectURL(new Blob([code]))); @@ -120,62 +124,52 @@ export default function Editors({ onPersist, currentJson }: EditorsPageProps) { onChange(originalJson, newSpacing); }; - const search = (dataTestId: string) => { - const editor = document.querySelector(`[data-testid=${dataTestId}] .cm-content`) as HTMLElement; - if (editor) { - editor.focus(); - editor.dispatchEvent(new KeyboardEvent('keydown', { - 'key': 'f', - ctrlKey: true, - })); - } - }; - return ( -
-
- - onChange(text, spacing)} - onSearch={() => search('json')} - /> - - onChange(event.value, spacing)} - data-testid="json" - contenteditable={true} - /> - -
- {inProgress ? - - : null} -
- - search('result')} - /> - - - -
-
- {error &&

{error}

} + return
+
+ + onChange(text, spacing)} + onSearch={() => openSearchPanel(jsonReferenceEditor.current?.view!!)} + /> + + onChange(event.value, spacing)} + data-testid="json" + contenteditable={true} + ref={jsonReferenceEditor as Ref | undefined} + /> + +
+ {inProgress ? + + : null}
+ + openSearchPanel(resultReferenceEditor.current.view)} + /> + + + +
+
+ {error &&

{error}

}
- ); +
; }