imagetextalternativeui.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  12. import View from '@ckeditor/ckeditor5-ui/src/view';
  13. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  14. import { setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  15. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  16. describe( 'ImageTextAlternativeUI', () => {
  17. let editor, model, doc, plugin, command, form, balloon, editorElement, button;
  18. beforeEach( () => {
  19. editorElement = global.document.createElement( 'div' );
  20. global.document.body.appendChild( editorElement );
  21. return ClassicTestEditor
  22. .create( editorElement, {
  23. plugins: [ ImageTextAlternativeEditing, ImageTextAlternativeUI, Image, Paragraph ]
  24. } )
  25. .then( newEditor => {
  26. editor = newEditor;
  27. model = editor.model;
  28. doc = model.document;
  29. newEditor.editing.view.attachDomRoot( editorElement );
  30. plugin = editor.plugins.get( ImageTextAlternativeUI );
  31. command = editor.commands.get( 'imageTextAlternative' );
  32. form = plugin._form;
  33. balloon = editor.plugins.get( 'ContextualBalloon' );
  34. button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  35. } );
  36. } );
  37. afterEach( () => {
  38. editorElement.remove();
  39. return editor.destroy();
  40. } );
  41. it( 'should be named', () => {
  42. expect( ImageTextAlternativeUI.pluginName ).to.equal( 'ImageTextAlternativeUI' );
  43. } );
  44. describe( 'toolbar button', () => {
  45. it( 'should be registered in component factory', () => {
  46. expect( button ).to.be.instanceOf( ButtonView );
  47. } );
  48. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  49. command.isEnabled = true;
  50. expect( button.isEnabled ).to.be.true;
  51. command.isEnabled = false;
  52. expect( button.isEnabled ).to.be.false;
  53. } );
  54. it( 'should show balloon panel on execute', () => {
  55. expect( balloon.visibleView ).to.be.null;
  56. setData( model, '[<image src="" alt="foo bar"></image>]' );
  57. button.fire( 'execute' );
  58. expect( balloon.visibleView ).to.equal( form );
  59. // Make sure successive execute does not throw, e.g. attempting
  60. // to display the form twice.
  61. button.fire( 'execute' );
  62. expect( balloon.visibleView ).to.equal( form );
  63. } );
  64. it( 'should set alt attribute value to textarea and select it', () => {
  65. const spy = sinon.spy( form.labeledInput.fieldView, 'select' );
  66. setData( model, '[<image src="" alt="foo bar"></image>]' );
  67. button.fire( 'execute' );
  68. sinon.assert.calledOnce( spy );
  69. expect( form.labeledInput.fieldView.value ).equals( 'foo bar' );
  70. } );
  71. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  72. const spy = sinon.spy( form.labeledInput.fieldView, 'select' );
  73. setData( model, '[<image src=""></image>]' );
  74. button.fire( 'execute' );
  75. sinon.assert.calledOnce( spy );
  76. expect( form.labeledInput.fieldView.value ).equals( '' );
  77. } );
  78. it( 'should disable CSS transitions before showing the form to avoid unnecessary animations (and then enable them again)', () => {
  79. const addSpy = sinon.spy( balloon, 'add' );
  80. const disableCssTransitionsSpy = sinon.spy( form, 'disableCssTransitions' );
  81. const enableCssTransitionsSpy = sinon.spy( form, 'enableCssTransitions' );
  82. const selectSpy = sinon.spy( form.labeledInput.fieldView, 'select' );
  83. setData( model, '[<image src="" alt="foo bar"></image>]' );
  84. button.fire( 'execute' );
  85. sinon.assert.callOrder( disableCssTransitionsSpy, addSpy, selectSpy, enableCssTransitionsSpy );
  86. } );
  87. } );
  88. describe( 'balloon panel form', () => {
  89. // https://github.com/ckeditor/ckeditor5-image/issues/114
  90. it( 'should make sure the input always stays in sync with the value of the command', () => {
  91. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  92. // Mock the value of the input after some past editing.
  93. form.labeledInput.fieldView.value = 'foo';
  94. // Mock the user using the form, changing the value but clicking "Cancel".
  95. // so the command's value is not updated.
  96. form.labeledInput.fieldView.element.value = 'This value was canceled.';
  97. // Mock the user editing the same image once again.
  98. setData( model, '[<image src="" alt="foo"></image>]' );
  99. button.fire( 'execute' );
  100. expect( form.labeledInput.fieldView.element.value ).to.equal( 'foo' );
  101. } );
  102. it( 'should execute command on submit', () => {
  103. const spy = sinon.spy( editor, 'execute' );
  104. form.fire( 'submit' );
  105. sinon.assert.calledOnce( spy );
  106. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  107. newValue: form.labeledInput.fieldView.element.value
  108. } );
  109. } );
  110. it( 'should hide the panel on cancel and focus the editing view', () => {
  111. const spy = sinon.spy( editor.editing.view, 'focus' );
  112. setData( model, '[<image src="" alt="foo bar"></image>]' );
  113. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  114. expect( balloon.visibleView ).to.equal( form );
  115. form.fire( 'cancel' );
  116. expect( balloon.visibleView ).to.be.null;
  117. sinon.assert.calledOnce( spy );
  118. } );
  119. it( 'should not engage when the form is in the balloon yet invisible', () => {
  120. setData( model, '[<image src=""></image>]' );
  121. button.fire( 'execute' );
  122. expect( balloon.visibleView ).to.equal( form );
  123. const lastView = new View();
  124. lastView.element = document.createElement( 'div' );
  125. balloon.add( {
  126. view: lastView,
  127. position: {
  128. target: document.body
  129. }
  130. } );
  131. expect( balloon.visibleView ).to.equal( lastView );
  132. button.fire( 'execute' );
  133. expect( balloon.visibleView ).to.equal( lastView );
  134. } );
  135. // https://github.com/ckeditor/ckeditor5/issues/1501
  136. it( 'should blur url input element before hiding the view', () => {
  137. setData( model, '[<image src="" alt="foo bar"></image>]' );
  138. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  139. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  140. const buttonFocusSpy = sinon.spy( form.saveButtonView, 'focus' );
  141. form.focusTracker.isFocused = true;
  142. form.fire( 'submit' );
  143. expect( buttonFocusSpy.calledBefore( editableFocusSpy ) ).to.equal( true );
  144. } );
  145. // https://github.com/ckeditor/ckeditor5-image/issues/299
  146. it( 'should not blur url input element before hiding the view when view was not focused', () => {
  147. setData( model, '[<image src="" alt="foo bar"></image>]' );
  148. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  149. const buttonFocusSpy = sinon.spy( form.saveButtonView, 'focus' );
  150. form.focusTracker.isFocused = false;
  151. form.fire( 'cancel' );
  152. sinon.assert.notCalled( buttonFocusSpy );
  153. } );
  154. it( 'should be removed from balloon when is in not visible stack', () => {
  155. setData( model, '<paragraph>foo</paragraph>[<image src="" alt="foo bar"></image>]' );
  156. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  157. const customView = new View();
  158. balloon.add( {
  159. view: customView,
  160. position: { target: {} },
  161. stackId: 'custom'
  162. } );
  163. balloon.showStack( 'custom' );
  164. model.change( writer => {
  165. const root = model.document.getRoot();
  166. writer.setSelection( root.getChild( 0 ), 0 );
  167. } );
  168. expect( balloon.hasView( form ) ).to.equal( false );
  169. } );
  170. describe( 'integration with the editor selection (ui#update event)', () => {
  171. it( 'should re-position the form', () => {
  172. setData( model, '[<image src=""></image>]' );
  173. button.fire( 'execute' );
  174. const spy = sinon.spy( balloon, 'updatePosition' );
  175. editor.ui.fire( 'update' );
  176. sinon.assert.calledOnce( spy );
  177. } );
  178. it( 'should hide the form and focus editable when image widget has been removed by external change', () => {
  179. setData( model, '[<image src=""></image>]' );
  180. button.fire( 'execute' );
  181. const removeSpy = sinon.spy( balloon, 'remove' );
  182. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  183. model.enqueueChange( 'transparent', writer => {
  184. writer.remove( doc.selection.getFirstRange() );
  185. } );
  186. sinon.assert.calledWithExactly( removeSpy, form );
  187. sinon.assert.calledOnce( focusSpy );
  188. } );
  189. } );
  190. describe( 'close listeners', () => {
  191. describe( 'keyboard', () => {
  192. it( 'should close upon Esc key press and focus the editing view', () => {
  193. const hideSpy = sinon.spy( plugin, '_hideForm' );
  194. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  195. setData( model, '[<image src=""></image>]' );
  196. button.fire( 'execute' );
  197. const keyEvtData = {
  198. keyCode: keyCodes.esc,
  199. preventDefault: sinon.spy(),
  200. stopPropagation: sinon.spy()
  201. };
  202. form.keystrokes.press( keyEvtData );
  203. sinon.assert.calledOnce( hideSpy );
  204. sinon.assert.calledOnce( keyEvtData.preventDefault );
  205. sinon.assert.calledOnce( focusSpy );
  206. } );
  207. } );
  208. describe( 'mouse', () => {
  209. it( 'should close and not focus editable on click outside the panel', () => {
  210. const hideSpy = sinon.spy( plugin, '_hideForm' );
  211. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  212. setData( model, '[<image src=""></image>]' );
  213. button.fire( 'execute' );
  214. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  215. sinon.assert.called( hideSpy );
  216. sinon.assert.notCalled( focusSpy );
  217. } );
  218. it( 'should not close on click inside the panel', () => {
  219. const spy = sinon.spy( plugin, '_hideForm' );
  220. setData( model, '[<image src=""></image>]' );
  221. button.fire( 'execute' );
  222. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  223. sinon.assert.notCalled( spy );
  224. } );
  225. } );
  226. } );
  227. } );
  228. } );