Skip to content

Commit

Permalink
Added ckeditor5-easy-image/_src.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Feb 13, 2023
1 parent d67b63c commit 261a1ea
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
88 changes: 88 additions & 0 deletions packages/ckeditor5-easy-image/_src/cloudservicesuploadadapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module easy-image/cloudservicesuploadadapter
*/

import { Plugin } from 'ckeditor5/src/core';
import { FileRepository } from 'ckeditor5/src/upload';

/**
* A plugin that enables upload to [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services/).
*
* It is mainly used by the {@link module:easy-image/easyimage~EasyImage} feature.
*
* After enabling this adapter you need to configure the CKEditor Cloud Services integration through
* {@link module:cloud-services/cloudservices~CloudServicesConfig `config.cloudServices`}.
*
* @extends module:core/plugin~Plugin
*/
export default class CloudServicesUploadAdapter extends Plugin {
/**
* @inheritDoc
*/
static get pluginName() {
return 'CloudServicesUploadAdapter';
}

/**
* @inheritDoc
*/
static get requires() {
return [ 'CloudServices', FileRepository ];
}

/**
* @inheritDoc
*/
init() {
const editor = this.editor;

const cloudServices = editor.plugins.get( 'CloudServices' );

const token = cloudServices.token;
const uploadUrl = cloudServices.uploadUrl;

if ( !token ) {
return;
}

this._uploadGateway = editor.plugins.get( 'CloudServicesCore' ).createUploadGateway( token, uploadUrl );

editor.plugins.get( FileRepository ).createUploadAdapter = loader => {
return new Adapter( this._uploadGateway, loader );
};
}
}

/**
* @private
*/
class Adapter {
constructor( uploadGateway, loader ) {
this.uploadGateway = uploadGateway;

this.loader = loader;
}

upload() {
return this.loader.file.then( file => {
this.fileUploader = this.uploadGateway.upload( file );

this.fileUploader.on( 'progress', ( evt, data ) => {
this.loader.uploadTotal = data.total;
this.loader.uploaded = data.uploaded;
} );

return this.fileUploader.send();
} );
}

abort() {
this.fileUploader.abort();
}
}

78 changes: 78 additions & 0 deletions packages/ckeditor5-easy-image/_src/easyimage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module easy-image/easyimage
*/

import { Plugin } from 'ckeditor5/src/core';
import { logWarning } from 'ckeditor5/src/utils';

import CloudServicesUploadAdapter from './cloudservicesuploadadapter';

/**
* The Easy Image feature, which makes the image upload in CKEditor 5 possible with virtually zero
* server setup. A part of the [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services/)
* family.
*
* This is a "glue" plugin which enables:
*
* * {@link module:easy-image/cloudservicesuploadadapter~CloudServicesUploadAdapter}.
*
* This plugin requires plugin to be present in the editor configuration:
*
* * {@link module:image/image~Image},
* * {@link module:image/imageupload~ImageUpload},
*
* See the {@glink features/images/image-upload/easy-image "Easy Image integration" guide} to learn how to configure
* and use this feature.
*
* Check out the {@glink features/images/image-upload/image-upload comprehensive "Image upload" guide} to learn about
* other ways to upload images into CKEditor 5.
*
* **Note**: After enabling the Easy Image plugin you need to configure the
* [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services/)
* integration through {@link module:cloud-services/cloudservices~CloudServicesConfig `config.cloudServices`}.
*
* @extends module:core/plugin~Plugin
*/
export default class EasyImage extends Plugin {
/**
* @inheritDoc
*/
static get requires() {
return [ CloudServicesUploadAdapter, 'ImageUpload' ];
}

/**
* @inheritDoc
*/
init() {
const editor = this.editor;

if ( !editor.plugins.has( 'ImageBlockEditing' ) && !editor.plugins.has( 'ImageInlineEditing' ) ) {
/**
* The Easy Image feature requires one of the following plugins to be loaded to work correctly:
*
* * {@link module:image/imageblock~ImageBlock},
* * {@link module:image/imageinline~ImageInline},
* * {@link module:image/image~Image} (loads both `ImageBlock` and `ImageInline`)
*
* Please make sure your editor configuration is correct.
*
* @error easy-image-image-feature-missing
* @param {module:core/editor/editor~Editor} editor
*/
logWarning( 'easy-image-image-feature-missing', editor );
}
}

/**
* @inheritDoc
*/
static get pluginName() {
return 'EasyImage';
}
}
11 changes: 11 additions & 0 deletions packages/ckeditor5-easy-image/_src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

/**
* @module easy-image
*/

export { default as EasyImage } from './easyimage';
export { default as CloudServicesUploadAdapter } from './cloudservicesuploadadapter';

0 comments on commit 261a1ea

Please sign in to comment.