decouplededitorui.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import View from '@ckeditor/ckeditor5-ui/src/view';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import DecoupledEditorUI from '../src/decouplededitorui';
  9. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  10. import DecoupledEditorUIView from '../src/decouplededitoruiview';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. describe( 'DecoupledEditorUI', () => {
  15. let editor, view, ui, viewElement;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. return VirtualDecoupledTestEditor
  19. .create( {
  20. toolbar: [ 'foo', 'bar' ],
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. ui = editor.ui;
  25. view = ui.view;
  26. viewElement = view.element;
  27. } );
  28. } );
  29. afterEach( () => {
  30. editor.destroy();
  31. } );
  32. describe( 'constructor()', () => {
  33. it( 'extends EditorUI', () => {
  34. expect( ui ).to.instanceof( EditorUI );
  35. } );
  36. } );
  37. describe( 'init()', () => {
  38. it( 'renders the #view', () => {
  39. expect( view.isRendered ).to.be.true;
  40. } );
  41. describe( 'editable', () => {
  42. it( 'registers view.editable#element in editor focus tracker', () => {
  43. ui.focusTracker.isFocused = false;
  44. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  45. expect( ui.focusTracker.isFocused ).to.true;
  46. } );
  47. it( 'sets view.editable#name', () => {
  48. const editable = editor.editing.view.document.getRoot();
  49. expect( view.editable.name ).to.equal( editable.rootName );
  50. } );
  51. it( 'binds view.editable#isFocused', () => {
  52. utils.assertBinding(
  53. view.editable,
  54. { isFocused: false },
  55. [
  56. [ editor.editing.view.document, { isFocused: true } ]
  57. ],
  58. { isFocused: true }
  59. );
  60. } );
  61. it( 'binds view.editable#isReadOnly', () => {
  62. const editable = editor.editing.view.document.getRoot();
  63. utils.assertBinding(
  64. view.editable,
  65. { isReadOnly: false },
  66. [
  67. [ editable, { isReadOnly: true } ]
  68. ],
  69. { isReadOnly: true }
  70. );
  71. } );
  72. it( 'attaches editable UI as view\'s DOM root', () => {
  73. expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
  74. } );
  75. } );
  76. describe( 'view.toolbar#items', () => {
  77. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  78. return VirtualDecoupledTestEditor
  79. .create( {
  80. toolbar: [ 'foo', 'bar' ]
  81. } )
  82. .then( editor => {
  83. const items = editor.ui.view.toolbar.items;
  84. expect( items.get( 0 ).name ).to.equal( 'foo' );
  85. expect( items.get( 1 ).name ).to.equal( 'bar' );
  86. return editor.destroy();
  87. } );
  88. } );
  89. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  90. return VirtualDecoupledTestEditor
  91. .create( {
  92. toolbar: {
  93. items: [ 'foo', 'bar' ],
  94. viewportTopOffset: 100
  95. }
  96. } )
  97. .then( editor => {
  98. const items = editor.ui.view.toolbar.items;
  99. expect( items.get( 0 ).name ).to.equal( 'foo' );
  100. expect( items.get( 1 ).name ).to.equal( 'bar' );
  101. return editor.destroy();
  102. } );
  103. } );
  104. } );
  105. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  106. return VirtualDecoupledTestEditor.create()
  107. .then( editor => {
  108. const ui = editor.ui;
  109. const view = ui.view;
  110. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  111. ui.focusTracker.isFocused = true;
  112. ui.view.toolbar.focusTracker.isFocused = false;
  113. editor.keystrokes.press( {
  114. keyCode: keyCodes.f10,
  115. altKey: true,
  116. preventDefault: sinon.spy(),
  117. stopPropagation: sinon.spy()
  118. } );
  119. sinon.assert.calledOnce( spy );
  120. return editor.destroy();
  121. } );
  122. } );
  123. } );
  124. describe( 'element()', () => {
  125. it( 'returns correct element instance', () => {
  126. expect( ui.element ).to.equal( viewElement );
  127. } );
  128. } );
  129. describe( 'getEditableElement()', () => {
  130. it( 'returns editable element (default)', () => {
  131. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  132. } );
  133. it( 'returns editable element (root name passed)', () => {
  134. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  135. } );
  136. it( 'returns undefined if editable with the given name is absent', () => {
  137. expect( ui.getEditableElement( 'absent' ) ).to.be.undefined;
  138. } );
  139. } );
  140. } );
  141. function viewCreator( name ) {
  142. return locale => {
  143. const view = new View( locale );
  144. view.name = name;
  145. view.element = document.createElement( 'a' );
  146. return view;
  147. };
  148. }
  149. class VirtualDecoupledTestEditor extends VirtualTestEditor {
  150. constructor( config ) {
  151. super( config );
  152. const view = new DecoupledEditorUIView( this.locale );
  153. this.ui = new DecoupledEditorUI( this, view );
  154. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  155. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  156. }
  157. destroy() {
  158. this.ui.destroy();
  159. return super.destroy();
  160. }
  161. static create( config ) {
  162. return new Promise( resolve => {
  163. const editor = new this( config );
  164. resolve(
  165. editor.initPlugins()
  166. .then( () => {
  167. editor.ui.init();
  168. editor.fire( 'dataReady' );
  169. editor.fire( 'ready' );
  170. } )
  171. .then( () => editor )
  172. );
  173. } );
  174. }
  175. }