-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editable: implement TinyMCE on the core Editable component (#330)
* TinyMCE: V1 of the Editable Component initializing TinyMCE * Drop the global contenteditable * Expose package globals in test build * Rename TinyMCE dependency to clarify nightly More importantly to avoid potential naming clash with existing plugins. “Nightly” might be more accurately a version description, but this is all temporary anyways. * Adding native browser spellcheck
- Loading branch information
1 parent
8e62d60
commit ca4111c
Showing
8 changed files
with
106 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,8 @@ | |
} | ||
}, | ||
"globals": { | ||
"wp": true | ||
"wp": true, | ||
"tinymce": true | ||
}, | ||
"plugins": [ | ||
"react", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,75 @@ | ||
export default function Editable( { value } ) { | ||
return <p>{ value }</p>; | ||
export default class Editable extends wp.element.Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.onInit = this.onInit.bind( this ); | ||
this.onSetup = this.onSetup.bind( this ); | ||
this.onChange = this.onChange.bind( this ); | ||
this.bindNode = this.bindNode.bind( this ); | ||
} | ||
|
||
componentDidMount() { | ||
this.initialize(); | ||
} | ||
|
||
initialize() { | ||
const config = { | ||
target: this.node, | ||
theme: false, | ||
inline: true, | ||
toolbar: false, | ||
browser_spellcheck: true, | ||
entity_encoding: 'raw', | ||
setup: this.onSetup, | ||
formats: { | ||
strikethrough: { inline: 'del' } | ||
} | ||
}; | ||
|
||
tinymce.init( config ); | ||
} | ||
|
||
onSetup( editor ) { | ||
this.editor = editor; | ||
editor.on( 'init', this.onInit ); | ||
editor.on( 'focusout', this.onChange ); | ||
} | ||
|
||
onInit() { | ||
this.editor.setContent( this.props.value ); | ||
} | ||
|
||
onChange() { | ||
if ( ! this.editor.isDirty() ) { | ||
return; | ||
} | ||
const value = this.editor.getContent(); | ||
this.editor.save(); | ||
this.props.onChange( value ); | ||
} | ||
|
||
bindNode( ref ) { | ||
this.node = ref; | ||
} | ||
|
||
updateContent() { | ||
const bookmark = this.editor.selection.getBookmark( 2, true ); | ||
this.editor.setContent( this.props.value ); | ||
this.editor.selection.moveToBookmark( bookmark ); | ||
} | ||
|
||
componentWillUnmount() { | ||
if ( this.editor ) { | ||
this.editor.destroy(); | ||
} | ||
} | ||
|
||
componentDidUpdate( prevProps ) { | ||
if ( this.props.value !== prevProps.value ) { | ||
this.updateContent(); | ||
} | ||
} | ||
|
||
render() { | ||
return <div ref={ this.bindNode } />; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters