comparison core/modules/ckeditor/js/ckeditor.stylescombo.admin.es6.js @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children a9cd425dd02b
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 /**
2 * @file
3 * CKEditor StylesCombo admin behavior.
4 */
5
6 (function ($, Drupal, drupalSettings, _) {
7 /**
8 * Ensures that the "stylescombo" button's metadata remains up-to-date.
9 *
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.
14 *
15 * @type {Drupal~behavior}
16 *
17 * @prop {Drupal~behaviorAttach} attach
18 * Attaches admin behaviour to the "stylescombo" button.
19 */
20 Drupal.behaviors.ckeditorStylesComboSettings = {
21 attach(context) {
22 const $context = $(context);
23
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
29 // to fire.)
30 const $ckeditorActiveToolbar = $context
31 .find('.ckeditor-toolbar-configuration')
32 .find('.ckeditor-toolbar-active');
33 let previousStylesSet = drupalSettings.ckeditor.hiddenCKEditorConfig.stylesSet;
34 const that = this;
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', [
42 { stylesSet },
43 ]);
44 }
45 });
46 },
47
48 /**
49 * Builds the "stylesSet" configuration part of the CKEditor JS settings.
50 *
51 * @see \Drupal\ckeditor\Plugin\ckeditor\plugin\StylesCombo::generateStylesSetSetting()
52 *
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.
56 *
57 * @param {string} styles
58 * The "styles" setting.
59 *
60 * @return {Array}
61 * An array containing the "stylesSet" configuration.
62 */
63 _generateStylesSetSetting(styles) {
64 const stylesSet = [];
65
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]);
70
71 // Ignore empty lines in between non-empty lines.
72 if (style.length === 0) {
73 continue;
74 }
75
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.
79 continue;
80 }
81
82 // Parse.
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();
88
89 // Build the data structure CKEditor's stylescombo plugin expects.
90 // @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Styles
91 stylesSet.push({
92 attributes: { class: classes.join(' ') },
93 element,
94 name: label,
95 });
96 }
97
98 return stylesSet;
99 },
100 };
101
102 /**
103 * Provides the summary for the "stylescombo" plugin settings vertical tab.
104 *
105 * @type {Drupal~behavior}
106 *
107 * @prop {Drupal~behaviorAttach} attach
108 * Attaches summary behaviour to the plugin settings vertical tab.
109 */
110 Drupal.behaviors.ckeditorStylesComboSettingsSummary = {
111 attach() {
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');
116 }
117
118 const count = $.trim(styles).split('\n').length;
119 return Drupal.t('@count styles configured', { '@count': count });
120 });
121 },
122 };
123 }(jQuery, Drupal, drupalSettings, _));