plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. ( function() {
  6. function addCombo( editor, comboName, styleType, lang, entries, defaultLabel, styleDefinition, order ) {
  7. var config = editor.config,
  8. style = new CKEDITOR.style( styleDefinition );
  9. // Gets the list of fonts from the settings.
  10. var names = entries.split( ';' ),
  11. values = [];
  12. // Create style objects for all fonts.
  13. var styles = {};
  14. for ( var i = 0; i < names.length; i++ ) {
  15. var parts = names[ i ];
  16. if ( parts ) {
  17. parts = parts.split( '/' );
  18. var vars = {},
  19. name = names[ i ] = parts[ 0 ];
  20. vars[ styleType ] = values[ i ] = parts[ 1 ] || name;
  21. styles[ name ] = new CKEDITOR.style( styleDefinition, vars );
  22. styles[ name ]._.definition.name = name;
  23. } else {
  24. names.splice( i--, 1 );
  25. }
  26. }
  27. editor.ui.addRichCombo( comboName, {
  28. label: lang.label,
  29. title: lang.panelTitle,
  30. toolbar: 'styles,' + order,
  31. allowedContent: style,
  32. requiredContent: style,
  33. panel: {
  34. css: [ CKEDITOR.skin.getPath( 'editor' ) ].concat( config.contentsCss ),
  35. multiSelect: false,
  36. attributes: { 'aria-label': lang.panelTitle }
  37. },
  38. init: function() {
  39. this.startGroup( lang.panelTitle );
  40. for ( var i = 0; i < names.length; i++ ) {
  41. var name = names[ i ];
  42. // Add the tag entry to the panel list.
  43. this.add( name, styles[ name ].buildPreview(), name );
  44. }
  45. },
  46. onClick: function( value ) {
  47. editor.focus();
  48. editor.fire( 'saveSnapshot' );
  49. var previousValue = this.getValue(),
  50. style = styles[ value ];
  51. // When applying one style over another, first remove the previous one (#12403).
  52. // NOTE: This is only a temporary fix. It will be moved to the styles system (#12687).
  53. if ( previousValue && value != previousValue ) {
  54. var previousStyle = styles[ previousValue ],
  55. range = editor.getSelection().getRanges()[ 0 ];
  56. // If the range is collapsed we can't simply use the editor.removeStyle method
  57. // because it will remove the entire element and we want to split it instead.
  58. if ( range.collapsed ) {
  59. var path = editor.elementPath(),
  60. // Find the style element.
  61. matching = path.contains( function( el ) {
  62. return previousStyle.checkElementRemovable( el );
  63. } );
  64. if ( matching ) {
  65. var startBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.START ),
  66. endBoundary = range.checkBoundaryOfElement( matching, CKEDITOR.END ),
  67. node, bm;
  68. // If we are at both boundaries it means that the element is empty.
  69. // Remove it but in a way that we won't lose other empty inline elements inside it.
  70. // Example: <p>x<span style="font-size:48px"><em>[]</em></span>x</p>
  71. // Result: <p>x<em>[]</em>x</p>
  72. if ( startBoundary && endBoundary ) {
  73. bm = range.createBookmark();
  74. // Replace the element with its children (TODO element.replaceWithChildren).
  75. while ( ( node = matching.getFirst() ) ) {
  76. node.insertBefore( matching );
  77. }
  78. matching.remove();
  79. range.moveToBookmark( bm );
  80. // If we are at the boundary of the style element, just move out.
  81. } else if ( startBoundary ) {
  82. range.moveToPosition( matching, CKEDITOR.POSITION_BEFORE_START );
  83. } else if ( endBoundary ) {
  84. range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );
  85. } else {
  86. // Split the element and clone the elements that were in the path
  87. // (between the startContainer and the matching element)
  88. // into the new place.
  89. range.splitElement( matching );
  90. range.moveToPosition( matching, CKEDITOR.POSITION_AFTER_END );
  91. cloneSubtreeIntoRange( range, path.elements.slice(), matching );
  92. }
  93. editor.getSelection().selectRanges( [ range ] );
  94. }
  95. } else {
  96. editor.removeStyle( previousStyle );
  97. }
  98. }
  99. editor[ previousValue == value ? 'removeStyle' : 'applyStyle' ]( style );
  100. editor.fire( 'saveSnapshot' );
  101. },
  102. onRender: function() {
  103. editor.on( 'selectionChange', function( ev ) {
  104. var currentValue = this.getValue();
  105. var elementPath = ev.data.path,
  106. elements = elementPath.elements;
  107. // For each element into the elements path.
  108. for ( var i = 0, element; i < elements.length; i++ ) {
  109. element = elements[ i ];
  110. // Check if the element is removable by any of
  111. // the styles.
  112. for ( var value in styles ) {
  113. if ( styles[ value ].checkElementMatch( element, true, editor ) ) {
  114. if ( value != currentValue )
  115. this.setValue( value );
  116. return;
  117. }
  118. }
  119. }
  120. // If no styles match, just empty it.
  121. this.setValue( '', defaultLabel );
  122. }, this );
  123. },
  124. refresh: function() {
  125. if ( !editor.activeFilter.check( style ) )
  126. this.setState( CKEDITOR.TRISTATE_DISABLED );
  127. }
  128. } );
  129. }
  130. // Clones the subtree between subtreeStart (exclusive) and the
  131. // leaf (inclusive) and inserts it into the range.
  132. //
  133. // @param range
  134. // @param {CKEDITOR.dom.element[]} elements Elements path in the standard order: leaf -> root.
  135. // @param {CKEDITOR.dom.element/null} substreeStart The start of the subtree.
  136. // If null, then the leaf belongs to the subtree.
  137. function cloneSubtreeIntoRange( range, elements, subtreeStart ) {
  138. var current = elements.pop();
  139. if ( !current ) {
  140. return;
  141. }
  142. // Rewind the elements array up to the subtreeStart and then start the real cloning.
  143. if ( subtreeStart ) {
  144. return cloneSubtreeIntoRange( range, elements, current.equals( subtreeStart ) ? null : subtreeStart );
  145. }
  146. var clone = current.clone();
  147. range.insertNode( clone );
  148. range.moveToPosition( clone, CKEDITOR.POSITION_AFTER_START );
  149. cloneSubtreeIntoRange( range, elements );
  150. }
  151. CKEDITOR.plugins.add( 'font', {
  152. requires: 'richcombo',
  153. // jscs:disable maximumLineLength
  154. lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  155. // jscs:enable maximumLineLength
  156. init: function( editor ) {
  157. var config = editor.config;
  158. addCombo( editor, 'Font', 'family', editor.lang.font, config.font_names, config.font_defaultLabel, config.font_style, 30 );
  159. addCombo( editor, 'FontSize', 'size', editor.lang.font.fontSize, config.fontSize_sizes, config.fontSize_defaultLabel, config.fontSize_style, 40 );
  160. }
  161. } );
  162. } )();
  163. /**
  164. * The list of fonts names to be displayed in the Font combo in the toolbar.
  165. * Entries are separated by semi-colons (`';'`), while it's possible to have more
  166. * than one font for each entry, in the HTML way (separated by comma).
  167. *
  168. * A display name may be optionally defined by prefixing the entries with the
  169. * name and the slash character. For example, `'Arial/Arial, Helvetica, sans-serif'`
  170. * will be displayed as `'Arial'` in the list, but will be outputted as
  171. * `'Arial, Helvetica, sans-serif'`.
  172. *
  173. * config.font_names =
  174. * 'Arial/Arial, Helvetica, sans-serif;' +
  175. * 'Times New Roman/Times New Roman, Times, serif;' +
  176. * 'Verdana';
  177. *
  178. * config.font_names = 'Arial;Times New Roman;Verdana';
  179. *
  180. * @cfg {String} [font_names=see source]
  181. * @member CKEDITOR.config
  182. */
  183. CKEDITOR.config.font_names = 'Arial/Arial, Helvetica, sans-serif;' +
  184. 'Comic Sans MS/Comic Sans MS, cursive;' +
  185. 'Courier New/Courier New, Courier, monospace;' +
  186. 'Georgia/Georgia, serif;' +
  187. 'Lucida Sans Unicode/Lucida Sans Unicode, Lucida Grande, sans-serif;' +
  188. 'Tahoma/Tahoma, Geneva, sans-serif;' +
  189. 'Times New Roman/Times New Roman, Times, serif;' +
  190. 'Trebuchet MS/Trebuchet MS, Helvetica, sans-serif;' +
  191. 'Verdana/Verdana, Geneva, sans-serif';
  192. /**
  193. * The text to be displayed in the Font combo is none of the available values
  194. * matches the current cursor position or text selection.
  195. *
  196. * // If the default site font is Arial, we may making it more explicit to the end user.
  197. * config.font_defaultLabel = 'Arial';
  198. *
  199. * @cfg {String} [font_defaultLabel='']
  200. * @member CKEDITOR.config
  201. */
  202. CKEDITOR.config.font_defaultLabel = '';
  203. /**
  204. * The style definition to be used to apply the font in the text.
  205. *
  206. * // This is actually the default value for it.
  207. * config.font_style = {
  208. * element: 'span',
  209. * styles: { 'font-family': '#(family)' },
  210. * overrides: [ { element: 'font', attributes: { 'face': null } } ]
  211. * };
  212. *
  213. * @cfg {Object} [font_style=see example]
  214. * @member CKEDITOR.config
  215. */
  216. CKEDITOR.config.font_style = {
  217. element: 'span',
  218. styles: { 'font-family': '#(family)' },
  219. overrides: [ {
  220. element: 'font', attributes: { 'face': null }
  221. } ]
  222. };
  223. /**
  224. * The list of fonts size to be displayed in the Font Size combo in the
  225. * toolbar. Entries are separated by semi-colons (`';'`).
  226. *
  227. * Any kind of "CSS like" size can be used, like `'12px'`, `'2.3em'`, `'130%'`,
  228. * `'larger'` or `'x-small'`.
  229. *
  230. * A display name may be optionally defined by prefixing the entries with the
  231. * name and the slash character. For example, `'Bigger Font/14px'` will be
  232. * displayed as `'Bigger Font'` in the list, but will be outputted as `'14px'`.
  233. *
  234. * config.fontSize_sizes = '16/16px;24/24px;48/48px;';
  235. *
  236. * config.fontSize_sizes = '12px;2.3em;130%;larger;x-small';
  237. *
  238. * config.fontSize_sizes = '12 Pixels/12px;Big/2.3em;30 Percent More/130%;Bigger/larger;Very Small/x-small';
  239. *
  240. * @cfg {String} [fontSize_sizes=see source]
  241. * @member CKEDITOR.config
  242. */
  243. CKEDITOR.config.fontSize_sizes = '8/8px;9/9px;10/10px;11/11px;12/12px;14/14px;16/16px;18/18px;20/20px;22/22px;24/24px;26/26px;28/28px;36/36px;48/48px;72/72px';
  244. /**
  245. * The text to be displayed in the Font Size combo is none of the available
  246. * values matches the current cursor position or text selection.
  247. *
  248. * // If the default site font size is 12px, we may making it more explicit to the end user.
  249. * config.fontSize_defaultLabel = '12px';
  250. *
  251. * @cfg {String} [fontSize_defaultLabel='']
  252. * @member CKEDITOR.config
  253. */
  254. CKEDITOR.config.fontSize_defaultLabel = '';
  255. /**
  256. * The style definition to be used to apply the font size in the text.
  257. *
  258. * // This is actually the default value for it.
  259. * config.fontSize_style = {
  260. * element: 'span',
  261. * styles: { 'font-size': '#(size)' },
  262. * overrides: [ { element :'font', attributes: { 'size': null } } ]
  263. * };
  264. *
  265. * @cfg {Object} [fontSize_style=see example]
  266. * @member CKEDITOR.config
  267. */
  268. CKEDITOR.config.fontSize_style = {
  269. element: 'span',
  270. styles: { 'font-size': '#(size)' },
  271. overrides: [ {
  272. element: 'font', attributes: { 'size': null }
  273. } ]
  274. };