inlineeditorui.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 RootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
  9. import InlineEditorUI from '../src/inlineeditorui';
  10. import InlineEditorUIView from '../src/inlineeditoruiview';
  11. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  12. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  15. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  16. testUtils.createSinonSandbox();
  17. describe( 'InlineEditorUI', () => {
  18. let editor, view, ui;
  19. beforeEach( () => {
  20. return VirtualInlineTestEditor
  21. .create( {
  22. toolbar: [ 'foo', 'bar' ],
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. ui = editor.ui;
  27. view = ui.view;
  28. } );
  29. } );
  30. afterEach( () => {
  31. editor.destroy();
  32. } );
  33. describe( 'constructor()', () => {
  34. it( 'sets #editor', () => {
  35. expect( ui.editor ).to.equal( editor );
  36. } );
  37. it( 'sets #view', () => {
  38. expect( ui.view ).to.equal( view );
  39. } );
  40. it( 'creates #componentFactory factory', () => {
  41. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  42. } );
  43. it( 'creates #focusTracker', () => {
  44. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  45. } );
  46. it( 'creates root editable element', () => {
  47. expect( editor.editing.view.getRoot() ).to.be.instanceOf( RootEditableElement );
  48. } );
  49. } );
  50. describe( 'init()', () => {
  51. it( 'renders the #view', () => {
  52. expect( view.isRendered ).to.be.true;
  53. } );
  54. describe( 'panel', () => {
  55. it( 'binds view.panel#isVisible to editor.ui#focusTracker', () => {
  56. ui.focusTracker.isFocused = false;
  57. expect( view.panel.isVisible ).to.be.false;
  58. ui.focusTracker.isFocused = true;
  59. expect( view.panel.isVisible ).to.be.true;
  60. } );
  61. it( 'doesn\'t set the view#viewportTopOffset, if not specified in the config', () => {
  62. expect( view.viewportTopOffset ).to.equal( 0 );
  63. } );
  64. it( 'sets view#viewportTopOffset, if specified', () => {
  65. return VirtualInlineTestEditor
  66. .create( {
  67. toolbar: {
  68. viewportTopOffset: 100
  69. }
  70. } )
  71. .then( editor => {
  72. const ui = editor.ui;
  73. const view = ui.view;
  74. expect( view.viewportTopOffset ).to.equal( 100 );
  75. return editor.destroy();
  76. } );
  77. } );
  78. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  79. it( 'pin() is called on editor.editable.view#render', () => {
  80. const spy = sinon.stub( view.panel, 'pin' );
  81. view.panel.hide();
  82. editor.editing.view.fire( 'render' );
  83. sinon.assert.notCalled( spy );
  84. view.panel.show();
  85. editor.editing.view.fire( 'render' );
  86. sinon.assert.calledOnce( spy );
  87. sinon.assert.calledWithExactly( spy, {
  88. target: view.editableElement,
  89. positions: sinon.match.array
  90. } );
  91. } );
  92. } );
  93. describe( 'editable', () => {
  94. let editable;
  95. beforeEach( () => {
  96. editable = editor.editing.view.getRoot();
  97. } );
  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. describe( 'view.toolbar#items', () => {
  128. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  129. return VirtualInlineTestEditor
  130. .create( {
  131. toolbar: [ 'foo', 'bar' ]
  132. } )
  133. .then( editor => {
  134. const items = editor.ui.view.toolbar.items;
  135. expect( items.get( 0 ).name ).to.equal( 'foo' );
  136. expect( items.get( 1 ).name ).to.equal( 'bar' );
  137. return editor.destroy();
  138. } );
  139. } );
  140. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  141. return VirtualInlineTestEditor
  142. .create( {
  143. toolbar: {
  144. items: [ 'foo', 'bar' ],
  145. viewportTopOffset: 100
  146. }
  147. } )
  148. .then( editor => {
  149. const items = editor.ui.view.toolbar.items;
  150. expect( items.get( 0 ).name ).to.equal( 'foo' );
  151. expect( items.get( 1 ).name ).to.equal( 'bar' );
  152. return editor.destroy();
  153. } );
  154. } );
  155. } );
  156. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  157. return VirtualInlineTestEditor.create()
  158. .then( editor => {
  159. const ui = editor.ui;
  160. const view = ui.view;
  161. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  162. ui.focusTracker.isFocused = true;
  163. ui.view.toolbar.focusTracker.isFocused = false;
  164. editor.keystrokes.press( {
  165. keyCode: keyCodes.f10,
  166. altKey: true,
  167. preventDefault: sinon.spy(),
  168. stopPropagation: sinon.spy()
  169. } );
  170. sinon.assert.calledOnce( spy );
  171. return editor.destroy();
  172. } );
  173. } );
  174. } );
  175. describe( 'destroy()', () => {
  176. it( 'destroys the #view', () => {
  177. const spy = sinon.spy( view, 'destroy' );
  178. return editor.destroy()
  179. .then( () => {
  180. sinon.assert.calledOnce( spy );
  181. } );
  182. } );
  183. } );
  184. } );
  185. function viewCreator( name ) {
  186. return locale => {
  187. const view = new View( locale );
  188. view.name = name;
  189. view.element = document.createElement( 'a' );
  190. return view;
  191. };
  192. }
  193. class VirtualInlineTestEditor extends VirtualTestEditor {
  194. constructor( config ) {
  195. super( config );
  196. const view = new InlineEditorUIView( this.locale );
  197. this.ui = new InlineEditorUI( this, view );
  198. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  199. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  200. }
  201. destroy() {
  202. this.ui.destroy();
  203. return super.destroy();
  204. }
  205. static create( config ) {
  206. return new Promise( resolve => {
  207. const editor = new this( config );
  208. resolve(
  209. editor.initPlugins()
  210. .then( () => {
  211. editor.ui.init();
  212. editor.fire( 'uiReady' );
  213. editor.fire( 'dataReady' );
  214. editor.fire( 'ready' );
  215. } )
  216. .then( () => editor )
  217. );
  218. } );
  219. }
  220. }