ckeditor.stylescombo.admin.es6.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * @file
  3. * CKEditor StylesCombo admin behavior.
  4. */
  5. (function($, Drupal, drupalSettings, _) {
  6. /**
  7. * Ensures that the "stylescombo" button's metadata remains up-to-date.
  8. *
  9. * Triggers the CKEditorPluginSettingsChanged event whenever the "stylescombo"
  10. * plugin settings change, to ensure that the corresponding feature metadata
  11. * is immediately updated — i.e. ensure that HTML tags and classes entered
  12. * here are known to be "required", which may affect filter settings.
  13. *
  14. * @type {Drupal~behavior}
  15. *
  16. * @prop {Drupal~behaviorAttach} attach
  17. * Attaches admin behavior to the "stylescombo" button.
  18. */
  19. Drupal.behaviors.ckeditorStylesComboSettings = {
  20. attach(context) {
  21. const $context = $(context);
  22. // React to changes in the list of user-defined styles: calculate the new
  23. // stylesSet setting up to 2 times per second, and if it is different,
  24. // fire the CKEditorPluginSettingsChanged event with the updated parts of
  25. // the CKEditor configuration. (This will, in turn, cause the hidden
  26. // CKEditor instance to be updated and a drupalEditorFeatureModified event
  27. // to fire.)
  28. const $ckeditorActiveToolbar = $context
  29. .find('.ckeditor-toolbar-configuration')
  30. .find('.ckeditor-toolbar-active');
  31. let previousStylesSet =
  32. drupalSettings.ckeditor.hiddenCKEditorConfig.stylesSet;
  33. const that = this;
  34. $context
  35. .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. * Builds the "stylesSet" configuration part of the CKEditor JS settings.
  49. *
  50. * @see \Drupal\ckeditor\Plugin\ckeditor\plugin\StylesCombo::generateStylesSetSetting()
  51. *
  52. * Note that this is a more forgiving implementation than the PHP version:
  53. * the parsing works identically, but instead of failing on invalid styles,
  54. * we just ignore those.
  55. *
  56. * @param {string} styles
  57. * The "styles" setting.
  58. *
  59. * @return {Array}
  60. * An array containing the "stylesSet" configuration.
  61. */
  62. _generateStylesSetSetting(styles) {
  63. const stylesSet = [];
  64. styles = styles.replace(/\r/g, '\n');
  65. const lines = styles.split('\n');
  66. for (let i = 0; i < lines.length; i++) {
  67. const style = $.trim(lines[i]);
  68. // Ignore empty lines in between non-empty lines.
  69. if (style.length === 0) {
  70. continue;
  71. }
  72. // Validate syntax: element[.class...]|label pattern expected.
  73. if (
  74. style.match(/^ *[a-zA-Z0-9]+ *(\.[a-zA-Z0-9_-]+ *)*\| *.+ *$/) ===
  75. null
  76. ) {
  77. // Instead of failing, we just ignore any invalid styles.
  78. continue;
  79. }
  80. // Parse.
  81. const parts = style.split('|');
  82. const selector = parts[0];
  83. const label = parts[1];
  84. const classes = selector.split('.');
  85. const element = classes.shift();
  86. // Build the data structure CKEditor's stylescombo plugin expects.
  87. // @see https://ckeditor.com/docs/ckeditor4/latest/guide/dev_howtos_styles.html
  88. stylesSet.push({
  89. attributes: { class: classes.join(' ') },
  90. element,
  91. name: label,
  92. });
  93. }
  94. return stylesSet;
  95. },
  96. };
  97. /**
  98. * Provides the summary for the "stylescombo" plugin settings vertical tab.
  99. *
  100. * @type {Drupal~behavior}
  101. *
  102. * @prop {Drupal~behaviorAttach} attach
  103. * Attaches summary behavior to the plugin settings vertical tab.
  104. */
  105. Drupal.behaviors.ckeditorStylesComboSettingsSummary = {
  106. attach() {
  107. $('[data-ckeditor-plugin-id="stylescombo"]').drupalSetSummary(context => {
  108. const styles = $.trim(
  109. $(
  110. '[data-drupal-selector="edit-editor-settings-plugins-stylescombo-styles"]',
  111. ).val(),
  112. );
  113. if (styles.length === 0) {
  114. return Drupal.t('No styles configured');
  115. }
  116. const count = $.trim(styles).split('\n').length;
  117. return Drupal.t('@count styles configured', { '@count': count });
  118. });
  119. },
  120. };
  121. })(jQuery, Drupal, drupalSettings, _);