|
|
@@ -1,4 +1,4 @@
|
|
|
-import React, { FC, ReactNode } from 'react';
|
|
|
+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';
|
|
|
@@ -8,47 +8,51 @@ window.CodeMirror = require('codemirror');
|
|
|
require('codemirror/addon/display/placeholder');
|
|
|
require('~/client/util/codemirror/gfm-growi.mode');
|
|
|
|
|
|
-export interface UncontrolledCodeMirrorProps extends AbstractEditorProps {
|
|
|
+export interface UncontrolledCodeMirrorProps extends AbstractEditorProps, ICodeMirror {
|
|
|
value: string;
|
|
|
+ options?: ICodeMirror['options'];
|
|
|
isGfmMode?: boolean;
|
|
|
indentSize?: number;
|
|
|
- placeholder?: string;
|
|
|
lineNumbers?: boolean;
|
|
|
- onChange: ICodeMirror['onChange'];
|
|
|
}
|
|
|
|
|
|
interface UncontrolledCodeMirrorCoreProps extends UncontrolledCodeMirrorProps {
|
|
|
editorContainer: Container<EditorContainer>;
|
|
|
+ forwardedRef: Ref<UncontrolledCodeMirrorCore>;
|
|
|
}
|
|
|
|
|
|
class UncontrolledCodeMirrorCore extends AbstractEditor<UncontrolledCodeMirrorCoreProps> {
|
|
|
|
|
|
render(): ReactNode {
|
|
|
- const { editorOptions } = this.props.editorContainer.state;
|
|
|
- console.log(editorOptions);
|
|
|
+
|
|
|
+ const {
|
|
|
+ value, isGfmMode, indentSize, lineNumbers, editorContainer, options, forwardedRef, ...rest
|
|
|
+ } = this.props;
|
|
|
+
|
|
|
+ const { editorOptions } = editorContainer.state;
|
|
|
|
|
|
return (
|
|
|
<CodeMirror
|
|
|
- value={this.props.value}
|
|
|
+ ref={forwardedRef}
|
|
|
+ value={value}
|
|
|
options={{
|
|
|
- lineNumbers: this.props.lineNumbers ?? true,
|
|
|
- mode: this.props.isGfmMode ? 'gfm-growi' : undefined,
|
|
|
+ lineNumbers: lineNumbers ?? true,
|
|
|
+ mode: isGfmMode ? 'gfm-growi' : undefined,
|
|
|
theme: editorOptions.theme,
|
|
|
styleActiveLine: editorOptions.styleActiveLine,
|
|
|
tabSize: 4,
|
|
|
- indentUnit: this.props.indentSize,
|
|
|
- placeholder: this.props.placeholder,
|
|
|
+ indentUnit: indentSize,
|
|
|
+ ...options,
|
|
|
}}
|
|
|
- onChange={this.props.onChange}
|
|
|
+ {...rest}
|
|
|
/>
|
|
|
);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-export const UncontrolledCodeMirror: FC<UncontrolledCodeMirrorProps> = props => (
|
|
|
+export const UncontrolledCodeMirror = forwardRef<UncontrolledCodeMirrorCore, UncontrolledCodeMirrorProps>((props, ref) => (
|
|
|
<Subscribe to={[EditorContainer]}>
|
|
|
- {(EditorContainer: Container<EditorContainer>) => <UncontrolledCodeMirrorCore {...props} editorContainer={EditorContainer} />}
|
|
|
+ {(EditorContainer: Container<EditorContainer>) => <UncontrolledCodeMirrorCore {...props} forwardedRef={ref} editorContainer={EditorContainer} />}
|
|
|
</Subscribe>
|
|
|
-);
|
|
|
+));
|