ballooneditorui.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals Event */
  6. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  7. import BalloonEditorUI from '../src/ballooneditorui';
  8. import BalloonEditorUIView from '../src/ballooneditoruiview';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  11. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. describe( 'BalloonEditorUI', () => {
  15. let editor, view, ui;
  16. beforeEach( () => {
  17. return VirtualBalloonTestEditor
  18. .create( {
  19. plugins: [ ContextualToolbar ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. ui = editor.ui;
  24. view = ui.view;
  25. } );
  26. } );
  27. afterEach( () => {
  28. editor.destroy();
  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.be.instanceOf( BalloonEditorUIView );
  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. } );
  44. describe( 'init()', () => {
  45. it( 'initializes the #view', () => {
  46. expect( view.isRendered ).to.be.true;
  47. } );
  48. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  49. const toolbar = editor.plugins.get( 'ContextualToolbar' );
  50. const toolbarFocusSpy = sinon.stub( toolbar.toolbarView, 'focus' ).returns( {} );
  51. const toolbarShowSpy = sinon.stub( toolbar, 'show' ).returns( {} );
  52. const toolbarHideSpy = sinon.stub( toolbar, 'hide' ).returns( {} );
  53. const editingFocusSpy = sinon.stub( editor.editing.view, 'focus' ).returns( {} );
  54. ui.focusTracker.isFocused = true;
  55. // #show and #hide are mocked so mocking the focus as well.
  56. toolbar.toolbarView.focusTracker.isFocused = false;
  57. editor.keystrokes.press( {
  58. keyCode: keyCodes.f10,
  59. altKey: true,
  60. preventDefault: sinon.spy(),
  61. stopPropagation: sinon.spy()
  62. } );
  63. sinon.assert.callOrder( toolbarShowSpy, toolbarFocusSpy );
  64. sinon.assert.notCalled( toolbarHideSpy );
  65. sinon.assert.notCalled( editingFocusSpy );
  66. // #show and #hide are mocked so mocking the focus as well.
  67. toolbar.toolbarView.focusTracker.isFocused = true;
  68. toolbar.toolbarView.keystrokes.press( {
  69. keyCode: keyCodes.esc,
  70. preventDefault: sinon.spy(),
  71. stopPropagation: sinon.spy()
  72. } );
  73. sinon.assert.callOrder( editingFocusSpy, toolbarHideSpy );
  74. } );
  75. describe( 'editable', () => {
  76. let editable;
  77. beforeEach( () => {
  78. editable = editor.editing.view.getRoot();
  79. } );
  80. it( 'registers view.editable#element in editor focus tracker', () => {
  81. ui.focusTracker.isFocused = false;
  82. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  83. expect( ui.focusTracker.isFocused ).to.true;
  84. } );
  85. it( 'sets view.editable#name', () => {
  86. expect( view.editable.name ).to.equal( editable.rootName );
  87. } );
  88. it( 'binds view.editable#isFocused', () => {
  89. utils.assertBinding(
  90. view.editable,
  91. { isFocused: false },
  92. [
  93. [ ui.focusTracker, { isFocused: true } ]
  94. ],
  95. { isFocused: true }
  96. );
  97. } );
  98. it( 'binds view.editable#isReadOnly', () => {
  99. utils.assertBinding(
  100. view.editable,
  101. { isReadOnly: false },
  102. [
  103. [ editable, { isReadOnly: true } ]
  104. ],
  105. { isReadOnly: true }
  106. );
  107. } );
  108. } );
  109. } );
  110. describe( 'destroy()', () => {
  111. it( 'destroys the #view', () => {
  112. const spy = sinon.spy( view, 'destroy' );
  113. return editor.destroy()
  114. .then( () => {
  115. sinon.assert.calledOnce( spy );
  116. } );
  117. } );
  118. } );
  119. } );
  120. class VirtualBalloonTestEditor extends VirtualTestEditor {
  121. constructor( config ) {
  122. super( config );
  123. const view = new BalloonEditorUIView( this.locale );
  124. this.ui = new BalloonEditorUI( this, view );
  125. }
  126. destroy() {
  127. this.ui.destroy();
  128. return super.destroy();
  129. }
  130. static create( config ) {
  131. return new Promise( resolve => {
  132. const editor = new this( config );
  133. resolve(
  134. editor.initPlugins()
  135. .then( () => {
  136. editor.ui.init();
  137. editor.fire( 'uiReady' );
  138. editor.fire( 'dataReady' );
  139. editor.fire( 'ready' );
  140. } )
  141. .then( () => editor )
  142. );
  143. } );
  144. }
  145. }