8
0

classiceditorui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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( 'stickyPanel', () => {
  60. it( 'binds view.stickyToolbar#isActive to editor.focusTracker#isFocused', () => {
  61. ui.focusTracker.isFocused = false;
  62. expect( view.stickyPanel.isActive ).to.be.false;
  63. ui.focusTracker.isFocused = true;
  64. expect( view.stickyPanel.isActive ).to.be.true;
  65. } );
  66. it( 'sets view.stickyToolbar#limiterElement', () => {
  67. expect( view.stickyPanel.limiterElement ).to.equal( view.element );
  68. } );
  69. it( 'doesn\'t set view.stickyToolbar#viewportTopOffset, if not specified in the config', () => {
  70. expect( view.stickyPanel.viewportTopOffset ).to.equal( 0 );
  71. } );
  72. it( 'sets view.stickyPanel#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. viewportTopOffset: 100
  79. }
  80. } )
  81. .then( editor => {
  82. view = new ClassicEditorUIView( editor.locale );
  83. ui = new ClassicEditorUI( editor, view );
  84. editable = editor.editing.view.getRoot();
  85. expect( view.stickyPanel.viewportTopOffset ).to.equal( 100 );
  86. editorElement.remove();
  87. return editor.destroy();
  88. } );
  89. } );
  90. } );
  91. describe( 'editable', () => {
  92. it( 'registers view.editable#element in editor focus tracker', () => {
  93. ui.focusTracker.isFocused = false;
  94. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  95. expect( ui.focusTracker.isFocused ).to.true;
  96. } );
  97. it( 'sets view.editable#name', () => {
  98. expect( view.editable.name ).to.equal( editable.rootName );
  99. } );
  100. it( 'binds view.editable#isFocused', () => {
  101. utils.assertBinding(
  102. view.editable,
  103. { isFocused: false },
  104. [
  105. [ editor.editing.view, { isFocused: true } ]
  106. ],
  107. { isFocused: true }
  108. );
  109. } );
  110. it( 'binds view.editable#isReadOnly', () => {
  111. utils.assertBinding(
  112. view.editable,
  113. { isReadOnly: false },
  114. [
  115. [ editable, { isReadOnly: true } ]
  116. ],
  117. { isReadOnly: true }
  118. );
  119. } );
  120. } );
  121. } );
  122. describe( 'init()', () => {
  123. afterEach( () => {
  124. ui.destroy();
  125. } );
  126. it( 'initializes the #view', () => {
  127. const spy = sinon.spy( view, 'init' );
  128. ui.init();
  129. sinon.assert.calledOnce( spy );
  130. } );
  131. describe( 'view.toolbar#items', () => {
  132. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  133. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  134. ui.init();
  135. sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
  136. } );
  137. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  138. editorElement = document.createElement( 'div' );
  139. document.body.appendChild( editorElement );
  140. return ClassicTestEditor
  141. .create( editorElement, {
  142. toolbar: {
  143. items: [ 'foo', 'bar' ],
  144. viewportTopOffset: 100
  145. }
  146. } )
  147. .then( editor => {
  148. view = new ClassicEditorUIView( editor.locale );
  149. ui = new ClassicEditorUI( editor, view );
  150. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  151. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  152. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  153. ui.init();
  154. sinon.assert.calledWithExactly( spy,
  155. editor.config.get( 'toolbar.items' ),
  156. ui.componentFactory
  157. );
  158. } );
  159. } );
  160. } );
  161. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  162. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  163. ui.init();
  164. ui.focusTracker.isFocused = true;
  165. ui.view.toolbar.focusTracker.isFocused = false;
  166. editor.keystrokes.press( {
  167. keyCode: keyCodes.f10,
  168. altKey: true,
  169. preventDefault: sinon.spy(),
  170. stopPropagation: sinon.spy()
  171. } );
  172. sinon.assert.calledOnce( spy );
  173. } );
  174. } );
  175. describe( 'destroy()', () => {
  176. beforeEach( () => {
  177. document.body.appendChild( view.element );
  178. } );
  179. it( 'destroys the #view', () => {
  180. const spy = sinon.spy( view, 'destroy' );
  181. ui.init();
  182. ui.destroy();
  183. sinon.assert.calledOnce( spy );
  184. } );
  185. } );
  186. } );
  187. function viewCreator( name ) {
  188. return locale => {
  189. const view = new View( locale );
  190. view.name = name;
  191. view.element = document.createElement( 'a' );
  192. return view;
  193. };
  194. }