| 1 |
import React, { Component } from 'react'; |
| 2 |
|
| 3 |
import Loader from 'Shared/Loader'; |
| 4 |
|
| 5 |
let editor_uid = 0; |
| 6 |
|
| 7 |
export default class FieldTinymce extends Component { |
| 8 |
constructor(props) { |
| 9 |
super(props); |
| 10 |
|
| 11 |
// Make sure each render gets a unique UID. |
| 12 |
const uid = editor_uid; |
| 13 |
editor_uid = uid + 1; |
| 14 |
|
| 15 |
this.state = { |
| 16 |
editorHtml: false, |
| 17 |
addedListeners: false, |
| 18 |
editorId: `wpupg-admin-modal-tinymce-${uid}`, |
| 19 |
} |
| 20 |
|
| 21 |
this.initEditor = this.initEditor.bind(this); |
| 22 |
this.initTinyMCE = this.initTinyMCE.bind(this); |
| 23 |
} |
| 24 |
|
| 25 |
componentDidMount() { |
| 26 |
const editor = document.getElementById( 'wpupg-admin-modal-tinymce-placeholder' ); |
| 27 |
let editorHtml = editor.innerHTML; |
| 28 |
editorHtml = editorHtml.replace( /wpupg-admin-modal-tinymce/g, this.state.editorId ); |
| 29 |
|
| 30 |
this.setState({ |
| 31 |
editorHtml, |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
componentDidUpdate( prevProps, prevState ) { |
| 36 |
if ( this.state.editorHtml && ! prevState.editorHtml ) { |
| 37 |
this.initEditor(); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
initEditor() { |
| 42 |
if ( typeof window.tinymce !== 'undefined' ) { |
| 43 |
this.initTinyMCE(); |
| 44 |
} else { |
| 45 |
this.initTextarea(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
initTextarea() { |
| 50 |
const textarea = document.getElementById( this.state.editorId ); |
| 51 |
|
| 52 |
if ( typeof window.quicktags !== 'undefined' ) { |
| 53 |
window.quicktags( { id: this.state.editorId } ); |
| 54 |
} |
| 55 |
|
| 56 |
if ( textarea ) { |
| 57 |
textarea.value = this.props.value; |
| 58 |
|
| 59 |
['input', 'blur'].forEach((event) => { |
| 60 |
textarea.addEventListener(event, () => { |
| 61 |
this.props.onChange( textarea.value ); |
| 62 |
}); |
| 63 |
}); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
initTinyMCE() { |
| 68 |
// Clean up first. |
| 69 |
const container = document.getElementById( `wp-${this.state.editorId}-editor-container` ); |
| 70 |
container.outerHTML = `<textarea id="${this.state.editorId}"></textarea>`; |
| 71 |
|
| 72 |
const $wrap = tinymce.$( `#wp-${this.state.editorId}-wrap` ); |
| 73 |
|
| 74 |
// Force to text mode and init. |
| 75 |
$wrap.removeClass( 'tmce-active' ).addClass( 'html-active' ); |
| 76 |
this.initTextarea(); |
| 77 |
|
| 78 |
// Force to visual mode and init. |
| 79 |
$wrap.removeClass( 'html-active' ).addClass( 'tmce-active' ); |
| 80 |
|
| 81 |
let args = {}; |
| 82 |
if ( typeof window.tinyMCEPreInit !== 'undefined' && tinyMCEPreInit.hasOwnProperty('mceInit') && tinyMCEPreInit.mceInit.hasOwnProperty('wpupg-admin-modal-tinymce') ) { |
| 83 |
args = tinyMCEPreInit.mceInit['wpupg-admin-modal-tinymce']; |
| 84 |
} |
| 85 |
if ( args.hasOwnProperty('body_class') ) { |
| 86 |
args.body_class = args.body_class.replace( /wpupg-admin-modal-tinymce/g, this.state.editorId ); |
| 87 |
} |
| 88 |
args.selector = `#${this.state.editorId}`; |
| 89 |
|
| 90 |
window.tinymce.init( args ); |
| 91 |
|
| 92 |
// Attach listener. |
| 93 |
const editor = window.tinymce.get(this.state.editorId); |
| 94 |
|
| 95 |
if ( editor ) { |
| 96 |
editor.on('change', () => { |
| 97 |
this.props.onChange( editor.getContent() ); |
| 98 |
}); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
componentWillUnmount() { |
| 103 |
if ( typeof window.tinyMCE !== 'undefined' ) { |
| 104 |
window.tinyMCE.remove(`#${this.state.editorId}`); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
render() { |
| 109 |
if ( ! this.state.editorHtml ) { |
| 110 |
return <Loader/>; |
| 111 |
} |
| 112 |
|
| 113 |
return ( |
| 114 |
<div |
| 115 |
className="wpupg-admin-modal-field-tinymce-container" |
| 116 |
dangerouslySetInnerHTML={ { __html: this.state.editorHtml } } |
| 117 |
/> |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|