diff --git a/.eslintrc.json b/.eslintrc.json index 902f6cb6acbbb..2cb6635e3c133 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -18,7 +18,8 @@ } }, "globals": { - "wp": true + "wp": true, + "tinymce": true }, "plugins": [ "react", diff --git a/blocks/components/editable/index.js b/blocks/components/editable/index.js index 59fe33dcba1d7..9a1638c4b6be3 100644 --- a/blocks/components/editable/index.js +++ b/blocks/components/editable/index.js @@ -1,3 +1,75 @@ -export default function Editable( { value } ) { - return
{ value }
; +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 ; + } } diff --git a/editor/blocks/text-block/index.js b/editor/blocks/text-block/index.js index 45f5ee45d722f..812f67ec73f9a 100644 --- a/editor/blocks/text-block/index.js +++ b/editor/blocks/text-block/index.js @@ -1,4 +1,4 @@ -const { query, html } = wp.blocks.query; +const { html } = wp.blocks.query; const Editable = wp.blocks.Editable; wp.blocks.registerBlock( 'core/text', { @@ -6,7 +6,7 @@ wp.blocks.registerBlock( 'core/text', { icon: 'text', attributes: { - value: query( 'p', html() ) + value: html() }, edit( attributes, onChange ) { diff --git a/editor/editor/editor.js b/editor/editor/editor.js index d3f7cd34ac15c..6d2e6f315d9bd 100644 --- a/editor/editor/editor.js +++ b/editor/editor/editor.js @@ -6,7 +6,7 @@ import InserterButton from '../inserter/button'; const Editor = ( { state: { blocks, inserter }, toggleInserter } ) => { return (