inlineeditorui.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 InlineEditorUI from '../src/inlineeditorui';
  9. import InlineEditorUIView from '../src/inlineeditoruiview';
  10. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  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( 'InlineEditorUI', () => {
  17. let editor, view, ui;
  18. describe( 'constructor()', () => {
  19. beforeEach( () => {
  20. return VirtualInlineTestEditor
  21. .create( {
  22. toolbar: [ 'foo', 'bar' ],
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. ui = editor.ui;
  27. view = ui.view;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editor.destroy();
  32. } );
  33. it( 'sets #editor', () => {
  34. expect( ui.editor ).to.equal( editor );
  35. } );
  36. it( 'sets #view', () => {
  37. expect( ui.view ).to.equal( view );
  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. describe( 'panel', () => {
  46. it( 'binds view.panel#isVisible to editor.ui#focusTracker', () => {
  47. ui.focusTracker.isFocused = false;
  48. expect( view.panel.isVisible ).to.be.false;
  49. ui.focusTracker.isFocused = true;
  50. expect( view.panel.isVisible ).to.be.true;
  51. } );
  52. it( 'doesn\'t set the view#viewportTopOffset, if not specified in the config', () => {
  53. expect( view.viewportTopOffset ).to.equal( 0 );
  54. } );
  55. it( 'sets view#viewportTopOffset, if specified', () => {
  56. return VirtualInlineTestEditor
  57. .create( {
  58. toolbar: {
  59. viewportTopOffset: 100
  60. }
  61. } )
  62. .then( editor => {
  63. const ui = editor.ui;
  64. const view = ui.view;
  65. expect( view.viewportTopOffset ).to.equal( 100 );
  66. return editor.destroy();
  67. } );
  68. } );
  69. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  70. it( 'pin() is called on editor.editable.view#render', () => {
  71. const spy = sinon.stub( view.panel, 'pin' );
  72. view.panel.hide();
  73. editor.editing.view.fire( 'render' );
  74. sinon.assert.notCalled( spy );
  75. view.panel.show();
  76. editor.editing.view.fire( 'render' );
  77. sinon.assert.calledOnce( spy );
  78. sinon.assert.calledWithExactly( spy, {
  79. target: view.editableElement,
  80. positions: sinon.match.array
  81. } );
  82. } );
  83. } );
  84. describe( 'editable', () => {
  85. let editable;
  86. beforeEach( () => {
  87. editable = editor.editing.view.getRoot();
  88. } );
  89. it( 'registers view.editable#element in editor focus tracker', () => {
  90. ui.focusTracker.isFocused = false;
  91. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  92. expect( ui.focusTracker.isFocused ).to.true;
  93. } );
  94. it( 'sets view.editable#name', () => {
  95. expect( view.editable.name ).to.equal( editable.rootName );
  96. } );
  97. it( 'binds view.editable#isFocused', () => {
  98. utils.assertBinding(
  99. view.editable,
  100. { isFocused: false },
  101. [
  102. [ ui.focusTracker, { isFocused: true } ]
  103. ],
  104. { isFocused: true }
  105. );
  106. } );
  107. it( 'binds view.editable#isReadOnly', () => {
  108. utils.assertBinding(
  109. view.editable,
  110. { isReadOnly: false },
  111. [
  112. [ editable, { isReadOnly: true } ]
  113. ],
  114. { isReadOnly: true }
  115. );
  116. } );
  117. } );
  118. } );
  119. describe( 'init()', () => {
  120. beforeEach( () => {
  121. return VirtualInlineTestEditor
  122. .create( {
  123. toolbar: [ 'foo', 'bar' ],
  124. } )
  125. .then( newEditor => {
  126. editor = newEditor;
  127. ui = editor.ui;
  128. view = ui.view;
  129. } );
  130. } );
  131. afterEach( () => {
  132. ui.destroy();
  133. editor.destroy();
  134. } );
  135. it( 'renders the #view', () => {
  136. expect( view.isRendered ).to.be.true;
  137. } );
  138. describe( 'view.toolbar#items', () => {
  139. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  140. return VirtualInlineTestEditor
  141. .create( {
  142. toolbar: [ 'foo', 'bar' ]
  143. } )
  144. .then( editor => {
  145. const items = editor.ui.view.toolbar.items;
  146. expect( items.get( 0 ).name ).to.equal( 'foo' );
  147. expect( items.get( 1 ).name ).to.equal( 'bar' );
  148. return editor.destroy();
  149. } );
  150. } );
  151. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  152. return VirtualInlineTestEditor
  153. .create( {
  154. toolbar: {
  155. items: [ 'foo', 'bar' ],
  156. viewportTopOffset: 100
  157. }
  158. } )
  159. .then( editor => {
  160. const items = editor.ui.view.toolbar.items;
  161. expect( items.get( 0 ).name ).to.equal( 'foo' );
  162. expect( items.get( 1 ).name ).to.equal( 'bar' );
  163. return editor.destroy();
  164. } );
  165. } );
  166. } );
  167. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  168. return VirtualInlineTestEditor.create()
  169. .then( editor => {
  170. const ui = editor.ui;
  171. const view = ui.view;
  172. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  173. ui.focusTracker.isFocused = true;
  174. ui.view.toolbar.focusTracker.isFocused = false;
  175. editor.keystrokes.press( {
  176. keyCode: keyCodes.f10,
  177. altKey: true,
  178. preventDefault: sinon.spy(),
  179. stopPropagation: sinon.spy()
  180. } );
  181. sinon.assert.calledOnce( spy );
  182. return editor.destroy();
  183. } );
  184. } );
  185. } );
  186. describe( 'destroy()', () => {
  187. it( 'destroys the #view', () => {
  188. const spy = sinon.spy( view, 'destroy' );
  189. return editor.destroy()
  190. .then( () => {
  191. sinon.assert.calledOnce( spy );
  192. } );
  193. } );
  194. } );
  195. } );
  196. function viewCreator( name ) {
  197. return locale => {
  198. const view = new View( locale );
  199. view.name = name;
  200. view.element = document.createElement( 'a' );
  201. return view;
  202. };
  203. }
  204. class VirtualInlineTestEditor extends VirtualTestEditor {
  205. constructor( config ) {
  206. super( config );
  207. const view = new InlineEditorUIView( this.locale );
  208. this.ui = new InlineEditorUI( this, view );
  209. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  210. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  211. }
  212. destroy() {
  213. this.ui.destroy();
  214. return super.destroy();
  215. }
  216. static create( config ) {
  217. return new Promise( resolve => {
  218. const editor = new this( config );
  219. resolve(
  220. editor.initPlugins()
  221. .then( () => {
  222. editor.ui.init();
  223. editor.fire( 'uiReady' );
  224. editor.fire( 'dataReady' );
  225. editor.fire( 'ready' );
  226. } )
  227. .then( () => editor )
  228. );
  229. } );
  230. }
  231. }