Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Try: TinyMCE: On-demand initialization
  • Loading branch information
aduth committed Aug 15, 2018
commit 305e21469da7d09bc07a9e2f28dfe6441fbca229
37 changes: 33 additions & 4 deletions packages/editor/src/components/rich-text/tinymce.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ 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();
this.bindEditorNode = this.bindEditorNode.bind( this );
this.initialize = this.initialize.bind( this );
this.destroy = this.destroy.bind( this );
}

shouldComponentUpdate() {
Expand Down Expand Up @@ -134,12 +133,20 @@ export default class TinyMCE extends Component {
}

componentWillUnmount() {
this.destroy( false );
}

destroy( isReset = true ) {
if ( ! this.editor ) {
return;
}

this.editor.destroy();
delete this.editor;

if ( isReset ) {
this.editorNode.contentEditable = 'true';
}
}

configureIsPlaceholderVisible( isPlaceholderVisible ) {
Expand All @@ -150,6 +157,10 @@ export default class TinyMCE extends Component {
}

initialize() {
if ( this.editor ) {
return;
}

const settings = this.props.getSettings( {
theme: false,
inline: true,
Expand All @@ -172,6 +183,22 @@ export default class TinyMCE extends Component {
setup: ( editor ) => {
this.editor = editor;
this.props.onSetup( editor );

// TinyMCE resets the element content on initialization, even
// when it's already identical to what exists currently. This
// behavior clobbers a selection which exists at the time of
// initialization, thus breaking writing flow navigation. The
// hack here neutralizes setHTML during initialization.

let setHTML;
editor.on( 'PreInit', () => {
setHTML = editor.dom.setHTML;
editor.dom.setHTML = () => {};
} );

editor.on( 'Init', () => {
editor.dom.setHTML = setHTML;
} );
},
} );
}
Expand Down Expand Up @@ -217,6 +244,8 @@ export default class TinyMCE extends Component {
contentEditable: true,
[ IS_PLACEHOLDER_VISIBLE_ATTR_NAME ]: isPlaceholderVisible,
ref: this.bindEditorNode,
onFocus: this.initialize,
onBlur: this.destroy,
style,
suppressContentEditableWarning: true,
dangerouslySetInnerHTML: { __html: valueToString( defaultValue, format ) },
Expand Down