imageengine.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/image/imageengine
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  10. import WidgetEngine from '../widget/widgetengine';
  11. import { viewToModelImage, createImageAttributeConverter } from './converters';
  12. import { toImageWidget } from './utils';
  13. import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
  14. import ViewEmptyElement from '@ckeditor/ckeditor5-engine/src/view/emptyelement';
  15. /**
  16. * The image engine plugin.
  17. * Registers `image` as a block element in document's schema and allows it to have two attributes: `src` and `alt`.
  18. * Registers converters for editing and data pipelines.
  19. *
  20. * @extends module:core/plugin~Plugin
  21. */
  22. export default class ImageEngine extends Plugin {
  23. /**
  24. * @inheritDoc
  25. */
  26. static get requires() {
  27. return [ WidgetEngine ];
  28. }
  29. /**
  30. * @inheritDoc
  31. */
  32. init() {
  33. const editor = this.editor;
  34. const doc = editor.document;
  35. const schema = doc.schema;
  36. const data = editor.data;
  37. const editing = editor.editing;
  38. const t = editor.t;
  39. // Configure schema.
  40. schema.registerItem( 'image' );
  41. schema.requireAttributes( 'image', [ 'src' ] );
  42. schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  43. schema.objects.add( 'image' );
  44. // Build converter from model to view for data pipeline.
  45. buildModelConverter().for( data.modelToView )
  46. .fromElement( 'image' )
  47. .toElement( () => createImageViewElement() );
  48. // Build converter from model to view for editing pipeline.
  49. buildModelConverter().for( editing.modelToView )
  50. .fromElement( 'image' )
  51. .toElement( () => toImageWidget( createImageViewElement(), t( 'image widget' ) ) );
  52. createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'src' );
  53. createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'alt' );
  54. // Converter for figure element from view to model.
  55. data.viewToModel.on( 'element:figure', viewToModelImage() );
  56. }
  57. }
  58. // Creates view element representing the image.
  59. //
  60. // <figure class="image"><img></img></figure>
  61. //
  62. // Note that `alt` and `src` attributes are converted separately, so they're not included.
  63. //
  64. // @private
  65. // @return {module:engine/view/containerelement~ContainerElement}
  66. export function createImageViewElement() {
  67. return new ViewContainerElement( 'figure', { class: 'image' }, new ViewEmptyElement( 'img' ) );
  68. }