inlineeditorui.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. editor = new ClassicTestEditor( editorElement, {
  22. toolbar: [ 'foo', 'bar' ]
  23. } );
  24. view = new InlineEditorUIView( editor.locale );
  25. ui = new InlineEditorUI( editor, view );
  26. editable = editor.editing.view.getRoot();
  27. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  28. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'sets #editor', () => {
  32. expect( ui.editor ).to.equal( editor );
  33. } );
  34. it( 'sets #view', () => {
  35. expect( ui.view ).to.equal( view );
  36. } );
  37. it( 'creates #componentFactory factory', () => {
  38. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  39. } );
  40. it( 'creates #focusTracker', () => {
  41. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  42. } );
  43. describe( 'panel', () => {
  44. it( 'binds view.panel#isVisible to editor.ui#focusTracker', () => {
  45. ui.focusTracker.isFocused = false;
  46. expect( view.panel.isVisible ).to.be.false;
  47. ui.focusTracker.isFocused = true;
  48. expect( view.panel.isVisible ).to.be.true;
  49. } );
  50. // https://github.com/ckeditor/ckeditor5-editor-inline/issues/4
  51. it( 'pin() is called on editor.editable.view#render', () => {
  52. const spy = sinon.spy( view.panel, 'pin' );
  53. view.panel.hide();
  54. editor.editing.view.fire( 'render' );
  55. sinon.assert.notCalled( spy );
  56. view.panel.show();
  57. editor.editing.view.fire( 'render' );
  58. sinon.assert.calledOnce( spy );
  59. sinon.assert.calledWithExactly( spy, {
  60. target: view.editableElement,
  61. positions: sinon.match.array
  62. } );
  63. } );
  64. } );
  65. describe( 'editable', () => {
  66. it( 'registers view.editable#element in editor focus tracker', () => {
  67. ui.focusTracker.isFocused = false;
  68. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  69. expect( ui.focusTracker.isFocused ).to.true;
  70. } );
  71. it( 'sets view.editable#name', () => {
  72. expect( view.editable.name ).to.equal( editable.rootName );
  73. } );
  74. it( 'binds view.editable#isFocused', () => {
  75. utils.assertBinding(
  76. view.editable,
  77. { isFocused: false },
  78. [
  79. [ ui.focusTracker, { isFocused: true } ]
  80. ],
  81. { isFocused: true }
  82. );
  83. } );
  84. it( 'binds view.editable#isReadOnly', () => {
  85. utils.assertBinding(
  86. view.editable,
  87. { isReadOnly: false },
  88. [
  89. [ editable, { isReadOnly: true } ]
  90. ],
  91. { isReadOnly: true }
  92. );
  93. } );
  94. } );
  95. } );
  96. describe( 'init()', () => {
  97. afterEach( () => {
  98. ui.destroy();
  99. } );
  100. it( 'initializes the #view', () => {
  101. const spy = sinon.spy( view, 'init' );
  102. ui.init();
  103. sinon.assert.calledOnce( spy );
  104. } );
  105. it( 'fills view.toolbar#items with editor config', () => {
  106. const spy = testUtils.sinon.spy( view.toolbar, 'fillFromConfig' );
  107. ui.init();
  108. sinon.assert.calledWithExactly( spy, editor.config.get( 'toolbar' ), ui.componentFactory );
  109. } );
  110. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  111. const spy = testUtils.sinon.spy( view.toolbar, 'focus' );
  112. ui.init();
  113. ui.focusTracker.isFocused = true;
  114. ui.view.toolbar.focusTracker.isFocused = false;
  115. editor.keystrokes.press( {
  116. keyCode: keyCodes.f10,
  117. altKey: true,
  118. preventDefault: sinon.spy(),
  119. stopPropagation: sinon.spy()
  120. } );
  121. sinon.assert.calledOnce( spy );
  122. } );
  123. } );
  124. describe( 'destroy()', () => {
  125. it( 'destroys the #view', () => {
  126. const spy = sinon.spy( view, 'destroy' );
  127. ui.init();
  128. ui.destroy();
  129. sinon.assert.calledOnce( spy );
  130. } );
  131. } );
  132. } );
  133. function viewCreator( name ) {
  134. return locale => {
  135. const view = new View( locale );
  136. view.name = name;
  137. view.element = document.createElement( 'a' );
  138. return view;
  139. };
  140. }