imageengine.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, modelToViewSelection, 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. // Configure schema.
  39. schema.registerItem( 'image' );
  40. schema.requireAttributes( 'image', [ 'src' ] );
  41. schema.allow( { name: 'image', attributes: [ 'alt', 'src' ], inside: '$root' } );
  42. schema.objects.add( 'image' );
  43. // Build converter from model to view for data pipeline.
  44. buildModelConverter().for( data.modelToView )
  45. .fromElement( 'image' )
  46. .toElement( () => createImageViewElement() );
  47. // Build converter from model to view for editing pipeline.
  48. buildModelConverter().for( editing.modelToView )
  49. .fromElement( 'image' )
  50. .toElement( () => toImageWidget( createImageViewElement() ) );
  51. createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'src' );
  52. createImageAttributeConverter( [ editing.modelToView, data.modelToView ], 'alt' );
  53. // Converter for figure element from view to model.
  54. data.viewToModel.on( 'element:figure', viewToModelImage() );
  55. // Creates fake selection label if selection is placed around image widget.
  56. editing.modelToView.on( 'selection', modelToViewSelection( editor.t ), { priority: 'lowest' } );
  57. }
  58. }
  59. // Creates view element representing the image.
  60. //
  61. // <figure class="image"><img></img></figure>
  62. //
  63. // Note that `alt` and `src` attributes are converted separately, so they're not included.
  64. //
  65. // @private
  66. // @return {module:engine/view/containerelement~ContainerElement}
  67. export function createImageViewElement() {
  68. return new ViewContainerElement( 'figure', { class: 'image' }, new ViewEmptyElement( 'img' ) );
  69. }