easyimage.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals console, window, document, XMLHttpRequest */
  6. import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
  7. import EasyImage from '../../src/easyimage';
  8. import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
  9. import uid from '@ckeditor/ckeditor5-utils/src/uid';
  10. // WARNING: The URL below should not be used for any other purpose than Easy Image plugin development.
  11. // Images uploaded using the testing token service may be deleted automatically at any moment.
  12. // If you would like to try the Easy Image service, please wait until the official launch of Easy Image and sign up for a free trial.
  13. // Images uploaded during the free trial will not be deleted for the whole trial period and additionally the trial service can be converted
  14. // into a subscription at any moment, allowing you to preserve all uploaded images.
  15. const CLOUD_SERVICE_TOKEN_URL = 'https://j2sns7jmy0.execute-api.eu-central-1.amazonaws.com/prod/token';
  16. getToken( { id: uid() } )
  17. .then( token => {
  18. return ClassicEditor
  19. .create( document.querySelector( '#editor' ), {
  20. cloudServices: {
  21. token
  22. },
  23. plugins: [ ArticlePluginSet, EasyImage ],
  24. toolbar: [ 'headings', 'undo', 'redo', 'insertImage' ],
  25. image: {
  26. toolbar: [ 'imageStyleFull', 'imageStyleSide', '|', 'imageTextAlternative' ]
  27. }
  28. } )
  29. .then( editor => {
  30. window.editor = editor;
  31. } );
  32. } )
  33. .catch( err => {
  34. console.error( err.stack );
  35. } );
  36. function getToken( user ) {
  37. return new Promise( ( resolve, reject ) => {
  38. const xhr = new XMLHttpRequest();
  39. xhr.open( 'GET', `${ CLOUD_SERVICE_TOKEN_URL }?user.id=${ user.id }` );
  40. xhr.onload = () => {
  41. if ( xhr.status >= 200 && xhr.status < 300 ) {
  42. const response = JSON.parse( xhr.responseText );
  43. resolve( response.token );
  44. } else {
  45. reject( new Error( `XHR status: ${ xhr.status }` ) );
  46. }
  47. };
  48. xhr.onerror = err => {
  49. reject( err );
  50. };
  51. xhr.send( null );
  52. } );
  53. }