From 69429650c4980ae943228de07417db0b0234df66 Mon Sep 17 00:00:00 2001 From: skodak Date: Sat, 20 Jun 2009 11:00:40 +0000 Subject: [PATCH] MDL-19211, MDL-19544 improved editor embedding, media and dragmath enabled only when moodle configuration and current user permissions allow it to work 100% --- lib/adminlib.php | 5 +- lib/editor/textarea/lib.php | 12 +- lib/editor/tinymce/extra/tinymce.js.php | 179 ---------------------- lib/editor/tinymce/extra/tinymce_utils.js | 58 +++++++ lib/editor/tinymce/lib.php | 91 +++++++++-- lib/editorlib.php | 16 +- lib/form/editor.php | 19 +-- lib/weblib.php | 5 +- mod/data/templates.php | 8 +- 9 files changed, 153 insertions(+), 240 deletions(-) delete mode 100644 lib/editor/tinymce/extra/tinymce.js.php create mode 100644 lib/editor/tinymce/extra/tinymce_utils.js diff --git a/lib/adminlib.php b/lib/adminlib.php index 75a74a2d99..66715c581d 100644 --- a/lib/adminlib.php +++ b/lib/adminlib.php @@ -1595,11 +1595,10 @@ class admin_setting_confightmleditor extends admin_setting_configtext { } $editor = get_preferred_texteditor(FORMAT_HTML); - $editorclass = $editor->get_legacy_textarea_class(); - $editor->use_editor($this->get_id()); + $editor->use_editor($this->get_id(), array('noclean'=>true)); return format_admin_setting($this, $this->visiblename, - '
', + '
', $this->description, true, '', $defaultinfo, $query); } } diff --git a/lib/editor/textarea/lib.php b/lib/editor/textarea/lib.php index d6400e92ef..1258d5fc88 100644 --- a/lib/editor/textarea/lib.php +++ b/lib/editor/textarea/lib.php @@ -45,17 +45,9 @@ class textarea_texteditor extends texteditor { return true; } - public function get_editor_element_class() { - return 'form-textarea-simple'; - } - - public function get_legacy_textarea_class() { - return 'form-textarea-legacy'; - } - - public function use_editor($elementid=null) { + public function use_editor($elementid, array $options=null) { return; - } + } } diff --git a/lib/editor/tinymce/extra/tinymce.js.php b/lib/editor/tinymce/extra/tinymce.js.php deleted file mode 100644 index a244098149..0000000000 --- a/lib/editor/tinymce/extra/tinymce.js.php +++ /dev/null @@ -1,179 +0,0 @@ -. - -/** - * On-the-fly conversion of Moodle lang strings to TinyMCE expected JS format. - * - * @package moodlecore - * @subpackage editor - * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -define('NO_MOODLE_COOKIES', true); -define('NO_UPGRADE_CHECK', true); - -require_once('../../../../config.php'); - -$lang = optional_param('elanguage', 'en_utf8', PARAM_SAFEDIR); -$theme = optional_param('etheme', 'standard', PARAM_SAFEDIR); -$usehttps = optional_param('eusehttps', 0, PARAM_BOOL); - -if (file_exists("$CFG->dataroot/lang/$lang") or file_exists("$CFG->dirroot/lang/$lang")) { - $SESSION->lang = $lang; -} else if (file_exists("$CFG->dataroot/lang/{$lang}_utf8") or file_exists("$CFG->dirroot/lang/{$lang}_utf8")) { - $SESSION->lang = $lang.'_utf8'; -} - -$xmlruleset = file_get_contents('xhtml_ruleset.txt'); - -$directionality = get_string('thisdirection'); - -$strtime = get_string('strftimetime'); -$strdate = get_string('strftimedaydate'); - -$lang = str_replace('_utf8', '', $lang); // use more standard language codes - -if ($usehttps) { - $wwwbase = str_replace('http:', 'https:', $CFG->wwwroot); -} else { - $wwwbase = $CFG->wwwroot; -} - -// $contentcss should be customizable -$contentcss = "$CFG->themewww/$theme/styles.php"; - -//TODO: reimplement spellchecker support - the TinyMCE one is hardcoded for linux, has encoding problems, etc. - -$output = <<. + +/** + * TinyMCE helper javascript functions + * + * @package moodlecore + * @subpackage editor + * @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +function mce_init_editor(elementid, params) { + tinyMCE.init(params); +} + +function mce_toggleEditor(id) { + tinyMCE.execCommand('mceToggleEditor',false,id); +} + +function mce_saveOnSubmit(id) { + var prevOnSubmit = document.getElementById(id).form.onsubmit; + document.getElementById(id).form.onsubmit = function() { + tinyMCE.triggerSave(); + var ret = true; + if (prevOnSubmit != undefined) { + if (prevOnSubmit()) { + ret = true; + prevOnSubmit = null; + } else { + ret = false; + } + } + return ret; + }; +} + +function mce_moodlefilemanager(field_name, url, type, win) { + var client_id = id2clientid[tinyMCE.selectedInstance.editorId]; + var picker = document.createElement('DIV'); + picker.className = "file-picker"; + picker.id = 'file-picker-'+client_id; + document.body.appendChild(picker); + var el = win.document.getElementById(field_name); + eval('open_filepicker(client_id, {"env":"editor","target":el,"filetype":type})'); +} diff --git a/lib/editor/tinymce/lib.php b/lib/editor/tinymce/lib.php index 3e2ef835b9..2d66a6a5f1 100644 --- a/lib/editor/tinymce/lib.php +++ b/lib/editor/tinymce/lib.php @@ -35,7 +35,7 @@ class tinymce_texteditor extends texteditor { } else if (check_browser_version('Opera', 9)) { return true; } - + return true; } @@ -51,22 +51,85 @@ class tinymce_texteditor extends texteditor { return true; } - public function get_editor_element_class() { - return 'form-tinymce-advanced'; - } - - public function get_legacy_textarea_class() { - return 'form-tinymce-legacy'; + public function use_editor($elementid, array $options=null) { + global $CFG, $PAGE; + + $PAGE->requires->js('/lib/editor/tinymce/tiny_mce_src.js'); + $PAGE->requires->js('/lib/editor/tinymce/extra/tinymce_utils.js'); + $PAGE->requires->js_function_call('mce_init_editor', array($elementid, $this->get_init_params($elementid, $options))); } - public function use_editor($elementid=null) { + protected function get_init_params($elementid, array $options=null) { global $CFG, $PAGE; - $usehttps = (int)($CFG->httpswwwroot !== $CFG->wwwroot); //hmm, is there a better test? - //TODO: requirements manager does not support parameters :-( + //TODO: we need to implement user preferences that affec tthe editor setup too + + //TODO: reimplement rulesets, maybe it would be better to implement it some other way + //$xmlruleset = file_get_contents('extra/xhtml_ruleset.txt'); + + $directionality = get_string('thisdirection'); + $strtime = get_string('strftimetime'); + $strdate = get_string('strftimedaydate'); + $lang = str_replace('_utf8', '', current_language()); // use more standard language codes + $contentcss = $CFG->themewww.'/'.current_theme().'/styles.php'; // should be customizable + + $context = empty($options['context']) ? get_context_instance(CONTEXT_SYSTEM) : $options['context']; + if (!empty($options['legacy'])) { + $xmedia = ''; + } else { + if (!empty($options['noclean']) or !empty($options['trusted'])) { + $xmedia = 'media,'; + } else { + $xmedia = ''; + } + } + + $filters = filter_get_active_in_context($context); + if (array_key_exists('filter/tex', $filters)) { + $xdragmath = 'dragmath,'; + } else { + $xdragmath = ''; + } + + $params = array( + 'mode' => "exact", + 'elements' => $elementid, + 'relative_urls' => false, + 'document_base_url' => $CFG->httpswwwroot, + 'content_css' => $contentcss, + 'language' => $lang, + 'directionality' => $directionality, + 'plugin_insertdate_dateFormat ' => $strdate, + 'plugin_insertdate_timeFormat ' => $strtime, + 'theme' => "advanced", + 'skin' => "o2k7", + 'skin_variant' => "silver", + 'apply_source_formatting' => true, + 'remove_script_host' => false, + 'entity_encoding' => "raw", + 'plugins' => "safari,table,style,layer,advhr,advimage,advlink,emotions,inlinepopups,{$xmedia}searchreplace,paste,directionality,fullscreen,moodlenolink,{$xdragmath}nonbreaking,contextmenu,insertdatetime,save,iespell,preview,print,noneditable,visualchars,xhtmlxtras,template,pagebreak", + 'theme_advanced_font_sizes' => "1,2,3,4,5,6,7", + 'theme_advanced_layout_manager' => "SimpleLayout", + 'theme_advanced_toolbar_align' => "left", + 'theme_advanced_buttons1' => "fontselect,fontsizeselect,formatselect,styleselect", + 'theme_advanced_buttons1_add' => "|,undo,redo,|,search,replace,|,fullscreen", + 'theme_advanced_buttons2' => "bold,italic,underline,strikethrough,sub,sup,|,justifyleft,justifycenter,justifyright,justifyfull,|,cite,abbr,acronym", + 'theme_advanced_buttons2_add' => "|,selectall,cleanup,removeformat,pastetext,pasteword,|,forecolor,backcolor,|,ltr,rtl", + 'theme_advanced_buttons3' => "bullist,numlist,outdent,indent,|,link,unlink,moodlenolink,anchor,|,insertdate,inserttime,|,emotions,image,{$xmedia}{$xdragmath}advhr,nonbreaking,charmap", + 'theme_advanced_buttons3_add' => "|,table,insertlayer,styleprops,visualchars,|,code,preview", + 'theme_advanced_fonts' => "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", + 'theme_advanced_resize_horizontal' => true, + 'theme_advanced_resizing' => true, + 'theme_advanced_toolbar_location' => "top", + 'theme_advanced_statusbar_location' => "bottom", + ); + + if (empty($options['legacy'])) { + if (isset($options['maxfiles']) and $options['maxfiles'] != 0) { + $params['file_browser_callback'] = "mce_moodlefilemanager"; + } + } - $PAGE->requires->js($CFG->httpswwwroot.'/lib/editor/tinymce/tiny_mce_src.js', true); - $PAGE->requires->js($CFG->httpswwwroot.'/lib/editor/tinymce/extra/tinymce.js.php?elanguage='.current_language().'&etheme='.current_theme().'&eusehttps='.$usehttps, true); + return $params; } - -} \ No newline at end of file +} diff --git a/lib/editorlib.php b/lib/editorlib.php index f42ed7eb5a..cb629f48c1 100644 --- a/lib/editorlib.php +++ b/lib/editorlib.php @@ -143,23 +143,13 @@ abstract class texteditor { */ public abstract function supports_repositories(); - /** - * Returns textarea class in formslib editor element - * @return string - */ - public abstract function get_editor_element_class(); - - /** - * Returns textarea class for legacy text editor - * @return string - */ - public abstract function get_legacy_textarea_class(); - /** * Add required JS needed for editor + * @param string $elementid id of text area to be converted to editor + * @param array $options * @return void */ - public abstract function use_editor($elementid=null); + public abstract function use_editor($elementid, array $options=null); } diff --git a/lib/form/editor.php b/lib/form/editor.php index 72a7e2b65a..9de29d3988 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -6,11 +6,11 @@ require_once('HTML/QuickForm/element.php'); // * locking // * freezing // * ajax format conversion -// * better area files handling class MoodleQuickForm_editor extends HTML_QuickForm_element { protected $_helpbutton = ''; - protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0); + protected $_options = array('subdirs'=>0, 'maxbytes'=>0, 'maxfiles'=>0, 'changeformat'=>0, + 'context'=>null, 'noclean'=>0, 'trusttext'=>0); protected $_values = array('text'=>null, 'format'=>null, 'itemid'=>null); function MoodleQuickForm_editor($elementName=null, $elementLabel=null, $attributes=null, $options=null) { @@ -25,6 +25,9 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { if (!empty($options['maxbytes'])) { $this->_options['maxbytes'] = get_max_upload_file_size($CFG->maxbytes, $options['maxbytes']); } + if (!$this->_options['context']) { + $this->_options['context'] = get_context_instance(CONTEXT_SYSTEM); + } parent::HTML_QuickForm_element($elementName, $elementLabel, $attributes); } @@ -136,10 +139,10 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { } /// print text area - TODO: add on-the-fly switching, size configuration, etc. - $editorclass = $editor->get_editor_element_class(); - $editor->use_editor($id); + $editor->use_editor($id, $this->_options); + $ctx = $this->_options['context']; - $str .= '
'; @@ -160,12 +163,6 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $draftitemid = $this->_values['itemid']; } $str .= '
'; - - if (empty($COURSE->context)) { - $ctx = get_context_instance(CONTEXT_SYSTEM); - } else { - $ctx = $COURSE->context; - } /// embedded image files - TODO: hide on the fly when switching editors $str .= '
'; $editorurl = "$CFG->wwwroot/repository/filepicker.php?action=embedded&itemid=$draftitemid&subdirs=$subdirs&maxbytes=$maxbytes&ctx_id=".$ctx->id; diff --git a/lib/weblib.php b/lib/weblib.php index 424a0b5826..43671abff2 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -4962,13 +4962,12 @@ function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $v if ($usehtmleditor) { $editor = get_preferred_texteditor(FORMAT_HTML); - $editorclass = $editor->get_legacy_textarea_class(); - $editor->use_editor($id); + $editor->use_editor($id, array('legacy'=>true)); } else { $editorclass = ''; } - $str .= "\n".'