8
0

imageuploadui.js 6.5 KB

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