classiceditorui.js 6.2 KB

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