Skip to content

Commit

Permalink
fix codechecker, js lint and phpcs issues
Browse files Browse the repository at this point in the history
  • Loading branch information
srobotta committed Mar 31, 2023
1 parent 868a233 commit 943a46d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ TinyMCE multilanguage plugin

[![Moodle Plugin CI](https://github.com/bfh/moodle-tiny_multilang2/workflows/Moodle%20Plugin%20CI/badge.svg?branch=master)](https://github.com/bfh/moodle-tiny_multilang2/actions?query=workflow%3A%22Moodle+Plugin+CI%22+branch%3Amaster)
![Release](https://img.shields.io/badge/release-v0.2.0-blue.svg)
![Supported](https://img.shields.io/badge/supported-4.1%2C%204.2-green.svg)
![Supported](https://img.shields.io/badge/supported-4.1-green.svg)

This plugin will make the creation of multilingual contents on Moodle much easier with the TinyMCE editor.

The plugin is developed to work with [Iñaki Arenaza's multilang2 filter](https://github.com/iarenaza/moodle-filter_multilang2), and this plugin is the adaption
on [his plugin for TinyMCE editor](https://github.com/iarenaza/moodle-tinymce_moodlelang2) and will work with TinyMCE 6
that is included in Moodle 4.x.
that is included in Moodle ≥ 4.1.

After the installation there is a new Button (with a globe icon) and a menu entry in the Format section where you can
select a language. Clicking on a language entry adds a language opening and closing tag to your text at the current
Expand All @@ -36,5 +36,5 @@ The plugin [filter_multilang2](https://github.com/iarenaza/moodle-filter_multila
## Troubleshooting

If the language selection does not appear in the editor:
- Check that the multilang filter is installed and enabled.
- Check that the multilang2 filter is installed and enabled.
- Check that your site has at least two languages installed.
2 changes: 1 addition & 1 deletion amd/build/ui.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion amd/build/ui.min.js.map

Large diffs are not rendered by default.

46 changes: 24 additions & 22 deletions amd/src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ import {getHighlightCss, isContentToHighlight} from './options';


// This class inside a <span> identified the {mlang} tag that is encapsulated in a span.
const _span_class = 'class="multilang-begin mceNonEditable"';
const spanClass = 'class="multilang-begin mceNonEditable"';
// This is the <span> element with the data attribute.
const _span_fixed_attrs = '<span ' + _span_class + ' data-mce-contenteditable="false"';
const spanFixedAttrs = '<span ' + spanClass + ' data-mce-contenteditable="false"';
// The begin span needs the language attributes inside the span and the mlang attribute.
const _span_multilang_begin = _span_fixed_attrs + ' lang="%lang" xml:lang="%lang">{mlang %lang}</span>';
const spanMultilangBegin = spanFixedAttrs + ' lang="%lang" xml:lang="%lang">{mlang %lang}</span>';
// The end span doesn't need information about the used language.
const _span_multilang_end = _span_fixed_attrs.replace('begin', 'end') + '>{mlang}</span>';
const spanMultilangEnd = spanFixedAttrs.replace('begin', 'end') + '>{mlang}</span>';
// Helper functions
const trim = v => v.toString().replace(/^\s+/, '').replace(/\s+$/, '');
const isNull = a => a === null || a === undefined;
Expand All @@ -42,20 +42,21 @@ const isNull = a => a === null || a === undefined;
* Convert {mlang xx} and {mlang} strings to spans, so we can style them visually.
* Remove superflous whitespace while at it.
* @param {tinymce.Editor} ed
* @return {string}
*/
const _add_visual_styling = function(ed) {
const addVisualStyling = function(ed) {
let content = ed.getContent();

// Do not use a variable whether text is already highlighted, do a check for the existing class
// because this is safe for many tiny element windows at one page.
if (content.indexOf(_span_class) !== -1) {
if (content.indexOf(spanClass) !== -1) {
return content;
}

content = content.replace(new RegExp('{\\s*mlang\\s+([^}]+?)\\s*}', 'ig'), function(match, p1) {
return _span_multilang_begin.replace(new RegExp('%lang', 'g'), p1);
return spanMultilangBegin.replace(new RegExp('%lang', 'g'), p1);
});
content = content.replace(new RegExp('{\\s*mlang\\s*}', 'ig'), _span_multilang_end);
content = content.replace(new RegExp('{\\s*mlang\\s*}', 'ig'), spanMultilangEnd);

return content;
};
Expand All @@ -65,8 +66,8 @@ const _add_visual_styling = function(ed) {
* Also make sure we lowercase the multilang 'tags'
* @param {tinymce.Editor} ed
*/
const _remove_visual_styling = function(ed) {
['begin', 'end'].forEach(function (t) {
const removeVisualStyling = function(ed) {
['begin', 'end'].forEach(function(t) {
let nodes = ed.dom.select('span.multilang-' + t);
for (let n = 0, l = nodes.length; n < l; n++) {
const span = nodes[n];
Expand All @@ -81,8 +82,9 @@ const _remove_visual_styling = function(ed) {
* search param.
* @param {tinymce.Editor} ed
* @param {string} search
* @return {Node|null} The encapsulating span tag if found.
*/
const _get_highlight_node_from_select = function(ed, search) {
const getHighlightNodeFromSelect = function(ed, search) {
let span;
ed.dom.getParents(ed.selection.getStart(), elm => {
// Are we in a span that highlights the lang tag.
Expand Down Expand Up @@ -111,7 +113,7 @@ const _get_highlight_node_from_select = function(ed, search) {
const onInit = function(ed) {
if (isContentToHighlight(ed)) {
ed.dom.addStyle(getHighlightCss(ed));
ed.setContent(_add_visual_styling(ed));
ed.setContent(addVisualStyling(ed));
}
};

Expand All @@ -127,14 +129,14 @@ const onBeforeGetContent = function(ed, content) {
// source code dialog view, make sure we re-add the visual styling.
var onClose = function(ed) {
ed.off('close', onClose);
ed.setContent(_add_visual_styling(ed));
ed.setContent(addVisualStyling(ed));
};
ed.on('CloseWindow', () => {
onClose(ed);
});

if (isContentToHighlight(ed)) {
_remove_visual_styling(ed);
removeVisualStyling(ed);
}
}
};
Expand All @@ -147,7 +149,7 @@ const onBeforeGetContent = function(ed, content) {
const onPreProcess = function(ed, node) {
if (typeof node.save !== 'undefined' && node.save === true) {
if (isContentToHighlight(ed)) {
_remove_visual_styling(ed);
removeVisualStyling(ed);
}
}
};
Expand All @@ -158,13 +160,13 @@ const onPreProcess = function(ed, node) {
* @param {tinymce.Editor} ed
* @param {Object} event
*/
const onDelete = function (ed, event) {
const onDelete = function(ed, event) {
if (event.isComposing || (event.keyCode !== 46 && event.keyCode !== 8) || !isContentToHighlight(ed)) {
return;
}
// Key <del> was pressed, to delete some content. Check if we are inside a span for the lang.
const begin = _get_highlight_node_from_select(ed, 'begin');
const end = _get_highlight_node_from_select(ed, 'end');
const begin = getHighlightNodeFromSelect(ed, 'begin');
const end = getHighlightNodeFromSelect(ed, 'end');
// Only if both, start and end tag are found, then delete the nodes here and prevent the default handling
// because the stuff to be deleted is already gone.
if (!isNull(begin) && !isNull(end)) {
Expand All @@ -190,7 +192,7 @@ const applyLanguage = function(ed, iso) {
if (trim(text) === '') {
let newtext;
if (isContentToHighlight(ed)) {
newtext = _span_multilang_begin.replace(new RegExp('%lang', 'g'), iso) + ' ' + _span_multilang_end;
newtext = spanMultilangBegin.replace(new RegExp('%lang', 'g'), iso) + ' ' + spanMultilangEnd;
} else {
newtext = '{mlang ' + iso + '}' + ' ' + '{mlang}';
}
Expand All @@ -206,14 +208,14 @@ const applyLanguage = function(ed, iso) {
}
// Syntax highlighting is on. Check if we are on a special span that encapsulates the language tags. Search
// for the start span tag.
const span = _get_highlight_node_from_select(ed, 'begin');
const span = getHighlightNodeFromSelect(ed, 'begin');
// If we have a span, then it's the opening tag, and we just replace this one with the new iso.
if (!isNull(span)) {
ed.dom.setOuterHTML(span, _span_multilang_begin.replace(new RegExp('%lang', 'g'), iso));
ed.dom.setOuterHTML(span, spanMultilangBegin.replace(new RegExp('%lang', 'g'), iso));
return;
}
// Not inside a lang tag, insert a new opening and closing tag with the selection inside.
const newtext = _span_multilang_begin.replace(new RegExp('%lang', 'g'), iso) + text + _span_multilang_end;
const newtext = spanMultilangBegin.replace(new RegExp('%lang', 'g'), iso) + text + spanMultilangEnd;
ed.selection.setContent(newtext);
};

Expand Down
2 changes: 2 additions & 0 deletions classes/plugininfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
class plugininfo extends plugin implements plugin_with_menuitems, plugin_with_buttons, plugin_with_configuration {

/**
* Check if user has sufficient rights to use the plugin.
*
* @param context $context
* @param array $options
* @param array $fpoptions
Expand Down
2 changes: 1 addition & 1 deletion db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Plugin for Moodle 'Multilingual content' drop down menu in TinyMCE 6.
*
* @package tinymce_moodlelang2
* @package tiny_multilang2
* @author Iñaki Arenaza <[email protected]>
* @author Stephan Robotta <[email protected]>
* @copyright 2015 onwards Iñaki Arenaza & Mondragon Unibertsitatea
Expand Down
2 changes: 1 addition & 1 deletion settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Multi-language integration settings.
*
* @package tinymce_multilang2
* @package tiny_multilang2
* @author Iñaki Arenaza <[email protected]>
* @author Stephan Robotta <[email protected]>
* @copyright 2015 onwards Iñaki Arenaza & Mondragon Unibertsitatea
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* TinyMCE custom steps definitions for the multilang plugin. It's
* basically all what the TinyMCE steps provide, but with this extensions:
* TinyMCE custom steps definitions for the multilang plugin.
*
* It's basically all what the TinyMCE steps provide, but with this extensions:
* - New step to select the inner text of an element, e.g. the paragraph without
* the paragraph tags.
* - Menu items can have sub items that need to be clicked as well. In this case
Expand All @@ -30,12 +31,17 @@

require_once(__DIR__ . '/../../../../tests/behat/behat_editor_tiny.php');

class behat_editor_tiny_multilang extends behat_editor_tiny {
/**
* Extends general TinyMCE test to test the tiny_multilang2 plugin.
*/
class behat_editor_tiny_multilang2 extends behat_editor_tiny {

/**
* Click on a button for the specified TinyMCE editor.
*
* phpcs:disable
* @When /^I click on the "(?P<menuitem_string>(?:[^"]|\\")*)" submenu item for the "(?P<locator_string>(?:[^"]|\\")*)" TinyMCE editor$/
* phpcs:enable
*
* @param string $menuitem The label of the menu item
* @param string $locator The locator for the editor
Expand Down Expand Up @@ -78,7 +84,10 @@ public function i_click_on_submenuitem_in_menu(string $menuitem, string $locator
* selects the part until the next sibling that would be a formatting node such as bold, italics etc. Also,
* a linebreak (<br/>) intercepts the selection.
*
* phpcs:disable
* @When /^I select the inner "(?P<textlocator_string>(?:[^"]|\\")*)" element in position "(?P<position_int>(?:[^"]|\\")*)" of the "(?P<locator_string>(?:[^"]|\\")*)" TinyMCE editor$/
* phpcs:enable
*
* @param string $textlocator The type of element to select (for example `p` or `span`)
* @param int $position The zero-indexed position
* @param string $locator The editor to select within
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @package tiny_multilang2
* @author Iñaki Arenaza <[email protected]>
* @author Stephan Robotta <[email protected]>
* @copyright 2015 onwards Iñaki Arenaza & Mondragon Unibertsitatea
* @copyright 2015 onwards Iñaki Arenaza & Mondragon Unibertsitatea
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

Expand Down

0 comments on commit 943a46d

Please sign in to comment.