8
0

classiceditorui.js 5.6 KB

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