imagetextalternativeui.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* global Event, document */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  7. import Image from '../../src/image';
  8. import ImageTextAlternativeEditing from '../../src/imagetextalternative/imagetextalternativeediting';
  9. import ImageTextAlternativeUI from '../../src/imagetextalternative/imagetextalternativeui';
  10. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  11. import View from '@ckeditor/ckeditor5-ui/src/view';
  12. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  13. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  14. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  15. describe( 'ImageTextAlternativeUI', () => {
  16. let editor, model, doc, plugin, command, form, balloon, editorElement, button;
  17. beforeEach( () => {
  18. editorElement = global.document.createElement( 'div' );
  19. global.document.body.appendChild( editorElement );
  20. return ClassicTestEditor
  21. .create( editorElement, {
  22. plugins: [ ImageTextAlternativeEditing, ImageTextAlternativeUI, Image ]
  23. } )
  24. .then( newEditor => {
  25. editor = newEditor;
  26. model = editor.model;
  27. doc = model.document;
  28. newEditor.editing.view.attachDomRoot( editorElement );
  29. plugin = editor.plugins.get( ImageTextAlternativeUI );
  30. command = editor.commands.get( 'imageTextAlternative' );
  31. form = plugin._form;
  32. balloon = editor.plugins.get( 'ContextualBalloon' );
  33. button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  34. } );
  35. } );
  36. afterEach( () => {
  37. editorElement.remove();
  38. return editor.destroy();
  39. } );
  40. it( 'should be named', () => {
  41. expect( ImageTextAlternativeUI.pluginName ).to.equal( 'ImageTextAlternativeUI' );
  42. } );
  43. describe( 'toolbar button', () => {
  44. it( 'should be registered in component factory', () => {
  45. expect( button ).to.be.instanceOf( ButtonView );
  46. } );
  47. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  48. command.isEnabled = true;
  49. expect( button.isEnabled ).to.be.true;
  50. command.isEnabled = false;
  51. expect( button.isEnabled ).to.be.false;
  52. } );
  53. it( 'should show balloon panel on execute', () => {
  54. expect( balloon.visibleView ).to.be.null;
  55. setData( model, '[<image src="" alt="foo bar"></image>]' );
  56. button.fire( 'execute' );
  57. expect( balloon.visibleView ).to.equal( form );
  58. // Make sure successive execute does not throw, e.g. attempting
  59. // to display the form twice.
  60. button.fire( 'execute' );
  61. expect( balloon.visibleView ).to.equal( form );
  62. } );
  63. it( 'should set alt attribute value to textarea and select it', () => {
  64. const spy = sinon.spy( form.labeledInput, 'select' );
  65. setData( model, '[<image src="" alt="foo bar"></image>]' );
  66. button.fire( 'execute' );
  67. sinon.assert.calledOnce( spy );
  68. expect( form.labeledInput.value ).equals( 'foo bar' );
  69. } );
  70. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  71. const spy = sinon.spy( form.labeledInput, 'select' );
  72. setData( model, '[<image src=""></image>]' );
  73. button.fire( 'execute' );
  74. sinon.assert.calledOnce( spy );
  75. expect( form.labeledInput.value ).equals( '' );
  76. } );
  77. } );
  78. describe( 'balloon panel form', () => {
  79. // https://github.com/ckeditor/ckeditor5-image/issues/114
  80. it( 'should make sure the input always stays in sync with the value of the command', () => {
  81. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  82. // Mock the value of the input after some past editing.
  83. form.labeledInput.value = 'foo';
  84. // Mock the user using the form, changing the value but clicking "Cancel".
  85. // so the command's value is not updated.
  86. form.labeledInput.inputView.element.value = 'This value was canceled.';
  87. // Mock the user editing the same image once again.
  88. setData( model, '[<image src="" alt="foo"></image>]' );
  89. button.fire( 'execute' );
  90. expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
  91. } );
  92. it( 'should execute command on submit', () => {
  93. const spy = sinon.spy( editor, 'execute' );
  94. form.fire( 'submit' );
  95. sinon.assert.calledOnce( spy );
  96. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  97. newValue: form.labeledInput.inputView.element.value
  98. } );
  99. } );
  100. it( 'should hide the panel on cancel and focus the editing view', () => {
  101. const spy = sinon.spy( editor.editing.view, 'focus' );
  102. setData( model, '[<image src="" alt="foo bar"></image>]' );
  103. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  104. expect( balloon.visibleView ).to.equal( form );
  105. form.fire( 'cancel' );
  106. expect( balloon.visibleView ).to.be.null;
  107. sinon.assert.calledOnce( spy );
  108. } );
  109. it( 'should not engage when the form is in the balloon yet invisible', () => {
  110. setData( model, '[<image src=""></image>]' );
  111. button.fire( 'execute' );
  112. expect( balloon.visibleView ).to.equal( form );
  113. const lastView = new View();
  114. lastView.element = document.createElement( 'div' );
  115. balloon.add( {
  116. view: lastView,
  117. position: {
  118. target: document.body
  119. }
  120. } );
  121. expect( balloon.visibleView ).to.equal( lastView );
  122. button.fire( 'execute' );
  123. expect( balloon.visibleView ).to.equal( lastView );
  124. } );
  125. // https://github.com/ckeditor/ckeditor5/issues/1501
  126. it( 'should blur url input element before hiding the view', () => {
  127. setData( model, '[<image src="" alt="foo bar"></image>]' );
  128. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  129. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  130. const buttonFocusSpy = sinon.spy( form.saveButtonView, 'focus' );
  131. form.fire( 'submit' );
  132. expect( buttonFocusSpy.calledBefore( editableFocusSpy ) ).to.equal( true );
  133. } );
  134. describe( 'integration with the editor selection (ui#update event)', () => {
  135. it( 'should re-position the form', () => {
  136. setData( model, '[<image src=""></image>]' );
  137. button.fire( 'execute' );
  138. const spy = sinon.spy( balloon, 'updatePosition' );
  139. editor.ui.fire( 'update' );
  140. sinon.assert.calledOnce( spy );
  141. } );
  142. it( 'should hide the form and focus editable when image widget has been removed by external change', () => {
  143. setData( model, '[<image src=""></image>]' );
  144. button.fire( 'execute' );
  145. const removeSpy = sinon.spy( balloon, 'remove' );
  146. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  147. model.enqueueChange( 'transparent', writer => {
  148. writer.remove( doc.selection.getFirstRange() );
  149. } );
  150. sinon.assert.calledWithExactly( removeSpy, form );
  151. sinon.assert.calledOnce( focusSpy );
  152. } );
  153. } );
  154. describe( 'close listeners', () => {
  155. describe( 'keyboard', () => {
  156. it( 'should close upon Esc key press and focus the editing view', () => {
  157. const hideSpy = sinon.spy( plugin, '_hideForm' );
  158. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  159. setData( model, '[<image src=""></image>]' );
  160. button.fire( 'execute' );
  161. const keyEvtData = {
  162. keyCode: keyCodes.esc,
  163. preventDefault: sinon.spy(),
  164. stopPropagation: sinon.spy()
  165. };
  166. form.keystrokes.press( keyEvtData );
  167. sinon.assert.calledOnce( hideSpy );
  168. sinon.assert.calledOnce( keyEvtData.preventDefault );
  169. sinon.assert.calledOnce( focusSpy );
  170. } );
  171. } );
  172. describe( 'mouse', () => {
  173. it( 'should close and not focus editable on click outside the panel', () => {
  174. const hideSpy = sinon.spy( plugin, '_hideForm' );
  175. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  176. setData( model, '[<image src=""></image>]' );
  177. button.fire( 'execute' );
  178. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  179. sinon.assert.called( hideSpy );
  180. sinon.assert.notCalled( focusSpy );
  181. } );
  182. it( 'should not close on click inside the panel', () => {
  183. const spy = sinon.spy( plugin, '_hideForm' );
  184. setData( model, '[<image src=""></image>]' );
  185. button.fire( 'execute' );
  186. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  187. sinon.assert.notCalled( spy );
  188. } );
  189. } );
  190. } );
  191. } );
  192. } );