Skip to content

Commit

Permalink
Fix CSS stylesheet integration during issue editing
Browse files Browse the repository at this point in the history
  • Loading branch information
dasistwas committed Apr 25, 2024
1 parent 7cd1182 commit b7a3fec
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 59 deletions.
2 changes: 1 addition & 1 deletion amd/build/editor.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/editor.min.js.map

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

19 changes: 9 additions & 10 deletions amd/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

// import {setupForElementId} from 'editor_tiny/editor';

export const loadCss = async function() {
/**
* This is obsolete. To be deleted in the future. Maybe if Moodle does a better integration of Tiny 6, it could be used again.
*
* @param editorconfig
* @returns {Promise<void>}
*/
export const loadCss = async function(editorconfig) {
var select = document.querySelector('#id_stylesheetid');
if (select) {
select.addEventListener('change', change_stylesheet);
Expand All @@ -39,7 +43,6 @@ export const loadCss = async function() {
setTimeout(checkIfLoaded, 100); // Check every 100 milliseconds
}
}

checkIfLoaded();
});
}
Expand All @@ -50,19 +53,15 @@ export const loadCss = async function() {
waitUntilTinyMCELoaded()
.then((tinyMCE) => {
console.log('tinyMCE is loaded:', tinyMCE);
const existingEditor = tinyMCE.EditorManager.get('id_htmlcontent');
const existingEditor = tinyMCE.add('id_htmlcontent');
if(existingEditor) {
console.log(existingEditor);
}
// Call function to change CSS for TinyMCE content
tinyMCE.init(editorconfig);
change_stylesheet();
});

// var Tiny = setupForElementId({
// elementId: "id_htmlcontent",
// options: {},
// });

/**
* Changes the stylesheet based on the selected option.
*/
Expand Down
46 changes: 3 additions & 43 deletions classes/newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use context_module;
use core\event\user_created;
use core_user;
use editor_tiny\editor;
use editor_tiny\manager;
use mod_newsletter_instance_store;
use newsletter_section_list;
Expand Down Expand Up @@ -831,49 +832,8 @@ private function view_edit_issue_page(array $params) {
'filename',
false
);
$cssurls = [];
$cssurls[NEWSLETTER_DEFAULT_STYLESHEET] = "{$CFG->wwwroot}/mod/newsletter/reset.css";
if (!empty($files)) {
foreach ($files as $file) {
$url = "{$CFG->wwwroot}/pluginfile.php/{$file->get_contextid()}/mod_newsletter/" . NEWSLETTER_FILE_AREA_STYLESHEET;
$cssurls[$file->get_id()] = $url . $file->get_filepath() . $file->get_itemid() . '/' . $file->get_filename();
}
}
$editormanager = new manager();
$siteconfig = get_config('editor_tiny');
$editorconfig = json_encode([
'context' => $context->id,

// File picker options.
'filepicker' => [],

'currentLanguage' => current_language(),

'branding' => property_exists($siteconfig, 'branding') ? !empty($siteconfig->branding) : true,

// Language options.
'language' => [
'currentlang' => current_language(),
'installed' => get_string_manager()->get_list_of_translations(true),
'available' => get_string_manager()->get_list_of_languages()
],

// Placeholder selectors.
// Some contents (Example: placeholder elements) are only shown in the editor, and not to users. It is unrelated to the
// real display. We created a list of placeholder selectors, so we can decide to or not to apply rules, styles... to
// these elements.
// The default of this list will be empty.
// Other plugins can register their placeholder elements to placeholderSelectors list by calling
// editor_tiny/options::registerPlaceholderSelectors.
'placeholderSelectors' => [],

// Nest menu inside parent DOM.
'nestedmenu' => true,
]);
$plugins = json_encode($editormanager->get_plugin_configuration($context));
$PAGE->requires->js_call_amd('mod_newsletter/editor', 'loadCss',
[$cssurls, $issue->stylesheetid, $editorconfig, $plugins]);

$editor = new newsletter_editor();
$editor->use_editor('id_htmlcontent', ['context' => $this->context], null, $issue, $files);
$mform = new issue_form(
null,
array('newsletter' => $this, 'issue' => $issue, 'context' => $context)
Expand Down
131 changes: 131 additions & 0 deletions classes/newsletter_editor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace mod_newsletter;
use editor_tiny\editor;

/**
* Add custom css to the default editor. This is quite a hack due to the hacky implementation of Tiny 6 editor in Moodle.
*/
class newsletter_editor extends editor {
/**
* Use this editor for given element.
*
* @param string $elementid
* @param array $options
* @param null $fpoptions
* @param null $issue
*/
public function use_editor($elementid, array $options = null, $fpoptions = null, $issue = null, array $files = []) {
global $PAGE, $CFG;
// Ensure that the default configuration is set.
self::reset_default_configuration();
self::set_default_configuration($this->manager);

if ($fpoptions === null) {
$fpoptions = [];
}

$context = $PAGE->context;

if (isset($options['context']) && ($options['context'] instanceof \context)) {
// A different context was provided.
// Use that instead.
$context = $options['context'];
}

$cssurls = [];
$cssurls[NEWSLETTER_DEFAULT_STYLESHEET] = "{$CFG->wwwroot}/mod/newsletter/reset.css";
if (!empty($files)) {
foreach ($files as $file) {
$url = "{$CFG->wwwroot}/pluginfile.php/{$file->get_contextid()}/mod_newsletter/" . NEWSLETTER_FILE_AREA_STYLESHEET;
$cssurls[$file->get_id()] = $url . $file->get_filepath() . $file->get_itemid() . '/' . $file->get_filename();
}
}
// Generate the configuration for this editor.
$siteconfig = get_config('editor_tiny');
$config = (object) [
// The URL to the CSS file for the editor.
'css' => $cssurls[$issue->stylesheetid],

// The current context for this page or editor.
'context' => $context->id,

// File picker options.
'filepicker' => $fpoptions,

'currentLanguage' => current_language(),

'branding' => property_exists($siteconfig, 'branding') ? !empty($siteconfig->branding) : true,

// Language options.
'language' => [
'currentlang' => current_language(),
'installed' => get_string_manager()->get_list_of_translations(true),
'available' => get_string_manager()->get_list_of_languages()
],

// Placeholder selectors.
// Some contents (Example: placeholder elements) are only shown in the editor, and not to users. It is unrelated to the
// real display. We created a list of placeholder selectors, so we can decide to or not to apply rules, styles... to
// these elements.
// The default of this list will be empty.
// Other plugins can register their placeholder elements to placeholderSelectors list by calling
// editor_tiny/options::registerPlaceholderSelectors.
'placeholderSelectors' => [],

// Plugin configuration.
'plugins' => $this->manager->get_plugin_configuration($context, $options, $fpoptions, $this),

// Nest menu inside parent DOM.
'nestedmenu' => true,
];

if (defined('BEHAT_SITE_RUNNING') && BEHAT_SITE_RUNNING) {
// Add sample selectors for Behat test.
$config->placeholderSelectors = ['.behat-tinymce-placeholder'];
}

foreach ($fpoptions as $fp) {
// Guess the draftitemid for the editor.
// Note: This is the best we can do at the moment.
if (!empty($fp->itemid)) {
$config->draftitemid = $fp->itemid;
break;
}
}

$configoptions = json_encode(convert_to_array($config));

// Note: This is not ideal but the editor does not have control over any HTML output.
// The Editor API only allows you to run JavaScript.
// In the future we will extend the editor API to allow it to generate the textarea, or attributes to use in the
// textarea or its wrapper.
// For now we cannot use the `js_call_amd()` API call because it warns if the parameters passed exceed a
// relatively low character limit.
$inlinejs = <<<EOF
M.util.js_pending('editor_tiny/editor');
require(['editor_tiny/editor'], (Tiny) => {
Tiny.setupForElementId({
elementId: "${elementid}",
options: ${configoptions},
});
M.util.js_complete('editor_tiny/editor');
});
EOF;
$PAGE->requires->js_amd_inline($inlinejs);
}
}
Binary file removed classes/task/._send_newsletter.php
Binary file not shown.
Binary file removed lang/de/._newsletter.php
Binary file not shown.
2 changes: 1 addition & 1 deletion lang/de/newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
$string['health_2'] = 'Blacklisted';
$string['health_4'] = 'Abgemeldet';
$string['issue_htmlcontent'] = 'HTML Inhalt';
$string['issue_stylesheet'] = 'Stylesheet-Datei für HTML Inhalt';
$string['issue_stylesheet'] = 'Stylesheet-Datei für HTML Inhalt. ';
$string['issue_title'] = 'Ausgabentitel';
$string['issue_title_help'] = 'Geben Sie hier den Titel der Ausgabe ein (erforderlich).';
$string['manage_subscriptions'] = 'Abonnements verwalten';
Expand Down
Binary file removed lang/en/._newsletter.php
Binary file not shown.
2 changes: 1 addition & 1 deletion lang/en/newsletter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
$string['issue_title_help'] = 'Type in the title of this issue. Required.';

$string['issue_htmlcontent'] = 'HTML content';
$string['issue_stylesheet'] = 'Stylesheet file for HTML content';
$string['issue_stylesheet'] = 'Stylesheet-Datei für HTML-Inhalte. Um korrekt gestyltes HTML beim Bearbeiten zu sehen, müssen Sie die Ausgabe zuerst speichern und dann erneut bearbeiten. Eine Änderung des Stylesheets während des Editierens ist derzeit nicht möglich.';

$string['mode_group_by_year'] = 'Group issues by year';
$string['mode_group_by_month'] = 'Group issues by month';
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024032900;
$plugin->version = 2024042500;
$plugin->requires = 2022041900;
$plugin->maturity = MATURITY_STABLE;
$plugin->release = 'v2.4.0-UkrainskaPravda'; // Already used names: Der Standard, Le Monde Diplomatique, NewYorkTimes.
$plugin->release = 'v2.4.1-UkrainskaPravda'; // Already used names: Der Standard, Le Monde Diplomatique, NewYorkTimes.
$plugin->component = 'mod_newsletter';

0 comments on commit b7a3fec

Please sign in to comment.