inlineeditorui.js 6.5 KB

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