-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.js
244 lines (218 loc) · 6.6 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/**
* External dependencies
*/
import { createElement, Component } from 'wp-elements';
import tinymce from 'tinymce';
import { isEqual } from 'lodash';
function initialize( node, inline, onSetup ) {
if ( ! node ) {
return;
}
const config = {
target: node.querySelector( '[contenteditable=true]' ),
theme: false,
inline: true,
toolbar: false,
skin_url: '//s1.wp.com/wp-includes/js/tinymce/skins/lightgray',
entity_encoding: 'raw',
setup: onSetup,
formats: {
strikethrough: { inline: 'del' }
}
};
if ( inline ) {
config.valid_elements = '#p,br,b,i,strong,em,del,a[href|target]';
}
tinymce.init( config );
}
export default class EditableComponent extends Component {
static defaultProps = {
onChange: () => {},
splitValue: () => {},
onFocusChange: () => {},
onType: () => {},
initialContent: '',
inline: false,
single: false,
};
componentDidMount() {
initialize( this.node, this.props.inline, this.onSetup );
if ( this.props.focusConfig ) {
this.focus();
}
}
updateContent() {
// This could not be called on each content change, it used to change the cursor position
let bookmark;
if ( this.props.focusConfig ) {
bookmark = this.editor.selection.getBookmark( 2, true );
}
this.editor.setContent( this.props.content );
if ( this.props.focusConfig ) {
this.editor.selection.moveToBookmark( bookmark );
}
}
executeCommand = ( ...args ) => {
this.editor.execCommand( ...args );
};
componentWillUnmount() {
if ( this.editor ) {
this.editor.destroy();
}
}
componentDidUpdate( prevProps ) {
if ( this.props.focusConfig !== prevProps.focusConfig && this.props.focusConfig ) {
this.focus();
}
if ( this.props.content !== prevProps.content ) {
this.updateContent();
}
}
focus() {
if ( this.props.focusConfig.bookmark ) {
return;
}
const { start = false, end = false, bookmark = false } = this.props.focusConfig;
this.editor.focus();
if ( start ) {
this.editor.focus();
this.editor.selection.setCursorLocation( undefined, 0 );
} else if ( end ) {
this.editor.selection.select( this.editor.getBody(), true );
this.editor.selection.collapse( false );
} else if ( bookmark ) {
// this.editor.selection.moveToBookmark( bookmark );
}
}
isStartOfEditor() {
const range = this.editor.selection.getRng();
if ( range.startOffset !== 0 || ! range.collapsed ) {
return false;
}
const start = this.editor.selection.getStart();
const body = this.editor.getBody();
let element = start;
do {
const child = element;
element = element.parentNode;
if ( element.childNodes[ 0 ] !== child ) {
return false;
}
} while ( element !== body );
return true;
}
onKeyDown = ( event ) => {
if ( event.keyCode === 38 || event.keyCode === 37 ) {
if ( this.isStartOfEditor() ) {
event.preventDefault();
this.props.moveCursorUp();
}
} else if ( event.keyCode === 40 || event.keyCode === 39 ) {
const bookmark = this.editor.selection.getBookmark();
this.editor.selection.select( this.editor.getBody(), true );
this.editor.selection.collapse( false );
const finalBookmark = this.editor.selection.getBookmark( 2, true );
this.editor.selection.moveToBookmark( bookmark );
if ( isEqual( this.editor.selection.getBookmark( 2, true ), finalBookmark ) ) {
event.preventDefault();
this.props.moveCursorDown();
}
} else if ( event.keyCode === 13 && this.props.single ) {
// Wait for the event to propagate
setTimeout( () => {
this.editor.selection.getStart();
// Remove bogus nodes to avoid grammar bugs
Array.from( this.editor.getBody().querySelectorAll( '[data-mce-bogus]' ) )
.forEach( node => node.removeAttribute( 'data-mce-bogus' ) );
const childNodes = Array.from( this.editor.getBody().childNodes );
const splitIndex = childNodes.indexOf( this.editor.selection.getStart() );
const getHtml = ( nodes ) => nodes.reduce( ( memo, node ) => memo + node.outerHTML, '' );
const before = getHtml( childNodes.slice( 0, splitIndex ) );
const after = getHtml( childNodes.slice( splitIndex ) );
const hasAfter = !! childNodes.slice( splitIndex )
.reduce( ( memo, node ) => memo + node.textContent, '' );
this.editor.setContent( this.props.content );
if ( before ) {
this.props.splitValue( before, hasAfter ? after : '' );
}
} );
} else if ( event.keyCode === 8 ) {
if ( this.isStartOfEditor() ) {
event.preventDefault();
if ( this.editor.getBody().textContent ) {
this.onChange();
this.props.mergeWithPrevious();
} else {
this.props.remove();
}
}
}
}
onPaste = ( event ) => {
if ( this.props.inline ) {
event.preventDefault();
const clipboardData = event.clipboardData || window.clipboardData;
const text = clipboardData.getData( 'Text' );
this.editor.execCommand( 'mceInsertContent', false, text );
}
}
syncToolbar = ( event ) => {
if ( ! this.props.setToolbarState ) {
return;
}
const formats = [
{ id: 'bold', nodes: [ 'STRONG', 'B' ] },
{ id: 'italic', nodes: [ 'EM', 'I' ] },
{ id: 'strikethrough', nodes: [ 'DEL' ] }
];
formats.forEach( format => {
const formatValue =
format.nodes.indexOf( event.element.nodeName ) !== -1 ||
!! event.parents.find( parent => format.nodes.indexOf( parent.nodeName ) !== -1 );
this.props.setToolbarState( format.id, formatValue );
} );
// Link format
const link = event.element.nodeName === 'A'
? event.element
: event.parents.find( parent => parent.nodeName === 'A' );
const linkValue = link ? link.getAttribute( 'href' ) : '';
this.props.setToolbarState( 'link', linkValue );
};
onFocus = () => {
const bookmark = this.editor.selection.getBookmark( 2, true );
this.props.onFocusChange( { bookmark } );
};
onSetup = ( editor ) => {
this.editor = editor;
editor.on( 'init', this.onInit );
editor.on( 'focusout undo redo', this.onChange );
editor.on( 'keydown', this.onKeyDown );
editor.on( 'paste', this.onPaste );
editor.on( 'nodechange', this.syncToolbar );
editor.on( 'focusin', this.onFocus );
editor.on( 'paste keydown undo redo', this.props.onType );
};
onInit = () => {
this.editor.setContent( this.props.content );
};
onChange = () => {
// TODO: `getContent` is slow, but formats better than 'raw'. We
// should check implication of performance and see if we can rely
// on raw formatting instead.
const content = this.editor.getContent();
if ( content === this.props.content ) {
return;
}
this.props.onChange( content );
};
setRef = ( node ) => {
this.node = node;
};
render() {
return (
<div ref={ this.setRef }>
<div contentEditable />
</div>
);
}
}