Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try: TinyMCE: On-demand initialization #9040

Closed
wants to merge 1 commit into from
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
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