ballooneditorui.js 4.7 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 BalloonEditorUI from '../src/ballooneditorui';
  7. import EditorUI from '@ckeditor/ckeditor5-core/src/editor/editorui';
  8. import BalloonEditorUIView from '../src/ballooneditoruiview';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import BalloonToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/balloon/balloontoolbar';
  11. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  12. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. describe( 'BalloonEditorUI', () => {
  15. let editor, view, ui, viewElement;
  16. testUtils.createSinonSandbox();
  17. beforeEach( () => {
  18. return VirtualBalloonTestEditor
  19. .create( {
  20. plugins: [ BalloonToolbar ]
  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( 'initializes the #view', () => {
  39. expect( view.isRendered ).to.be.true;
  40. } );
  41. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  42. const toolbar = editor.plugins.get( 'BalloonToolbar' );
  43. const toolbarFocusSpy = testUtils.sinon.stub( toolbar.toolbarView, 'focus' ).returns( {} );
  44. const toolbarShowSpy = testUtils.sinon.stub( toolbar, 'show' ).returns( {} );
  45. const toolbarHideSpy = testUtils.sinon.stub( toolbar, 'hide' ).returns( {} );
  46. const editingFocusSpy = testUtils.sinon.stub( editor.editing.view, 'focus' ).returns( {} );
  47. ui.focusTracker.isFocused = true;
  48. // #show and #hide are mocked so mocking the focus as well.
  49. toolbar.toolbarView.focusTracker.isFocused = false;
  50. editor.keystrokes.press( {
  51. keyCode: keyCodes.f10,
  52. altKey: true,
  53. preventDefault: sinon.spy(),
  54. stopPropagation: sinon.spy()
  55. } );
  56. sinon.assert.callOrder( toolbarShowSpy, toolbarFocusSpy );
  57. sinon.assert.notCalled( toolbarHideSpy );
  58. sinon.assert.notCalled( editingFocusSpy );
  59. // #show and #hide are mocked so mocking the focus as well.
  60. toolbar.toolbarView.focusTracker.isFocused = true;
  61. toolbar.toolbarView.keystrokes.press( {
  62. keyCode: keyCodes.esc,
  63. preventDefault: sinon.spy(),
  64. stopPropagation: sinon.spy()
  65. } );
  66. sinon.assert.callOrder( editingFocusSpy, toolbarHideSpy );
  67. } );
  68. describe( 'editable', () => {
  69. let editable;
  70. beforeEach( () => {
  71. editable = editor.editing.view.document.getRoot();
  72. } );
  73. it( 'registers view.editable#element in editor focus tracker', () => {
  74. ui.focusTracker.isFocused = false;
  75. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  76. expect( ui.focusTracker.isFocused ).to.true;
  77. } );
  78. it( 'sets view.editable#name', () => {
  79. expect( view.editable.name ).to.equal( editable.rootName );
  80. } );
  81. it( 'binds view.editable#isFocused', () => {
  82. utils.assertBinding(
  83. view.editable,
  84. { isFocused: false },
  85. [
  86. [ ui.focusTracker, { isFocused: true } ]
  87. ],
  88. { isFocused: true }
  89. );
  90. } );
  91. it( 'binds view.editable#isReadOnly', () => {
  92. utils.assertBinding(
  93. view.editable,
  94. { isReadOnly: false },
  95. [
  96. [ editable, { isReadOnly: true } ]
  97. ],
  98. { isReadOnly: true }
  99. );
  100. } );
  101. } );
  102. } );
  103. describe( 'element()', () => {
  104. it( 'returns correct element instance', () => {
  105. expect( ui.element ).to.equal( viewElement );
  106. } );
  107. } );
  108. describe( 'getEditableElement()', () => {
  109. it( 'returns editable element (default)', () => {
  110. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  111. } );
  112. it( 'returns editable element (root name passed)', () => {
  113. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  114. } );
  115. it( 'returns null if editable with the given name is absent', () => {
  116. expect( ui.getEditableElement( 'absent' ) ).to.null;
  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( 'dataReady' );
  138. editor.fire( 'ready' );
  139. } )
  140. .then( () => editor )
  141. );
  142. } );
  143. }
  144. }