plugin.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. (function () {
  2. function addCombo(editor, comboName, styleType, lang, entries, defaultLabel, styleDefinition, order) {
  3. var config = editor.config, style = new CKEDITOR.style(styleDefinition);
  4. var names = entries.split(';'), values = [];
  5. var styles = {};
  6. for (var i = 0; i < names.length; i++) {
  7. var parts = names[i];
  8. if (parts) {
  9. parts = parts.split('/');
  10. var vars = {}, name = names[i] = parts[0];
  11. vars[styleType] = values[i] = parts[1] || name;
  12. styles[name] = new CKEDITOR.style(styleDefinition, vars);
  13. styles[name]._.definition.name = name;
  14. } else
  15. names.splice(i--, 1);
  16. }
  17. editor.ui.addRichCombo(comboName, {
  18. label: editor.lang.lineheight.title,
  19. title: editor.lang.lineheight.title,
  20. toolbar: 'styles,' + order,
  21. allowedContent: style,
  22. requiredContent: style,
  23. panel: {
  24. css: [CKEDITOR.skin.getPath('editor')].concat(config.contentsCss),
  25. multiSelect: false,
  26. attributes: {'aria-label': editor.lang.lineheight.title}
  27. },
  28. init: function () {
  29. this.startGroup(editor.lang.lineheight.title);
  30. for (var i = 0; i < names.length; i++) {
  31. var name = names[i];
  32. this.add(name, styles[name].buildPreview(), name);
  33. }
  34. },
  35. onClick: function (value) {
  36. editor.focus();
  37. editor.fire('saveSnapshot');
  38. var style = styles[value];
  39. editor[this.getValue() == value ? 'removeStyle' : 'applyStyle'](style);
  40. editor.fire('saveSnapshot');
  41. },
  42. onRender: function () {
  43. editor.on('selectionChange', function (ev) {
  44. var currentValue = this.getValue();
  45. var elementPath = ev.data.path, elements = elementPath.elements;
  46. for (var i = 0, element; i < elements.length; i++) {
  47. element = elements[i];
  48. for (var value in styles) {
  49. if (styles[value].checkElementMatch(element, true, editor)) {
  50. if (value != currentValue)
  51. this.setValue(value);
  52. return;
  53. }
  54. }
  55. }
  56. this.setValue('', defaultLabel);
  57. }, this);
  58. },
  59. refresh: function () {
  60. if (!editor.activeFilter.check(style))
  61. this.setState(CKEDITOR.TRISTATE_DISABLED);
  62. }
  63. });
  64. }
  65. CKEDITOR.plugins.add('lineheight', {
  66. lang: 'en,zh,zh-cn',
  67. requires: 'richcombo',
  68. init: function (editor) {
  69. var config = editor.config;
  70. addCombo(editor, 'lineheight', 'size', editor.lang.lineheight.title, config.line_height, editor.lang.lineheight.title, config.lineHeight_style, 40);
  71. }
  72. });
  73. })();
  74. CKEDITOR.config.line_height = '1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50;51;52;53;54;55;56;57;58;59;60;61;62;63;64;65;66;67;68;69;70;71;72';
  75. CKEDITOR.config.lineHeight_style = {
  76. element: 'span',
  77. styles: {'line-height': '#(size)'},
  78. overrides: [{
  79. element: 'line-height', attributes: {'size': null}
  80. }]
  81. };