inlineeditorui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import View from '@ckeditor/ckeditor5-ui/src/view';
  7. import InlineEditorUI from '../src/inlineeditorui';
  8. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  9. import InlineEditorUIView from '../src/inlineeditoruiview';
  10. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  13. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. describe( 'InlineEditorUI', () => {
  15. let editor, view, ui;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. return VirtualInlineTestEditor
  19. .create( {
  20. toolbar: [ 'foo', 'bar' ],
  21. } )
  22. .then( newEditor => {
  23. editor = newEditor;
  24. ui = editor.ui;
  25. view = ui.view;
  26. } );
  27. } );
  28. afterEach( () => {
  29. editor.destroy();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'extends EditorUI', () => {
  33. expect( ui ).to.instanceof( EditorUI );
  34. } );
  35. } );
  36. describe( 'init()', () => {
  37. it( 'renders the #view', () => {
  38. expect( view.isRendered ).to.be.true;
  39. } );
  40. describe( 'panel', () => {
  41. it( 'binds view.panel#isVisible to editor.ui#focusTracker', () => {
  42. ui.focusTracker.isFocused = false;
  43. expect( view.panel.isVisible ).to.be.false;
  44. ui.focusTracker.isFocused = true;
  45. expect( view.panel.isVisible ).to.be.true;
  46. } );
  47. it( 'doesn\'t set the view#viewportTopOffset, if not specified in the config', () => {
  48. expect( view.viewportTopOffset ).to.equal( 0 );
  49. } );
  50. it( 'sets view#viewportTopOffset, if specified', () => {
  51. return VirtualInlineTestEditor
  52. .create( {
  53. toolbar: {
  54. viewportTopOffset: 100
  55. }
  56. } )
  57. .then( editor => {
  58. const ui = editor.ui;
  59. const view = ui.view;
  60. expect( view.viewportTopOffset ).to.equal( 100 );
  61. return editor.destroy();
  62. } );
  63. } );
  64. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  65. it( 'pin() is called on editor.ui#update', () => {
  66. const spy = sinon.stub( view.panel, 'pin' );
  67. view.panel.hide();
  68. editor.ui.fire( 'update' );
  69. sinon.assert.notCalled( spy );
  70. view.panel.show();
  71. editor.ui.fire( 'update' );
  72. sinon.assert.calledOnce( spy );
  73. sinon.assert.calledWithExactly( spy, {
  74. target: view.editable.editableElement,
  75. positions: sinon.match.array
  76. } );
  77. } );
  78. } );
  79. describe( 'editable', () => {
  80. let editable;
  81. beforeEach( () => {
  82. editable = editor.editing.view.document.getRoot();
  83. } );
  84. it( 'registers view.editable#element in editor focus tracker', () => {
  85. ui.focusTracker.isFocused = false;
  86. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  87. expect( ui.focusTracker.isFocused ).to.true;
  88. } );
  89. it( 'sets view.editable#name', () => {
  90. expect( view.editable.name ).to.equal( editable.rootName );
  91. } );
  92. it( 'binds view.editable#isFocused', () => {
  93. utils.assertBinding(
  94. view.editable,
  95. { isFocused: false },
  96. [
  97. [ ui.focusTracker, { isFocused: true } ]
  98. ],
  99. { isFocused: true }
  100. );
  101. } );
  102. it( 'binds view.editable#isReadOnly', () => {
  103. utils.assertBinding(
  104. view.editable,
  105. { isReadOnly: false },
  106. [
  107. [ editable, { isReadOnly: true } ]
  108. ],
  109. { isReadOnly: true }
  110. );
  111. } );
  112. } );
  113. describe( 'view.toolbar#items', () => {
  114. it( 'are filled with the config.toolbar (specified as an Array)', () => {
  115. return VirtualInlineTestEditor
  116. .create( {
  117. toolbar: [ 'foo', 'bar' ]
  118. } )
  119. .then( editor => {
  120. const items = editor.ui.view.toolbar.items;
  121. expect( items.get( 0 ).name ).to.equal( 'foo' );
  122. expect( items.get( 1 ).name ).to.equal( 'bar' );
  123. return editor.destroy();
  124. } );
  125. } );
  126. it( 'are filled with the config.toolbar (specified as an Object)', () => {
  127. return VirtualInlineTestEditor
  128. .create( {
  129. toolbar: {
  130. items: [ 'foo', 'bar' ],
  131. viewportTopOffset: 100
  132. }
  133. } )
  134. .then( editor => {
  135. const items = editor.ui.view.toolbar.items;
  136. expect( items.get( 0 ).name ).to.equal( 'foo' );
  137. expect( items.get( 1 ).name ).to.equal( 'bar' );
  138. return editor.destroy();
  139. } );
  140. } );
  141. } );
  142. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  143. return VirtualInlineTestEditor.create()
  144. .then( editor => {
  145. const ui = editor.ui;
  146. const view = ui.view;
  147. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  148. ui.focusTracker.isFocused = true;
  149. ui.view.toolbar.focusTracker.isFocused = false;
  150. editor.keystrokes.press( {
  151. keyCode: keyCodes.f10,
  152. altKey: true,
  153. preventDefault: sinon.spy(),
  154. stopPropagation: sinon.spy()
  155. } );
  156. sinon.assert.calledOnce( spy );
  157. return editor.destroy();
  158. } );
  159. } );
  160. } );
  161. describe( 'getEditableElement()', () => {
  162. it( 'returns editable element (default)', () => {
  163. expect( ui.getEditableElement() ).to.equal( view.editable );
  164. } );
  165. it( 'returns editable element (root name passed)', () => {
  166. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable );
  167. } );
  168. it( 'returns null if editable with the given name is absent', () => {
  169. expect( ui.getEditableElement( 'absent' ) ).to.null;
  170. } );
  171. } );
  172. } );
  173. function viewCreator( name ) {
  174. return locale => {
  175. const view = new View( locale );
  176. view.name = name;
  177. view.element = document.createElement( 'a' );
  178. return view;
  179. };
  180. }
  181. class VirtualInlineTestEditor extends VirtualTestEditor {
  182. constructor( config ) {
  183. super( config );
  184. const view = new InlineEditorUIView( this.locale );
  185. this.ui = new InlineEditorUI( this, view );
  186. this.ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  187. this.ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  188. }
  189. destroy() {
  190. this.ui.destroy();
  191. return super.destroy();
  192. }
  193. static create( config ) {
  194. return new Promise( resolve => {
  195. const editor = new this( config );
  196. resolve(
  197. editor.initPlugins()
  198. .then( () => {
  199. editor.ui.init();
  200. editor.ui.ready();
  201. editor.fire( 'uiReady' );
  202. editor.fire( 'dataReady' );
  203. editor.fire( 'ready' );
  204. } )
  205. .then( () => editor )
  206. );
  207. } );
  208. }
  209. }