imageuploadengine.js 12 KB

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