imageuploadui.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. describe( 'file dialog button', () => {
  35. beforeEach( () => {
  36. editorElement = document.createElement( 'div' );
  37. document.body.appendChild( editorElement );
  38. return ClassicEditor
  39. .create( editorElement, {
  40. plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
  41. } )
  42. .then( newEditor => {
  43. editor = newEditor;
  44. model = editor.model;
  45. // Hide all notifications (prevent alert() calls).
  46. const notification = editor.plugins.get( Notification );
  47. notification.on( 'show', evt => evt.stop() );
  48. } );
  49. } );
  50. afterEach( () => {
  51. editorElement.remove();
  52. return editor.destroy();
  53. } );
  54. it( 'should register imageUpload file dialog button', () => {
  55. const button = editor.ui.componentFactory.create( 'imageUpload' );
  56. expect( button ).to.be.instanceOf( FileDialogButtonView );
  57. } );
  58. it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
  59. editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
  60. const button = editor.ui.componentFactory.create( 'imageUpload' );
  61. expect( button.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 button = editor.ui.componentFactory.create( 'imageUpload' );
  74. const command = editor.commands.get( 'imageUpload' );
  75. const spy = sinon.spy();
  76. button.render();
  77. button.buttonView.on( 'execute', spy );
  78. command.isEnabled = false;
  79. button.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 button = editor.ui.componentFactory.create( 'imageUpload' );
  85. const files = [ createNativeFileMock() ];
  86. button.fire( 'done', files );
  87. sinon.assert.calledOnce( executeStub );
  88. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  89. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  90. } );
  91. it( 'should execute imageUpload command with multiple files', () => {
  92. const executeStub = sinon.stub( editor, 'execute' );
  93. const button = editor.ui.componentFactory.create( 'imageUpload' );
  94. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  95. button.fire( 'done', files );
  96. sinon.assert.calledOnce( executeStub );
  97. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  98. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  99. } );
  100. it( 'should optimize the insertion position', () => {
  101. const button = editor.ui.componentFactory.create( 'imageUpload' );
  102. const files = [ createNativeFileMock() ];
  103. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  104. button.fire( 'done', files );
  105. const id = fileRepository.getLoader( files[ 0 ] ).id;
  106. expect( getModelData( model ) ).to.equal(
  107. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  108. '<paragraph>foo</paragraph>'
  109. );
  110. } );
  111. it( 'should correctly insert multiple files', () => {
  112. const button = editor.ui.componentFactory.create( 'imageUpload' );
  113. const files = [ createNativeFileMock(), createNativeFileMock() ];
  114. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  115. button.fire( 'done', files );
  116. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  117. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  118. expect( getModelData( model ) ).to.equal(
  119. '<paragraph>foo</paragraph>' +
  120. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  121. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  122. '<paragraph>bar</paragraph>'
  123. );
  124. } );
  125. it( 'should not execute imageUpload if the file is not an image', () => {
  126. const executeStub = sinon.stub( editor, 'execute' );
  127. const button = editor.ui.componentFactory.create( 'imageUpload' );
  128. const file = {
  129. type: 'media/mp3',
  130. size: 1024
  131. };
  132. button.fire( 'done', [ file ] );
  133. sinon.assert.notCalled( executeStub );
  134. } );
  135. it( 'should work even if the FileList does not support iterators', () => {
  136. const executeStub = sinon.stub( editor, 'execute' );
  137. const button = editor.ui.componentFactory.create( 'imageUpload' );
  138. const files = {
  139. 0: createNativeFileMock(),
  140. length: 1
  141. };
  142. button.fire( 'done', files );
  143. sinon.assert.calledOnce( executeStub );
  144. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  145. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
  146. } );
  147. it( 'should add the new image after the selected one, without replacing the selected image', () => {
  148. const button = editor.ui.componentFactory.create( 'imageUpload' );
  149. const files = [ createNativeFileMock() ];
  150. setModelData( model, '[<image src="/assets/sample.png"></image>]<paragraph>bar</paragraph>' );
  151. button.fire( 'done', files );
  152. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  153. expect( getModelData( model ) ).to.equal(
  154. '<image src="/assets/sample.png"></image>' +
  155. `[<image uploadId="${ id1 }" uploadStatus="reading"></image>]` +
  156. '<paragraph>bar</paragraph>'
  157. );
  158. } );
  159. } );
  160. describe( 'dropdown', () => {
  161. beforeEach( () => {
  162. editorElement = document.createElement( 'div' );
  163. document.body.appendChild( editorElement );
  164. return ClassicEditor
  165. .create( editorElement, {
  166. plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ],
  167. image: {
  168. upload: {
  169. panel: {
  170. items: [
  171. 'insertImageViaUrl'
  172. ]
  173. }
  174. }
  175. }
  176. } )
  177. .then( newEditor => {
  178. editor = newEditor;
  179. model = editor.model;
  180. // Hide all notifications (prevent alert() calls).
  181. const notification = editor.plugins.get( Notification );
  182. notification.on( 'show', evt => evt.stop() );
  183. } );
  184. } );
  185. afterEach( () => {
  186. editorElement.remove();
  187. return editor.destroy();
  188. } );
  189. it( 'should register imageUpload dropdown', () => {
  190. const button = editor.ui.componentFactory.create( 'imageUpload' );
  191. expect( button ).to.be.instanceOf( DropdownView );
  192. } );
  193. it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
  194. editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
  195. const plugin = editor.plugins.get( 'ImageUploadUI' );
  196. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  197. expect( fileDialogButton.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
  198. } );
  199. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  200. const button = editor.ui.componentFactory.create( 'imageUpload' );
  201. const command = editor.commands.get( 'imageUpload' );
  202. command.isEnabled = true;
  203. expect( button.buttonView.isEnabled ).to.true;
  204. command.isEnabled = false;
  205. expect( button.buttonView.isEnabled ).to.false;
  206. } );
  207. // ckeditor5-upload/#77
  208. it( 'should be properly bound with ImageUploadCommand', () => {
  209. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  210. const command = editor.commands.get( 'imageUpload' );
  211. const spy = sinon.spy();
  212. dropdown.render();
  213. dropdown.buttonView.on( 'execute', spy );
  214. command.isEnabled = false;
  215. dropdown.buttonView.element.dispatchEvent( new Event( 'click' ) );
  216. sinon.assert.notCalled( spy );
  217. } );
  218. it( 'should execute imageUpload command', () => {
  219. const executeStub = sinon.stub( editor, 'execute' );
  220. const plugin = editor.plugins.get( 'ImageUploadUI' );
  221. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  222. const files = [ createNativeFileMock() ];
  223. fileDialogButton.fire( 'done', files );
  224. sinon.assert.calledOnce( executeStub );
  225. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  226. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  227. } );
  228. it( 'should execute imageUpload command with multiple files', () => {
  229. const executeStub = sinon.stub( editor, 'execute' );
  230. const plugin = editor.plugins.get( 'ImageUploadUI' );
  231. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  232. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  233. fileDialogButton.fire( 'done', files );
  234. sinon.assert.calledOnce( executeStub );
  235. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  236. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  237. } );
  238. it( 'should optimize the insertion position', () => {
  239. const plugin = editor.plugins.get( 'ImageUploadUI' );
  240. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  241. const files = [ createNativeFileMock() ];
  242. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  243. fileDialogButton.fire( 'done', files );
  244. const id = fileRepository.getLoader( files[ 0 ] ).id;
  245. expect( getModelData( model ) ).to.equal(
  246. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  247. '<paragraph>foo</paragraph>'
  248. );
  249. } );
  250. it( 'should correctly insert multiple files', () => {
  251. const plugin = editor.plugins.get( 'ImageUploadUI' );
  252. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  253. const files = [ createNativeFileMock(), createNativeFileMock() ];
  254. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  255. fileDialogButton.fire( 'done', files );
  256. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  257. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  258. expect( getModelData( model ) ).to.equal(
  259. '<paragraph>foo</paragraph>' +
  260. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  261. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  262. '<paragraph>bar</paragraph>'
  263. );
  264. } );
  265. it( 'should not execute imageUpload if the file is not an image', () => {
  266. const executeStub = sinon.stub( editor, 'execute' );
  267. const plugin = editor.plugins.get( 'ImageUploadUI' );
  268. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  269. const file = {
  270. type: 'media/mp3',
  271. size: 1024
  272. };
  273. fileDialogButton.fire( 'done', [ file ] );
  274. sinon.assert.notCalled( executeStub );
  275. } );
  276. it( 'should work even if the FileList does not support iterators', () => {
  277. const executeStub = sinon.stub( editor, 'execute' );
  278. const plugin = editor.plugins.get( 'ImageUploadUI' );
  279. const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
  280. const files = {
  281. 0: createNativeFileMock(),
  282. length: 1
  283. };
  284. fileDialogButton.fire( 'done', files );
  285. sinon.assert.calledOnce( executeStub );
  286. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  287. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
  288. } );
  289. describe( 'dropdown action button', () => {
  290. it( 'should be an instance of FileDialogButtonView', () => {
  291. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  292. expect( dropdown.buttonView.actionView ).to.be.instanceOf( FileDialogButtonView );
  293. } );
  294. } );
  295. describe( 'dropdown panel buttons', () => {
  296. it( 'should have "Update" label on submit button when URL input is already filled', () => {
  297. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  298. const viewDocument = editor.editing.view.document;
  299. editor.setData( '<figure><img src="/assets/sample.png" /></figure>' );
  300. editor.editing.view.change( writer => {
  301. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  302. } );
  303. const img = viewDocument.selection.getSelectedElement();
  304. const data = fakeEventData();
  305. const eventInfo = new EventInfo( img, 'click' );
  306. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  307. viewDocument.fire( 'click', domEventDataMock );
  308. dropdown.isOpen = true;
  309. const inputValue = dropdown.panelView.children.first.imageURLInputValue;
  310. expect( dropdown.isOpen ).to.be.true;
  311. expect( inputValue ).to.equal( '/assets/sample.png' );
  312. expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Update' );
  313. } );
  314. it( 'should have "Insert" label on submit button on uploading a new image', () => {
  315. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  316. const viewDocument = editor.editing.view.document;
  317. editor.setData( '<p>test</p>' );
  318. editor.editing.view.change( writer => {
  319. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'end' );
  320. } );
  321. const el = viewDocument.selection.getSelectedElement();
  322. const data = fakeEventData();
  323. const eventInfo = new EventInfo( el, 'click' );
  324. const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
  325. viewDocument.fire( 'click', domEventDataMock );
  326. dropdown.isOpen = true;
  327. const inputValue = dropdown.panelView.children.first.imageURLInputValue;
  328. expect( dropdown.isOpen ).to.be.true;
  329. expect( inputValue ).to.equal( '' );
  330. expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Insert' );
  331. } );
  332. } );
  333. it( 'should remove all attributes from model except "src" when updating the image source URL', () => {
  334. const viewDocument = editor.editing.view.document;
  335. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  336. const insertButtonView = dropdown.panelView.children.first.insertButtonView;
  337. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  338. const submitSpy = sinon.spy();
  339. dropdown.isOpen = true;
  340. editor.setData( '<figure><img src="image-url-800w.jpg"' +
  341. 'srcset="image-url-480w.jpg 480w,image-url-800w.jpg 800w"' +
  342. 'sizes="(max-width: 600px) 480px,800px"' +
  343. 'alt="test-image"></figure>' );
  344. editor.editing.view.change( writer => {
  345. writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
  346. } );
  347. const selectedElement = editor.model.document.selection.getSelectedElement();
  348. expect( selectedElement.getAttribute( 'src' ) ).to.equal( 'image-url-800w.jpg' );
  349. expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.true;
  350. dropdown.panelView.children.first.imageURLInputValue = '/assets/sample3.png';
  351. dropdown.on( 'submit', submitSpy );
  352. insertButtonView.fire( 'execute' );
  353. sinon.assert.notCalled( commandSpy );
  354. sinon.assert.calledOnce( submitSpy );
  355. expect( dropdown.isOpen ).to.be.false;
  356. expect( selectedElement.getAttribute( 'src' ) ).to.equal( '/assets/sample3.png' );
  357. expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.false;
  358. expect( selectedElement.hasAttribute( 'sizes' ) ).to.be.false;
  359. } );
  360. describe( 'events', () => {
  361. it( 'should emit "submit" event when clicking on submit button', () => {
  362. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  363. const insertButtonView = dropdown.panelView.children.first.insertButtonView;
  364. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  365. const submitSpy = sinon.spy();
  366. dropdown.isOpen = true;
  367. dropdown.on( 'submit', submitSpy );
  368. insertButtonView.fire( 'execute' );
  369. expect( dropdown.isOpen ).to.be.false;
  370. sinon.assert.calledOnce( commandSpy );
  371. sinon.assert.calledOnce( submitSpy );
  372. } );
  373. it( 'should emit "cancel" event when clicking on cancel button', () => {
  374. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  375. const cancelButtonView = dropdown.panelView.children.first.cancelButtonView;
  376. const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
  377. const cancelSpy = sinon.spy();
  378. dropdown.isOpen = true;
  379. dropdown.on( 'cancel', cancelSpy );
  380. cancelButtonView.fire( 'execute' );
  381. expect( dropdown.isOpen ).to.be.false;
  382. sinon.assert.notCalled( commandSpy );
  383. sinon.assert.calledOnce( cancelSpy );
  384. } );
  385. } );
  386. it( 'should inject integrations to the dropdown panel view from the config', async () => {
  387. const editor = await ClassicEditor
  388. .create( editorElement, {
  389. plugins: [
  390. CKFinder,
  391. Paragraph,
  392. Image,
  393. ImageUploadEditing,
  394. ImageUploadUI,
  395. FileRepository,
  396. UploadAdapterPluginMock,
  397. Clipboard
  398. ],
  399. image: {
  400. upload: {
  401. panel: {
  402. items: [
  403. 'insertImageViaUrl',
  404. 'openCKFinder'
  405. ]
  406. }
  407. }
  408. }
  409. } );
  410. const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
  411. expect( dropdown.panelView.children.first._integrations.length ).to.equal( 2 );
  412. expect( dropdown.panelView.children.first._integrations.first ).to.be.instanceOf( LabeledFieldView );
  413. expect( dropdown.panelView.children.first._integrations.last ).to.be.instanceOf( ButtonView );
  414. editor.destroy();
  415. } );
  416. } );
  417. } );
  418. function fakeEventData() {
  419. return {
  420. preventDefault: sinon.spy()
  421. };
  422. }