plugin.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  6. * @fileOverview [Language](http://ckeditor.com/addon/language) plugin.
  7. */
  8. 'use strict';
  9. ( function() {
  10. var allowedContent = 'span[!lang,!dir]',
  11. requiredContent = 'span[lang,dir]';
  12. CKEDITOR.plugins.add( 'language', {
  13. requires: 'menubutton',
  14. lang: 'ar,bg,ca,cs,cy,da,de,el,en,en-gb,eo,es,fa,fi,fo,fr,gl,he,hr,hu,it,ja,km,ko,ku,nb,nl,no,pl,pt,pt-br,ru,sk,sl,sq,sv,tr,tt,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  15. icons: 'language', // %REMOVE_LINE_CORE%
  16. hidpi: true, // %REMOVE_LINE_CORE%
  17. init: function( editor ) {
  18. var languagesConfigStrings = ( editor.config.language_list || [ 'ar:Arabic:rtl', 'fr:French', 'es:Spanish' ] ),
  19. plugin = this,
  20. lang = editor.lang.language,
  21. items = {},
  22. parts,
  23. curLanguageId, // 2-letter language identifier.
  24. languageButtonId, // Will store button namespaced identifier, like "language_en".
  25. i;
  26. // Registers command.
  27. editor.addCommand( 'language', {
  28. allowedContent: allowedContent,
  29. requiredContent: requiredContent,
  30. contextSensitive: true,
  31. exec: function( editor, languageId ) {
  32. var item = items[ 'language_' + languageId ];
  33. if ( item )
  34. editor[ item.style.checkActive( editor.elementPath(), editor ) ? 'removeStyle' : 'applyStyle' ]( item.style );
  35. },
  36. refresh: function( editor ) {
  37. this.setState( plugin.getCurrentLangElement( editor ) ?
  38. CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
  39. }
  40. } );
  41. // Parse languagesConfigStrings, and create items entry for each lang.
  42. for ( i = 0; i < languagesConfigStrings.length; i++ ) {
  43. parts = languagesConfigStrings[ i ].split( ':' );
  44. curLanguageId = parts[ 0 ];
  45. languageButtonId = 'language_' + curLanguageId;
  46. items[ languageButtonId ] = {
  47. label: parts[ 1 ],
  48. langId: curLanguageId,
  49. group: 'language',
  50. order: i,
  51. // Tells if this language is left-to-right oriented (default: true).
  52. ltr: ( '' + parts[ 2 ] ).toLowerCase() != 'rtl',
  53. onClick: function() {
  54. editor.execCommand( 'language', this.langId );
  55. },
  56. role: 'menuitemcheckbox'
  57. };
  58. // Init style property.
  59. items[ languageButtonId ].style = new CKEDITOR.style( {
  60. element: 'span',
  61. attributes: {
  62. lang: curLanguageId,
  63. dir: items[ languageButtonId ].ltr ? 'ltr' : 'rtl'
  64. }
  65. } );
  66. }
  67. // Remove language indicator button.
  68. items.language_remove = {
  69. label: lang.remove,
  70. group: 'language_remove',
  71. state: CKEDITOR.TRISTATE_DISABLED,
  72. order: items.length,
  73. onClick: function() {
  74. var currentLanguagedElement = plugin.getCurrentLangElement( editor );
  75. if ( currentLanguagedElement )
  76. editor.execCommand( 'language', currentLanguagedElement.getAttribute( 'lang' ) );
  77. }
  78. };
  79. // Initialize groups for menu.
  80. editor.addMenuGroup( 'language', 1 );
  81. editor.addMenuGroup( 'language_remove' ); // Group order is skipped intentionally, it will be placed at the end.
  82. editor.addMenuItems( items );
  83. editor.ui.add( 'Language', CKEDITOR.UI_MENUBUTTON, {
  84. label: lang.button,
  85. // MenuButtons do not (yet) has toFeature method, so we cannot do this:
  86. // toFeature: function( editor ) { return editor.getCommand( 'language' ); }
  87. // Set feature's properties directly on button.
  88. allowedContent: allowedContent,
  89. requiredContent: requiredContent,
  90. toolbar: 'bidi,30',
  91. command: 'language',
  92. onMenu: function() {
  93. var activeItems = {},
  94. currentLanguagedElement = plugin.getCurrentLangElement( editor );
  95. for ( var prop in items )
  96. activeItems[ prop ] = CKEDITOR.TRISTATE_OFF;
  97. activeItems.language_remove = currentLanguagedElement ? CKEDITOR.TRISTATE_OFF : CKEDITOR.TRISTATE_DISABLED;
  98. if ( currentLanguagedElement )
  99. activeItems[ 'language_' + currentLanguagedElement.getAttribute( 'lang' ) ] = CKEDITOR.TRISTATE_ON;
  100. return activeItems;
  101. }
  102. } );
  103. },
  104. // Gets the first language element for the current editor selection.
  105. // @param {CKEDITOR.editor} editor
  106. // @returns {CKEDITOR.dom.element} The language element, if any.
  107. getCurrentLangElement: function( editor ) {
  108. var elementPath = editor.elementPath(),
  109. activePath = elementPath && elementPath.elements,
  110. pathMember, ret;
  111. // IE8: upon initialization if there is no path elementPath() returns null.
  112. if ( elementPath ) {
  113. for ( var i = 0; i < activePath.length; i++ ) {
  114. pathMember = activePath[ i ];
  115. if ( !ret && pathMember.getName() == 'span' && pathMember.hasAttribute( 'dir' ) && pathMember.hasAttribute( 'lang' ) )
  116. ret = pathMember;
  117. }
  118. }
  119. return ret;
  120. }
  121. } );
  122. } )();
  123. /**
  124. * Specifies the list of languages available in the
  125. * [Language](http://ckeditor.com/addon/language) plugin. Each entry
  126. * should be a string in the following format:
  127. *
  128. * <languageCode>:<languageLabel>[:<textDirection>]
  129. *
  130. * * _languageCode_: The language code used for the `lang` attribute in ISO 639 format.
  131. * Language codes can be found [here](http://www.loc.gov/standards/iso639-2/php/English_list.php).
  132. * You can use both 2-letter ISO-639-1 codes and 3-letter ISO-639-2 codes, though
  133. * for consistency it is recommended to stick to ISO-639-1 2-letter codes.
  134. * * _languageLabel_: The label to show for this language in the list.
  135. * * _textDirection_: (optional) One of the following values: `rtl` or `ltr`,
  136. * indicating the reading direction of the language. Defaults to `ltr`.
  137. *
  138. * config.language_list = [ 'he:Hebrew:rtl', 'pt:Portuguese', 'de:German' ];
  139. *
  140. * @cfg {Array} [language_list = [ 'ar:Arabic:rtl', 'fr:French', 'es:Spanish' ]]
  141. * @member CKEDITOR.config
  142. */