| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* globals document, Event */
- import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
- import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
- import Image from '../../src/image';
- import DropdownView from '@ckeditor/ckeditor5-ui/src/dropdown/dropdownview';
- import FileDialogButtonView from '@ckeditor/ckeditor5-upload/src/ui/filedialogbuttonview';
- import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository';
- import ImageUploadUI from '../../src/imageupload/imageuploadui';
- import ImageUploadEditing from '../../src/imageupload/imageuploadediting';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import Notification from '@ckeditor/ckeditor5-ui/src/notification/notification';
- import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
- import EventInfo from '@ckeditor/ckeditor5-utils/src/eventinfo';
- import DomEventData from '@ckeditor/ckeditor5-engine/src/view/observer/domeventdata';
- import CKFinder from '@ckeditor/ckeditor5-ckfinder/src/ckfinder';
- import LabeledFieldView from '@ckeditor/ckeditor5-ui/src/labeledfield/labeledfieldview';
- import ButtonView from '@ckeditor/ckeditor5-ui/src/button/buttonview';
- import { createNativeFileMock, UploadAdapterMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks';
- import { setData as setModelData, getData as getModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- describe( 'ImageUploadUI', () => {
- let editor, model, editorElement, fileRepository;
- class UploadAdapterPluginMock extends Plugin {
- init() {
- fileRepository = this.editor.plugins.get( FileRepository );
- fileRepository.createUploadAdapter = loader => {
- return new UploadAdapterMock( loader );
- };
- }
- }
- describe( 'file dialog button', () => {
- beforeEach( () => {
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
- return ClassicEditor
- .create( editorElement, {
- plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- // Hide all notifications (prevent alert() calls).
- const notification = editor.plugins.get( Notification );
- notification.on( 'show', evt => evt.stop() );
- } );
- } );
- afterEach( () => {
- editorElement.remove();
- return editor.destroy();
- } );
- it( 'should register imageUpload file dialog button', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- expect( button ).to.be.instanceOf( FileDialogButtonView );
- } );
- it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
- editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- expect( button.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
- } );
- it( 'should be disabled while ImageUploadCommand is disabled', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const command = editor.commands.get( 'imageUpload' );
- command.isEnabled = true;
- expect( button.buttonView.isEnabled ).to.true;
- command.isEnabled = false;
- expect( button.buttonView.isEnabled ).to.false;
- } );
- // ckeditor5-upload/#77
- it( 'should be properly bound with ImageUploadCommand', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const command = editor.commands.get( 'imageUpload' );
- const spy = sinon.spy();
- button.render();
- button.buttonView.on( 'execute', spy );
- command.isEnabled = false;
- button.buttonView.element.dispatchEvent( new Event( 'click' ) );
- sinon.assert.notCalled( spy );
- } );
- it( 'should execute imageUpload command', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = [ createNativeFileMock() ];
- button.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
- } );
- it( 'should execute imageUpload command with multiple files', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
- button.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
- } );
- it( 'should optimize the insertion position', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = [ createNativeFileMock() ];
- setModelData( model, '<paragraph>f[]oo</paragraph>' );
- button.fire( 'done', files );
- const id = fileRepository.getLoader( files[ 0 ] ).id;
- expect( getModelData( model ) ).to.equal(
- `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
- '<paragraph>foo</paragraph>'
- );
- } );
- it( 'should correctly insert multiple files', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = [ createNativeFileMock(), createNativeFileMock() ];
- setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
- button.fire( 'done', files );
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
- const id2 = fileRepository.getLoader( files[ 1 ] ).id;
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
- `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not execute imageUpload if the file is not an image', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const file = {
- type: 'media/mp3',
- size: 1024
- };
- button.fire( 'done', [ file ] );
- sinon.assert.notCalled( executeStub );
- } );
- it( 'should work even if the FileList does not support iterators', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = {
- 0: createNativeFileMock(),
- length: 1
- };
- button.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
- } );
- it( 'should add the new image after the selected one, without replacing the selected image', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const files = [ createNativeFileMock() ];
- setModelData( model, '[<image src="/assets/sample.png"></image>]<paragraph>bar</paragraph>' );
- button.fire( 'done', files );
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
- expect( getModelData( model ) ).to.equal(
- '<image src="/assets/sample.png"></image>' +
- `[<image uploadId="${ id1 }" uploadStatus="reading"></image>]` +
- '<paragraph>bar</paragraph>'
- );
- } );
- } );
- describe( 'dropdown', () => {
- beforeEach( () => {
- editorElement = document.createElement( 'div' );
- document.body.appendChild( editorElement );
- return ClassicEditor
- .create( editorElement, {
- plugins: [ Paragraph, Image, ImageUploadEditing, ImageUploadUI, FileRepository, UploadAdapterPluginMock, Clipboard ],
- image: {
- upload: {
- panel: {
- items: [
- 'insertImageViaUrl'
- ]
- }
- }
- }
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- // Hide all notifications (prevent alert() calls).
- const notification = editor.plugins.get( Notification );
- notification.on( 'show', evt => evt.stop() );
- } );
- } );
- afterEach( () => {
- editorElement.remove();
- return editor.destroy();
- } );
- it( 'should register imageUpload dropdown', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- expect( button ).to.be.instanceOf( DropdownView );
- } );
- it( 'should set proper accepted mime-types for imageUpload button as defined in configuration', () => {
- editor.config.set( 'image.upload.types', [ 'svg+xml', 'jpeg', 'vnd.microsoft.icon', 'x-xbitmap' ] );
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- expect( fileDialogButton.acceptedType ).to.equal( 'image/svg+xml,image/jpeg,image/vnd.microsoft.icon,image/x-xbitmap' );
- } );
- it( 'should be disabled while ImageUploadCommand is disabled', () => {
- const button = editor.ui.componentFactory.create( 'imageUpload' );
- const command = editor.commands.get( 'imageUpload' );
- command.isEnabled = true;
- expect( button.buttonView.isEnabled ).to.true;
- command.isEnabled = false;
- expect( button.buttonView.isEnabled ).to.false;
- } );
- // ckeditor5-upload/#77
- it( 'should be properly bound with ImageUploadCommand', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const command = editor.commands.get( 'imageUpload' );
- const spy = sinon.spy();
- dropdown.render();
- dropdown.buttonView.on( 'execute', spy );
- command.isEnabled = false;
- dropdown.buttonView.element.dispatchEvent( new Event( 'click' ) );
- sinon.assert.notCalled( spy );
- } );
- it( 'should execute imageUpload command', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const files = [ createNativeFileMock() ];
- fileDialogButton.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
- } );
- it( 'should execute imageUpload command with multiple files', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const files = [ createNativeFileMock(), createNativeFileMock(), createNativeFileMock() ];
- fileDialogButton.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( files );
- } );
- it( 'should optimize the insertion position', () => {
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const files = [ createNativeFileMock() ];
- setModelData( model, '<paragraph>f[]oo</paragraph>' );
- fileDialogButton.fire( 'done', files );
- const id = fileRepository.getLoader( files[ 0 ] ).id;
- expect( getModelData( model ) ).to.equal(
- `[<image uploadId="${ id }" uploadStatus="reading"></image>]` +
- '<paragraph>foo</paragraph>'
- );
- } );
- it( 'should correctly insert multiple files', () => {
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const files = [ createNativeFileMock(), createNativeFileMock() ];
- setModelData( model, '<paragraph>foo[]</paragraph><paragraph>bar</paragraph>' );
- fileDialogButton.fire( 'done', files );
- const id1 = fileRepository.getLoader( files[ 0 ] ).id;
- const id2 = fileRepository.getLoader( files[ 1 ] ).id;
- expect( getModelData( model ) ).to.equal(
- '<paragraph>foo</paragraph>' +
- `<image uploadId="${ id1 }" uploadStatus="reading"></image>` +
- `[<image uploadId="${ id2 }" uploadStatus="reading"></image>]` +
- '<paragraph>bar</paragraph>'
- );
- } );
- it( 'should not execute imageUpload if the file is not an image', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const file = {
- type: 'media/mp3',
- size: 1024
- };
- fileDialogButton.fire( 'done', [ file ] );
- sinon.assert.notCalled( executeStub );
- } );
- it( 'should work even if the FileList does not support iterators', () => {
- const executeStub = sinon.stub( editor, 'execute' );
- const plugin = editor.plugins.get( 'ImageUploadUI' );
- const fileDialogButton = plugin._createFileDialogButtonView( editor.locale );
- const files = {
- 0: createNativeFileMock(),
- length: 1
- };
- fileDialogButton.fire( 'done', files );
- sinon.assert.calledOnce( executeStub );
- expect( executeStub.firstCall.args[ 0 ] ).to.equal( 'imageUpload' );
- expect( executeStub.firstCall.args[ 1 ].file ).to.deep.equal( [ files[ 0 ] ] );
- } );
- describe( 'dropdown action button', () => {
- it( 'should be an instance of FileDialogButtonView', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- expect( dropdown.buttonView.actionView ).to.be.instanceOf( FileDialogButtonView );
- } );
- } );
- describe( 'dropdown panel buttons', () => {
- it( 'should have "Update" label on submit button when URL input is already filled', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const viewDocument = editor.editing.view.document;
- editor.setData( '<figure><img src="/assets/sample.png" /></figure>' );
- editor.editing.view.change( writer => {
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
- } );
- const img = viewDocument.selection.getSelectedElement();
- const data = fakeEventData();
- const eventInfo = new EventInfo( img, 'click' );
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
- viewDocument.fire( 'click', domEventDataMock );
- dropdown.isOpen = true;
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
- expect( dropdown.isOpen ).to.be.true;
- expect( inputValue ).to.equal( '/assets/sample.png' );
- expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Update' );
- } );
- it( 'should have "Insert" label on submit button on uploading a new image', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const viewDocument = editor.editing.view.document;
- editor.setData( '<p>test</p>' );
- editor.editing.view.change( writer => {
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'end' );
- } );
- const el = viewDocument.selection.getSelectedElement();
- const data = fakeEventData();
- const eventInfo = new EventInfo( el, 'click' );
- const domEventDataMock = new DomEventData( viewDocument, eventInfo, data );
- viewDocument.fire( 'click', domEventDataMock );
- dropdown.isOpen = true;
- const inputValue = dropdown.panelView.children.first.imageURLInputValue;
- expect( dropdown.isOpen ).to.be.true;
- expect( inputValue ).to.equal( '' );
- expect( dropdown.panelView.children.first.insertButtonView.label ).to.equal( 'Insert' );
- } );
- } );
- it( 'should remove all attributes from model except "src" when updating the image source URL', () => {
- const viewDocument = editor.editing.view.document;
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const insertButtonView = dropdown.panelView.children.first.insertButtonView;
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
- const submitSpy = sinon.spy();
- dropdown.isOpen = true;
- editor.setData( '<figure><img src="image-url-800w.jpg"' +
- 'srcset="image-url-480w.jpg 480w,image-url-800w.jpg 800w"' +
- 'sizes="(max-width: 600px) 480px,800px"' +
- 'alt="test-image"></figure>' );
- editor.editing.view.change( writer => {
- writer.setSelection( viewDocument.getRoot().getChild( 0 ), 'on' );
- } );
- const selectedElement = editor.model.document.selection.getSelectedElement();
- expect( selectedElement.getAttribute( 'src' ) ).to.equal( 'image-url-800w.jpg' );
- expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.true;
- dropdown.panelView.children.first.imageURLInputValue = '/assets/sample3.png';
- dropdown.on( 'submit', submitSpy );
- insertButtonView.fire( 'execute' );
- sinon.assert.notCalled( commandSpy );
- sinon.assert.calledOnce( submitSpy );
- expect( dropdown.isOpen ).to.be.false;
- expect( selectedElement.getAttribute( 'src' ) ).to.equal( '/assets/sample3.png' );
- expect( selectedElement.hasAttribute( 'srcset' ) ).to.be.false;
- expect( selectedElement.hasAttribute( 'sizes' ) ).to.be.false;
- } );
- describe( 'events', () => {
- it( 'should emit "submit" event when clicking on submit button', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const insertButtonView = dropdown.panelView.children.first.insertButtonView;
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
- const submitSpy = sinon.spy();
- dropdown.isOpen = true;
- dropdown.on( 'submit', submitSpy );
- insertButtonView.fire( 'execute' );
- expect( dropdown.isOpen ).to.be.false;
- sinon.assert.calledOnce( commandSpy );
- sinon.assert.calledOnce( submitSpy );
- } );
- it( 'should emit "cancel" event when clicking on cancel button', () => {
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- const cancelButtonView = dropdown.panelView.children.first.cancelButtonView;
- const commandSpy = sinon.spy( editor.commands.get( 'imageInsert' ), 'execute' );
- const cancelSpy = sinon.spy();
- dropdown.isOpen = true;
- dropdown.on( 'cancel', cancelSpy );
- cancelButtonView.fire( 'execute' );
- expect( dropdown.isOpen ).to.be.false;
- sinon.assert.notCalled( commandSpy );
- sinon.assert.calledOnce( cancelSpy );
- } );
- } );
- it( 'should inject integrations to the dropdown panel view from the config', async () => {
- const editor = await ClassicEditor
- .create( editorElement, {
- plugins: [
- CKFinder,
- Paragraph,
- Image,
- ImageUploadEditing,
- ImageUploadUI,
- FileRepository,
- UploadAdapterPluginMock,
- Clipboard
- ],
- image: {
- upload: {
- panel: {
- items: [
- 'insertImageViaUrl',
- 'openCKFinder'
- ]
- }
- }
- }
- } );
- const dropdown = editor.ui.componentFactory.create( 'imageUpload' );
- expect( dropdown.panelView.children.first._integrations.length ).to.equal( 2 );
- expect( dropdown.panelView.children.first._integrations.first ).to.be.instanceOf( LabeledFieldView );
- expect( dropdown.panelView.children.first._integrations.last ).to.be.instanceOf( ButtonView );
- editor.destroy();
- } );
- } );
- } );
- function fakeEventData() {
- return {
- preventDefault: sinon.spy()
- };
- }
|