8
0

cloudservices.js 7.1 KB

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