3 * CKEditor StylesCombo admin behavior.
6 (function ($, Drupal, drupalSettings, _) {
8 * Ensures that the "stylescombo" button's metadata remains up-to-date.
10 * Triggers the CKEditorPluginSettingsChanged event whenever the "stylescombo"
11 * plugin settings change, to ensure that the corresponding feature metadata
12 * is immediately updated — i.e. ensure that HTML tags and classes entered
13 * here are known to be "required", which may affect filter settings.
15 * @type {Drupal~behavior}
17 * @prop {Drupal~behaviorAttach} attach
18 * Attaches admin behaviour to the "stylescombo" button.
20 Drupal.behaviors.ckeditorStylesComboSettings = {
22 const $context = $(context);
24 // React to changes in the list of user-defined styles: calculate the new
25 // stylesSet setting up to 2 times per second, and if it is different,
26 // fire the CKEditorPluginSettingsChanged event with the updated parts of
27 // the CKEditor configuration. (This will, in turn, cause the hidden
28 // CKEditor instance to be updated and a drupalEditorFeatureModified event
30 const $ckeditorActiveToolbar = $context
31 .find('.ckeditor-toolbar-configuration')
32 .find('.ckeditor-toolbar-active');
33 let previousStylesSet = drupalSettings.ckeditor.hiddenCKEditorConfig.stylesSet;
35 $context.find('[name="editor[settings][plugins][stylescombo][styles]"]')
36 .on('blur.ckeditorStylesComboSettings', function () {
37 const styles = $.trim($(this).val());
38 const stylesSet = that._generateStylesSetSetting(styles);
39 if (!_.isEqual(previousStylesSet, stylesSet)) {
40 previousStylesSet = stylesSet;
41 $ckeditorActiveToolbar.trigger('CKEditorPluginSettingsChanged', [
49 * Builds the "stylesSet" configuration part of the CKEditor JS settings.
51 * @see \Drupal\ckeditor\Plugin\ckeditor\plugin\StylesCombo::generateStylesSetSetting()
53 * Note that this is a more forgiving implementation than the PHP version:
54 * the parsing works identically, but instead of failing on invalid styles,
55 * we just ignore those.
57 * @param {string} styles
58 * The "styles" setting.
61 * An array containing the "stylesSet" configuration.
63 _generateStylesSetSetting(styles) {
66 styles = styles.replace(/\r/g, '\n');
67 const lines = styles.split('\n');
68 for (let i = 0; i < lines.length; i++) {
69 const style = $.trim(lines[i]);
71 // Ignore empty lines in between non-empty lines.
72 if (style.length === 0) {
76 // Validate syntax: element[.class...]|label pattern expected.
77 if (style.match(/^ *[a-zA-Z0-9]+ *(\.[a-zA-Z0-9_-]+ *)*\| *.+ *$/) === null) {
78 // Instead of failing, we just ignore any invalid styles.
83 const parts = style.split('|');
84 const selector = parts[0];
85 const label = parts[1];
86 const classes = selector.split('.');
87 const element = classes.shift();
89 // Build the data structure CKEditor's stylescombo plugin expects.
90 // @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Styles
92 attributes: { class: classes.join(' ') },
103 * Provides the summary for the "stylescombo" plugin settings vertical tab.
105 * @type {Drupal~behavior}
107 * @prop {Drupal~behaviorAttach} attach
108 * Attaches summary behaviour to the plugin settings vertical tab.
110 Drupal.behaviors.ckeditorStylesComboSettingsSummary = {
112 $('[data-ckeditor-plugin-id="stylescombo"]').drupalSetSummary((context) => {
113 const styles = $.trim($('[data-drupal-selector="edit-editor-settings-plugins-stylescombo-styles"]').val());
114 if (styles.length === 0) {
115 return Drupal.t('No styles configured');
118 const count = $.trim(styles).split('\n').length;
119 return Drupal.t('@count styles configured', { '@count': count });
123 }(jQuery, Drupal, drupalSettings, _));