|
|
@@ -1,10 +1,10 @@
|
|
|
import React, { forwardRef, ReactNode, Ref } from 'react';
|
|
|
|
|
|
import { ICodeMirror, UnControlled as CodeMirror } from 'react-codemirror2';
|
|
|
-import { Container, Subscribe } from 'unstated';
|
|
|
|
|
|
-import EditorContainer from '~/client/services/EditorContainer';
|
|
|
import AbstractEditor, { AbstractEditorProps } from '~/components/PageEditor/AbstractEditor';
|
|
|
+import { DEFAULT_THEME } from '~/interfaces/editor-settings';
|
|
|
+import { useEditorSettings } from '~/stores/editor';
|
|
|
|
|
|
window.CodeMirror = require('codemirror');
|
|
|
require('codemirror/addon/display/placeholder');
|
|
|
@@ -18,8 +18,9 @@ export interface UncontrolledCodeMirrorProps extends AbstractEditorProps {
|
|
|
}
|
|
|
|
|
|
interface UncontrolledCodeMirrorCoreProps extends UncontrolledCodeMirrorProps {
|
|
|
- editorContainer: Container<EditorContainer>;
|
|
|
forwardedRef: Ref<UncontrolledCodeMirrorCore>;
|
|
|
+ theme?: string,
|
|
|
+ styleActiveLine?: boolean,
|
|
|
}
|
|
|
|
|
|
class UncontrolledCodeMirrorCore extends AbstractEditor<UncontrolledCodeMirrorCoreProps> {
|
|
|
@@ -27,12 +28,11 @@ class UncontrolledCodeMirrorCore extends AbstractEditor<UncontrolledCodeMirrorCo
|
|
|
render(): ReactNode {
|
|
|
|
|
|
const {
|
|
|
- value, isGfmMode, lineNumbers, editorContainer, options, forwardedRef, ...rest
|
|
|
+ value, isGfmMode, lineNumbers, options, forwardedRef,
|
|
|
+ theme, styleActiveLine,
|
|
|
+ ...rest
|
|
|
} = this.props;
|
|
|
|
|
|
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
- const editorOptions = (editorContainer.state as any).editorOptions;
|
|
|
-
|
|
|
return (
|
|
|
<CodeMirror
|
|
|
ref={forwardedRef}
|
|
|
@@ -40,8 +40,8 @@ class UncontrolledCodeMirrorCore extends AbstractEditor<UncontrolledCodeMirrorCo
|
|
|
options={{
|
|
|
lineNumbers: lineNumbers ?? true,
|
|
|
mode: isGfmMode ? 'gfm-growi' : undefined,
|
|
|
- theme: editorOptions.theme,
|
|
|
- styleActiveLine: editorOptions.styleActiveLine,
|
|
|
+ theme: theme ?? DEFAULT_THEME,
|
|
|
+ styleActiveLine,
|
|
|
tabSize: 4,
|
|
|
...options,
|
|
|
}}
|
|
|
@@ -52,8 +52,15 @@ class UncontrolledCodeMirrorCore extends AbstractEditor<UncontrolledCodeMirrorCo
|
|
|
|
|
|
}
|
|
|
|
|
|
-export const UncontrolledCodeMirror = forwardRef<UncontrolledCodeMirrorCore, UncontrolledCodeMirrorProps>((props, ref) => (
|
|
|
- <Subscribe to={[EditorContainer]}>
|
|
|
- {(EditorContainer: Container<EditorContainer>) => <UncontrolledCodeMirrorCore {...props} forwardedRef={ref} editorContainer={EditorContainer} />}
|
|
|
- </Subscribe>
|
|
|
-));
|
|
|
+export const UncontrolledCodeMirror = forwardRef<UncontrolledCodeMirrorCore, UncontrolledCodeMirrorProps>((props, ref) => {
|
|
|
+ const { data: editorSettings } = useEditorSettings();
|
|
|
+
|
|
|
+ return (
|
|
|
+ <UncontrolledCodeMirrorCore
|
|
|
+ {...props}
|
|
|
+ forwardedRef={ref}
|
|
|
+ theme={editorSettings?.theme}
|
|
|
+ styleActiveLine={editorSettings?.styleActiveLine}
|
|
|
+ />
|
|
|
+ );
|
|
|
+});
|