linkengine.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module link/linkengine
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import buildModelConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildmodelconverter';
  10. import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildviewconverter';
  11. import LinkElement from './linkelement';
  12. import LinkCommand from './linkcommand';
  13. import UnlinkCommand from './unlinkcommand';
  14. /**
  15. * The link engine feature.
  16. *
  17. * It introduces the `linkHref="url"` attribute in the model which renders to the view as a `<a href="url">` element.
  18. *
  19. * @extends module:core/plugin~Plugin
  20. */
  21. export default class LinkEngine extends Plugin {
  22. /**
  23. * @inheritDoc
  24. */
  25. init() {
  26. const editor = this.editor;
  27. const data = editor.data;
  28. const editing = editor.editing;
  29. // Allow link attribute on all inline nodes.
  30. editor.model.schema.allow( { name: '$inline', attributes: 'linkHref', inside: '$block' } );
  31. // Temporary workaround. See https://github.com/ckeditor/ckeditor5/issues/477.
  32. editor.model.schema.allow( { name: '$inline', attributes: 'linkHref', inside: '$clipboardHolder' } );
  33. // Build converter from model to view for data and editing pipelines.
  34. buildModelConverter().for( data.modelToView, editing.modelToView )
  35. .fromAttribute( 'linkHref' )
  36. .toElement( linkHref => {
  37. const linkElement = new LinkElement( 'a', { href: linkHref } );
  38. // https://github.com/ckeditor/ckeditor5-link/issues/121
  39. linkElement.priority = 5;
  40. return linkElement;
  41. } );
  42. // Build converter from view to model for data pipeline.
  43. buildViewConverter().for( data.viewToModel )
  44. // Convert <a> with href (value doesn't matter).
  45. .from( { name: 'a', attribute: { href: /.?/ } } )
  46. .toAttribute( viewElement => ( {
  47. key: 'linkHref',
  48. value: viewElement.getAttribute( 'href' )
  49. } ) );
  50. // Create linking commands.
  51. editor.commands.add( 'link', new LinkCommand( editor ) );
  52. editor.commands.add( 'unlink', new UnlinkCommand( editor ) );
  53. }
  54. }