diff --git a/ckeditor5.module b/ckeditor5.module index 13a826f..97d6bd7 100644 --- a/ckeditor5.module +++ b/ckeditor5.module @@ -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; @@ -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'], @@ -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. @@ -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 @@ -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); } @@ -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), ); @@ -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]; } /** @@ -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. *