cloudservices.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module cloud-services/cloudservices
  7. */
  8. import ContextPlugin from '@ckeditor/ckeditor5-core/src/contextplugin';
  9. import Token from '@ckeditor/ckeditor-cloud-services-core/src/token/token';
  10. /**
  11. * Plugin introducing integration between CKEditor 5 and CKEditor Cloud Services .
  12. *
  13. * It initializes the token provider based on
  14. * the {@link module:cloud-services/cloudservices~CloudServicesConfig `config.cloudService`}.
  15. *
  16. * @extends module:core/plugin~Plugin
  17. */
  18. export default class CloudServices extends ContextPlugin {
  19. /**
  20. * @inheritdoc
  21. */
  22. static get pluginName() {
  23. return 'CloudServices';
  24. }
  25. /**
  26. * @inheritDoc
  27. */
  28. init() {
  29. const config = this.context.config;
  30. const options = config.get( 'cloudServices' ) || {};
  31. for ( const optionName in options ) {
  32. this[ optionName ] = options[ optionName ];
  33. }
  34. /**
  35. * The authentication token URL for CKEditor Cloud Services or a callback to the token value promise. See the
  36. * {@link module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl} for more details.
  37. *
  38. * @readonly
  39. * @member {String|Function|undefined} #tokenUrl
  40. */
  41. /**
  42. * The URL to which the files should be uploaded.
  43. *
  44. * @readonly
  45. * @member {String} #uploadUrl
  46. */
  47. /**
  48. * Other plugins use this token for the authorization process. It handles token requesting and refreshing.
  49. * Its value is `null` when {@link module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl} is not provided.
  50. *
  51. * @readonly
  52. * @member {Object|null} #token
  53. */
  54. if ( !this.tokenUrl ) {
  55. this.token = null;
  56. return;
  57. }
  58. this.token = new CloudServices.Token( this.tokenUrl );
  59. return this.token.init();
  60. }
  61. /**
  62. * @inheritDoc
  63. */
  64. destroy() {
  65. super.destroy();
  66. if ( this.token ) {
  67. this.token.destroy();
  68. }
  69. }
  70. }
  71. CloudServices.Token = Token;
  72. /**
  73. * The configuration of CKEditor Cloud Services. Introduced by the {@link module:cloud-services/cloudservices~CloudServices} plugin.
  74. *
  75. * Read more in {@link module:cloud-services/cloudservices~CloudServicesConfig}.
  76. *
  77. * @member {module:cloud-services/cloudservices~CloudServicesConfig} module:core/editor/editorconfig~EditorConfig#cloudServices
  78. */
  79. /**
  80. * The configuration for all plugins using CKEditor Cloud Services.
  81. *
  82. * ClassicEditor
  83. * .create( document.querySelector( '#editor' ), {
  84. * cloudServices: {
  85. * tokenUrl: 'https://example.com/cs-token-endpoint',
  86. * uploadUrl: 'https://your-organization-id.cke-cs.com/easyimage/upload/'
  87. * }
  88. * } )
  89. * .then( ... )
  90. * .catch( ... );
  91. *
  92. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  93. *
  94. * @interface CloudServicesConfig
  95. */
  96. /**
  97. * A token URL or a token request function.
  98. *
  99. * As a string, it should be a URL to the security token endpoint in your application. The role of this endpoint is to securely authorize
  100. * the end users of your application to use [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services) only
  101. * if they should have access e.g. to upload files with {@glink @cs guides/easy-image/quick-start Easy Image} or to use the
  102. * {@glink @cs guides/collaboration/quick-start Collaboration} service.
  103. *
  104. * ClassicEditor
  105. * .create( document.querySelector( '#editor' ), {
  106. * cloudServices: {
  107. * tokenUrl: 'https://example.com/cs-token-endpoint',
  108. * ...
  109. * }
  110. * } )
  111. * .then( ... )
  112. * .catch( ... );
  113. *
  114. * As a function, it should provide a promise to the token value, so you can highly customize the token and provide your token URL endpoint.
  115. * By using this approach you can set your own headers for the request.
  116. *
  117. * ClassicEditor
  118. * .create( document.querySelector( '#editor' ), {
  119. * cloudServices: {
  120. * tokenUrl: () => new Promise( ( resolve, reject ) => {
  121. * const xhr = new XMLHttpRequest();
  122. *
  123. * xhr.open( 'GET', 'https://example.com/cs-token-endpoint' );
  124. *
  125. * xhr.addEventListener( 'load', () => {
  126. * const statusCode = xhr.status;
  127. * const xhrResponse = xhr.response;
  128. *
  129. * if ( statusCode < 200 || statusCode > 299 ) {
  130. * return reject( new Error( 'Cannot download new token!' ) );
  131. * }
  132. *
  133. * return resolve( xhrResponse );
  134. * } );
  135. *
  136. * xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) );
  137. * xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) );
  138. *
  139. * xhr.setRequestHeader( customHeader, customValue );
  140. *
  141. * xhr.send();
  142. * } ),
  143. * ...
  144. * }
  145. * } )
  146. *
  147. * You can find more information about token endpoints in the
  148. * {@glink @cs guides/easy-image/quick-start#create-token-endpoint Cloud Services - Quick start}
  149. * and {@glink @cs guides/security/token-endpoint Cloud Services - Token endpoint} documentation.
  150. *
  151. * Without a properly working token endpoint (token URL) CKEditor plugins will not be able to connect to CKEditor Cloud Services.
  152. *
  153. * @member {String|Function} module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl
  154. */
  155. /**
  156. * The endpoint URL for [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services) uploads.
  157. * This option must be set for Easy Image to work correctly.
  158. *
  159. * The upload URL is unique for each customer and can be found in the
  160. * [CKEditor Ecosystem customer dashboard](https://dashboard.ckeditor.com) after subscribing to the Easy Image service.
  161. * To learn how to start using Easy Image, check the {@glink @cs guides/easy-image/quick-start Easy Image - Quick start} documentation.
  162. *
  163. * Note: Make sure to also set the {@link module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl} configuration option.
  164. *
  165. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#uploadUrl
  166. */
  167. /**
  168. * The URL for web socket communication, used by the `RealTimeCollaborativeEditing` plugin. Every customer (organization in the CKEditor
  169. * Ecosystem dashboard) has their own, unique URLs to communicate with CKEditor Cloud Services. The URL can be found in the
  170. * CKEditor Ecosystem customer dashboard.
  171. *
  172. * Note: Unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and needs to be installed manually.
  173. * Check [Collaboration overview](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/overview.html) for more details.
  174. *
  175. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#webSocketUrl
  176. */
  177. /**
  178. * An optional parameter used for integration with CKEditor Cloud Services when uploading the editor build to cloud services.
  179. *
  180. * Whenever the editor build or the configuration changes, this parameter should be set to a new, unique value to differentiate
  181. * the new bundle (build + configuration) from the old ones.
  182. *
  183. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#bundleVersion
  184. */