8
0

ballooneditorui.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @license Copyright (c) 2003-2017, 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 RootEditableElement from '@ckeditor/ckeditor5-engine/src/view/rooteditableelement';
  13. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  14. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. describe( 'BalloonEditorUI', () => {
  16. let editor, view, ui;
  17. beforeEach( () => {
  18. return VirtualBalloonTestEditor
  19. .create( {
  20. plugins: [ ContextualToolbar ]
  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( 'sets #editor', () => {
  33. expect( ui.editor ).to.equal( editor );
  34. } );
  35. it( 'sets #view', () => {
  36. expect( ui.view ).to.be.instanceOf( BalloonEditorUIView );
  37. } );
  38. it( 'creates #componentFactory factory', () => {
  39. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  40. } );
  41. it( 'creates #focusTracker', () => {
  42. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  43. } );
  44. it( 'creates root editable element', () => {
  45. expect( editor.editing.view.getRoot() ).to.be.instanceOf( RootEditableElement );
  46. } );
  47. } );
  48. describe( 'init()', () => {
  49. it( 'initializes the #view', () => {
  50. expect( view.isRendered ).to.be.true;
  51. } );
  52. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  53. const toolbar = editor.plugins.get( 'ContextualToolbar' );
  54. const toolbarFocusSpy = sinon.stub( toolbar.toolbarView, 'focus' ).returns( {} );
  55. const toolbarShowSpy = sinon.stub( toolbar, 'show' ).returns( {} );
  56. const toolbarHideSpy = sinon.stub( toolbar, 'hide' ).returns( {} );
  57. const editingFocusSpy = sinon.stub( editor.editing.view, 'focus' ).returns( {} );
  58. ui.focusTracker.isFocused = true;
  59. // #show and #hide are mocked so mocking the focus as well.
  60. toolbar.toolbarView.focusTracker.isFocused = false;
  61. editor.keystrokes.press( {
  62. keyCode: keyCodes.f10,
  63. altKey: true,
  64. preventDefault: sinon.spy(),
  65. stopPropagation: sinon.spy()
  66. } );
  67. sinon.assert.callOrder( toolbarShowSpy, toolbarFocusSpy );
  68. sinon.assert.notCalled( toolbarHideSpy );
  69. sinon.assert.notCalled( editingFocusSpy );
  70. // #show and #hide are mocked so mocking the focus as well.
  71. toolbar.toolbarView.focusTracker.isFocused = true;
  72. toolbar.toolbarView.keystrokes.press( {
  73. keyCode: keyCodes.esc,
  74. preventDefault: sinon.spy(),
  75. stopPropagation: sinon.spy()
  76. } );
  77. sinon.assert.callOrder( editingFocusSpy, toolbarHideSpy );
  78. } );
  79. describe( 'editable', () => {
  80. let editable;
  81. beforeEach( () => {
  82. editable = editor.editing.view.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. } );
  114. describe( 'destroy()', () => {
  115. it( 'destroys the #view', () => {
  116. const spy = sinon.spy( view, 'destroy' );
  117. return editor.destroy()
  118. .then( () => {
  119. sinon.assert.calledOnce( spy );
  120. } );
  121. } );
  122. } );
  123. } );
  124. class VirtualBalloonTestEditor extends VirtualTestEditor {
  125. constructor( config ) {
  126. super( config );
  127. const view = new BalloonEditorUIView( this.locale );
  128. this.ui = new BalloonEditorUI( this, view );
  129. }
  130. destroy() {
  131. this.ui.destroy();
  132. return super.destroy();
  133. }
  134. static create( config ) {
  135. return new Promise( resolve => {
  136. const editor = new this( config );
  137. resolve(
  138. editor.initPlugins()
  139. .then( () => {
  140. editor.ui.init();
  141. editor.fire( 'uiReady' );
  142. editor.fire( 'dataReady' );
  143. editor.fire( 'ready' );
  144. } )
  145. .then( () => editor )
  146. );
  147. } );
  148. }
  149. }