8
0

imageinsertcommand.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. import ImageUploadCommand from '@ckeditor/ckeditor5-image/src/imageupload/imageuploadcommand';
  6. import { isImageAllowed } from '../image/utils';
  7. /**
  8. * @module image/imageinsert/imageinsertcommand
  9. */
  10. // TODO?
  11. /**
  12. * The image upload command.
  13. *
  14. * The command is registered by the {@link module:image/imageinsert/imageinsertediting~ImageUploadEditing} plugin as `'imageUpload'`.
  15. *
  16. * In order to upload an image at the current selection position
  17. * (according to the {@link module:widget/utils~findOptimalInsertionPosition} algorithm),
  18. * execute the command and pass the native image file instance:
  19. *
  20. * this.listenTo( editor.editing.view.document, 'clipboardInput', ( evt, data ) => {
  21. * // Assuming that only images were pasted:
  22. * const images = Array.from( data.dataTransfer.files );
  23. *
  24. * // Upload the first image:
  25. * editor.execute( 'imageUpload', { file: images[ 0 ] } );
  26. * } );
  27. *
  28. * It is also possible to insert multiple images at once:
  29. *
  30. * editor.execute( 'imageUpload', {
  31. * file: [
  32. * file1,
  33. * file2
  34. * ]
  35. * } );
  36. *
  37. * @extends module:core/command~Command
  38. */
  39. export default class ImageInsertCommand extends ImageUploadCommand {
  40. /**
  41. * @inheritDoc
  42. */
  43. refresh() {
  44. const imageElement = this.editor.model.document.selection.getSelectedElement();
  45. const isImage = imageElement && imageElement.name === 'image' || false;
  46. this.isEnabled = isImageAllowed( this.editor.model ) || isImage;
  47. }
  48. }