imageuploadui.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* globals document, Event */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  8. import Image from '../../src/image';
  9. import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
  10. import FileDialogButtonView from '@ckeditor/ckeditor5-upload/src/ui/filedialogbuttonview';
  11. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  12. import ImageUploadUI from '../../src/imageupload/imageuploadui';
  13. import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
  14. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  15. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  16. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  17. import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
  18. import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
  19. import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
  20. import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
  21. import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
  22. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  23. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  24. describe( 'ImageUploadUI', () => {
  25. let editor, model, editorElement, fileRepository;
  26. class UploadAdapterPluginMock extends Plugin {
  27. init() {
  28. fileRepository = this.editor.plugins.get( FileRepository );
  29. fileRepository.createUploadAdapter = loader => {
  30. return new UploadAdapterMock( loader );
  31. };
  32. }
  33. }
  34. beforeEach( () => {
  35. editorElement = document.createElement( 'div' );
  36. document.body.appendChild( editorElement );
  37. return ClassicEditor
  38. .create( editorElement, {
  39. plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
  40. } )
  41. .then( newEditor => {
  42. editor = newEditor;
  43. model = editor.model;
  44. // Hide all notifications (prevent alert() calls).
  45. const notification = editor.plugins.get( Notification );
  46. notification.on( 'show', evt => evt.stop() );
  47. } );
  48. } );
  49. afterEach( () => {
  50. editorElement.remove();
  51. return editor.destroy();
  52. } );
  53. it( 'should register imageUpload dropdown', () => {
  54. const button = editor.ui.componentFactory.create( 'imageUpload' );
  55. expect( button ).to.be.instanceOf( DropdownView );
  56. } );
  57. it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
  58. editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
  59. const plugin = editor.plugins.get( 'ImageUploadUI' );
  60. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  61. expect( fileDialogButton.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
  62. } );
  63. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  64. const button = editor.ui.componentFactory.create( 'imageUpload' );
  65. const command = editor.commands.get( 'imageUpload' );
  66. command.isEnabled = true;
  67. expect( button.buttonView.isEnabled ).to.true;
  68. command.isEnabled = false;
  69. expect( button.buttonView.isEnabled ).to.false;
  70. } );
  71. // ckeditor5-upload/#77
  72. it( 'should be properly bound with ImageUploadCommand', () => {
  73. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  74. const command = editor.commands.get( 'imageUpload' );
  75. const spy = sinon.spy();
  76. dropdown.render();
  77. dropdown.buttonView.on( 'execute', spy );
  78. command.isEnabled = false;
  79. dropdown.buttonView.element.dispatchEvent( new Event( 'click' ) );
  80. sinon.assert.notCalled( spy );
  81. } );
  82. it( 'should execute imageUpload command', () => {
  83. const executeStub = sinon.stub( editor, 'execute' );
  84. const plugin = editor.plugins.get( 'ImageUploadUI' );
  85. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  86. const files = [ createNativeFileMock() ];
  87. fileDialogButton.fire( 'done', files );
  88. sinon.assert.calledOnce( executeStub );
  89. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  90. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  91. } );
  92. it( 'should execute imageUpload command with multiple files', () => {
  93. const executeStub = sinon.stub( editor, 'execute' );
  94. const plugin = editor.plugins.get( 'ImageUploadUI' );
  95. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  96. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  97. fileDialogButton.fire( 'done', files );
  98. sinon.assert.calledOnce( executeStub );
  99. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  100. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  101. } );
  102. it( 'should optimize the insertion position', () => {
  103. const plugin = editor.plugins.get( 'ImageUploadUI' );
  104. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  105. const files = [ createNativeFileMock() ];
  106. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  107. fileDialogButton.fire( 'done', files );
  108. const id = fileRepository.getLoader( files[ 0 ] ).id;
  109. expect( getModelData( model ) ).to.equal(
  110. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  111. '<paragraph>foo</paragraph>'
  112. );
  113. } );
  114. it( 'should correctly insert multiple files', () => {
  115. const plugin = editor.plugins.get( 'ImageUploadUI' );
  116. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  117. const files = [ createNativeFileMock(), createNativeFileMock() ];
  118. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  119. fileDialogButton.fire( 'done', files );
  120. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  121. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  122. expect( getModelData( model ) ).to.equal(
  123. '<paragraph>foo</paragraph>' +
  124. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  125. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  126. '<paragraph>bar</paragraph>'
  127. );
  128. } );
  129. it( 'should not execute imageUpload if the file is not an image', () => {
  130. const executeStub = sinon.stub( editor, 'execute' );
  131. const plugin = editor.plugins.get( 'ImageUploadUI' );
  132. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  133. const file = {
  134. type: 'media/mp3',
  135. size: 1024
  136. };
  137. fileDialogButton.fire( 'done', [ file ] );
  138. sinon.assert.notCalled( executeStub );
  139. } );
  140. it( 'should work even if the FileList does not support iterators', () => {
  141. const executeStub = sinon.stub( editor, 'execute' );
  142. const plugin = editor.plugins.get( 'ImageUploadUI' );
  143. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  144. const files = {
  145. 0: createNativeFileMock(),
  146. length: 1
  147. };
  148. fileDialogButton.fire( 'done', files );
  149. sinon.assert.calledOnce( executeStub );
  150. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  151. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
  152. } );
  153. describe( 'dropdown action button', () => {
  154. it( 'should be an instance of FileDialogButtonView', () => {
  155. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  156. expect( dropdown.buttonView.actionView ).to.be.instanceOf( FileDialogButtonView );
  157. } );
  158. } );
  159. describe( 'dropdown panel buttons', () => {
  160. it( 'should have "Update" label on submit button when URL input is already filled', () => {
  161. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  162. const viewDocument = editor.editing.view.document;
  163. editor.setData( '<figure><img src="/assets/sample.png" /></figure>' );
  164. editor.editing.view.change( writer => {
  165. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  166. } );
  167. const img = viewDocument.selection.getSelectedElement();
  168. const data = fakeEventData();
  169. const eventInfo = new EventInfo( img, 'click' );
  170. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  171. viewDocument.fire( 'click', domEventDataMock );
  172. dropdown.isOpen = true;
  173. const inputValue = dropdown.panelView.children.first.imageURLInputValue;
  174. expect( dropdown.isOpen ).to.be.true;
  175. expect( inputValue ).to.equal( '/assets/sample.png' );
  176. expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Update' );
  177. } );
  178. it( 'should have "Insert" label on submit button on uploading a new image', () => {
  179. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  180. const viewDocument = editor.editing.view.document;
  181. editor.setData( '<p>test</p>' );
  182. editor.editing.view.change( writer => {
  183. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'end' );
  184. } );
  185. const el = viewDocument.selection.getSelectedElement();
  186. const data = fakeEventData();
  187. const eventInfo = new EventInfo( el, 'click' );
  188. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  189. viewDocument.fire( 'click', domEventDataMock );
  190. dropdown.isOpen = true;
  191. const inputValue = dropdown.panelView.children.first.imageURLInputValue;
  192. expect( dropdown.isOpen ).to.be.true;
  193. expect( inputValue ).to.equal( '' );
  194. expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Insert' );
  195. } );
  196. } );
  197. it( 'should remove all attributes from model except "src" when updating the image source URL', () => {
  198. const viewDocument = editor.editing.view.document;
  199. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  200. const insertButtonView = dropdown.panelView.children.first.insertButtonView;
  201. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  202. const submitSpy = sinon.spy();
  203. dropdown.isOpen = true;
  204. editor.setData( '<figure><img src="image-url-800w.jpg"' +
  205. 'srcset="image-url-480w.jpg 480w,image-url-800w.jpg 800w"' +
  206. 'sizes="(max-width: 600px) 480px,800px"' +
  207. 'alt="test-image"></figure>' );
  208. editor.editing.view.change( writer => {
  209. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  210. } );
  211. const selectedElement = editor.model.document.selection.getSelectedElement();
  212. expect( selectedElement.getAttribute( 'src' ) ).to.equal( 'image-url-800w.jpg' );
  213. expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.true;
  214. dropdown.panelView.children.first.imageURLInputValue = '/assets/sample3.png';
  215. dropdown.on( 'submit', submitSpy );
  216. insertButtonView.fire( 'execute' );
  217. sinon.assert.notCalled( commandSpy );
  218. sinon.assert.calledOnce( submitSpy );
  219. expect( dropdown.isOpen ).to.be.false;
  220. expect( selectedElement.getAttribute( 'src' ) ).to.equal( '/assets/sample3.png' );
  221. expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.false;
  222. expect( selectedElement.hasAttribute( 'sizes' ) ).to.be.false;
  223. } );
  224. describe( 'events', () => {
  225. it( 'should emit "submit" event when clicking on submit button', () => {
  226. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  227. const insertButtonView = dropdown.panelView.children.first.insertButtonView;
  228. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  229. const submitSpy = sinon.spy();
  230. dropdown.isOpen = true;
  231. dropdown.on( 'submit', submitSpy );
  232. insertButtonView.fire( 'execute' );
  233. expect( dropdown.isOpen ).to.be.false;
  234. sinon.assert.calledOnce( commandSpy );
  235. sinon.assert.calledOnce( submitSpy );
  236. } );
  237. it( 'should emit "cancel" event when clicking on cancel button', () => {
  238. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  239. const cancelButtonView = dropdown.panelView.children.first.cancelButtonView;
  240. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  241. const cancelSpy = sinon.spy();
  242. dropdown.isOpen = true;
  243. dropdown.on( 'cancel', cancelSpy );
  244. cancelButtonView.fire( 'execute' );
  245. expect( dropdown.isOpen ).to.be.false;
  246. sinon.assert.notCalled( commandSpy );
  247. sinon.assert.calledOnce( cancelSpy );
  248. } );
  249. } );
  250. it( 'should inject integrations to the dropdown panel view from the config', async () => {
  251. const editor = await ClassicEditor
  252. .create( editorElement, {
  253. plugins: [
  254. CKFinder,
  255. Paragraph,
  256. Image,
  257. ImageUploadEditing,
  258. ImageUploadUI,
  259. FileRepository,
  260. UploadAdapterPluginMock,
  261. Clipboard
  262. ],
  263. image: {
  264. upload: {
  265. panel: {
  266. items: [
  267. 'insertImageViaUrl',
  268. 'openCKFinder'
  269. ]
  270. }
  271. }
  272. }
  273. } );
  274. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  275. expect( dropdown.panelView.children.first._integrations.length ).to.equal( 2 );
  276. expect( dropdown.panelView.children.first._integrations.first ).to.be.instanceOf( LabeledFieldView );
  277. expect( dropdown.panelView.children.first._integrations.last ).to.be.instanceOf( ButtonView );
  278. editor.destroy();
  279. } );
  280. } );
  281. function fakeEventData() {
  282. return {
  283. preventDefault: sinon.spy()
  284. };
  285. }