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