ballooneditorui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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;
  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. } );
  27. } );
  28. afterEach( () => {
  29. editor.destroy();
  30. } );
  31. describe( 'constructor()', () => {
  32. it( 'extends EditorUI', () => {
  33. expect( ui ).to.instanceof( EditorUI );
  34. } );
  35. } );
  36. describe( 'init()', () => {
  37. it( 'initializes the #view', () => {
  38. expect( view.isRendered ).to.be.true;
  39. } );
  40. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  41. const toolbar = editor.plugins.get( 'BalloonToolbar' );
  42. const toolbarFocusSpy = testUtils.sinon.stub( toolbar.toolbarView, 'focus' ).returns( {} );
  43. const toolbarShowSpy = testUtils.sinon.stub( toolbar, 'show' ).returns( {} );
  44. const toolbarHideSpy = testUtils.sinon.stub( toolbar, 'hide' ).returns( {} );
  45. const editingFocusSpy = testUtils.sinon.stub( editor.editing.view, 'focus' ).returns( {} );
  46. ui.focusTracker.isFocused = true;
  47. // #show and #hide are mocked so mocking the focus as well.
  48. toolbar.toolbarView.focusTracker.isFocused = false;
  49. editor.keystrokes.press( {
  50. keyCode: keyCodes.f10,
  51. altKey: true,
  52. preventDefault: sinon.spy(),
  53. stopPropagation: sinon.spy()
  54. } );
  55. sinon.assert.callOrder( toolbarShowSpy, toolbarFocusSpy );
  56. sinon.assert.notCalled( toolbarHideSpy );
  57. sinon.assert.notCalled( editingFocusSpy );
  58. // #show and #hide are mocked so mocking the focus as well.
  59. toolbar.toolbarView.focusTracker.isFocused = true;
  60. toolbar.toolbarView.keystrokes.press( {
  61. keyCode: keyCodes.esc,
  62. preventDefault: sinon.spy(),
  63. stopPropagation: sinon.spy()
  64. } );
  65. sinon.assert.callOrder( editingFocusSpy, toolbarHideSpy );
  66. } );
  67. describe( 'editable', () => {
  68. let editable;
  69. beforeEach( () => {
  70. editable = editor.editing.view.document.getRoot();
  71. } );
  72. it( 'registers view.editable#element in editor focus tracker', () => {
  73. ui.focusTracker.isFocused = false;
  74. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  75. expect( ui.focusTracker.isFocused ).to.true;
  76. } );
  77. it( 'sets view.editable#name', () => {
  78. expect( view.editable.name ).to.equal( editable.rootName );
  79. } );
  80. it( 'binds view.editable#isFocused', () => {
  81. utils.assertBinding(
  82. view.editable,
  83. { isFocused: false },
  84. [
  85. [ ui.focusTracker, { isFocused: true } ]
  86. ],
  87. { isFocused: true }
  88. );
  89. } );
  90. it( 'binds view.editable#isReadOnly', () => {
  91. utils.assertBinding(
  92. view.editable,
  93. { isReadOnly: false },
  94. [
  95. [ editable, { isReadOnly: true } ]
  96. ],
  97. { isReadOnly: true }
  98. );
  99. } );
  100. } );
  101. } );
  102. describe( 'getEditableElement()', () => {
  103. it( 'returns editable element (default)', () => {
  104. expect( ui.getEditableElement() ).to.equal( view.editable.element );
  105. } );
  106. it( 'returns editable element (root name passed)', () => {
  107. expect( ui.getEditableElement( 'main' ) ).to.equal( view.editable.element );
  108. } );
  109. it( 'returns null if editable with the given name is absent', () => {
  110. expect( ui.getEditableElement( 'absent' ) ).to.null;
  111. } );
  112. } );
  113. } );
  114. class VirtualBalloonTestEditor extends VirtualTestEditor {
  115. constructor( config ) {
  116. super( config );
  117. const view = new BalloonEditorUIView( this.locale );
  118. this.ui = new BalloonEditorUI( this, view );
  119. }
  120. destroy() {
  121. this.ui.destroy();
  122. return super.destroy();
  123. }
  124. static create( config ) {
  125. return new Promise( resolve => {
  126. const editor = new this( config );
  127. resolve(
  128. editor.initPlugins()
  129. .then( () => {
  130. editor.ui.init();
  131. editor.ui.ready();
  132. editor.fire( 'uiReady' );
  133. editor.fire( 'dataReady' );
  134. editor.fire( 'ready' );
  135. } )
  136. .then( () => editor )
  137. );
  138. } );
  139. }
  140. }