classiceditorui.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import ClassicEditorUI from '../src/classiceditorui';
  9. import ClassicEditorUIView from '../src/classiceditoruiview';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  12. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. describe( 'ClassicEditorUI', () => {
  18. let editorElement, editor, editable, view, ui;
  19. beforeEach( () => {
  20. editorElement = document.createElement( 'div' );
  21. document.body.appendChild( editorElement );
  22. editor = new ClassicTestEditor( editorElement, {
  23. toolbar: [ 'foo', 'bar' ],
  24. ui: {
  25. width: 100,
  26. height: 200
  27. }
  28. } );
  29. view = new ClassicEditorUIView( editor.locale );
  30. ui = new ClassicEditorUI( editor, view );
  31. editable = editor.editing.view.getRoot();
  32. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  33. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  34. } );
  35. describe( 'constructor()', () => {
  36. it( 'sets #editor', () => {
  37. expect( ui.editor ).to.equal( editor );
  38. } );
  39. it( 'sets #view', () => {
  40. expect( ui.view ).to.equal( view );
  41. } );
  42. it( 'creates #componentFactory factory', () => {
  43. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  44. } );
  45. it( 'creates #focusTracker', () => {
  46. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  47. } );
  48. it( 'creates #keystrokes', () => {
  49. expect( ui.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  50. } );
  51. it( 'sets view#width and view#height', () => {
  52. expect( view.width ).to.equal( 100 );
  53. expect( view.height ).to.equal( 200 );
  54. } );
  55. describe( 'toolbar', () => {
  56. it( 'binds view.toolbar#isFocused to editor#focusTracker', () => {
  57. ui.focusTracker.isFocused = false;
  58. expect( view.toolbar.isActive ).to.be.false;
  59. ui.focusTracker.isFocused = true;
  60. expect( view.toolbar.isActive ).to.be.true;
  61. } );
  62. it( 'sets view.toolbar#limiterElement', () => {
  63. expect( view.toolbar.limiterElement ).to.equal( view.element );
  64. } );
  65. } );
  66. describe( 'editable', () => {
  67. it( 'registers view.editable#element in editor focus tracker', () => {
  68. ui.focusTracker.isFocused = false;
  69. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  70. expect( ui.focusTracker.isFocused ).to.true;
  71. } );
  72. it( 'sets view.editable#name', () => {
  73. expect( view.editable.name ).to.equal( editable.rootName );
  74. } );
  75. it( 'binds view.editable#isFocused', () => {
  76. utils.assertBinding(
  77. view.editable,
  78. { isFocused: false },
  79. [
  80. [ editor.editing.view, { isFocused: true } ]
  81. ],
  82. { isFocused: true }
  83. );
  84. } );
  85. it( 'binds view.editable#isReadOnly', () => {
  86. utils.assertBinding(
  87. view.editable,
  88. { isReadOnly: false },
  89. [
  90. [ editable, { isReadOnly: true } ]
  91. ],
  92. { isReadOnly: true }
  93. );
  94. } );
  95. } );
  96. } );
  97. describe( 'init()', () => {
  98. afterEach( () => {
  99. return ui.destroy();
  100. } );
  101. it( 'returns a promise', () => {
  102. document.body.appendChild( view.element );
  103. const promise = ui.init().then( () => {
  104. expect( promise ).to.be.instanceof( Promise );
  105. } );
  106. return promise;
  107. } );
  108. it( 'initializes the #view', () => {
  109. const spy = sinon.spy( view, 'init' );
  110. return ui.init().then( () => {
  111. sinon.assert.calledOnce( spy );
  112. } );
  113. } );
  114. it( 'fills view.toolbar#items with editor config', () => {
  115. return ui.init().then( () => {
  116. expect( view.toolbar.items ).to.have.length( 2 );
  117. expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
  118. expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
  119. } );
  120. } );
  121. describe( 'activates keyboard navigation for the toolbar', () => {
  122. it( 'listens for keystrokes coming from various parts of the UI', () => {
  123. const spy = sinon.spy( ui.keystrokes, 'listenTo' );
  124. return ui.init().then( () => {
  125. sinon.assert.calledTwice( spy );
  126. sinon.assert.calledWithExactly( spy.firstCall, ui.view.element );
  127. sinon.assert.calledWithExactly( spy.secondCall, ui.view._bodyCollectionContainer );
  128. } );
  129. } );
  130. it( 'alt + f10: focus the first focusable toolbar item', () => {
  131. return ui.init().then( () => {
  132. const spy = sinon.spy( view.toolbar, 'focus' );
  133. const toolbarFocusTracker = view.toolbar.focusTracker;
  134. const keyEvtData = {
  135. keyCode: keyCodes.f10,
  136. altKey: true,
  137. preventDefault: sinon.spy(),
  138. stopPropagation: sinon.spy()
  139. };
  140. toolbarFocusTracker.isFocused = false;
  141. ui.focusTracker.isFocused = false;
  142. ui.keystrokes.press( keyEvtData );
  143. sinon.assert.notCalled( spy );
  144. toolbarFocusTracker.isFocused = true;
  145. ui.focusTracker.isFocused = true;
  146. ui.keystrokes.press( keyEvtData );
  147. sinon.assert.notCalled( spy );
  148. toolbarFocusTracker.isFocused = false;
  149. ui.focusTracker.isFocused = true;
  150. ui.keystrokes.press( keyEvtData );
  151. sinon.assert.calledOnce( spy );
  152. sinon.assert.calledOnce( keyEvtData.preventDefault );
  153. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  154. } );
  155. } );
  156. it( 'esc: re–foucus editable when toolbar is focused', () => {
  157. return ui.init().then( () => {
  158. const spy = sinon.spy( editor.editing.view, 'focus' );
  159. const toolbarFocusTracker = view.toolbar.focusTracker;
  160. const keyEvtData = { keyCode: keyCodes.esc,
  161. preventDefault: sinon.spy(),
  162. stopPropagation: sinon.spy()
  163. };
  164. toolbarFocusTracker.isFocused = false;
  165. ui.keystrokes.press( keyEvtData );
  166. sinon.assert.notCalled( spy );
  167. toolbarFocusTracker.isFocused = true;
  168. ui.keystrokes.press( keyEvtData );
  169. sinon.assert.calledOnce( spy );
  170. sinon.assert.calledOnce( keyEvtData.preventDefault );
  171. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  172. } );
  173. } );
  174. } );
  175. } );
  176. describe( 'destroy()', () => {
  177. beforeEach( () => {
  178. document.body.appendChild( view.element );
  179. } );
  180. it( 'returns a promise', () => {
  181. return ui.init().then( () => {
  182. const promise = ui.destroy().then( () => {
  183. expect( promise ).to.be.instanceof( Promise );
  184. } );
  185. return promise;
  186. } );
  187. } );
  188. it( 'destroys the #view', () => {
  189. const spy = sinon.spy( view, 'destroy' );
  190. return ui.init()
  191. .then( () => ui.destroy() )
  192. .then( () => {
  193. sinon.assert.calledOnce( spy );
  194. } );
  195. } );
  196. } );
  197. } );
  198. function viewCreator( name ) {
  199. return ( locale ) => {
  200. const view = new View( locale );
  201. view.name = name;
  202. view.element = document.createElement( 'a' );
  203. return view;
  204. };
  205. }