Newer
Older
Jennifer Hodgdon
committed
/**
* @file
Angie Byron
committed
* CKEditor StylesCombo admin behavior.
Jennifer Hodgdon
committed
*/
Alex Pott
committed
(function ($, Drupal, drupalSettings) {
Dries Buytaert
committed
'use strict';
Alex Pott
committed
/**
* Ensures that the "stylescombo" button's metadata remains up-to-date.
Alex Pott
committed
*
* Triggers the CKEditorPluginSettingsChanged event whenever the "stylescombo"
Angie Byron
committed
* plugin settings change, to ensure that the corresponding feature metadata
* is immediately updated — i.e. ensure that HTML tags and classes entered
* here are known to be "required", which may affect filter settings.
Jennifer Hodgdon
committed
*
* @type {Drupal~behavior}
Angie Byron
committed
*
* @prop {Drupal~behaviorAttach} attach
* Attaches admin behaviour to the "stylescombo" button.
Alex Pott
committed
*/
Drupal.behaviors.ckeditorStylesComboSettings = {
attach: function (context) {
var $context = $(context);
Alex Pott
committed
// React to changes in the list of user-defined styles: calculate the new
Angie Byron
committed
// stylesSet setting up to 2 times per second, and if it is different,
// fire the CKEditorPluginSettingsChanged event with the updated parts of
// the CKEditor configuration. (This will, in turn, cause the hidden
// CKEditor instance to be updated and a drupalEditorFeatureModified event
// to fire.)
var $ckeditorActiveToolbar = $context
.find('.ckeditor-toolbar-configuration')
.find('.ckeditor-toolbar-active');
var previousStylesSet = drupalSettings.ckeditor.hiddenCKEditorConfig.stylesSet;
var that = this;
$context.find('[name="editor[settings][plugins][stylescombo][styles]"]')
.on('blur.ckeditorStylesComboSettings', function () {
var styles = $.trim($(this).val());
var stylesSet = that._generateStylesSetSetting(styles);
if (!_.isEqual(previousStylesSet, stylesSet)) {
previousStylesSet = stylesSet;
$ckeditorActiveToolbar.trigger('CKEditorPluginSettingsChanged', [
}
});
},
Alex Pott
committed
/**
* Builds the "stylesSet" configuration part of the CKEditor JS settings.
*
* @see \Drupal\ckeditor\Plugin\ckeditor\plugin\StylesCombo::generateStylesSetSetting()
*
Angie Byron
committed
* Note that this is a more forgiving implementation than the PHP version:
* the parsing works identically, but instead of failing on invalid styles,
* we just ignore those.
*
Jennifer Hodgdon
committed
* @param {string} styles
* The "styles" setting.
*
Jennifer Hodgdon
committed
* @return {Array}
* An array containing the "stylesSet" configuration.
*/
_generateStylesSetSetting: function (styles) {
var stylesSet = [];
Alex Pott
committed
styles = styles.replace(/\r/g, '\n');
var lines = styles.split('\n');
for (var i = 0; i < lines.length; i++) {
var style = $.trim(lines[i]);
Alex Pott
committed
// Ignore empty lines in between non-empty lines.
if (style.length === 0) {
continue;
}
Alex Pott
committed
// Validate syntax: element[.class...]|label pattern expected.
if (style.match(/^ *[a-zA-Z0-9]+ *(\.[a-zA-Z0-9_-]+ *)*\| *.+ *$/) === null) {
// Instead of failing, we just ignore any invalid styles.
continue;
}
Dries Buytaert
committed
// Parse.
var parts = style.split('|');
var selector = parts[0];
var label = parts[1];
var classes = selector.split('.');
var element = classes.shift();
// Build the data structure CKEditor's stylescombo plugin expects.
// @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Styles
stylesSet.push({
attributes: {class: classes.join(' ')},
element: element,
name: label
});
Dries Buytaert
committed
}
return stylesSet;
}
};
/**
* Provides the summary for the "stylescombo" plugin settings vertical tab.
Jennifer Hodgdon
committed
*
* @type {Drupal~behavior}
Angie Byron
committed
*
* @prop {Drupal~behaviorAttach} attach
* Attaches summary behaviour to the plugin settings vertical tab.
*/
Drupal.behaviors.ckeditorStylesComboSettingsSummary = {
attach: function () {
$('[data-ckeditor-plugin-id="stylescombo"]').drupalSetSummary(function (context) {
var styles = $.trim($('[data-drupal-selector="edit-editor-settings-plugins-stylescombo-styles"]').val());
if (styles.length === 0) {
return Drupal.t('No styles configured');
}
else {
var count = $.trim(styles).split('\n').length;
return Drupal.t('@count styles configured', {'@count': count});
}
});
}
};
Dries Buytaert
committed
Alex Pott
committed
})(jQuery, Drupal, drupalSettings);