classiceditorui.js 6.4 KB

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