8
0

linkengine.js 1.6 KB

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