imageuploadui.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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 be disabled while ImageUploadCommand is disabled', () => {
  52. const button = editor.ui.componentFactory.create( 'imageUpload' );
  53. const command = editor.commands.get( 'imageUpload' );
  54. command.isEnabled = true;
  55. expect( button.buttonView.isEnabled ).to.true;
  56. command.isEnabled = false;
  57. expect( button.buttonView.isEnabled ).to.false;
  58. } );
  59. // ckeditor5-upload/#77
  60. it( 'should be properly bound with ImageUploadCommand', () => {
  61. const button = editor.ui.componentFactory.create( 'imageUpload' );
  62. const command = editor.commands.get( 'imageUpload' );
  63. const spy = sinon.spy();
  64. button.render();
  65. button.buttonView.on( 'execute', spy );
  66. command.isEnabled = false;
  67. button.buttonView.element.dispatchEvent( new Event( 'click' ) );
  68. sinon.assert.notCalled( spy );
  69. } );
  70. it( 'should execute imageUpload command', () => {
  71. const executeStub = sinon.stub( editor, 'execute' );
  72. const button = editor.ui.componentFactory.create( 'imageUpload' );
  73. const files = [ createNativeFileMock() ];
  74. button.fire( 'done', files );
  75. sinon.assert.calledOnce( executeStub );
  76. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  77. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( files );
  78. } );
  79. it( 'should execute imageUpload command with multiple files', () => {
  80. const executeStub = sinon.stub( editor, 'execute' );
  81. const button = editor.ui.componentFactory.create( 'imageUpload' );
  82. const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
  83. button.fire( 'done', files );
  84. sinon.assert.calledOnce( executeStub );
  85. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  86. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( files );
  87. } );
  88. it( 'should optimize the insertion position', () => {
  89. const button = editor.ui.componentFactory.create( 'imageUpload' );
  90. const files = [ createNativeFileMock() ];
  91. setModelData( model, '<paragraph>f[]oo</paragraph>' );
  92. button.fire( 'done', files );
  93. const id = fileRepository.getLoader( files[ 0 ] ).id;
  94. expect( getModelData( model ) ).to.equal(
  95. `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
  96. '<paragraph>foo</paragraph>'
  97. );
  98. } );
  99. it( 'should correctly insert multiple files', () => {
  100. const button = editor.ui.componentFactory.create( 'imageUpload' );
  101. const files = [ createNativeFileMock(), createNativeFileMock() ];
  102. setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
  103. button.fire( 'done', files );
  104. const id1 = fileRepository.getLoader( files[ 0 ] ).id;
  105. const id2 = fileRepository.getLoader( files[ 1 ] ).id;
  106. expect( getModelData( model ) ).to.equal(
  107. '<paragraph>foo</paragraph>' +
  108. `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
  109. `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
  110. '<paragraph>bar</paragraph>'
  111. );
  112. } );
  113. it( 'should not execute imageUpload if the file is not an image', () => {
  114. const executeStub = sinon.stub( editor, 'execute' );
  115. const button = editor.ui.componentFactory.create( 'imageUpload' );
  116. const file = {
  117. type: 'media/mp3',
  118. size: 1024
  119. };
  120. button.fire( 'done', [ file ] );
  121. sinon.assert.notCalled( executeStub );
  122. } );
  123. it( 'should work even if the FileList does not support iterators', () => {
  124. const executeStub = sinon.stub( editor, 'execute' );
  125. const button = editor.ui.componentFactory.create( 'imageUpload' );
  126. const files = {
  127. 0: createNativeFileMock(),
  128. length: 1
  129. };
  130. button.fire( 'done', files );
  131. sinon.assert.calledOnce( executeStub );
  132. expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
  133. expect( executeStub.firstCall.args[ 1 ].files ).to.deep.equal( [ files[ 0 ] ] );
  134. } );
  135. } );