8
0

inlineeditorui.js 6.4 KB

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