8
0

cloudservices.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 CKEditor 5's Cloud Services integration.
  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 the
  92. * 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 Easy Image or to access the Collaboraation 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 that approach you can set your own headers to 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/token-endpoints/tokenendpoint Cloud Services - Creating 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 [CKEditor Ecosystem dashboard](https://dashboard.ckeditor.com)
  151. * after subscribing to Easy Image service.
  152. * To learn how to start using Easy Image check {@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 `RealTimeCollaborativeEditing` plugin. Every customer (organization in the CKEditor
  160. * Ecosystem dashboard) has its own, unique URLs to communicate with CKEditor Cloud Services. The URL can be found in the
  161. * CKEditor Ecosystem dashboard.
  162. *
  163. * Note: unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and has 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. * Document ID, used by `RealTimeCollaborativeEditing` plugin. All editor instances created with the same document ID will collaborate.
  170. * It means that each document needs a different document ID if you do not want to start collaboration between these documents.
  171. * The ID is usually a primary key of the document in the database, but you are free to provide whatever identifier fits your scenario.
  172. *
  173. * Note: unlike most plugins, `RealTimeCollaborativeEditing` is not included in any CKEditor 5 build and has to be installed manually.
  174. * Check [Collaboration overview](https://ckeditor.com/docs/ckeditor5/latest/features/collaboration/overview.html) for more details.
  175. *
  176. * @member {String} module:cloud-services/cloudservices~CloudServicesConfig#documentId
  177. */