Skip to content

Commit

Permalink
Issue #5: Improve attaching plugin libraries.
Browse files Browse the repository at this point in the history
  • Loading branch information
quicksketch committed Oct 31, 2023
1 parent f1da7fa commit 0e07dbf
Showing 1 changed file with 70 additions and 12 deletions.
82 changes: 70 additions & 12 deletions ckeditor5.module
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function ckeditor5_editor_info() {
),
'file' => 'ckeditor5.admin.inc',
'settings callback' => 'ckeditor5_settings_form',
'js settings callback' => 'ckeditor5_get_config',
'js settings callback' => '_ckeditor5_js_settings',
);

return $editors;
Expand Down Expand Up @@ -85,15 +85,27 @@ function ckeditor5_library_info() {
array('filter', 'filter'),
array('system', 'backdrop.ajax'),
array('ckeditor5', 'ckeditor5'),
// CKEditor 5 does not provide its own plugin loader, so any added
// potentially needed plugins are added as dependencies.
// @todo: Improve loading to use libraries only when needed.
array('ckeditor5', 'backdrop.ckeditor5.backdrop-basic-styles'),
array('ckeditor5', 'backdrop.ckeditor5.backdrop-image'),
array('ckeditor5', 'backdrop.ckeditor5.backdrop-link'),
),
);

// CKEditor 5 does not have a built-in script loader. Any needed dependencies
// should be present on the page when CKEditor loads. Loop through all text
// formats to determine the combined set of dependencies for all formats.
$text_formats = filter_formats();
$plugin_libraries = array();
foreach ($text_formats as $text_format) {
if ($text_format->editor === 'ckeditor5') {
$ckeditor5_text_format_libraries = _ckeditor5_get_libraries($text_format);
foreach ($ckeditor5_text_format_libraries as $library) {
$plugin_libraries[$library[0] . '--' . $library[1]] = $library;
}
}
}
$libraries['backdrop.ckeditor5']['dependencies'] = array_merge(
$libraries['backdrop.ckeditor5']['dependencies'],
array_values($plugin_libraries)
);

$libraries['backdrop.ckeditor5.backdrop-basic-styles'] = array(
'title' => 'CKEditor plugin to convert basic HTML tags to preferred tags.',
'version' => $info['version'],
Expand Down Expand Up @@ -794,7 +806,7 @@ function _ckeditor5_link_image_plugin_check($format, $plugin_name) {
}

/**
* Editor JS settings callback; Add CKEditor config to the page for a format.
* Get the full CKEditor 5 configuration for a given text format.
*
* Note that this function uses the term "config" to match that of CKEditor's
* own terminology. It is not related to Backdrop's configuration system.
Expand All @@ -803,10 +815,15 @@ function _ckeditor5_link_image_plugin_check($format, $plugin_name) {
*
* @param object $format
* The filter format object for which CKEditor is adding its config.
* @param array $existing_settings
* Settings that have already been added to the page by filters.
*/
function ckeditor5_get_config($format, array $existing_settings) {
function ckeditor5_get_config($format) {
// Static cache the configuration per format.
$ckeditor5_configs = &backdrop_static(__FUNCTION__, array());
$format_id = $format->name;
if (isset($ckeditor5_configs[$format_id])) {
return $ckeditor5_configs[$format_id];
}

global $language;

// Loop through all available plugins and check to see if it has been
Expand Down Expand Up @@ -858,6 +875,9 @@ function ckeditor5_get_config($format, array $existing_settings) {
if (isset($all_buttons[$button_name]['plugin']['name'])) {
$plugin_name = $all_buttons[$button_name]['plugin']['name'];
$plugins[] = $plugin_name;
if (isset($plugin_info[$plugin_name]['library'])) {
$libraries[] = $plugin_info[$plugin_name]['library'];
}
if (isset($plugin_info[$plugin_name]['plugin_dependencies'])) {
$plugins = array_merge($plugin_info[$plugin_name]['plugin_dependencies'], $plugins);
}
Expand Down Expand Up @@ -903,6 +923,10 @@ function ckeditor5_get_config($format, array $existing_settings) {
// file. Exclude the pseudo-plugins that are configuration-only.
'pluginList' => array_values(array_diff($plugins, $pseudo_plugins)),

// The library list also not a CKEditor configuration value. It is used by
// Backdrop to attach needed libraries for CKEditor.
'libraries' => $libraries,

// 'contentsCss' => array_values($css),
);

Expand Down Expand Up @@ -1004,7 +1028,9 @@ function ckeditor5_get_config($format, array $existing_settings) {

backdrop_alter('ckeditor5_config', $config, $format);

return $config;
// Save the configuration into the static variable.
$ckeditor5_configs[$format_id] = $config;
return $ckeditor5_configs[$format_id];
}

/**
Expand Down Expand Up @@ -1203,6 +1229,38 @@ function ckeditor5_get_generic_html_settings($format) {
return array($allowed, $disallowed);
}

/**
* Editor JS settings callback; Add CKEditor config to the page for a format.
*
* @param object $format
* The filter format object for which CKEditor is adding its config.
* @param array $existing_settings
* Settings that have already been added to the page by filters.
*
* @return void
*/
function _ckeditor5_js_settings($format, $existing_settings) {
$config = ckeditor5_get_config($format);
// Remove the Backdrop-specific libraries key.
unset($config['libraries']);
return $config;
}

/**
* Retrieve a list of CKEditor-related libraries used by a text format.
*
* @param object $format
* The filter format object for which CKEditor is adding its config.
*
* @return array
* An array of library keys.
*/
function _ckeditor5_get_libraries($format) {
$config = ckeditor5_get_config($format);
// Return the Backdrop-specific libraries key.;
return isset($config['libraries']) ? $config['libraries'] : array();
}

/**
* Retrieves the default theme's CKEditor stylesheets defined in the .info file.
*
Expand Down

0 comments on commit 0e07dbf

Please sign in to comment.