-
Notifications
You must be signed in to change notification settings - Fork 1
/
Editor.tsx
46 lines (38 loc) · 1.38 KB
/
Editor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { EditorCore, WrapperProps } from '@react-editor-js/core';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
import { Component, InputHTMLAttributes, PropsWithoutRef } from 'react';
import { createReactEditorJS } from 'react-editor-js';
const ReactEditorJS = createReactEditorJS();
export type EditorProps = PropsWithoutRef<
WrapperProps &
Pick<InputHTMLAttributes<HTMLInputElement>, 'name' | 'required'>
>;
@observer
export class Editor extends Component<EditorProps> {
static displayName = 'Editor';
private core?: EditorCore;
@observable
accessor innerValue = this.props.defaultValue;
save = async () => this.core && (this.innerValue = await this.core.save());
render() {
const { name, required, children, ...editorProps } = this.props,
{ innerValue } = this;
return (
<div className="form-control" tabIndex={-1} onBlur={this.save}>
<input
hidden
{...{ name, required }}
value={JSON.stringify(innerValue)}
/>
<ReactEditorJS
{...editorProps}
onInitialize={core => {
this.core = core;
editorProps.onInitialize?.(core);
}}
/>
</div>
);
}
}