linkimageediting.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module link/linkimageediting
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
  10. import LinkEditing from './linkediting';
  11. /**
  12. * The link image engine feature.
  13. *
  14. * It accepts the `linkHref="url"` attribute in the model for the {@link module:image/image~Image `<image>`} element
  15. * which allows linking images.
  16. *
  17. * @extends module:core/plugin~Plugin
  18. */
  19. export default class LinkImageEditing extends Plugin {
  20. /**
  21. * @inheritDoc
  22. */
  23. static get requires() {
  24. return [ ImageEditing, LinkEditing ];
  25. }
  26. /**
  27. * @inheritDoc
  28. */
  29. static get pluginName() {
  30. return 'LinkImageEditing';
  31. }
  32. init() {
  33. const editor = this.editor;
  34. editor.model.schema.extend( 'image', { allowAttributes: [ 'linkHref' ] } );
  35. editor.conversion.for( 'upcast' ).add( upcastLink() );
  36. editor.conversion.for( 'downcast' ).add( downcastImageLink() );
  37. }
  38. }
  39. // Returns a converter that consumes the 'href' attribute if a link contains an image.
  40. //
  41. // @private
  42. // @returns {Function}
  43. //
  44. function upcastLink() {
  45. return dispatcher => {
  46. dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
  47. const viewLink = data.viewItem;
  48. const imageInLink = Array.from( viewLink.getChildren() ).find( child => child.name === 'img' );
  49. if ( !imageInLink ) {
  50. return;
  51. }
  52. // There's an image inside an <a> element - we consume it so it won't be picked up by the Link plugin.
  53. const consumableAttributes = { attributes: [ 'href' ] };
  54. // Consume the `href` attribute so the default one will not convert it to $text attribute.
  55. if ( !conversionApi.consumable.consume( viewLink, consumableAttributes ) ) {
  56. // Might be consumed by something else - i.e. other converter with priority=highest - a standard check.
  57. return;
  58. }
  59. const linkHref = viewLink.getAttribute( 'href' );
  60. // Missing the 'href' attribute.
  61. if ( !linkHref ) {
  62. return;
  63. }
  64. // A full definition of the image feature.
  65. // figure > a > img: parent of the link element is an image element.
  66. let modelElement = data.modelCursor.parent;
  67. if ( !modelElement.is( 'image' ) ) {
  68. // a > img: parent of the link is not the image element. We need to convert it manually.
  69. const conversionResult = conversionApi.convertItem( imageInLink, data.modelCursor );
  70. // Set image range as conversion result.
  71. data.modelRange = conversionResult.modelRange;
  72. // Continue conversion where image conversion ends.
  73. data.modelCursor = conversionResult.modelCursor;
  74. modelElement = data.modelCursor.nodeBefore;
  75. }
  76. if ( modelElement && modelElement.is( 'image' ) ) {
  77. // Set the linkHref attribute from link element on model image element.
  78. conversionApi.writer.setAttribute( 'linkHref', linkHref, modelElement );
  79. }
  80. }, { priority: 'high' } );
  81. };
  82. }
  83. // Return a converter that adds the `<a>` element to data.
  84. //
  85. // @private
  86. // @returns {Function}
  87. //
  88. function downcastImageLink() {
  89. return dispatcher => {
  90. dispatcher.on( 'attribute:linkHref:image', ( evt, data, conversionApi ) => {
  91. // The image will be already converted - so it will be present in the view.
  92. const viewFigure = conversionApi.mapper.toViewElement( data.item );
  93. const writer = conversionApi.writer;
  94. // But we need to check whether the link element exists.
  95. const linkInImage = Array.from( viewFigure.getChildren() ).find( child => child.name === 'a' );
  96. // If so, update the attribute if it's defined or remove the entire link if the attribute is empty.
  97. if ( linkInImage ) {
  98. if ( data.attributeNewValue ) {
  99. writer.setAttribute( 'href', data.attributeNewValue, linkInImage );
  100. } else {
  101. const viewImage = Array.from( linkInImage.getChildren() ).find( child => child.name === 'img' );
  102. writer.move( writer.createRangeOn( viewImage ), writer.createPositionAt( viewFigure, 0 ) );
  103. writer.remove( linkInImage );
  104. }
  105. } else {
  106. // But if it does not exist. Let's wrap already converted image by newly created link element.
  107. // 1. Create an empty link element.
  108. const linkElement = writer.createContainerElement( 'a', { href: data.attributeNewValue } );
  109. // 2. Insert link inside the associated image.
  110. writer.insert( writer.createPositionAt( viewFigure, 0 ), linkElement );
  111. // 3. Move the image to the link.
  112. writer.move( writer.createRangeOn( viewFigure.getChild( 1 ) ), writer.createPositionAt( linkElement, 0 ) );
  113. }
  114. } );
  115. };
  116. }