classiceditorui.js 6.3 KB

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