|
| 1 | +/** |
| 2 | + * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. |
| 3 | + * For licensing, see LICENSE.md. |
| 4 | + */ |
| 5 | + |
| 6 | +/** |
| 7 | + * @module cloudservices/cloudservices |
| 8 | + */ |
| 9 | + |
| 10 | +import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; |
| 11 | +import Token from '@ckeditor/ckeditor-cloudservices-core/src/token/token'; |
| 12 | +import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; |
| 13 | + |
| 14 | +/** |
| 15 | + * Base plugin for Cloud Services. It takes care about the `cloudServices` config options and initializes token provider. |
| 16 | + */ |
| 17 | +export default class CloudServices extends Plugin { |
| 18 | + /** |
| 19 | + * @inheritDoc |
| 20 | + */ |
| 21 | + init() { |
| 22 | + const editor = this.editor; |
| 23 | + const config = editor.config; |
| 24 | + |
| 25 | + const options = config.get( 'cloudServices' ); |
| 26 | + |
| 27 | + for ( const optionName in options ) { |
| 28 | + this[ optionName ] = options[ optionName ]; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * The authentication token URL for CloudServices. |
| 33 | + * |
| 34 | + * @readonly |
| 35 | + * @member {String} #tokenUrl |
| 36 | + */ |
| 37 | + |
| 38 | + if ( !this.tokenUrl ) { |
| 39 | + /** |
| 40 | + * The authentication `cloudServices.token` config is not provided. |
| 41 | + * |
| 42 | + * @error cloudservices-token-endpoint-not-provided |
| 43 | + */ |
| 44 | + throw new CKEditorError( |
| 45 | + 'cloudservices-token-endpoint-not-provided: The authentication `cloudServices.token` config is not provided.' |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Other plugins use this token for authorization process. It handles token requesting and refreshing. |
| 51 | + * |
| 52 | + * @readonly |
| 53 | + */ |
| 54 | + this.token = new CloudServices.Token( this.tokenUrl ); |
| 55 | + |
| 56 | + return this.token.init(); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +CloudServices.Token = Token; |
| 61 | + |
| 62 | +/** |
| 63 | + * The configuration of the Cloud Services. Introduced by the {@link module:cloudservices/cloudservices~CloudServices} plugin. |
| 64 | + * |
| 65 | + * Read more in {@link module:cloudservices/cloudservices~CloudServices}. |
| 66 | + * |
| 67 | + * @member {module:cloudservices/cloudservices~CloudServicesConfig} module:core/editor/editorconfig~EditorConfig#cloudServices |
| 68 | + */ |
| 69 | + |
| 70 | +/** |
| 71 | + * The configuration for all plugins using Cloud Services. |
| 72 | + * |
| 73 | + * ClassicEditor |
| 74 | + * .create( { |
| 75 | + * cloudServices: ... // CloudServices config. |
| 76 | + * } ) |
| 77 | + * .then( ... ) |
| 78 | + * .catch( ... ); |
| 79 | + * |
| 80 | + * @interface CloudServicesConfig |
| 81 | + */ |
| 82 | + |
| 83 | +/** |
| 84 | + * The authentication token URL for CloudServices. Token us used to authenticate all plugins using CloudServices, |
| 85 | + * for instance Easy Image. The token URL have to point to the service where the token is generated. |
| 86 | + * |
| 87 | + * ClassicEditor |
| 88 | + * .create( document.querySelector( '#editor' ), { |
| 89 | + * cloudServices: { |
| 90 | + * tokenUrl: TOKEN_URL |
| 91 | + * }, |
| 92 | + * plugins: [ ArticlePluginSet, EasyImage ], |
| 93 | + * toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ], |
| 94 | + * image: { |
| 95 | + * toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ] |
| 96 | + * } |
| 97 | + * } ); |
| 98 | + * |
| 99 | + * @member {String} module:cloudservices/cloudservices~CloudServicesConfig#tokenUrl |
| 100 | + */ |
0 commit comments