imageinsertui.js 16 KB

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