imageinsertcommand.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 Command from '@ckeditor/ckeditor5-core/src/command';
  6. import { insertImage, isImageAllowed } from './utils';
  7. /**
  8. * @module image/image/imageinsertcommand
  9. */
  10. /**
  11. * Insert image command.
  12. *
  13. * The command is registered by the {@link module:image/image/imageediting~ImageEditing} plugin as `'imageInsert'`.
  14. *
  15. * In order to insert an image at the current selection position
  16. * (according to the {@link module:widget/utils~findOptimalInsertionPosition} algorithm),
  17. * execute the command and specify the image source:
  18. *
  19. * editor.execute( 'imageInsert', { source: 'http://url.to.the/image' } );
  20. *
  21. * It is also possible to insert multiple images at once:
  22. *
  23. * editor.execute( 'imageInsert', {
  24. * source: [
  25. * 'path/to/image.jpg',
  26. * 'path/to/other-image.jpg'
  27. * ]
  28. * } );
  29. *
  30. * @extends module:core/command~Command
  31. */
  32. export default class ImageInsertCommand extends Command {
  33. /**
  34. * @inheritDoc
  35. */
  36. refresh() {
  37. this.isEnabled = isImageAllowed( this.editor.model );
  38. }
  39. /**
  40. * Executes the command.
  41. *
  42. * @fires execute
  43. * @param {Object} options Options for the executed command.
  44. * @param {String|Array.<String>} options.source The image source or an array of image sources to insert.
  45. */
  46. execute( options ) {
  47. const model = this.editor.model;
  48. model.change( writer => {
  49. const sources = Array.isArray( options.source ) ? options.source : [ options.source ];
  50. for ( const src of sources ) {
  51. insertImage( model, { src } );
  52. }
  53. } );
  54. }
  55. }