inlineeditorui.js 6.3 KB

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