inlineeditorui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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, editable, view, ui;
  18. describe( 'constructor()', () => {
  19. beforeEach( () => {
  20. return VirtualTestEditor
  21. .create( {
  22. toolbar: [ 'foo', 'bar' ]
  23. } )
  24. .then( newEditor => {
  25. initializeUiAndView( newEditor );
  26. ui.init();
  27. } );
  28. } );
  29. afterEach( () => {
  30. ui.destroy();
  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 VirtualTestEditor
  57. .create( {
  58. toolbar: {
  59. viewportTopOffset: 100
  60. }
  61. } )
  62. .then( editor => {
  63. const view = new InlineEditorUIView( editor.locale );
  64. const ui = new InlineEditorUI( editor, view );
  65. ui.init();
  66. expect( view.viewportTopOffset ).to.equal( 100 );
  67. ui.destroy();
  68. return editor.destroy();
  69. } );
  70. } );
  71. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  72. it( 'pin() is called on editor.editable.view#render', () => {
  73. const spy = sinon.stub( view.panel, 'pin' );
  74. view.panel.hide();
  75. editor.editing.view.fire( 'render' );
  76. sinon.assert.notCalled( spy );
  77. view.panel.show();
  78. editor.editing.view.fire( 'render' );
  79. sinon.assert.calledOnce( spy );
  80. sinon.assert.calledWithExactly( spy, {
  81. target: view.editableElement,
  82. positions: sinon.match.array
  83. } );
  84. } );
  85. } );
  86. describe( 'editable', () => {
  87. it( 'registers view.editable#element in editor focus tracker', () => {
  88. ui.focusTracker.isFocused = false;
  89. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  90. expect( ui.focusTracker.isFocused ).to.true;
  91. } );
  92. it( 'sets view.editable#name', () => {
  93. expect( view.editable.name ).to.equal( editable.rootName );
  94. } );
  95. it( 'binds view.editable#isFocused', () => {
  96. utils.assertBinding(
  97. view.editable,
  98. { isFocused: false },
  99. [
  100. [ ui.focusTracker, { isFocused: true } ]
  101. ],
  102. { isFocused: true }
  103. );
  104. } );
  105. it( 'binds view.editable#isReadOnly', () => {
  106. utils.assertBinding(
  107. view.editable,
  108. { isReadOnly: false },
  109. [
  110. [ editable, { isReadOnly: true } ]
  111. ],
  112. { isReadOnly: true }
  113. );
  114. } );
  115. } );
  116. } );
  117. describe( 'init()', () => {
  118. beforeEach( () => {
  119. return VirtualTestEditor
  120. .create( {
  121. toolbar: [ 'foo', 'bar' ]
  122. } )
  123. .then( newEditor => {
  124. initializeUiAndView( newEditor );
  125. } );
  126. } );
  127. afterEach( () => {
  128. ui.destroy();
  129. editor.destroy();
  130. } );
  131. it( 'initializes the #view', () => {
  132. const spy = sinon.spy( view, 'init' );
  133. ui.init();
  134. sinon.assert.calledOnce( spy );
  135. } );
  136. describe( 'view.toolbar#items', () => {
  137. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  138. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  139. ui.init();
  140. sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
  141. } );
  142. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  143. return VirtualTestEditor
  144. .create( {
  145. toolbar: {
  146. items: [ 'foo', 'bar' ],
  147. viewportTopOffset: 100
  148. }
  149. } )
  150. .then( newEditor => {
  151. initializeUiAndView( newEditor );
  152. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  153. ui.init();
  154. sinon.assert.calledWithExactly( spy,
  155. editor.config.get( 'toolbar.items' ),
  156. ui.componentFactory
  157. );
  158. ui.destroy();
  159. return editor.destroy();
  160. } );
  161. } );
  162. } );
  163. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  164. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  165. ui.init();
  166. ui.focusTracker.isFocused = true;
  167. ui.view.toolbar.focusTracker.isFocused = false;
  168. editor.keystrokes.press( {
  169. keyCode: keyCodes.f10,
  170. altKey: true,
  171. preventDefault: sinon.spy(),
  172. stopPropagation: sinon.spy()
  173. } );
  174. sinon.assert.calledOnce( spy );
  175. } );
  176. } );
  177. describe( 'destroy()', () => {
  178. it( 'destroys the #view', () => {
  179. return VirtualTestEditor.create()
  180. .then( editor => {
  181. initializeUiAndView( editor );
  182. const spy = sinon.spy( view, 'destroy' );
  183. ui.init();
  184. ui.destroy();
  185. sinon.assert.calledOnce( spy );
  186. return editor.destroy();
  187. } );
  188. } );
  189. } );
  190. function initializeUiAndView( newEditor ) {
  191. editor = newEditor;
  192. view = new InlineEditorUIView( editor.locale );
  193. ui = new InlineEditorUI( editor, view );
  194. editable = editor.editing.view.getRoot();
  195. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  196. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  197. }
  198. } );
  199. function viewCreator( name ) {
  200. return locale => {
  201. const view = new View( locale );
  202. view.name = name;
  203. view.element = document.createElement( 'a' );
  204. return view;
  205. };
  206. }