inlineeditorui.js 6.4 KB

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