8
0

imageengine.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Feature from '../core/feature.js';
  6. import buildModelConverter from '../engine/conversion/buildmodelconverter.js';
  7. import {
  8. modelToViewImage,
  9. modelToViewSelection,
  10. viewToModelImage
  11. } from './converters.js';
  12. /**
  13. * The image engine feature.
  14. * Registers `image` as block element in document's schema and allows it to have two attributes: `src` and `alt`.
  15. * Creates model converter for data and editing pipelines, and view converter for data pipeline.
  16. *
  17. * @memberof image
  18. * @extends core.Feature.
  19. */
  20. export default class ImageEngine extends Feature {
  21. /**
  22. * @inheritDoc
  23. */
  24. init() {
  25. const editor = this.editor;
  26. const document = editor.document;
  27. const dataPipeline = editor.data;
  28. const editingPipeline = editor.editing;
  29. // Configure schema.
  30. document.schema.registerItem( 'image', '$block' );
  31. document.schema.allow( { name: 'image', attributes: [ 'src', 'alt' ] } );
  32. // Build converter from model to view for data pipeline.
  33. buildModelConverter().for( dataPipeline.modelToView )
  34. .fromElement( 'image' )
  35. .toElement( modelToViewImage( true ) );
  36. // Build converter from model to view for editing pipeline.
  37. buildModelConverter().for( editingPipeline.modelToView )
  38. .fromElement( 'image' )
  39. .toElement( modelToViewImage() );
  40. // Converter for figure element from view to model.
  41. dataPipeline.viewToModel.on( 'element:figure', viewToModelImage() );
  42. // Selection converter from view to model - applies fake selection if model selection is on widget.
  43. editingPipeline.modelToView.on( 'selection', modelToViewSelection(), { priority: 'low' } );
  44. }
  45. }