inlineeditorui.js 6.4 KB

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