inlineeditorui.js 8.0 KB

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