From c64f13178a02e06e9d5f94979b3c191acff54770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Fri, 31 Aug 2012 22:52:45 +0200 Subject: [PATCH 1/2] MDL-35172 rework editor toolbar setting --- lib/editor/tinymce/classes/plugin.php | 61 ++++++++++++++++--- lib/editor/tinymce/db/upgrade.php | 41 +++++++++++++ lib/editor/tinymce/lang/en/editor_tinymce.php | 4 +- lib/editor/tinymce/lib.php | 44 +++++++------ lib/editor/tinymce/settings.php | 7 ++- lib/editor/tinymce/version.php | 4 +- 6 files changed, 123 insertions(+), 38 deletions(-) create mode 100644 lib/editor/tinymce/db/upgrade.php diff --git a/lib/editor/tinymce/classes/plugin.php b/lib/editor/tinymce/classes/plugin.php index 305f8275c7b..35fe4f2a3ed 100644 --- a/lib/editor/tinymce/classes/plugin.php +++ b/lib/editor/tinymce/classes/plugin.php @@ -152,7 +152,12 @@ abstract class editor_tinymce_plugin { */ protected function add_button_after(array &$params, $row, $button, $after = '', $alwaysadd = true) { - $this->check_row($row); + + if ($this->is_button_present($params, $button)) { + return true; + } + + $row = $this->fix_row($params, $row); $field = 'theme_advanced_buttons' . $row; $old = $params[$field]; @@ -190,7 +195,7 @@ abstract class editor_tinymce_plugin { * to see if it succeeded. * * @param array $params TinyMCE init parameters array - * @param int $row Row to add button to (1 to 3) + * @param int $row Row to add button to (1 to 10) * @param string $button Identifier of button/plugin * @param string $before Adds button directly before the named plugin * @param bool $alwaysadd If specified $after string not found, add at start @@ -198,7 +203,11 @@ abstract class editor_tinymce_plugin { */ protected function add_button_before(array &$params, $row, $button, $before = '', $alwaysadd = true) { - $this->check_row($row); + + if ($this->is_button_present($params, $button)) { + return true; + } + $row = $this->fix_row($params, $row); $field = 'theme_advanced_buttons' . $row; $old = $params[$field]; @@ -226,15 +235,47 @@ abstract class editor_tinymce_plugin { } /** - * Checks the row value is valid. - * - * @param int $row Row to add button to (1 to 3) - * @throws coding_exception If row value is outside the range 1-3 + * Tests if button already present. + * @param array $params + * @param string $button + * @return bool */ - private function check_row($row) { - if ($row < 1 || $row > 3) { - throw new coding_exception("Invalid row option: $row"); + private function is_button_present(array $params, $button) { + for($i=1; $i<=10; $i++) { + $field = 'theme_advanced_buttons' . $i; + if (!isset($params[$field])) { + continue; + } + $buttons = explode(',', $params[$field]); + if (in_array($button, $buttons)) { + return true; + } } + return false; + } + + /** + * Checks the row value is valid, fix if necessary. + * + * @param array $params TinyMCE init parameters array + * @param int $row Row to add button if exists + * @return int requested row if exists, lower number if does not exist. + */ + private function fix_row(array &$params, $row) { + $row = ($row < 1) ? 1 : (int)$row; + $row = ($row > 10) ? 10 : $row; + + $field = 'theme_advanced_buttons' . $row; + if (isset($params[$field])) { + return $row; + } + for($i=$row; $i>=1; $i--) { + if (isset($params[$field])) { + return $row; + } + } + // This should not happen. + return 1; } /** diff --git a/lib/editor/tinymce/db/upgrade.php b/lib/editor/tinymce/db/upgrade.php new file mode 100644 index 00000000000..42d931e7807 --- /dev/null +++ b/lib/editor/tinymce/db/upgrade.php @@ -0,0 +1,41 @@ +. + +/** + * TinyMCE editor integration upgrade. + * + * @package editor_tinymce + * @copyright 2012 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +function xmldb_editor_tinymce_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + + if ($oldversion < 2012083100) { + // Reset redesigned editor toolbar setting. + unset_config('customtoolbar', 'editor_tinymce'); + upgrade_plugin_savepoint(true, 2012083100, 'editor', 'tinymce'); + } + + + return true; +} diff --git a/lib/editor/tinymce/lang/en/editor_tinymce.php b/lib/editor/tinymce/lang/en/editor_tinymce.php index d08d0f11745..fa01e4e1a12 100644 --- a/lib/editor/tinymce/lang/en/editor_tinymce.php +++ b/lib/editor/tinymce/lang/en/editor_tinymce.php @@ -27,8 +27,8 @@ $string['availablebuttons'] = 'Available buttons'; $string['common:browseimage'] = 'Find or upload an image...'; $string['common:browsemedia'] = 'Find or upload a sound, video or applet...'; -$string['customtoolbar'] = 'Custom editor toolbar'; -$string['customtoolbar_desc'] = 'Each line contains a list of comma separated button names, use "|" as a group separator. Leave empty if you want standard toolbar. See {$a} for the list of default TinyMCE buttons.'; +$string['customtoolbar'] = 'Editor toolbar'; +$string['customtoolbar_desc'] = 'Each line contains a list of comma separated button names, use "|" as a group separator, empty lines are ignored. See {$a} for the list of default TinyMCE buttons.'; $string['fontselectlist'] = 'Available fonts list'; $string['media_dlg:filename'] = 'Filename'; $string['pluginname'] = 'TinyMCE HTML editor'; diff --git a/lib/editor/tinymce/lib.php b/lib/editor/tinymce/lib.php index d079dae7498..d1faa1e43b0 100644 --- a/lib/editor/tinymce/lib.php +++ b/lib/editor/tinymce/lib.php @@ -127,7 +127,6 @@ class tinymce_texteditor extends texteditor { } $fontselectlist = empty($config->fontselectlist) ? '' : $config->fontselectlist; - $fontbutton = ($fontselectlist === '') ? '' : 'fontselect,'; $params = array( 'moodle_config' => $config, @@ -154,13 +153,6 @@ class tinymce_texteditor extends texteditor { 'theme_advanced_font_sizes' => "1,2,3,4,5,6,7", 'theme_advanced_layout_manager' => "SimpleLayout", 'theme_advanced_toolbar_align' => "left", - 'theme_advanced_buttons1' => $fontbutton . 'fontsizeselect,formatselect,|,' . - 'undo,redo,|,search,replace,|,fullscreen', - 'theme_advanced_buttons2' => 'bold,italic,underline,strikethrough,sub,sup,|,' . - 'justifyleft,justifycenter,justifyright,|,' . - 'cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl', - 'theme_advanced_buttons3' => 'bullist,numlist,outdent,indent,|,' . - 'link,unlink,|,image,nonbreaking,charmap,table,|,code', 'theme_advanced_fonts' => $fontselectlist, 'theme_advanced_resize_horizontal' => true, 'theme_advanced_resizing' => true, @@ -170,6 +162,19 @@ class tinymce_texteditor extends texteditor { 'theme_advanced_statusbar_location' => "bottom", ); + // Should we override the default toolbar layout unconditionally? + $customtoolbar = self::parse_toolbar_setting($config->customtoolbar); + if ($customtoolbar) { + $i = 1; + foreach ($customtoolbar as $line) { + $params['theme_advanced_buttons'.$i] = $line; + $i++; + } + } else { + // At least one line is required. + $params['theme_advanced_buttons1'] = ''; + } + if (!empty($options['legacy']) or !empty($options['noclean']) or !empty($options['trusted'])) { // now deal somehow with non-standard tags, people scream when we do not make moodle code xtml strict, // but they scream even more when we strip all tags that are not strict :-( @@ -188,20 +193,6 @@ class tinymce_texteditor extends texteditor { // Allow plugins to adjust parameters. editor_tinymce_plugin::all_update_init_params($params, $context, $options); - // Should we override the default toolbar layout unconditionally? - $customtoolbar = self::parse_toolbar_setting($config->customtoolbar); - if ($customtoolbar) { - unset($params['theme_advanced_buttons1']); - unset($params['theme_advanced_buttons2']); - unset($params['theme_advanced_buttons3']); - unset($params['theme_advanced_buttons4']); - $i = 1; - foreach ($customtoolbar as $line) { - $params['theme_advanced_buttons'.$i] = $line; - $i++; - } - } - // Remove temporary parameters. unset($params['moodle_config']); @@ -221,6 +212,7 @@ class tinymce_texteditor extends texteditor { } $customtoolbar = str_replace("\r", "\n", $customtoolbar); $customtoolbar = strtolower($customtoolbar); + $i = 0; foreach (explode("\n", $customtoolbar) as $line) { $line = preg_replace('/[^a-z0-9_,\|\-]/', ',', $line); $line = str_replace('|', ',|,', $line); @@ -229,7 +221,13 @@ class tinymce_texteditor extends texteditor { if ($line === '') { continue; } - $result[] = $line; + if ($i == 9) { + // Maximum is ten lines, merge the rest to the last line. + $result[9] = $result[9].','.$line; + } else { + $result[] = $line; + $i++; + } } return $result; } diff --git a/lib/editor/tinymce/settings.php b/lib/editor/tinymce/settings.php index d7ffb88332d..56ee3d03fab 100644 --- a/lib/editor/tinymce/settings.php +++ b/lib/editor/tinymce/settings.php @@ -31,8 +31,13 @@ if ($ADMIN->fulltree) { require_once(__DIR__.'/adminlib.php'); $settings->add(new tiynce_subplugins_settings()); $settings->add(new admin_setting_heading('tinymcegeneralheader', new lang_string('settings'), '')); + $default = "fontselect,fontsizeselect,formatselect,|,undo,redo,|,search,replace,|,fullscreen + +bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright,|,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl + +bullist,numlist,outdent,indent,|,link,unlink,|,image,nonbreaking,charmap,table,|,code"; $settings->add(new admin_setting_configtextarea('editor_tinymce/customtoolbar', - get_string('customtoolbar', 'editor_tinymce'), get_string('customtoolbar_desc', 'editor_tinymce', 'http://www.tinymce.com/wiki.php/Buttons/controls'), '', PARAM_RAW, 100, 6)); + get_string('customtoolbar', 'editor_tinymce'), get_string('customtoolbar_desc', 'editor_tinymce', 'http://www.tinymce.com/wiki.php/Buttons/controls'), $default, PARAM_RAW, 100, 8)); $settings->add(new admin_setting_configtextarea('editor_tinymce/fontselectlist', get_string('fontselectlist', 'editor_tinymce'), '', 'Trebuchet=Trebuchet MS,Verdana,Arial,Helvetica,sans-serif;Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact;Wingdings=wingdings', PARAM_RAW)); diff --git a/lib/editor/tinymce/version.php b/lib/editor/tinymce/version.php index 345b637d280..4d537de6065 100644 --- a/lib/editor/tinymce/version.php +++ b/lib/editor/tinymce/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2012081000; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2012061700; // Requires this Moodle version +$plugin->version = 2012083100; // The current plugin version (Date: YYYYMMDDXX) +$plugin->requires = 2012083100; // Requires this Moodle version $plugin->component = 'editor_tinymce'; // Full name of the plugin (used for diagnostics) $plugin->release = '3.6.0'; // This is NOT a directory name, see lib.php if you need to know where is the editor code! From 717a993122b873dd15cd6e9313089909ade88aa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20S=CC=8Ckoda?= Date: Wed, 5 Sep 2012 08:23:36 +0200 Subject: [PATCH 2/2] MDL-35172 fix handling of more than 10 tinymce toolbars --- lib/editor/tinymce/lib.php | 2 +- lib/editor/tinymce/tests/editor_test.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/editor/tinymce/lib.php b/lib/editor/tinymce/lib.php index d1faa1e43b0..ae784d3a1d9 100644 --- a/lib/editor/tinymce/lib.php +++ b/lib/editor/tinymce/lib.php @@ -221,7 +221,7 @@ class tinymce_texteditor extends texteditor { if ($line === '') { continue; } - if ($i == 9) { + if ($i == 10) { // Maximum is ten lines, merge the rest to the last line. $result[9] = $result[9].','.$line; } else { diff --git a/lib/editor/tinymce/tests/editor_test.php b/lib/editor/tinymce/tests/editor_test.php index 27db61bb075..1704bab5813 100644 --- a/lib/editor/tinymce/tests/editor_test.php +++ b/lib/editor/tinymce/tests/editor_test.php @@ -49,5 +49,8 @@ class editor_tinymce_testcase extends advanced_testcase { $result = tinymce_texteditor::parse_toolbar_setting("| \n\n| \n \r"); $this->assertSame(array(), $result); + + $result = tinymce_texteditor::parse_toolbar_setting("one\ntwo\n\nthree\nfour\nfive\nsix\nseven\neight\nnine\nten"); + $this->assertSame(array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten'), $result); } }