-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathRawEditor.js
156 lines (140 loc) · 4.26 KB
/
RawEditor.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from 'react';
import PropTypes from 'prop-types';
import ImmutablePropTypes from 'react-immutable-proptypes';
import styled from '@emotion/styled';
import { ClassNames } from '@emotion/core';
import { debounce } from 'lodash';
import { Value } from 'slate';
import { Editor as Slate, setEventTransfer } from 'slate-react';
import Plain from 'slate-plain-serializer';
import isHotkey from 'is-hotkey';
import { lengths, fonts } from 'netlify-cms-ui-default';
import { markdownToHtml } from '../serializers';
import { editorStyleVars, EditorControlBar } from '../styles';
import Toolbar from './Toolbar';
function rawEditorStyles({ minimal }) {
return `
position: relative;
overflow: hidden;
overflow-x: auto;
min-height: ${minimal ? 'auto' : lengths.richTextEditorMinHeight};
font-family: ${fonts.mono};
border-top-left-radius: 0;
border-top-right-radius: 0;
border-top: 0;
margin-top: -${editorStyleVars.stickyDistanceBottom};
`;
}
const RawEditorContainer = styled.div`
position: relative;
`;
export default class RawEditor extends React.Component {
constructor(props) {
super(props);
this.state = {
value: Plain.deserialize(this.props.value || ''),
};
}
shouldComponentUpdate(nextProps, nextState) {
return (
!this.state.value.equals(nextState.value) ||
nextProps.value !== Plain.serialize(nextState.value)
);
}
componentDidUpdate(prevProps) {
if (prevProps.value !== this.props.value) {
this.setState({ value: Plain.deserialize(this.props.value) });
}
}
componentDidMount() {
if (this.props.pendingFocus) {
this.editor.focus();
this.props.pendingFocus();
}
}
handleCopy = (event, editor) => {
const { getAsset, resolveWidget } = this.props;
const markdown = Plain.serialize(Value.create({ document: editor.value.fragment }));
const html = markdownToHtml(markdown, { getAsset, resolveWidget });
setEventTransfer(event, 'text', markdown);
setEventTransfer(event, 'html', html);
event.preventDefault();
};
handleCut = (event, editor, next) => {
this.handleCopy(event, editor, next);
editor.delete();
};
handlePaste = (event, editor, next) => {
event.preventDefault();
const data = event.clipboardData;
if (isHotkey('shift', event)) {
return next();
}
const value = Plain.deserialize(data.getData('text/plain'));
return editor.insertFragment(value.document);
};
handleChange = editor => {
if (!this.state.value.document.equals(editor.value.document)) {
this.handleDocumentChange(editor);
}
this.setState({ value: editor.value });
};
/**
* When the document value changes, serialize from Slate's AST back to plain
* text (which is Markdown) and pass that up as the new value.
*/
handleDocumentChange = debounce(editor => {
const value = Plain.serialize(editor.value);
this.props.onChange(value);
}, 150);
handleToggleMode = () => {
this.props.onMode('rich_text');
};
processRef = ref => {
this.editor = ref;
};
render() {
const { className, field, isShowModeToggle, t } = this.props;
return (
<RawEditorContainer>
<EditorControlBar>
<Toolbar
onToggleMode={this.handleToggleMode}
buttons={field.get('buttons')}
disabled
rawMode
isShowModeToggle={isShowModeToggle}
t={t}
/>
</EditorControlBar>
<ClassNames>
{({ css, cx }) => (
<Slate
className={cx(
className,
css`
${rawEditorStyles({ minimal: field.get('minimal') })}
`,
)}
value={this.state.value}
onChange={this.handleChange}
onPaste={this.handlePaste}
onCut={this.handleCut}
onCopy={this.handleCopy}
ref={this.processRef}
/>
)}
</ClassNames>
</RawEditorContainer>
);
}
}
RawEditor.propTypes = {
onChange: PropTypes.func.isRequired,
onMode: PropTypes.func.isRequired,
className: PropTypes.string.isRequired,
value: PropTypes.string,
field: ImmutablePropTypes.map.isRequired,
isShowModeToggle: PropTypes.bool.isRequired,
t: PropTypes.func.isRequired,
};