-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
tinymce.js
231 lines (202 loc) · 7.22 KB
/
tinymce.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
/**
* External dependencies
*/
import tinymce from 'tinymce';
import { isEqual } from 'lodash';
import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Component, createElement } from '@wordpress/element';
import { keycodes } from '@wordpress/utils';
/**
* Internal dependencies
*/
import { diffAriaProps, pickAriaProps } from './aria';
import { valueToString } from './format';
const { BACKSPACE, DELETE } = keycodes;
/**
* Determines whether we need a fix to provide `input` events for contenteditable.
*
* @param {Element} editorNode The root editor node.
*
* @return {boolean} A boolean indicating whether the fix is needed.
*/
function needsInternetExplorerInputFix( editorNode ) {
return (
// Rely on userAgent in the absence of a reasonable feature test for contenteditable `input` events.
/Trident/.test( window.navigator.userAgent ) &&
// IE11 dispatches input events for `<input>` and `<textarea>`.
! /input/i.test( editorNode.tagName ) &&
! /textarea/i.test( editorNode.tagName )
);
}
/**
* Applies a fix that provides `input` events for contenteditable in Internet Explorer.
*
* @param {Element} editorNode The root editor node.
*
* @return {Function} A function to remove the fix (for cleanup).
*/
function applyInternetExplorerInputFix( editorNode ) {
/**
* Dispatches `input` events in response to `textinput` events.
*
* IE provides a `textinput` event that is similar to an `input` event,
* and we use it to manually dispatch an `input` event.
* `textinput` is dispatched for text entry but for not deletions.
*
* @param {Event} textInputEvent An Internet Explorer `textinput` event.
*/
function mapTextInputEvent( textInputEvent ) {
textInputEvent.stopImmediatePropagation();
const inputEvent = document.createEvent( 'Event' );
inputEvent.initEvent( 'input', true, false );
inputEvent.data = textInputEvent.data;
textInputEvent.target.dispatchEvent( inputEvent );
}
/**
* Dispatches `input` events in response to Delete and Backspace keyup.
*
* It would be better dispatch an `input` event after each deleting
* `keydown` because the DOM is updated after each, but it is challenging
* to determine the right time to dispatch `input` since propagation of
* `keydown` can be stopped at any point.
*
* It's easier to listen for `keyup` in the capture phase and dispatch
* `input` before `keyup` propagates further. It's not perfect, but should
* be good enough.
*
* @param {KeyboardEvent} keyUp
* @param {Node} keyUp.target The event target.
* @param {number} keyUp.keyCode The key code.
*/
function mapDeletionKeyUpEvents( { target, keyCode } ) {
const isDeletion = BACKSPACE === keyCode || DELETE === keyCode;
if ( isDeletion && editorNode.contains( target ) ) {
const inputEvent = document.createEvent( 'Event' );
inputEvent.initEvent( 'input', true, false );
inputEvent.data = null;
target.dispatchEvent( inputEvent );
}
}
editorNode.addEventListener( 'textinput', mapTextInputEvent );
document.addEventListener( 'keyup', mapDeletionKeyUpEvents, true );
return function removeInternetExplorerInputFix() {
editorNode.removeEventListener( 'textinput', mapTextInputEvent );
document.removeEventListener( 'keyup', mapDeletionKeyUpEvents, true );
};
}
const IS_PLACEHOLDER_VISIBLE_ATTR_NAME = 'data-is-placeholder-visible';
export default class TinyMCE extends Component {
constructor() {
super();
this.bindEditorNode = this.bindEditorNode.bind( this );
}
componentDidMount() {
this.initialize();
}
shouldComponentUpdate() {
// We must prevent rerenders because TinyMCE will modify the DOM, thus
// breaking React's ability to reconcile changes.
//
// See: https://github.com/facebook/react/issues/6802
return false;
}
componentWillReceiveProps( nextProps ) {
this.configureIsPlaceholderVisible( nextProps.isPlaceholderVisible );
if ( ! isEqual( this.props.style, nextProps.style ) ) {
this.editorNode.setAttribute( 'style', '' );
Object.assign( this.editorNode.style, nextProps.style );
}
if ( ! isEqual( this.props.className, nextProps.className ) ) {
this.editorNode.className = classnames( nextProps.className, 'editor-rich-text__tinymce' );
}
const { removedKeys, updatedKeys } = diffAriaProps( this.props, nextProps );
removedKeys.forEach( ( key ) =>
this.editorNode.removeAttribute( key ) );
updatedKeys.forEach( ( key ) =>
this.editorNode.setAttribute( key, nextProps[ key ] ) );
}
componentWillUnmount() {
if ( ! this.editor ) {
return;
}
// This hack prevents TinyMCE from trying to remove the container node
// while cleaning for destroy, since removal is handled by React. It
// does so by substituting the container to be removed.
this.editor.container = document.createDocumentFragment();
this.editor.destroy();
delete this.editor;
}
configureIsPlaceholderVisible( isPlaceholderVisible ) {
const isPlaceholderVisibleString = String( !! isPlaceholderVisible );
if ( this.editorNode.getAttribute( IS_PLACEHOLDER_VISIBLE_ATTR_NAME ) !== isPlaceholderVisibleString ) {
this.editorNode.setAttribute( IS_PLACEHOLDER_VISIBLE_ATTR_NAME, isPlaceholderVisibleString );
}
}
initialize() {
const settings = this.props.getSettings( {
theme: false,
inline: true,
toolbar: false,
browser_spellcheck: true,
entity_encoding: 'raw',
convert_urls: false,
inline_boundaries_selector: 'a[href],code,b,i,strong,em,del,ins,sup,sub',
plugins: [],
formats: {
strikethrough: { inline: 'del' },
},
} );
settings.plugins.push( 'paste' );
tinymce.init( {
...settings,
target: this.editorNode,
setup: ( editor ) => {
this.editor = editor;
this.props.onSetup( editor );
},
} );
}
bindEditorNode( editorNode ) {
this.editorNode = editorNode;
/**
* A ref function can be used for cleanup because React calls it with
* `null` when unmounting.
*/
if ( this.removeInternetExplorerInputFix ) {
this.removeInternetExplorerInputFix();
this.removeInternetExplorerInputFix = null;
}
if ( editorNode && needsInternetExplorerInputFix( editorNode ) ) {
this.removeInternetExplorerInputFix = applyInternetExplorerInputFix( editorNode );
}
}
render() {
const { tagName = 'div', style, defaultValue, className, isPlaceholderVisible, format } = this.props;
const ariaProps = pickAriaProps( this.props );
/*
* The role=textbox and aria-multiline=true must always be used together
* as TinyMCE always behaves like a sort of textarea where text wraps in
* multiple lines. Only the table block editable element is excluded.
*/
if ( tagName !== 'table' ) {
ariaProps.role = 'textbox';
ariaProps[ 'aria-multiline' ] = true;
}
// If a default value is provided, render it into the DOM even before
// TinyMCE finishes initializing. This avoids a short delay by allowing
// us to show and focus the content before it's truly ready to edit.
return createElement( tagName, {
...ariaProps,
className: classnames( className, 'editor-rich-text__tinymce' ),
contentEditable: true,
[ IS_PLACEHOLDER_VISIBLE_ATTR_NAME ]: isPlaceholderVisible,
ref: this.bindEditorNode,
style,
suppressContentEditableWarning: true,
dangerouslySetInnerHTML: { __html: valueToString( defaultValue, format ) },
} );
}
}