decouplededitorui.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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( 'config', () => {
  51. it( 'does nothing if not specified', () => {
  52. expect( view.toolbar.element.parentElement ).to.be.null;
  53. expect( view.editable.element.parentElement ).to.be.null;
  54. } );
  55. it( 'allocates view#toolbar', () => {
  56. return VirtualDecoupledTestEditor
  57. .create( {
  58. toolbar: [ 'foo', 'bar' ],
  59. toolbarContainer: document.body
  60. } )
  61. .then( newEditor => {
  62. expect( newEditor.ui.view.toolbar.element.parentElement ).to.equal( document.body );
  63. return newEditor;
  64. } )
  65. .then( newEditor => {
  66. newEditor.destroy();
  67. } );
  68. } );
  69. it( 'allocates view#editable', () => {
  70. return VirtualDecoupledTestEditor
  71. .create( {
  72. toolbar: [ 'foo', 'bar' ],
  73. editableContainer: document.body
  74. } )
  75. .then( newEditor => {
  76. expect( newEditor.ui.view.editable.element.parentElement ).to.equal( document.body );
  77. return newEditor;
  78. } )
  79. .then( newEditor => {
  80. newEditor.destroy();
  81. } );
  82. } );
  83. } );
  84. describe( 'editable', () => {
  85. it( 'registers view.editable#element in editor focus tracker', () => {
  86. ui.focusTracker.isFocused = false;
  87. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  88. expect( ui.focusTracker.isFocused ).to.true;
  89. } );
  90. it( 'sets view.editable#name', () => {
  91. const editable = editor.editing.view.document.getRoot();
  92. expect( view.editable.name ).to.equal( editable.rootName );
  93. } );
  94. it( 'binds view.editable#isFocused', () => {
  95. utils.assertBinding(
  96. view.editable,
  97. { isFocused: false },
  98. [
  99. [ editor.editing.view.document, { isFocused: true } ]
  100. ],
  101. { isFocused: true }
  102. );
  103. } );
  104. it( 'binds view.editable#isReadOnly', () => {
  105. const editable = editor.editing.view.document.getRoot();
  106. utils.assertBinding(
  107. view.editable,
  108. { isReadOnly: false },
  109. [
  110. [ editable, { isReadOnly: true } ]
  111. ],
  112. { isReadOnly: true }
  113. );
  114. } );
  115. } );
  116. describe( 'view.toolbar#items', () => {
  117. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  118. return VirtualDecoupledTestEditor
  119. .create( {
  120. toolbar: [ 'foo', 'bar' ]
  121. } )
  122. .then( editor => {
  123. const items = editor.ui.view.toolbar.items;
  124. expect( items.get( 0 ).name ).to.equal( 'foo' );
  125. expect( items.get( 1 ).name ).to.equal( 'bar' );
  126. return editor.destroy();
  127. } );
  128. } );
  129. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  130. return VirtualDecoupledTestEditor
  131. .create( {
  132. toolbar: {
  133. items: [ 'foo', 'bar' ],
  134. viewportTopOffset: 100
  135. }
  136. } )
  137. .then( editor => {
  138. const items = editor.ui.view.toolbar.items;
  139. expect( items.get( 0 ).name ).to.equal( 'foo' );
  140. expect( items.get( 1 ).name ).to.equal( 'bar' );
  141. return editor.destroy();
  142. } );
  143. } );
  144. } );
  145. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  146. return VirtualDecoupledTestEditor.create()
  147. .then( editor => {
  148. const ui = editor.ui;
  149. const view = ui.view;
  150. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  151. ui.focusTracker.isFocused = true;
  152. ui.view.toolbar.focusTracker.isFocused = false;
  153. editor.keystrokes.press( {
  154. keyCode: keyCodes.f10,
  155. altKey: true,
  156. preventDefault: sinon.spy(),
  157. stopPropagation: sinon.spy()
  158. } );
  159. sinon.assert.calledOnce( spy );
  160. return editor.destroy();
  161. } );
  162. } );
  163. } );
  164. describe( 'destroy()', () => {
  165. it( 'destroys the #view', () => {
  166. const spy = sinon.spy( view, 'destroy' );
  167. return editor.destroy()
  168. .then( () => {
  169. sinon.assert.calledOnce( spy );
  170. sinon.assert.calledWithExactly( spy, false, false );
  171. } );
  172. } );
  173. it( 'removes view#toolbar from DOM, if config.toolbarContainer is specified', () => {
  174. let spy;
  175. return VirtualDecoupledTestEditor
  176. .create( {
  177. toolbar: [ 'foo', 'bar' ],
  178. toolbarContainer: document.body
  179. } )
  180. .then( newEditor => {
  181. spy = sinon.spy( newEditor.ui.view, 'destroy' );
  182. newEditor.destroy();
  183. } )
  184. .then( () => {
  185. sinon.assert.calledWithExactly( spy, true, false );
  186. } );
  187. } );
  188. it( 'removes view#editable from DOM, if config.editableContainer is specified', () => {
  189. let spy;
  190. return VirtualDecoupledTestEditor
  191. .create( {
  192. toolbar: [ 'foo', 'bar' ],
  193. editableContainer: document.body
  194. } )
  195. .then( newEditor => {
  196. spy = sinon.spy( newEditor.ui.view, 'destroy' );
  197. newEditor.destroy();
  198. } )
  199. .then( () => {
  200. sinon.assert.calledWithExactly( spy, false, true );
  201. } );
  202. } );
  203. } );
  204. } );
  205. function viewCreator( name ) {
  206. return locale => {
  207. const view = new View( locale );
  208. view.name = name;
  209. view.element = document.createElement( 'a' );
  210. return view;
  211. };
  212. }
  213. class VirtualDecoupledTestEditor extends VirtualTestEditor {
  214. constructor( config ) {
  215. super( config );
  216. const view = new DecoupledEditorUIView( this.locale );
  217. this.ui = new DecoupledEditorUI( this, view );
  218. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  219. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  220. }
  221. destroy() {
  222. this.ui.destroy();
  223. return super.destroy();
  224. }
  225. static create( config ) {
  226. return new Promise( resolve => {
  227. const editor = new this( config );
  228. resolve(
  229. editor.initPlugins()
  230. .then( () => {
  231. editor.ui.init();
  232. editor.fire( 'uiReady' );
  233. editor.fire( 'dataReady' );
  234. editor.fire( 'ready' );
  235. } )
  236. .then( () => editor )
  237. );
  238. } );
  239. }
  240. }