linkengine.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 buildViewConverter from '../engine/conversion/buildviewconverter.js';
  8. import AttributeElement from '../engine/view/attributeelement.js';
  9. import LinkCommand from './linkcommand.js';
  10. /**
  11. * The link engine feature.
  12. *
  13. * It introduces the `link="url"` attribute in the model which renders to the view as a `<a href="url">` element.
  14. *
  15. * @memberOf link
  16. * @extends core.Feature
  17. */
  18. export default class LinkEngine extends Feature {
  19. /**
  20. * @inheritDoc
  21. */
  22. init() {
  23. const editor = this.editor;
  24. const data = editor.data;
  25. const editing = editor.editing;
  26. // Allow link attribute on all inline nodes.
  27. editor.document.schema.allow( { name: '$inline', attributes: [ 'link' ] } );
  28. // Build converter from model to view for data and editing pipelines.
  29. buildModelConverter().for( data.modelToView, editing.modelToView )
  30. .fromAttribute( 'link' )
  31. .toElement( ( href ) => new AttributeElement( 'a', { href } ) );
  32. // Build converter from view to model for data pipeline.
  33. buildViewConverter().for( data.viewToModel )
  34. .fromElement( 'a' )
  35. .toAttribute( ( viewElement ) => ( {
  36. key: 'link',
  37. value: viewElement.getAttribute( 'href' )
  38. } ) );
  39. // Create link command.
  40. editor.commands.set( 'link', new LinkCommand( editor ) );
  41. }
  42. }