classiceditorui.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. testUtils.createSinonSandbox();
  16. describe( 'ClassicEditorUI', () => {
  17. let editorElement, editor, editable, view, ui;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. toolbar: [ 'foo', 'bar' ],
  24. ui: {
  25. width: 100,
  26. height: 200
  27. }
  28. } )
  29. .then( newEditor => {
  30. editor = newEditor;
  31. view = new ClassicEditorUIView( editor.locale );
  32. ui = new ClassicEditorUI( editor, view );
  33. editable = editor.editing.view.getRoot();
  34. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  35. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  36. } );
  37. } );
  38. afterEach( () => {
  39. editorElement.remove();
  40. editor.destroy();
  41. } );
  42. describe( 'constructor()', () => {
  43. it( 'sets #editor', () => {
  44. expect( ui.editor ).to.equal( editor );
  45. } );
  46. it( 'sets #view', () => {
  47. expect( ui.view ).to.equal( view );
  48. } );
  49. it( 'creates #componentFactory factory', () => {
  50. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  51. } );
  52. it( 'creates #focusTracker', () => {
  53. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  54. } );
  55. it( 'sets view#width and view#height', () => {
  56. expect( view.width ).to.equal( 100 );
  57. expect( view.height ).to.equal( 200 );
  58. } );
  59. describe( 'toolbar', () => {
  60. it( 'binds view.toolbar#isFocused to editor#focusTracker', () => {
  61. ui.focusTracker.isFocused = false;
  62. expect( view.toolbar.isActive ).to.be.false;
  63. ui.focusTracker.isFocused = true;
  64. expect( view.toolbar.isActive ).to.be.true;
  65. } );
  66. it( 'sets view.toolbar#limiterElement', () => {
  67. expect( view.toolbar.limiterElement ).to.equal( view.element );
  68. } );
  69. it( 'doesn\'t set view.toolbar#viewportTopOffset, if not specified in the config', () => {
  70. expect( view.toolbar.viewportTopOffset ).to.equal( 0 );
  71. } );
  72. it( 'sets view.toolbar#viewportTopOffset, when specified in the config', () => {
  73. editorElement = document.createElement( 'div' );
  74. document.body.appendChild( editorElement );
  75. return ClassicTestEditor
  76. .create( editorElement, {
  77. toolbar: {
  78. items: [ 'foo', 'bar' ],
  79. viewportTopOffset: 100
  80. }
  81. } )
  82. .then( editor => {
  83. view = new ClassicEditorUIView( editor.locale );
  84. ui = new ClassicEditorUI( editor, view );
  85. editable = editor.editing.view.getRoot();
  86. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  87. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  88. expect( view.toolbar.viewportTopOffset ).to.equal( 100 );
  89. editorElement.remove();
  90. return editor.destroy();
  91. } );
  92. } );
  93. } );
  94. describe( 'editable', () => {
  95. it( 'registers view.editable#element in editor focus tracker', () => {
  96. ui.focusTracker.isFocused = false;
  97. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  98. expect( ui.focusTracker.isFocused ).to.true;
  99. } );
  100. it( 'sets view.editable#name', () => {
  101. expect( view.editable.name ).to.equal( editable.rootName );
  102. } );
  103. it( 'binds view.editable#isFocused', () => {
  104. utils.assertBinding(
  105. view.editable,
  106. { isFocused: false },
  107. [
  108. [ editor.editing.view, { isFocused: true } ]
  109. ],
  110. { isFocused: true }
  111. );
  112. } );
  113. it( 'binds view.editable#isReadOnly', () => {
  114. utils.assertBinding(
  115. view.editable,
  116. { isReadOnly: false },
  117. [
  118. [ editable, { isReadOnly: true } ]
  119. ],
  120. { isReadOnly: true }
  121. );
  122. } );
  123. } );
  124. } );
  125. describe( 'init()', () => {
  126. afterEach( () => {
  127. ui.destroy();
  128. } );
  129. it( 'initializes the #view', () => {
  130. const spy = sinon.spy( view, 'init' );
  131. ui.init();
  132. sinon.assert.calledOnce( spy );
  133. } );
  134. describe( 'view.toolbar#items', () => {
  135. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  136. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  137. ui.init();
  138. sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
  139. } );
  140. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  141. editorElement = document.createElement( 'div' );
  142. document.body.appendChild( editorElement );
  143. return ClassicTestEditor
  144. .create( editorElement, {
  145. toolbar: {
  146. items: [ 'foo', 'bar' ],
  147. viewportTopOffset: 100
  148. }
  149. } )
  150. .then( editor => {
  151. view = new ClassicEditorUIView( editor.locale );
  152. ui = new ClassicEditorUI( editor, view );
  153. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  154. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  155. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  156. ui.init();
  157. sinon.assert.calledWithExactly( spy,
  158. editor.config.get( 'toolbar.items' ),
  159. ui.componentFactory
  160. );
  161. } );
  162. } );
  163. } );
  164. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  165. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  166. ui.init();
  167. ui.focusTracker.isFocused = true;
  168. ui.view.toolbar.focusTracker.isFocused = false;
  169. editor.keystrokes.press( {
  170. keyCode: keyCodes.f10,
  171. altKey: true,
  172. preventDefault: sinon.spy(),
  173. stopPropagation: sinon.spy()
  174. } );
  175. sinon.assert.calledOnce( spy );
  176. } );
  177. } );
  178. describe( 'destroy()', () => {
  179. beforeEach( () => {
  180. document.body.appendChild( view.element );
  181. } );
  182. it( 'destroys the #view', () => {
  183. const spy = sinon.spy( view, 'destroy' );
  184. ui.init();
  185. ui.destroy();
  186. sinon.assert.calledOnce( spy );
  187. } );
  188. } );
  189. } );
  190. function viewCreator( name ) {
  191. return locale => {
  192. const view = new View( locale );
  193. view.name = name;
  194. view.element = document.createElement( 'a' );
  195. return view;
  196. };
  197. }