decouplededitorui.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 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. testUtils.createSinonSandbox();
  15. describe( 'DecoupledEditorUI', () => {
  16. let editor, view, ui;
  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. } );
  27. } );
  28. afterEach( () => {
  29. editor.destroy();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'extends EditorUI', () => {
  33. expect( ui ).to.instanceof( EditorUI );
  34. } );
  35. } );
  36. describe( 'init()', () => {
  37. it( 'renders the #view', () => {
  38. expect( view.isRendered ).to.be.true;
  39. } );
  40. describe( 'editable', () => {
  41. it( 'registers view.editable#element in editor focus tracker', () => {
  42. ui.focusTracker.isFocused = false;
  43. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  44. expect( ui.focusTracker.isFocused ).to.true;
  45. } );
  46. it( 'sets view.editable#name', () => {
  47. const editable = editor.editing.view.document.getRoot();
  48. expect( view.editable.name ).to.equal( editable.rootName );
  49. } );
  50. it( 'binds view.editable#isFocused', () => {
  51. utils.assertBinding(
  52. view.editable,
  53. { isFocused: false },
  54. [
  55. [ editor.editing.view.document, { isFocused: true } ]
  56. ],
  57. { isFocused: true }
  58. );
  59. } );
  60. it( 'binds view.editable#isReadOnly', () => {
  61. const editable = editor.editing.view.document.getRoot();
  62. utils.assertBinding(
  63. view.editable,
  64. { isReadOnly: false },
  65. [
  66. [ editable, { isReadOnly: true } ]
  67. ],
  68. { isReadOnly: true }
  69. );
  70. } );
  71. it( 'attaches editable UI as view\'s DOM root', () => {
  72. expect( editor.editing.view.getDomRoot() ).to.equal( view.editable.element );
  73. } );
  74. } );
  75. describe( 'view.toolbar#items', () => {
  76. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  77. return VirtualDecoupledTestEditor
  78. .create( {
  79. toolbar: [ 'foo', 'bar' ]
  80. } )
  81. .then( editor => {
  82. const items = editor.ui.view.toolbar.items;
  83. expect( items.get( 0 ).name ).to.equal( 'foo' );
  84. expect( items.get( 1 ).name ).to.equal( 'bar' );
  85. return editor.destroy();
  86. } );
  87. } );
  88. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  89. return VirtualDecoupledTestEditor
  90. .create( {
  91. toolbar: {
  92. items: [ 'foo', 'bar' ],
  93. viewportTopOffset: 100
  94. }
  95. } )
  96. .then( editor => {
  97. const items = editor.ui.view.toolbar.items;
  98. expect( items.get( 0 ).name ).to.equal( 'foo' );
  99. expect( items.get( 1 ).name ).to.equal( 'bar' );
  100. return editor.destroy();
  101. } );
  102. } );
  103. } );
  104. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  105. return VirtualDecoupledTestEditor.create()
  106. .then( editor => {
  107. const ui = editor.ui;
  108. const view = ui.view;
  109. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  110. ui.focusTracker.isFocused = true;
  111. ui.view.toolbar.focusTracker.isFocused = false;
  112. editor.keystrokes.press( {
  113. keyCode: keyCodes.f10,
  114. altKey: true,
  115. preventDefault: sinon.spy(),
  116. stopPropagation: sinon.spy()
  117. } );
  118. sinon.assert.calledOnce( spy );
  119. return editor.destroy();
  120. } );
  121. } );
  122. } );
  123. } );
  124. function viewCreator( name ) {
  125. return locale => {
  126. const view = new View( locale );
  127. view.name = name;
  128. view.element = document.createElement( 'a' );
  129. return view;
  130. };
  131. }
  132. class VirtualDecoupledTestEditor extends VirtualTestEditor {
  133. constructor( config ) {
  134. super( config );
  135. const view = new DecoupledEditorUIView( this.locale );
  136. this.ui = new DecoupledEditorUI( this, view );
  137. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  138. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  139. }
  140. destroy() {
  141. this.ui.destroy();
  142. return super.destroy();
  143. }
  144. static create( config ) {
  145. return new Promise( resolve => {
  146. const editor = new this( config );
  147. resolve(
  148. editor.initPlugins()
  149. .then( () => {
  150. editor.ui.init();
  151. editor.fire( 'uiReady' );
  152. editor.fire( 'dataReady' );
  153. editor.fire( 'ready' );
  154. } )
  155. .then( () => editor )
  156. );
  157. } );
  158. }
  159. }