| @@ -1,0 +1,158 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.editor | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +JLoader::import('adapter.editor.editor'); | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * Editor class to handle a TinyMCE editor. | |
| 18 | + * | |
| 19 | + * @since 10.0 | |
| 20 | + */ | |
| 21 | +class JEditorTinyMCE extends JEditor | |
| 22 | +{ | |
| 23 | + /** | |
| 24 | + * @override | |
| 25 | + * Renders the editor area. | |
| 26 | + * | |
| 27 | + * @param string $name The control name. | |
| 28 | + * @param string $html The contents of the text area. | |
| 29 | + * @param string $width The width of the text area (px or %). | |
| 30 | + * @param string $height The height of the text area (px or %). | |
| 31 | + * @param integer $col The number of columns for the textarea. | |
| 32 | + * @param integer $row The number of rows for the textarea. | |
| 33 | + * @param boolean $buttons True and the editor buttons will be displayed. | |
| 34 | + * @param string $id An optional ID for the textarea (@since 10.1.20). | |
| 35 | + * @param array $params Associative array of editor parameters (@since 10.1.35). | |
| 36 | + * | |
| 37 | + * @return string The editor. | |
| 38 | + * | |
| 39 | + * @link https://codex.wordpress.org/Function_Reference/wp_editor | |
| 40 | + */ | |
| 41 | + protected function render($name, $html, $width, $height, $col, $row, $buttons, $id, $params) | |
| 42 | + { | |
| 43 | + $editor = [ | |
| 44 | + 'resize' => true, | |
| 45 | + 'wp_autoresize_on' => true, | |
| 46 | + 'add_unload_trigger' => false, | |
| 47 | + 'wp_keep_scroll_position' => true, | |
| 48 | + ]; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * It is now possible to override the parameters sent to the | |
| 52 | + * TinyMCE JS editor instance. | |
| 53 | + * | |
| 54 | + * @since 10.1.37 | |
| 55 | + */ | |
| 56 | + if (isset($params['tinymce'])) | |
| 57 | + { | |
| 58 | + // merge settings | |
| 59 | + $editor = array_merge($editor, $params['tinymce']); | |
| 60 | + // unset to avoid replacing the whole attribute while | |
| 61 | + // merging the global settings | |
| 62 | + unset($params['tinymce']); | |
| 63 | + } | |
| 64 | + | |
| 65 | + $settings = array( | |
| 66 | + 'media_buttons' => $buttons, | |
| 67 | + '_content_editor_dfw' => true, | |
| 68 | + 'drag_drop_upload' => true, | |
| 69 | + 'tabfocus_elements' => 'content-html,save-post', | |
| 70 | + 'editor_height' => $height, | |
| 71 | + 'tinymce' => $editor, | |
| 72 | + 'textarea_name' => $name, | |
| 73 | + ); | |
| 74 | + | |
| 75 | + /** | |
| 76 | + * It is now possible to override the parameters sent to the | |
| 77 | + * WordPress TinyMCE editor instance. | |
| 78 | + * | |
| 79 | + * @since 10.1.37 | |
| 80 | + */ | |
| 81 | + $settings = array_merge($settings, $params); | |
| 82 | + | |
| 83 | + // init editor | |
| 84 | + wp_editor($html, $id, $settings); | |
| 85 | + | |
| 86 | + $document = JFactory::getDocument(); | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Inject TinyMCE instance within Joomla.editors pool. | |
| 90 | + * | |
| 91 | + * @since 10.1.17 | |
| 92 | + */ | |
| 93 | + $document->addScriptDeclaration( | |
| 94 | +<<<JS | |
| 95 | +(function($) { | |
| 96 | + 'use strict'; | |
| 97 | + | |
| 98 | + $(function() { | |
| 99 | + if (Joomla.editors.instances.$id) { | |
| 100 | + tinyMCE.get('$id').remove(); | |
| 101 | + } | |
| 102 | + | |
| 103 | + Joomla.editors.instances['$id'] = { | |
| 104 | + id: '$id', | |
| 105 | + instance: null, | |
| 106 | + getValue: function() { | |
| 107 | + // get TinyMCE editor | |
| 108 | + var _e = tinyMCE.get(this.id); | |
| 109 | + if (_e) { | |
| 110 | + // editor exists, get contents | |
| 111 | + return _e.getContent(); | |
| 112 | + } | |
| 113 | + // get value from plain textarea | |
| 114 | + return $('textarea#' + this.id).val(); | |
| 115 | + }, | |
| 116 | + setValue: function(text) { | |
| 117 | + // get TinyMCE editor | |
| 118 | + var _e = tinyMCE.get(this.id); | |
| 119 | + if (_e) { | |
| 120 | + // editor exists, set contents | |
| 121 | + return _e.setContent(text); | |
| 122 | + } | |
| 123 | + // set value to plain textarea | |
| 124 | + return $('textarea#' + this.id).val(text); | |
| 125 | + }, | |
| 126 | + getSelection: function() { | |
| 127 | + return this.instance.selection.getContent({ | |
| 128 | + format: 'text' | |
| 129 | + }); | |
| 130 | + }, | |
| 131 | + replaceSelection: function(text) { | |
| 132 | + this.instance.execCommand('mceInsertContent', false, text); | |
| 133 | + }, | |
| 134 | + }; | |
| 135 | + | |
| 136 | + __isReady(() => { | |
| 137 | + // once the tinyMCE instance is ready... | |
| 138 | + return tinyMCE.get('$id'); | |
| 139 | + }).then((editor) => { | |
| 140 | + // ...register it into the CMS editors pool | |
| 141 | + Joomla.editors.instances['$id'].instance = editor; | |
| 142 | + }); | |
| 143 | + }); | |
| 144 | +})(jQuery); | |
| 145 | +JS | |
| 146 | + ); | |
| 147 | + | |
| 148 | + // if we are doing AJAX, render the editor via javascript | |
| 149 | + if (wp_doing_ajax()) | |
| 150 | + { | |
| 151 | + $document->addScriptDeclaration( | |
| 152 | +<<<JS | |
| 153 | +tinyMCE.execCommand('mceAddEditor', true, '$id'); | |
| 154 | +JS | |
| 155 | + ); | |
| 156 | + } | |
| 157 | + } | |
| 158 | +} | |