From f66da45fa543569374e09c483c7b1f2f032df475 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Thu, 4 Feb 2016 10:51:14 +0000 Subject: [PATCH] MDL-52996 Atto: Allow plugins to customise toolbar --- lib/editor/atto/lib.php | 16 +++- .../atto/tests/behat/customtoolbar.feature | 36 ++++++++ .../tests/fixtures/custom_toolbar_example.php | 86 +++++++++++++++++++ lib/editorlib.php | 6 +- 4 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 lib/editor/atto/tests/behat/customtoolbar.feature create mode 100644 lib/editor/atto/tests/fixtures/custom_toolbar_example.php diff --git a/lib/editor/atto/lib.php b/lib/editor/atto/lib.php index 9cc6d755f06..220d1ef65b0 100644 --- a/lib/editor/atto/lib.php +++ b/lib/editor/atto/lib.php @@ -69,6 +69,16 @@ class atto_texteditor extends texteditor { /** * Use this editor for given element. * + * Available Atto-specific options: + * atto:toolbar - set to a string to override the system config editor_atto/toolbar + * + * Available general options: + * context - set to the current context object + * enable_filemanagement - set false to get rid of the managefiles plugin + * autosave - true/false to control autosave + * + * Options are also passed through to the plugins. + * * @param string $elementid * @param array $options * @param null $fpoptions @@ -76,7 +86,11 @@ class atto_texteditor extends texteditor { public function use_editor($elementid, array $options=null, $fpoptions=null) { global $PAGE; - $configstr = get_config('editor_atto', 'toolbar'); + if (array_key_exists('atto:toolbar', $options)) { + $configstr = $options['atto:toolbar']; + } else { + $configstr = get_config('editor_atto', 'toolbar'); + } $grouplines = explode("\n", $configstr); diff --git a/lib/editor/atto/tests/behat/customtoolbar.feature b/lib/editor/atto/tests/behat/customtoolbar.feature new file mode 100644 index 00000000000..e9b88367a5d --- /dev/null +++ b/lib/editor/atto/tests/behat/customtoolbar.feature @@ -0,0 +1,36 @@ +@editor @editor_atto @atto +Feature: Atto editor with customised toolbar + In order to develop plugins that use Atto for specialised purposes + As a developer + I need to be able to configure Atto toolbar per-instance to include different plugins + + Background: + # Get to the fixture page. + Given the following "courses" exist: + | fullname | shortname | format | + | Course 1 | C1 | topics | + And the following "activities" exist: + | activity | name | intro | course | idnumber | + | label | L1 | FixtureLink | C1 | label1 | + When I log in as "admin" + And I am on site homepage + And I follow "Course 1" + And I follow "FixtureLink" + + @javascript + Scenario: Confirm that both editors have different toolbars but still function + Given ".atto_link_button" "css_element" should exist in the ".normaldiv" "css_element" + And ".atto_link_button" "css_element" should not exist in the ".specialdiv" "css_element" + + When I set the field "normaleditor" to "Frogs" + And I select the text in the "normaleditor" Atto editor + And I click on ".atto_bold_button_bold" "css_element" in the ".normaldiv" "css_element" + + And I set the field "specialeditor" to "Zombies" + And I select the text in the "specialeditor" Atto editor + And I click on ".atto_italic_button_italic" "css_element" in the ".specialdiv" "css_element" + + And I press "Submit and see the HTML" + + Then I should see "Frogs" + And I should see "Zombies" diff --git a/lib/editor/atto/tests/fixtures/custom_toolbar_example.php b/lib/editor/atto/tests/fixtures/custom_toolbar_example.php new file mode 100644 index 00000000000..683c2f9bc6a --- /dev/null +++ b/lib/editor/atto/tests/fixtures/custom_toolbar_example.php @@ -0,0 +1,86 @@ +. + +/** + * Demonstrates use of Atto editor with overridden toolbar setting. + * + * This fixture is only used by the Behat test. + * + * @package editor_atto + * @copyright 2016 The Open University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require(__DIR__ . '/../../../../../config.php'); +require_once($CFG->dirroot . '/lib/editor/atto/lib.php'); + +// Behat test fixture only. +defined('BEHAT_SITE_RUNNING') || die('Only available on Behat test server'); + +$PAGE->set_url('/lib/editor/atto/tests/fixtures/override_plugins_example.php'); +$PAGE->set_context(context_system::instance()); + +echo $OUTPUT->header(); + +// If this was sending some input, display it. +$normal = optional_param('normaleditor', '', PARAM_RAW); +$special = optional_param('specialeditor', '', PARAM_RAW); +if ($normal !== '' || $special !== '') { + echo html_writer::start_div('normalresult'); + echo s($normal); + echo html_writer::end_div(); + echo html_writer::start_div('specialresult'); + echo s($special); + echo html_writer::end_div(); +} else { + // Create a form. + echo html_writer::start_tag('form', array('method' => 'post', 'action' => 'custom_toolbar_example.php')); + echo html_writer::start_div(); + + // Basic editor options. + $options = array(); + $atto = new atto_texteditor(); + + // Normal Atto. + echo html_writer::start_div('normaldiv'); + echo $OUTPUT->heading('Normal Atto'); + echo html_writer::div(html_writer::tag('textarea', '', + array('id' => 'normaleditor', 'name' => 'normaleditor', 'rows' => 10))); + $atto->use_editor('normaleditor', $options); + echo html_writer::end_div(); + + // Second Atto with custom options. + echo html_writer::start_div('specialdiv'); + $options['atto:toolbar'] = <<heading('Special Atto'); + echo html_writer::div(html_writer::tag('textarea', '', + array('id' => 'specialeditor', 'name' => 'specialeditor', 'rows' => 10))); + $atto->use_editor('specialeditor', $options); + echo html_writer::end_div(); + + // Button to submit form. + echo html_writer::start_div('', array('style' => 'margin-top: 20px')); + echo html_writer::tag('button', 'Submit and see the HTML'); + echo html_writer::end_div(); + + echo html_writer::end_div(); + echo html_writer::end_tag('form'); +} + +echo $OUTPUT->footer(); diff --git a/lib/editorlib.php b/lib/editorlib.php index ce838c5320c..383f2bf6e52 100644 --- a/lib/editorlib.php +++ b/lib/editorlib.php @@ -225,8 +225,12 @@ abstract class texteditor { /** * Add required JS needed for editor + * + * Valid options may vary by editor. See the individual editor + * implementations of this function for documentation. + * * @param string $elementid id of text area to be converted to editor - * @param array $options + * @param array $options Editor options * @param obejct $fpoptions file picker options * @return void */ -- 2.43.0