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

Fix Editable tagName changes after #635 #661

Merged
merged 5 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
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
22 changes: 7 additions & 15 deletions blocks/components/editable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,24 +252,11 @@ export default class Editable extends wp.element.Component {
}
}

componentWillUpdate( nextProps ) {
if ( this.editor && this.props.tagName !== nextProps.tagName ) {
this.editor.destroy();
}
}

componentWillUnmount() {
if ( this.editor ) {
this.onChange();
this.editor.destroy();
}
this.onChange();
}

componentDidUpdate( prevProps ) {
if ( this.props.tagName !== prevProps.tagName ) {
this.initialize();
}

if ( !! this.props.focus && ! prevProps.focus ) {
this.focus();
}
Expand Down Expand Up @@ -339,14 +326,19 @@ export default class Editable extends wp.element.Component {
const { tagName, style, value, focus, className, showAlignments = false, formattingControls } = this.props;
const classes = classnames( 'blocks-editable', className );

// Generating a key that includes `tagName` ensures that if the tag
// changes, we unmount (+ destroy) the previous TinyMCE element, then
// mount (+ initialize) a new child element in its place.
const key = [ 'editor', tagName ].join();

let element = (
<TinyMCE
tagName={ tagName }
onSetup={ this.onSetup }
style={ style }
className={ classes }
defaultValue={ value }
key="editor" />
key={ key } />
);

if ( focus ) {
Expand Down
34 changes: 25 additions & 9 deletions blocks/components/editable/tinymce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
export default class TinyMCE extends wp.element.Component {
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;
}

componentWillUnmount() {
if ( ! this.editor ) {
return;
}

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

initialize() {
tinymce.init( {
target: this.editorNode,
theme: false,
Expand All @@ -8,7 +29,10 @@ export default class TinyMCE extends wp.element.Component {
browser_spellcheck: true,
entity_encoding: 'raw',
convert_urls: false,
setup: this.props.onSetup,
setup: ( editor ) => {
this.editor = editor;
this.props.onSetup( editor );
},
formats: {
strikethrough: { inline: 'del' }
}
Expand All @@ -19,14 +43,6 @@ export default class TinyMCE extends wp.element.Component {
}
}

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;
}

render() {
const { tagName = 'div', style, className, defaultValue } = this.props;

Expand Down