8
0

imagetextalternativeui.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. } );
  79. describe( 'balloon panel form', () => {
  80. // https://github.com/ckeditor/ckeditor5-image/issues/114
  81. it( 'should make sure the input always stays in sync with the value of the command', () => {
  82. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  83. // Mock the value of the input after some past editing.
  84. form.labeledInput.fieldView.value = 'foo';
  85. // Mock the user using the form, changing the value but clicking "Cancel".
  86. // so the command's value is not updated.
  87. form.labeledInput.fieldView.element.value = 'This value was canceled.';
  88. // Mock the user editing the same image once again.
  89. setData( model, '[<image src="" alt="foo"></image>]' );
  90. button.fire( 'execute' );
  91. expect( form.labeledInput.fieldView.element.value ).to.equal( 'foo' );
  92. } );
  93. it( 'should execute command on submit', () => {
  94. const spy = sinon.spy( editor, 'execute' );
  95. form.fire( 'submit' );
  96. sinon.assert.calledOnce( spy );
  97. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  98. newValue: form.labeledInput.fieldView.element.value
  99. } );
  100. } );
  101. it( 'should hide the panel on cancel and focus the editing view', () => {
  102. const spy = sinon.spy( editor.editing.view, 'focus' );
  103. setData( model, '[<image src="" alt="foo bar"></image>]' );
  104. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  105. expect( balloon.visibleView ).to.equal( form );
  106. form.fire( 'cancel' );
  107. expect( balloon.visibleView ).to.be.null;
  108. sinon.assert.calledOnce( spy );
  109. } );
  110. it( 'should not engage when the form is in the balloon yet invisible', () => {
  111. setData( model, '[<image src=""></image>]' );
  112. button.fire( 'execute' );
  113. expect( balloon.visibleView ).to.equal( form );
  114. const lastView = new View();
  115. lastView.element = document.createElement( 'div' );
  116. balloon.add( {
  117. view: lastView,
  118. position: {
  119. target: document.body
  120. }
  121. } );
  122. expect( balloon.visibleView ).to.equal( lastView );
  123. button.fire( 'execute' );
  124. expect( balloon.visibleView ).to.equal( lastView );
  125. } );
  126. // https://github.com/ckeditor/ckeditor5/issues/1501
  127. it( 'should blur url input element before hiding the view', () => {
  128. setData( model, '[<image src="" alt="foo bar"></image>]' );
  129. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  130. const editableFocusSpy = sinon.spy( editor.editing.view, 'focus' );
  131. const buttonFocusSpy = sinon.spy( form.saveButtonView, 'focus' );
  132. form.focusTracker.isFocused = true;
  133. form.fire( 'submit' );
  134. expect( buttonFocusSpy.calledBefore( editableFocusSpy ) ).to.equal( true );
  135. } );
  136. // https://github.com/ckeditor/ckeditor5-image/issues/299
  137. it( 'should not blur url input element before hiding the view when view was not focused', () => {
  138. setData( model, '[<image src="" alt="foo bar"></image>]' );
  139. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  140. const buttonFocusSpy = sinon.spy( form.saveButtonView, 'focus' );
  141. form.focusTracker.isFocused = false;
  142. form.fire( 'cancel' );
  143. sinon.assert.notCalled( buttonFocusSpy );
  144. } );
  145. it( 'should be removed from balloon when is in not visible stack', () => {
  146. setData( model, '<paragraph>foo</paragraph>[<image src="" alt="foo bar"></image>]' );
  147. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  148. const customView = new View();
  149. balloon.add( {
  150. view: customView,
  151. position: { target: {} },
  152. stackId: 'custom'
  153. } );
  154. balloon.showStack( 'custom' );
  155. model.change( writer => {
  156. const root = model.document.getRoot();
  157. writer.setSelection( root.getChild( 0 ), 0 );
  158. } );
  159. expect( balloon.hasView( form ) ).to.equal( false );
  160. } );
  161. describe( 'integration with the editor selection (ui#update event)', () => {
  162. it( 'should re-position the form', () => {
  163. setData( model, '[<image src=""></image>]' );
  164. button.fire( 'execute' );
  165. const spy = sinon.spy( balloon, 'updatePosition' );
  166. editor.ui.fire( 'update' );
  167. sinon.assert.calledOnce( spy );
  168. } );
  169. it( 'should hide the form and focus editable when image widget has been removed by external change', () => {
  170. setData( model, '[<image src=""></image>]' );
  171. button.fire( 'execute' );
  172. const removeSpy = sinon.spy( balloon, 'remove' );
  173. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  174. model.enqueueChange( 'transparent', writer => {
  175. writer.remove( doc.selection.getFirstRange() );
  176. } );
  177. sinon.assert.calledWithExactly( removeSpy, form );
  178. sinon.assert.calledOnce( focusSpy );
  179. } );
  180. } );
  181. describe( 'close listeners', () => {
  182. describe( 'keyboard', () => {
  183. it( 'should close upon Esc key press and focus the editing view', () => {
  184. const hideSpy = sinon.spy( plugin, '_hideForm' );
  185. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  186. setData( model, '[<image src=""></image>]' );
  187. button.fire( 'execute' );
  188. const keyEvtData = {
  189. keyCode: keyCodes.esc,
  190. preventDefault: sinon.spy(),
  191. stopPropagation: sinon.spy()
  192. };
  193. form.keystrokes.press( keyEvtData );
  194. sinon.assert.calledOnce( hideSpy );
  195. sinon.assert.calledOnce( keyEvtData.preventDefault );
  196. sinon.assert.calledOnce( focusSpy );
  197. } );
  198. } );
  199. describe( 'mouse', () => {
  200. it( 'should close and not focus editable on click outside the panel', () => {
  201. const hideSpy = sinon.spy( plugin, '_hideForm' );
  202. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  203. setData( model, '[<image src=""></image>]' );
  204. button.fire( 'execute' );
  205. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  206. sinon.assert.called( hideSpy );
  207. sinon.assert.notCalled( focusSpy );
  208. } );
  209. it( 'should not close on click inside the panel', () => {
  210. const spy = sinon.spy( plugin, '_hideForm' );
  211. setData( model, '[<image src=""></image>]' );
  212. button.fire( 'execute' );
  213. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  214. sinon.assert.notCalled( spy );
  215. } );
  216. } );
  217. } );
  218. } );
  219. } );