imagetextalternativeui.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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( 'ImageTextAlternative', () => {
  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. describe( 'toolbar button', () => {
  41. it( 'should be registered in component factory', () => {
  42. expect( button ).to.be.instanceOf( ButtonView );
  43. } );
  44. it( 'should have isEnabled property bind to command\'s isEnabled property', () => {
  45. command.isEnabled = true;
  46. expect( button.isEnabled ).to.be.true;
  47. command.isEnabled = false;
  48. expect( button.isEnabled ).to.be.false;
  49. } );
  50. it( 'should show balloon panel on execute', () => {
  51. expect( balloon.visibleView ).to.be.null;
  52. setData( model, '[<image src="" alt="foo bar"></image>]' );
  53. button.fire( 'execute' );
  54. expect( balloon.visibleView ).to.equal( form );
  55. // Make sure successive execute does not throw, e.g. attempting
  56. // to display the form twice.
  57. button.fire( 'execute' );
  58. expect( balloon.visibleView ).to.equal( form );
  59. } );
  60. it( 'should set alt attribute value to textarea and select it', () => {
  61. const spy = sinon.spy( form.labeledInput, 'select' );
  62. setData( model, '[<image src="" alt="foo bar"></image>]' );
  63. button.fire( 'execute' );
  64. sinon.assert.calledOnce( spy );
  65. expect( form.labeledInput.value ).equals( 'foo bar' );
  66. } );
  67. it( 'should set empty text to textarea and select it when there is no alt attribute', () => {
  68. const spy = sinon.spy( form.labeledInput, 'select' );
  69. setData( model, '[<image src=""></image>]' );
  70. button.fire( 'execute' );
  71. sinon.assert.calledOnce( spy );
  72. expect( form.labeledInput.value ).equals( '' );
  73. } );
  74. } );
  75. describe( 'balloon panel form', () => {
  76. // https://github.com/ckeditor/ckeditor5-image/issues/114
  77. it( 'should make sure the input always stays in sync with the value of the command', () => {
  78. const button = editor.ui.componentFactory.create( 'imageTextAlternative' );
  79. // Mock the value of the input after some past editing.
  80. form.labeledInput.value = 'foo';
  81. // Mock the user using the form, changing the value but clicking "Cancel".
  82. // so the command's value is not updated.
  83. form.labeledInput.inputView.element.value = 'This value was canceled.';
  84. // Mock the user editing the same image once again.
  85. setData( model, '[<image src="" alt="foo"></image>]' );
  86. button.fire( 'execute' );
  87. expect( form.labeledInput.inputView.element.value ).to.equal( 'foo' );
  88. } );
  89. it( 'should execute command on submit', () => {
  90. const spy = sinon.spy( editor, 'execute' );
  91. form.fire( 'submit' );
  92. sinon.assert.calledOnce( spy );
  93. sinon.assert.calledWithExactly( spy, 'imageTextAlternative', {
  94. newValue: form.labeledInput.inputView.element.value
  95. } );
  96. } );
  97. it( 'should hide the panel on cancel and focus the editing view', () => {
  98. const spy = sinon.spy( editor.editing.view, 'focus' );
  99. setData( model, '[<image src="" alt="foo bar"></image>]' );
  100. editor.ui.componentFactory.create( 'imageTextAlternative' ).fire( 'execute' );
  101. expect( balloon.visibleView ).to.equal( form );
  102. form.fire( 'cancel' );
  103. expect( balloon.visibleView ).to.be.null;
  104. sinon.assert.calledOnce( spy );
  105. } );
  106. it( 'should not engage when the form is in the balloon yet invisible', () => {
  107. setData( model, '[<image src=""></image>]' );
  108. button.fire( 'execute' );
  109. expect( balloon.visibleView ).to.equal( form );
  110. const lastView = new View();
  111. lastView.element = document.createElement( 'div' );
  112. balloon.add( {
  113. view: lastView,
  114. position: {
  115. target: document.body
  116. }
  117. } );
  118. expect( balloon.visibleView ).to.equal( lastView );
  119. button.fire( 'execute' );
  120. expect( balloon.visibleView ).to.equal( lastView );
  121. } );
  122. describe( 'integration with the editor selection (ui#update event)', () => {
  123. it( 'should re-position the form', () => {
  124. setData( model, '[<image src=""></image>]' );
  125. button.fire( 'execute' );
  126. const spy = sinon.spy( balloon, 'updatePosition' );
  127. editor.ui.fire( 'update' );
  128. sinon.assert.calledOnce( spy );
  129. } );
  130. it( 'should hide the form and focus editable when image widget has been removed by external change', () => {
  131. setData( model, '[<image src=""></image>]' );
  132. button.fire( 'execute' );
  133. const removeSpy = sinon.spy( balloon, 'remove' );
  134. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  135. model.enqueueChange( 'transparent', writer => {
  136. writer.remove( doc.selection.getFirstRange() );
  137. } );
  138. sinon.assert.calledWithExactly( removeSpy, form );
  139. sinon.assert.calledOnce( focusSpy );
  140. } );
  141. } );
  142. describe( 'close listeners', () => {
  143. describe( 'keyboard', () => {
  144. it( 'should close upon Esc key press and focus the editing view', () => {
  145. const hideSpy = sinon.spy( plugin, '_hideForm' );
  146. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  147. setData( model, '[<image src=""></image>]' );
  148. button.fire( 'execute' );
  149. const keyEvtData = {
  150. keyCode: keyCodes.esc,
  151. preventDefault: sinon.spy(),
  152. stopPropagation: sinon.spy()
  153. };
  154. form.keystrokes.press( keyEvtData );
  155. sinon.assert.calledOnce( hideSpy );
  156. sinon.assert.calledOnce( keyEvtData.preventDefault );
  157. sinon.assert.calledOnce( focusSpy );
  158. } );
  159. } );
  160. describe( 'mouse', () => {
  161. it( 'should close and not focus editable on click outside the panel', () => {
  162. const hideSpy = sinon.spy( plugin, '_hideForm' );
  163. const focusSpy = sinon.spy( editor.editing.view, 'focus' );
  164. setData( model, '[<image src=""></image>]' );
  165. button.fire( 'execute' );
  166. global.document.body.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  167. sinon.assert.called( hideSpy );
  168. sinon.assert.notCalled( focusSpy );
  169. } );
  170. it( 'should not close on click inside the panel', () => {
  171. const spy = sinon.spy( plugin, '_hideForm' );
  172. setData( model, '[<image src=""></image>]' );
  173. button.fire( 'execute' );
  174. form.element.dispatchEvent( new Event( 'mousedown', { bubbles: true } ) );
  175. sinon.assert.notCalled( spy );
  176. } );
  177. } );
  178. } );
  179. } );
  180. } );