highlightui.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global document */
  6. import HighlightEditing from '../src/highlightediting';
  7. import HighlightUI from '../src/highlightui';
  8. import markerIcon from '../theme/icons/marker.svg';
  9. import penIcon from '../theme/icons/pen.svg';
  10. import eraserIcon from '@ckeditor/ckeditor5-core/theme/icons/eraser.svg';
  11. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import { _clear as clearTranslations, add as addTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  14. describe( 'HighlightUI', () => {
  15. let editor, command, element;
  16. testUtils.createSinonSandbox();
  17. before( () => {
  18. addTranslations( 'en', {
  19. 'Highlight': 'Highlight',
  20. 'Yellow marker': 'Yellow marker',
  21. 'Green marker': 'Green marker',
  22. 'Pink marker': 'Pink marker',
  23. 'Red pen': 'Red pen',
  24. 'Blue pen': 'Blue pen',
  25. 'Remove highlight': 'Remove highlight'
  26. } );
  27. addTranslations( 'pl', {
  28. 'Highlight': 'Zakreślacz',
  29. 'Yellow marker': 'Żółty marker',
  30. 'Green marker': 'Zielony marker',
  31. 'Pink marker': 'Różowy marker',
  32. 'Blue marker': 'Niebieski marker',
  33. 'Red pen': 'Czerwony długopis',
  34. 'Green pen': 'Zielony długopis',
  35. 'Remove highlight': 'Usuń zaznaczenie'
  36. } );
  37. } );
  38. after( () => {
  39. clearTranslations();
  40. } );
  41. beforeEach( () => {
  42. element = document.createElement( 'div' );
  43. document.body.appendChild( element );
  44. return ClassicTestEditor
  45. .create( element, {
  46. plugins: [ HighlightEditing, HighlightUI ]
  47. } )
  48. .then( newEditor => {
  49. editor = newEditor;
  50. } );
  51. } );
  52. afterEach( () => {
  53. element.remove();
  54. return editor.destroy();
  55. } );
  56. describe( 'highlight Dropdown', () => {
  57. let dropdown;
  58. beforeEach( () => {
  59. command = editor.commands.get( 'highlight' );
  60. dropdown = editor.ui.componentFactory.create( 'highlight' );
  61. } );
  62. it( 'button has the base properties', () => {
  63. const button = dropdown.buttonView;
  64. expect( button ).to.have.property( 'tooltip', 'Highlight' );
  65. expect( button ).to.have.property( 'icon', markerIcon );
  66. expect( button ).to.have.property( 'isToggleable', true );
  67. } );
  68. it( 'toolbar nas the basic properties', () => {
  69. const toolbarView = dropdown.toolbarView;
  70. expect( toolbarView ).to.have.property( 'ariaLabel', 'Text highlight toolbar' );
  71. } );
  72. it( 'should have proper icons in dropdown', () => {
  73. const toolbar = dropdown.toolbarView;
  74. // Not in a selection with highlight.
  75. command.value = undefined;
  76. expect( toolbar.items.map( item => item.icon ) )
  77. .to.deep.equal( [ markerIcon, markerIcon, markerIcon, markerIcon, penIcon, penIcon, undefined, eraserIcon ] );
  78. } );
  79. it( 'should have proper colors in dropdown', () => {
  80. const toolbar = dropdown.toolbarView;
  81. expect( toolbar.items.map( item => item.iconView && item.iconView.fillColor ) ).to.deep.equal( [
  82. 'var(--ck-highlight-marker-yellow)',
  83. 'var(--ck-highlight-marker-green)',
  84. 'var(--ck-highlight-marker-pink)',
  85. 'var(--ck-highlight-marker-blue)',
  86. 'var(--ck-highlight-pen-red)',
  87. 'var(--ck-highlight-pen-green)',
  88. undefined,
  89. ''
  90. ] );
  91. } );
  92. it( 'should activate current option in dropdown', () => {
  93. const toolbar = dropdown.toolbarView;
  94. // Not in a selection with highlight.
  95. command.value = undefined;
  96. expect( toolbar.items.map( item => item.isOn ) )
  97. .to.deep.equal( [ false, false, false, false, false, false, undefined, false ] );
  98. // Inside a selection with highlight.
  99. command.value = 'greenMarker';
  100. // The second item is 'greenMarker' highlighter.
  101. expect( toolbar.items.map( item => item.isOn ) )
  102. .to.deep.equal( [ false, true, false, false, false, false, undefined, false ] );
  103. } );
  104. it( 'should mark as toggleable all markers and pens', () => {
  105. const toolbar = dropdown.toolbarView;
  106. expect( toolbar.items.map( item => item.isToggleable ) )
  107. .to.deep.equal( [ true, true, true, true, true, true, undefined, false ] );
  108. } );
  109. describe( 'toolbar button behavior', () => {
  110. let button, buttons, options;
  111. beforeEach( () => {
  112. button = dropdown.buttonView;
  113. buttons = dropdown.toolbarView.items.map( b => b );
  114. options = editor.config.get( 'highlight.options' );
  115. } );
  116. function validateButton( which ) {
  117. expect( button.icon ).to.equal( buttons[ which ].icon );
  118. expect( button.actionView.iconView.fillColor ).to.equal( options[ which ].color );
  119. }
  120. it( 'should have properties of first defined highlighter', () => {
  121. validateButton( 0 );
  122. } );
  123. it( 'should change button on selection', () => {
  124. command.value = 'redPen';
  125. validateButton( 4 );
  126. command.value = undefined;
  127. validateButton( 0 );
  128. } );
  129. it( 'should change button on execute option', () => {
  130. command.value = 'yellowMarker';
  131. validateButton( 0 );
  132. buttons[ 5 ].fire( 'execute' );
  133. command.value = 'greenPen';
  134. // Simulate selection moved to not highlighted text.
  135. command.value = undefined;
  136. validateButton( 5 );
  137. } );
  138. it( 'should focus view after command execution', () => {
  139. const focusSpy = testUtils.sinon.spy( editor.editing.view, 'focus' );
  140. dropdown.buttonView.commandName = 'highlight';
  141. dropdown.buttonView.fire( 'execute' );
  142. sinon.assert.calledOnce( focusSpy );
  143. } );
  144. } );
  145. describe( 'model to command binding', () => {
  146. it( 'isEnabled', () => {
  147. command.isEnabled = false;
  148. expect( dropdown.buttonView.isEnabled ).to.be.false;
  149. command.isEnabled = true;
  150. expect( dropdown.buttonView.isEnabled ).to.be.true;
  151. } );
  152. } );
  153. describe( 'localization', () => {
  154. beforeEach( () => {
  155. return localizedEditor();
  156. } );
  157. it( 'works for the #buttonView', () => {
  158. const buttonView = dropdown.buttonView;
  159. expect( buttonView.tooltip ).to.equal( 'Zakreślacz' );
  160. } );
  161. it( 'works for the listView#items in the panel', () => {
  162. const listView = dropdown.toolbarView;
  163. expect( listView.items.map( item => item.label ).filter( label => !!label ) ).to.deep.equal( [
  164. 'Żółty marker',
  165. 'Zielony marker',
  166. 'Różowy marker',
  167. 'Niebieski marker',
  168. 'Czerwony długopis',
  169. 'Zielony długopis',
  170. 'Usuń zaznaczenie'
  171. ] );
  172. } );
  173. function localizedEditor() {
  174. const editorElement = document.createElement( 'div' );
  175. document.body.appendChild( editorElement );
  176. return ClassicTestEditor
  177. .create( editorElement, {
  178. plugins: [ HighlightEditing, HighlightUI ],
  179. toolbar: [ 'highlight' ],
  180. language: 'pl'
  181. } )
  182. .then( newEditor => {
  183. dropdown = newEditor.ui.componentFactory.create( 'highlight' );
  184. command = newEditor.commands.get( 'highlight' );
  185. editorElement.remove();
  186. return newEditor.destroy();
  187. } );
  188. }
  189. } );
  190. } );
  191. } );