imageuploadengine.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals window, setTimeout */
  6. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  7. import ImageEngine from '@ckeditor/ckeditor5-image/src/image/imageengine';
  8. import ImageUploadEngine from '../src/imageuploadengine';
  9. import ImageUploadCommand from '../src/imageuploadcommand';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import UndoEngine from '@ckeditor/ckeditor5-undo/src/undoengine';
  12. import DataTransfer from '@ckeditor/ckeditor5-clipboard/src/datatransfer';
  13. import FileRepository from '../src/filerepository';
  14. import { AdapterMock, createNativeFileMock, NativeFileReaderMock } from './_utils/mocks';
  15. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  16. import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
  17. import { eventNameToConsumableType } from '@ckeditor/ckeditor5-engine/src/conversion/model-to-view-converters';
  18. import Range from '@ckeditor/ckeditor5-engine/src/model/range';
  19. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  20. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  21. describe( 'ImageUploadEngine', () => {
  22. // eslint-disable-next-line max-len
  23. const base64Sample = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=';
  24. let editor, doc, fileRepository, viewDocument, nativeReaderMock, loader, adapterMock;
  25. testUtils.createSinonSandbox();
  26. beforeEach( () => {
  27. testUtils.sinon.stub( window, 'FileReader' ).callsFake( () => {
  28. nativeReaderMock = new NativeFileReaderMock();
  29. return nativeReaderMock;
  30. } );
  31. return ClassicTestEditor.create( {
  32. plugins: [ ImageEngine, ImageUploadEngine, Paragraph, UndoEngine ]
  33. } )
  34. .then( newEditor => {
  35. editor = newEditor;
  36. doc = editor.document;
  37. viewDocument = editor.editing.view;
  38. fileRepository = editor.plugins.get( FileRepository );
  39. fileRepository.createAdapter = newLoader => {
  40. loader = newLoader;
  41. adapterMock = new AdapterMock( loader );
  42. return adapterMock;
  43. };
  44. } );
  45. } );
  46. it( 'should register proper schema rules', () => {
  47. expect( doc.schema.check( { name: 'image', attributes: [ 'uploadId' ], inside: '$root' } ) ).to.be.true;
  48. } );
  49. it( 'should register imageUpload command', () => {
  50. expect( editor.commands.get( 'imageUpload' ) ).to.be.instanceOf( ImageUploadCommand );
  51. } );
  52. it( 'should execute imageUpload command when image is pasted', () => {
  53. const spy = sinon.spy( editor, 'execute' );
  54. const fileMock = createNativeFileMock();
  55. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  56. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  57. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  58. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  59. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  60. sinon.assert.calledOnce( spy );
  61. sinon.assert.calledWith( spy, 'imageUpload' );
  62. const id = fileRepository.getLoader( fileMock ).id;
  63. expect( getModelData( doc ) ).to.equal(
  64. `<paragraph>foo</paragraph>[<image uploadId="${ id }" uploadStatus="reading"></image>]`
  65. );
  66. } );
  67. it( 'should execute imageUpload command with an optimized position when image is pasted', () => {
  68. const spy = sinon.spy( editor, 'execute' );
  69. const fileMock = createNativeFileMock();
  70. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  71. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  72. const paragraph = doc.getRoot().getChild( 0 );
  73. const targetRange = Range.createFromParentsAndOffsets( paragraph, 1, paragraph, 1 ); // f[]oo
  74. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  75. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  76. sinon.assert.calledOnce( spy );
  77. sinon.assert.calledWith( spy, 'imageUpload' );
  78. const id = fileRepository.getLoader( fileMock ).id;
  79. expect( getModelData( doc ) ).to.equal(
  80. `[<image uploadId="${ id }" uploadStatus="reading"></image>]<paragraph>foo</paragraph>`
  81. );
  82. } );
  83. it( 'should execute imageUpload command when multiple files image are pasted', () => {
  84. const spy = sinon.spy( editor, 'execute' );
  85. const files = [ createNativeFileMock(), createNativeFileMock() ];
  86. const dataTransfer = new DataTransfer( { files } );
  87. setModelData( doc, '<paragraph>[]foo</paragraph>' );
  88. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  89. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  90. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  91. sinon.assert.calledTwice( spy );
  92. sinon.assert.calledWith( spy, 'imageUpload' );
  93. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  94. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  95. expect( getModelData( doc ) ).to.equal(
  96. '<paragraph>foo</paragraph>' +
  97. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  98. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]`
  99. );
  100. } );
  101. it( 'should not execute imageUpload command when file is not an image', () => {
  102. const spy = sinon.spy( editor, 'execute' );
  103. const viewDocument = editor.editing.view;
  104. const fileMock = {
  105. type: 'media/mp3',
  106. size: 1024
  107. };
  108. const dataTransfer = new DataTransfer( { files: [ fileMock ] } );
  109. setModelData( doc, '<paragraph>foo[]</paragraph>' );
  110. const targetRange = Range.createFromParentsAndOffsets( doc.getRoot(), 1, doc.getRoot(), 1 );
  111. const targetViewRange = editor.editing.mapper.toViewRange( targetRange );
  112. viewDocument.fire( 'clipboardInput', { dataTransfer, targetRanges: [ targetViewRange ] } );
  113. sinon.assert.notCalled( spy );
  114. } );
  115. it( 'should not convert image\'s uploadId attribute if is consumed already', () => {
  116. editor.editing.modelToView.on( 'addAttribute:uploadId:image', ( evt, data, consumable ) => {
  117. consumable.consume( data.item, eventNameToConsumableType( evt.name ) );
  118. }, { priority: 'high' } );
  119. setModelData( doc, '<image uploadId="1234"></image>' );
  120. expect( getViewData( viewDocument ) ).to.equal(
  121. '[<figure class="image ck-widget" contenteditable="false">' +
  122. '<img></img>' +
  123. '</figure>]' );
  124. } );
  125. it( 'should use read data once it is present', done => {
  126. const file = createNativeFileMock();
  127. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  128. editor.execute( 'imageUpload', { file } );
  129. doc.once( 'changesDone', () => {
  130. expect( getViewData( viewDocument ) ).to.equal(
  131. '[<figure class="image ck-widget" contenteditable="false">' +
  132. `<img src="${ base64Sample }"></img>` +
  133. '</figure>]' +
  134. '<p>foo bar</p>' );
  135. expect( loader.status ).to.equal( 'uploading' );
  136. done();
  137. } );
  138. expect( loader.status ).to.equal( 'reading' );
  139. nativeReaderMock.mockSuccess( base64Sample );
  140. } );
  141. it( 'should replace read data with server response once it is present', done => {
  142. const file = createNativeFileMock();
  143. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  144. editor.execute( 'imageUpload', { file } );
  145. doc.once( 'changesDone', () => {
  146. doc.once( 'changesDone', () => {
  147. expect( getViewData( viewDocument ) ).to.equal(
  148. '[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]<p>foo bar</p>'
  149. );
  150. expect( loader.status ).to.equal( 'idle' );
  151. done();
  152. } );
  153. adapterMock.mockSuccess( { original: 'image.png' } );
  154. } );
  155. nativeReaderMock.mockSuccess( base64Sample );
  156. } );
  157. it( 'should fire notification event in case of error', done => {
  158. const notification = editor.plugins.get( Notification );
  159. const file = createNativeFileMock();
  160. notification.on( 'show:warning', ( evt, data ) => {
  161. expect( data.message ).to.equal( 'Reading error.' );
  162. expect( data.title ).to.equal( 'Upload failed' );
  163. evt.stop();
  164. done();
  165. }, { priority: 'high' } );
  166. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  167. editor.execute( 'imageUpload', { file } );
  168. nativeReaderMock.mockError( 'Reading error.' );
  169. } );
  170. it( 'should not fire notification on abort', done => {
  171. const notification = editor.plugins.get( Notification );
  172. const file = createNativeFileMock();
  173. const spy = testUtils.sinon.spy();
  174. notification.on( 'show:warning', evt => {
  175. spy();
  176. evt.stop();
  177. }, { priority: 'high' } );
  178. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  179. editor.execute( 'imageUpload', { file } );
  180. nativeReaderMock.abort();
  181. setTimeout( () => {
  182. sinon.assert.notCalled( spy );
  183. done();
  184. }, 0 );
  185. } );
  186. it( 'should do nothing if image does not have uploadId', () => {
  187. setModelData( doc, '<image src="image.png"></image>' );
  188. expect( getViewData( viewDocument ) ).to.equal(
  189. '[<figure class="image ck-widget" contenteditable="false"><img src="image.png"></img></figure>]'
  190. );
  191. } );
  192. it( 'should remove image in case of upload error', done => {
  193. const file = createNativeFileMock();
  194. const spy = testUtils.sinon.spy();
  195. const notification = editor.plugins.get( Notification );
  196. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  197. notification.on( 'show:warning', evt => {
  198. spy();
  199. evt.stop();
  200. }, { priority: 'high' } );
  201. editor.execute( 'imageUpload', { file } );
  202. doc.once( 'changesDone', () => {
  203. doc.once( 'changesDone', () => {
  204. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  205. sinon.assert.calledOnce( spy );
  206. done();
  207. } );
  208. } );
  209. nativeReaderMock.mockError( 'Upload error.' );
  210. } );
  211. it( 'should abort upload if image is removed', () => {
  212. const file = createNativeFileMock();
  213. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  214. editor.execute( 'imageUpload', { file } );
  215. const abortSpy = testUtils.sinon.spy( loader, 'abort' );
  216. expect( loader.status ).to.equal( 'reading' );
  217. nativeReaderMock.mockSuccess( base64Sample );
  218. const image = doc.getRoot().getChild( 0 );
  219. doc.enqueueChanges( () => {
  220. const batch = doc.batch();
  221. batch.remove( image );
  222. } );
  223. expect( loader.status ).to.equal( 'aborted' );
  224. sinon.assert.calledOnce( abortSpy );
  225. } );
  226. it( 'image should be permanently removed if it is removed by user during upload', done => {
  227. const file = createNativeFileMock();
  228. const notification = editor.plugins.get( Notification );
  229. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  230. // Prevent popping up alert window.
  231. notification.on( 'show:warning', evt => {
  232. evt.stop();
  233. }, { priority: 'high' } );
  234. editor.execute( 'imageUpload', { file } );
  235. doc.once( 'changesDone', () => {
  236. // This is called after "manual" remove.
  237. doc.once( 'changesDone', () => {
  238. // This is called after attributes are removed.
  239. let undone = false;
  240. doc.once( 'changesDone', () => {
  241. if ( !undone ) {
  242. undone = true;
  243. // This is called after abort remove.
  244. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  245. editor.execute( 'undo' );
  246. // Expect that the image has not been brought back.
  247. expect( getModelData( doc ) ).to.equal( '<paragraph>[]foo bar</paragraph>' );
  248. done();
  249. }
  250. } );
  251. } );
  252. } );
  253. const image = doc.getRoot().getChild( 0 );
  254. doc.enqueueChanges( () => {
  255. const batch = doc.batch();
  256. batch.remove( image );
  257. } );
  258. } );
  259. it( 'should create responsive image if server return multiple images', done => {
  260. const file = createNativeFileMock();
  261. setModelData( doc, '<paragraph>{}foo bar</paragraph>' );
  262. editor.execute( 'imageUpload', { file } );
  263. doc.once( 'changesDone', () => {
  264. doc.once( 'changesDone', () => {
  265. expect( getViewData( viewDocument ) ).to.equal(
  266. '[<figure class="image ck-widget" contenteditable="false">' +
  267. '<img sizes="100vw" src="image.png" srcset="image-500.png 500w, image-800.png 800w"></img>' +
  268. '</figure>]<p>foo bar</p>'
  269. );
  270. expect( loader.status ).to.equal( 'idle' );
  271. done();
  272. } );
  273. adapterMock.mockSuccess( { original: 'image.png', 500: 'image-500.png', 800: 'image-800.png' } );
  274. } );
  275. nativeReaderMock.mockSuccess( base64Sample );
  276. } );
  277. } );