imageuploadui.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 FileDialogButtonView from '@ckeditor/ckeditor5-upload/src/ui/filedialogbuttonview';
  10. import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
  11. import ImageUploadUI from '../../src/imageupload/imageuploadui';
  12. import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
  13. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  14. import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
  15. import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
  16. import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
  17. import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  18. describe( 'ImageUploadUI', () => {
  19. let editor, model, editorElement, fileRepository;
  20. class UploadAdapterPluginMock extends Plugin {
  21. init() {
  22. fileRepository = this.editor.plugins.get( FileRepository );
  23. fileRepository.createUploadAdapter = loader => {
  24. return new UploadAdapterMock( loader );
  25. };
  26. }
  27. }
  28. beforeEach( () => {
  29. editorElement = document.createElement( 'div' );
  30. document.body.appendChild( editorElement );
  31. return ClassicEditor
  32. .create( editorElement, {
  33. plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
  34. } )
  35. .then( newEditor => {
  36. editor = newEditor;
  37. model = editor.model;
  38. // Hide all notifications (prevent alert() calls).
  39. const notification = editor.plugins.get( Notification );
  40. notification.on( 'show', evt => evt.stop() );
  41. } );
  42. } );
  43. afterEach( () => {
  44. editorElement.remove();
  45. return editor.destroy();
  46. } );
  47. it( 'should register imageUpload button', () => {
  48. const button = editor.ui.componentFactory.create( 'imageUpload' );
  49. expect( button ).to.be.instanceOf( FileDialogButtonView );
  50. } );
  51. it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
  52. editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
  53. const button = editor.ui.componentFactory.create( 'imageUpload' );
  54. expect( button.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
  55. } );
  56. it( 'should be disabled while ImageUploadCommand is disabled', () => {
  57. const button = editor.ui.componentFactory.create( 'imageUpload' );
  58. const command = editor.commands.get( 'imageUpload' );
  59. command.isEnabled = true;
  60. expect( button.buttonView.isEnabled ).to.true;
  61. command.isEnabled = false;
  62. expect( button.buttonView.isEnabled ).to.false;
  63. } );
  64. // ckeditor5-upload/#77
  65. it( 'should be properly bound with ImageUploadCommand', () => {
  66. const button = editor.ui.componentFactory.create( 'imageUpload' );
  67. const command = editor.commands.get( 'imageUpload' );
  68. const spy = sinon.spy();
  69. button.render();
  70. button.buttonView.on( 'execute', spy );
  71. command.isEnabled = false;
  72. button.buttonView.element.dispatchEvent( new Event( 'click' ) );
  73. sinon.assert.notCalled( spy );
  74. } );
  75. it( 'should execute imageUpload command', () => {
  76. const executeStub = sinon.stub( editor, 'execute' );
  77. const button = editor.ui.componentFactory.create( 'imageUpload' );
  78. const files = [ createNativeFileMock() ];
  79. button.fire( 'done', files );
  80. sinon.assert.calledOnce( executeStub );
  81. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  82. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  83. } );
  84. it( 'should execute imageUpload command with multiple files', () => {
  85. const executeStub = sinon.stub( editor, 'execute' );
  86. const button = editor.ui.componentFactory.create( 'imageUpload' );
  87. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  88. button.fire( 'done', files );
  89. sinon.assert.calledOnce( executeStub );
  90. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  91. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
  92. } );
  93. it( 'should optimize the insertion position', () => {
  94. const button = editor.ui.componentFactory.create( 'imageUpload' );
  95. const files = [ createNativeFileMock() ];
  96. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  97. button.fire( 'done', files );
  98. const id = fileRepository.getLoader( files[ 0 ] ).id;
  99. expect( getModelData( model ) ).to.equal(
  100. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  101. '<paragraph>foo</paragraph>'
  102. );
  103. } );
  104. it( 'should correctly insert multiple files', () => {
  105. const button = editor.ui.componentFactory.create( 'imageUpload' );
  106. const files = [ createNativeFileMock(), createNativeFileMock() ];
  107. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  108. button.fire( 'done', files );
  109. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  110. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  111. expect( getModelData( model ) ).to.equal(
  112. '<paragraph>foo</paragraph>' +
  113. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  114. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  115. '<paragraph>bar</paragraph>'
  116. );
  117. } );
  118. it( 'should not execute imageUpload if the file is not an image', () => {
  119. const executeStub = sinon.stub( editor, 'execute' );
  120. const button = editor.ui.componentFactory.create( 'imageUpload' );
  121. const file = {
  122. type: 'media/mp3',
  123. size: 1024
  124. };
  125. button.fire( 'done', [ file ] );
  126. sinon.assert.notCalled( executeStub );
  127. } );
  128. it( 'should work even if the FileList does not support iterators', () => {
  129. const executeStub = sinon.stub( editor, 'execute' );
  130. const button = editor.ui.componentFactory.create( 'imageUpload' );
  131. const files = {
  132. 0: createNativeFileMock(),
  133. length: 1
  134. };
  135. button.fire( 'done', files );
  136. sinon.assert.calledOnce( executeStub );
  137. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  138. expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
  139. } );
  140. } );