decouplededitorui.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 DecoupledEditorUI from '../src/decouplededitorui';
  10. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  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( 'DecoupledEditorUI', () => {
  17. let editor, view, ui;
  18. beforeEach( () => {
  19. return VirtualDecoupledTestEditor
  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( DecoupledEditorUIView );
  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( 'editable', () => {
  51. it( 'registers view.editable#element in editor focus tracker', () => {
  52. ui.focusTracker.isFocused = false;
  53. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  54. expect( ui.focusTracker.isFocused ).to.true;
  55. } );
  56. it( 'sets view.editable#name', () => {
  57. const editable = editor.editing.view.document.getRoot();
  58. expect( view.editable.name ).to.equal( editable.rootName );
  59. } );
  60. it( 'binds view.editable#isFocused', () => {
  61. utils.assertBinding(
  62. view.editable,
  63. { isFocused: false },
  64. [
  65. [ editor.editing.view.document, { isFocused: true } ]
  66. ],
  67. { isFocused: true }
  68. );
  69. } );
  70. it( 'binds view.editable#isReadOnly', () => {
  71. const editable = editor.editing.view.document.getRoot();
  72. utils.assertBinding(
  73. view.editable,
  74. { isReadOnly: false },
  75. [
  76. [ editable, { isReadOnly: true } ]
  77. ],
  78. { isReadOnly: true }
  79. );
  80. } );
  81. it( 'attaches editable UI as view\'s DOM root', () => {
  82. expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
  83. } );
  84. } );
  85. describe( 'view.toolbar#items', () => {
  86. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  87. return VirtualDecoupledTestEditor
  88. .create( {
  89. toolbar: [ 'foo', 'bar' ]
  90. } )
  91. .then( editor => {
  92. const items = editor.ui.view.toolbar.items;
  93. expect( items.get( 0 ).name ).to.equal( 'foo' );
  94. expect( items.get( 1 ).name ).to.equal( 'bar' );
  95. return editor.destroy();
  96. } );
  97. } );
  98. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  99. return VirtualDecoupledTestEditor
  100. .create( {
  101. toolbar: {
  102. items: [ 'foo', 'bar' ],
  103. viewportTopOffset: 100
  104. }
  105. } )
  106. .then( editor => {
  107. const items = editor.ui.view.toolbar.items;
  108. expect( items.get( 0 ).name ).to.equal( 'foo' );
  109. expect( items.get( 1 ).name ).to.equal( 'bar' );
  110. return editor.destroy();
  111. } );
  112. } );
  113. } );
  114. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  115. return VirtualDecoupledTestEditor.create()
  116. .then( editor => {
  117. const ui = editor.ui;
  118. const view = ui.view;
  119. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  120. ui.focusTracker.isFocused = true;
  121. ui.view.toolbar.focusTracker.isFocused = false;
  122. editor.keystrokes.press( {
  123. keyCode: keyCodes.f10,
  124. altKey: true,
  125. preventDefault: sinon.spy(),
  126. stopPropagation: sinon.spy()
  127. } );
  128. sinon.assert.calledOnce( spy );
  129. return editor.destroy();
  130. } );
  131. } );
  132. } );
  133. describe( 'destroy()', () => {
  134. it( 'destroys the #view', () => {
  135. const spy = sinon.spy( view, 'destroy' );
  136. return editor.destroy()
  137. .then( () => {
  138. sinon.assert.calledOnce( spy );
  139. sinon.assert.calledWithExactly( spy );
  140. } );
  141. } );
  142. } );
  143. } );
  144. function viewCreator( name ) {
  145. return locale => {
  146. const view = new View( locale );
  147. view.name = name;
  148. view.element = document.createElement( 'a' );
  149. return view;
  150. };
  151. }
  152. class VirtualDecoupledTestEditor extends VirtualTestEditor {
  153. constructor( config ) {
  154. super( config );
  155. const view = new DecoupledEditorUIView( this.locale );
  156. this.ui = new DecoupledEditorUI( this, view );
  157. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  158. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  159. }
  160. destroy() {
  161. this.ui.destroy();
  162. return super.destroy();
  163. }
  164. static create( config ) {
  165. return new Promise( resolve => {
  166. const editor = new this( config );
  167. resolve(
  168. editor.initPlugins()
  169. .then( () => {
  170. editor.ui.init();
  171. editor.fire( 'uiReady' );
  172. editor.fire( 'dataReady' );
  173. editor.fire( 'ready' );
  174. } )
  175. .then( () => editor )
  176. );
  177. } );
  178. }
  179. }