Check editor value after update

This commit is contained in:
Damien Duhamel 2017-07-23 19:38:05 +02:00
parent 1d08f1a33b
commit 4d2ed6b1ea

View File

@ -105,10 +105,17 @@ export default class Editor extends Component {
} }
componentDidMount() { componentDidMount() {
this.view = new EditorView(this.ref, {
state: this.createEditorState(),
onAction: this.handleAction,
});
}
createEditorState() {
const { schema, parser } = this.state; const { schema, parser } = this.state;
const doc = parser.parse(this.props.value || ''); const doc = parser.parse(this.props.value || '');
this.view = new EditorView(this.ref, {
state: EditorState.create({ return EditorState.create({
doc, doc,
schema, schema,
plugins: [ plugins: [
@ -123,11 +130,17 @@ export default class Editor extends Component {
'Mod-y': history.redo, 'Mod-y': history.redo,
}), }),
], ],
}),
onAction: this.handleAction,
}); });
} }
componentDidUpdate(prevProps, prevState) {
const editorValue = this.state.serializer.serialize(this.view.state.doc);
if (editorValue !== this.props.value && editorValue !== prevProps.value) {
this.view.updateState(this.createEditorState());
}
}
handleAction = (action) => { handleAction = (action) => {
const { serializer } = this.state; const { serializer } = this.state;
const newState = this.view.state.applyAction(action); const newState = this.view.state.applyAction(action);