diff --git a/package.json b/package.json index 2900317..41fae78 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "ckeditor5-feature" ], "dependencies": { - "@ckeditor/ckeditor-cloudservices-core": "^0.1.0", + "@ckeditor/ckeditor-cloudservices-core": "^0.2.0", + "@ckeditor/ckeditor5-cloudservices": "ckeditor/ckeditor5-cloudservices", "@ckeditor/ckeditor5-core": "^1.0.0-alpha.1", "@ckeditor/ckeditor5-image": "^1.0.0-alpha.1", "@ckeditor/ckeditor5-upload": "^1.0.0-alpha.1" diff --git a/src/cloudservicesuploadadapter.js b/src/cloudservicesuploadadapter.js index dfe6dd9..2139618 100644 --- a/src/cloudservicesuploadadapter.js +++ b/src/cloudservicesuploadadapter.js @@ -10,6 +10,7 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository'; import UploadGateway from '@ckeditor/ckeditor-cloudservices-core/src/uploadgateway/uploadgateway'; +import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices'; /** * A plugin which enables upload to Cloud Services. @@ -26,7 +27,7 @@ export default class CloudServicesUploadAdapter extends Plugin { * @inheritDoc */ static get requires() { - return [ FileRepository ]; + return [ FileRepository, CloudServices ]; } /** @@ -34,16 +35,11 @@ export default class CloudServicesUploadAdapter extends Plugin { */ init() { const editor = this.editor; - const config = editor.config; - config.define( 'cloudServices.uploadUrl', 'https://files.cke-cs.com/upload/' ); + const cloudServices = editor.plugins.get( CloudServices ); - const token = config.get( 'cloudServices.token' ); - const uploadUrl = config.get( 'cloudServices.uploadUrl' ); - - if ( !token || !uploadUrl ) { - return; - } + const token = cloudServices.token; + const uploadUrl = cloudServices.uploadUrl || 'https://files.cke-cs.com/upload/'; this._uploadGateway = new CloudServicesUploadAdapter._UploadGateway( token, uploadUrl ); @@ -102,7 +98,7 @@ CloudServicesUploadAdapter._UploadGateway = UploadGateway; * ClassicEditor * .create( editorElement, { * cloudServices: { - * token: '...' + * tokenUrl: '...' * } * } ) * .then( ... ) diff --git a/tests/_utils/gettoken.js b/tests/_utils/gettoken.js deleted file mode 100644 index a7bbc71..0000000 --- a/tests/_utils/gettoken.js +++ /dev/null @@ -1,40 +0,0 @@ -/** - * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. - * For licensing, see LICENSE.md. - */ - -/* globals XMLHttpRequest */ - -import uid from '@ckeditor/ckeditor5-utils/src/uid'; - -// WARNING: The URL below should not be used for any other purpose than Easy Image plugin development. -// Images uploaded using the testing token service may be deleted automatically at any moment. -// If you would like to try the Easy Image service, please wait until the official launch of Easy Image and sign up for a free trial. -// Images uploaded during the free trial will not be deleted for the whole trial period and additionally the trial service can be converted -// into a subscription at any moment, allowing you to preserve all uploaded images. -const CLOUD_SERVICES_TOKEN_URL = 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token'; - -export default function getToken() { - return new Promise( ( resolve, reject ) => { - const xhr = new XMLHttpRequest(); - const userId = uid(); - - xhr.open( 'GET', `${ CLOUD_SERVICES_TOKEN_URL }?user.id=${ userId }` ); - - xhr.onload = () => { - if ( xhr.status >= 200 && xhr.status < 300 ) { - const response = JSON.parse( xhr.responseText ); - - resolve( response.token ); - } else { - reject( new Error( `XHR status: ${ xhr.status }` ) ); - } - }; - - xhr.onerror = err => { - reject( err ); - }; - - xhr.send( null ); - } ); -} diff --git a/tests/cloudservicesuploadadapter.js b/tests/cloudservicesuploadadapter.js index 98f7625..4980a2d 100644 --- a/tests/cloudservicesuploadadapter.js +++ b/tests/cloudservicesuploadadapter.js @@ -8,22 +8,26 @@ import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import CloudServicesUploadAdapter from '../src/cloudservicesuploadadapter'; import FileRepository from '@ckeditor/ckeditor5-upload/src/filerepository'; +import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices'; + import UploadGatewayMock from './_utils/uploadgatewaymock'; import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks'; +import TokenMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/tokenmock'; // Store original uploader. const CSUploader = CloudServicesUploadAdapter._UploadGateway; +const Token = CloudServices.Token; describe( 'CloudServicesUploadAdapter', () => { let div; before( () => { - // Mock uploader. + CloudServices.Token = TokenMock; CloudServicesUploadAdapter._UploadGateway = UploadGatewayMock; } ); after( () => { - // Restore original uploader. + CloudServices.Token = Token; CloudServicesUploadAdapter._UploadGateway = CSUploader; } ); @@ -39,53 +43,36 @@ describe( 'CloudServicesUploadAdapter', () => { describe( 'init()', () => { it( 'should set loader', () => { UploadGatewayMock.lastToken = undefined; + TokenMock.initialToken = 'token'; return ClassicTestEditor .create( div, { plugins: [ CloudServicesUploadAdapter ], cloudServices: { - token: 'abc', + tokenUrl: 'abc', uploadUrl: 'http://upload.mock.url/' } } ) .then( editor => { - expect( UploadGatewayMock.lastToken ).to.equal( 'abc' ); expect( UploadGatewayMock.lastUploadUrl ).to.equal( 'http://upload.mock.url/' ); return editor.destroy(); } ); } ); - it( 'should not set loader if there is no token', () => { - UploadGatewayMock.lastToken = undefined; - - return ClassicTestEditor - .create( div, { - plugins: [ CloudServicesUploadAdapter ] - } ) - .then( editor => { - expect( UploadGatewayMock.lastToken ).to.be.an( 'undefined' ); - - return editor.destroy(); - } ); - } ); - - it( 'should set the default config.cloudServices.uploadUrl', () => { + it( 'should set the default uploadUrl', () => { const expectedDefaultUrl = 'https://files.cke-cs.com/upload/'; return ClassicTestEditor .create( div, { plugins: [ CloudServicesUploadAdapter ], cloudServices: { - token: 'abc' + tokenUrl: 'abc' } } ) .then( editor => { - expect( UploadGatewayMock.lastToken ).to.equal( 'abc' ); expect( UploadGatewayMock.lastUploadUrl ).to.equal( expectedDefaultUrl ); - expect( editor.config.get( 'cloudServices.uploadUrl' ) ).to.equal( expectedDefaultUrl ); - return editor.destroy(); } ); } ); @@ -98,7 +85,7 @@ describe( 'CloudServicesUploadAdapter', () => { return ClassicTestEditor.create( div, { plugins: [ CloudServicesUploadAdapter ], cloudServices: { - token: 'abc', + tokenUrl: 'abc', uploadUrl: 'http://upload.mock.url/' } } ).then( _editor => { diff --git a/tests/easyimage.js b/tests/easyimage.js index d615dc6..cfaaf92 100644 --- a/tests/easyimage.js +++ b/tests/easyimage.js @@ -14,7 +14,20 @@ import UploadGatewayMock from './_utils/uploadgatewaymock'; import { createNativeFileMock } from '@ckeditor/ckeditor5-upload/tests/_utils/mocks'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; +import CloudServices from '@ckeditor/ckeditor5-cloudservices/src/cloudservices'; +import TokenMock from '@ckeditor/ckeditor5-cloudservices/tests/_utils/tokenmock'; + +const Token = CloudServices.Token + describe( 'EasyImage', () => { + before( () => { + CloudServices.Token = TokenMock; + } ); + + after( () => { + CloudServices.Token = Token; + } ); + it( 'should require other plugins', () => { const plugins = EasyImage.requires; @@ -29,7 +42,10 @@ describe( 'EasyImage', () => { return ClassicTestEditor .create( div, { - plugins: [ EasyImage ] + plugins: [ EasyImage ], + cloudServices: { + tokenUrl: 'abc', + } } ) .then( editor => { const easyImage = editor.plugins.get( EasyImage ); @@ -83,7 +99,7 @@ describe( 'EasyImage', () => { Paragraph, EasyImage ], cloudServices: { - token: 'abc', + tokenUrl: 'abc', uploadUrl: 'http://upload.mock.url/' } } ) diff --git a/tests/manual/easyimage.js b/tests/manual/easyimage.js index 938e5d3..bac7e7e 100644 --- a/tests/manual/easyimage.js +++ b/tests/manual/easyimage.js @@ -10,24 +10,21 @@ import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor' import EasyImage from '../../src/easyimage'; import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset'; -import getToken from '../_utils/gettoken'; +import { TOKEN_URL } from '@ckeditor/ckeditor5-cloudservices/tests/_utils/cloudservices-config'; -getToken() - .then( token => { - return ClassicEditor - .create( document.querySelector( '#editor' ), { - cloudServices: { - token - }, - plugins: [ ArticlePluginSet, EasyImage ], - toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ], - image: { - toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ] - } - } ) - .then( editor => { - window.editor = editor; - } ); +ClassicEditor + .create( document.querySelector( '#editor' ), { + cloudServices: { + tokenUrl: TOKEN_URL + }, + plugins: [ ArticlePluginSet, EasyImage ], + toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ], + image: { + toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ] + } + } ) + .then( editor => { + window.editor = editor; } ) .catch( err => { console.error( err.stack );