8
0

imageengine.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module image/imageengine
  7. */
  8. import Plugin from 'ckeditor5-core/src/plugin';
  9. import buildModelConverter from 'ckeditor5-engine/src/conversion/buildmodelconverter';
  10. import WidgetEngine from './widget/widgetengine';
  11. import { modelToViewImage, viewToModelImage, modelToViewSelection } from './converters';
  12. import { toImageWidget } from './utils';
  13. /**
  14. * The image engine plugin.
  15. * Registers `image` as a block element in document's schema and allows it to have two attributes: `src` and `alt`.
  16. * Registers converters for editing and data pipelines.
  17. *
  18. * @extends module:core/plugin~Plugin.
  19. */
  20. export default class ImageEngine extends Plugin {
  21. /**
  22. * @inheritDoc
  23. */
  24. static get requires() {
  25. return [ WidgetEngine ];
  26. }
  27. /**
  28. * @inheritDoc
  29. */
  30. init() {
  31. const editor = this.editor;
  32. const doc = editor.document;
  33. const schema = doc.schema;
  34. const data = editor.data;
  35. const editing = editor.editing;
  36. // Configure schema.
  37. schema.registerItem( 'image' );
  38. schema.requireAttributes( 'image', [ 'src' ] );
  39. schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  40. schema.objects.add( 'image' );
  41. // Build converter from model to view for data pipeline.
  42. buildModelConverter().for( data.modelToView )
  43. .fromElement( 'image' )
  44. .toElement( ( data ) => modelToViewImage( data.item ) );
  45. // Build converter from model to view for editing pipeline.
  46. buildModelConverter().for( editing.modelToView )
  47. .fromElement( 'image' )
  48. .toElement( ( data ) => toImageWidget( modelToViewImage( data.item ) ) );
  49. // Converter for figure element from view to model.
  50. data.viewToModel.on( 'element:figure', viewToModelImage() );
  51. // Creates fake selection label if selection is placed around image widget.
  52. editing.modelToView.on( 'selection', modelToViewSelection( editor.t ), { priority: 'lowest' } );
  53. }
  54. }