-
Notifications
You must be signed in to change notification settings - Fork 14
/
state.js
57 lines (51 loc) · 1.74 KB
/
state.js
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
47
48
49
50
51
52
53
54
55
56
57
import React, { Component } from 'react';
import { Raw, Plain } from 'slate';
import { batch } from '../utils/batch';
const parseValue = (value, initialState, terse) => {
try { return Raw.deserialize(value, { terse }); }
catch (err) {
try { return Raw.deserialize(value, { terse: !terse }); }
catch(err){
return initialState ? Raw.deserialize(initialState, { terse }) : Plain.deserialize('');
}
}
};
const defaultGetValue = ({ value }) => value;
const defaultChangeValue = ({ onChange }, value) => onChange ? onChange(value) : null;
export default ({ getValue = defaultGetValue, changeValue = defaultChangeValue, initialState, terse } = {}) => {
return Editor => class SlateStateDecorator extends Component {
static propTypes = { }
isFocused = false;
batch = batch(300);
rawValue = null;
constructor(props) {
super();
this.rawValue = getValue(props);
this.value = parseValue(this.rawValue, initialState, terse);
}
shouldComponentUpdate(props) {
const newValue = getValue(props);
const oldValue = getValue(this.props);
if (newValue !== oldValue && this.rawValue !== newValue) {
this.value = parseValue(newValue, undefined, terse);
return true;
} return false;
}
changeValue = value => {
this.value = value;
this.forceUpdate();
if (changeValue) {
const rawValue = Raw.serialize(value, { terse });
if (JSON.stringify(this.rawValue) !== JSON.stringify(rawValue)) {
this.rawValue = rawValue;
this.batch(() => changeValue(this.props, rawValue));
}
}
}
render() {
return (
<Editor {...this.props} value={this.value} onChange={this.changeValue} />
);
}
};
};