-
Notifications
You must be signed in to change notification settings - Fork 73
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
b4a1861
commit bc07fcb
Showing
5 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
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,55 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
import React from 'react'; | ||
import { basicSetup, EditorView } from 'codemirror'; | ||
import { EditorState, Compartment } from '@codemirror/state'; | ||
import { xml } from '@codemirror/lang-xml'; | ||
|
||
export type EditorAccessor = EditorView; | ||
|
||
interface Props { | ||
readOnly?: boolean; | ||
children?: string; | ||
editorRef?: React.MutableRefObject<EditorAccessor | undefined>; | ||
} | ||
|
||
export const CodeEditor: React.FC<Props> = ({ | ||
readOnly = false, | ||
children = '', | ||
editorRef, | ||
}) => { | ||
const divRef = React.useRef<HTMLDivElement>(null); | ||
const language = React.useMemo(() => new Compartment(), []); | ||
const tabSize = React.useMemo(() => new Compartment(), []); | ||
|
||
React.useEffect(() => { | ||
if (!divRef.current) { return; } | ||
const state = EditorState.create({ | ||
doc: children, | ||
extensions: [ | ||
basicSetup, | ||
language.of(xml()), | ||
tabSize.of(EditorState.tabSize.of(2)), | ||
EditorState.readOnly.of(readOnly), | ||
], | ||
}); | ||
|
||
const view = new EditorView({ | ||
state, | ||
parent: divRef.current, | ||
}); | ||
if (editorRef) { | ||
// eslint-disable-next-line no-param-reassign | ||
editorRef.current = view; | ||
} | ||
// eslint-disable-next-line consistent-return | ||
return () => { | ||
if (editorRef) { | ||
// eslint-disable-next-line no-param-reassign | ||
editorRef.current = undefined; | ||
} | ||
view.destroy(); // Clean up | ||
}; | ||
}, [divRef.current, readOnly, editorRef]); | ||
|
||
return <div ref={divRef} />; | ||
}; |
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