8
0

cloudservices.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. CloudServices.Token = Token;
  63. /**
  64. * The configuration of CKEditor Cloud Services. Introduced by the {@link module:cloud-services/cloudservices~CloudServices} plugin.
  65. *
  66. * Read more in {@link module:cloud-services/cloudservices~CloudServicesConfig}.
  67. *
  68. * @member {module:cloud-services/cloudservices~CloudServicesConfig} module:core/editor/editorconfig~EditorConfig#cloudServices
  69. */
  70. /**
  71. * The configuration for all plugins using CKEditor Cloud Services.
  72. *
  73. * ClassicEditor
  74. * .create( document.querySelector( '#editor' ), {
  75. * cloudServices: {
  76. * tokenUrl: 'https://example.com/cs-token-endpoint',
  77. * uploadUrl: 'https://your-organization-id.cke-cs.com/easyimage/upload/'
  78. * }
  79. * } )
  80. * .then( ... )
  81. * .catch( ... );
  82. *
  83. * See {@link module:core/editor/editorconfig~EditorConfig all editor options}.
  84. *
  85. * @interface CloudServicesConfig
  86. */
  87. /**
  88. * A token URL or a token request function.
  89. *
  90. * 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
  91. * the end users of your application to use [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services) only
  92. * if they should have access e.g. to upload files with {@glink @cs guides/easy-image/quick-start Easy Image} or to use the
  93. * {@glink @cs guides/collaboration/quick-start Collaboration} service.
  94. *
  95. * ClassicEditor
  96. * .create( document.querySelector( '#editor' ), {
  97. * cloudServices: {
  98. * tokenUrl: 'https://example.com/cs-token-endpoint',
  99. * ...
  100. * }
  101. * } )
  102. * .then( ... )
  103. * .catch( ... );
  104. *
  105. * 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.
  106. * By using this approach you can set your own headers for the request.
  107. *
  108. * ClassicEditor
  109. * .create( document.querySelector( '#editor' ), {
  110. * cloudServices: {
  111. * tokenUrl: () => new Promise( ( resolve, reject ) => {
  112. * const xhr = new XMLHttpRequest();
  113. *
  114. * xhr.open( 'GET', 'https://example.com/cs-token-endpoint' );
  115. *
  116. * xhr.addEventListener( 'load', () => {
  117. * const statusCode = xhr.status;
  118. * const xhrResponse = xhr.response;
  119. *
  120. * if ( statusCode < 200 || statusCode > 299 ) {
  121. * return reject( new Error( 'Cannot download new token!' ) );
  122. * }
  123. *
  124. * return resolve( xhrResponse );
  125. * } );
  126. *
  127. * xhr.addEventListener( 'error', () => reject( new Error( 'Network Error' ) ) );
  128. * xhr.addEventListener( 'abort', () => reject( new Error( 'Abort' ) ) );
  129. *
  130. * xhr.setRequestHeader( customHeader, customValue );
  131. *
  132. * xhr.send();
  133. * } ),
  134. * ...
  135. * }
  136. * } )
  137. *
  138. * You can find more information about token endpoints in the
  139. * {@glink @cs guides/easy-image/quick-start#create-token-endpoint Cloud Services - Quick start}
  140. * and {@glink @cs guides/security/token-endpoint Cloud Services - Token endpoint} documentation.
  141. *
  142. * Without a properly working token endpoint (token URL) CKEditor plugins will not be able to connect to CKEditor Cloud Services.
  143. *
  144. * @member {String|Function} module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl
  145. */
  146. /**
  147. * The endpoint URL for [CKEditor Cloud Services](https://ckeditor.com/ckeditor-cloud-services) uploads.
  148. * This option must be set for Easy Image to work correctly.
  149. *
  150. * The upload URL is unique for each customer and can be found in the
  151. * [CKEditor Ecosystem customer dashboard](https://dashboard.ckeditor.com) after subscribing to the Easy Image service.
  152. * To learn how to start using Easy Image, check the {@glink @cs guides/easy-image/quick-start Easy Image - Quick start} documentation.
  153. *
  154. * Note: Make sure to also set the {@link module:cloud-services/cloudservices~CloudServicesConfig#tokenUrl} configuration option.
  155. *
  156. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#uploadUrl
  157. */
  158. /**
  159. * The URL for web socket communication, used by the `RealTimeCollaborativeEditing` plugin. Every customer (organization in the CKEditor
  160. * Ecosystem dashboard) has their own, unique URLs to communicate with CKEditor Cloud Services. The URL can be found in the
  161. * CKEditor Ecosystem customer dashboard.
  162. *
  163. * Note: Unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and needs to be installed manually.
  164. * Check [Collaboration overview](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/overview.html) for more details.
  165. *
  166. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#webSocketUrl
  167. */
  168. /**
  169. * An optional parameter used for integration with CKEditor Cloud Services when uploading the editor build to cloud services.
  170. *
  171. * Whenever the editor build or the configuration changes, this parameter should be set to a new, unique value to differentiate
  172. * the new bundle (build + configuration) from the old ones.
  173. *
  174. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#bundleVersion
  175. */
  176. /* eslint-disable max-len */
  177. /**
  178. * **This configuration property has been deprecated and will be removed in the next major release.
  179. * Use [`config.collaboration.channelId`](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/real-time-collaboration/real-time-collaboration-integration.html#the-channelid-configuration-property) instead.**
  180. *
  181. * The document ID used by the `RealTimeCollaborativeEditing` plugin. All editor instances created with the same document ID will
  182. * collaborate. It means that each document needs a different document ID if you do not want to start collaboration between these
  183. * documents. The ID is usually a primary key of the document in the database but you are free to provide whatever identifier fits your
  184. * scenario.
  185. *
  186. * Note: Unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and needs to be installed manually.
  187. * Check [Collaboration overview](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/overview.html) for more details.
  188. *
  189. * @deprecated
  190. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#documentId
  191. */
  192. /* eslint-enable max-len */