ckeditor5 icon indicating copy to clipboard operation
ckeditor5 copied to clipboard

Allow linking images

Open davidpolberger opened this issue 9 years ago โ€ข 51 comments

๐Ÿž Is this a bug report or feature request? (choose one)

  • Feature request

๐Ÿ’ป Version of CKEditor

1.0.0-alpha.2

๐Ÿ“‹ Steps to reproduce

  1. Go to https://ckeditor5.github.io/
  2. Drag and drop an image into the editor to upload it.
  3. Select the image by clicking it.

โœ… Expected result

The link button should be enabled.

โŽ Actual result

The link button is disabled. Selecting the surrounding text in addition to the image enables the link button, but no link is created for the image when the button is pressed.

๐Ÿ“ƒ Other details that might be useful

This scenario works in CKEditor 4.

๐Ÿ‘ If you need this

(Edited by @reinmar:) We need to know how important is this feature for you. Please react with ๐Ÿ‘ if you need this feature.

davidpolberger avatar Dec 04 '17 22:12 davidpolberger

I thought there was a ticket for it already, but I can't find it. Definitely needed.

Reinmar avatar Dec 05 '17 10:12 Reinmar

@Reinmar I guess you were referring to https://github.com/ckeditor/ckeditor5-link/issues/85, right?

oleq avatar Dec 12 '17 16:12 oleq

Ah, right. I forgot it was the opposite issue in the past. So let's keep this one open too.

Reinmar avatar Dec 12 '17 16:12 Reinmar

BTW, I'm curious what are the use cases for linking images.

The thing is that the only time when I wanted to link an image was when I wanted to allow opening a high-res version of that image. But then, as a user I must know the link to that high-res version. So it seems that cases like this one should rather be handled by the system, not the content author. For example, GH automatically links to the full version of images, even if you'll create a normal image in CommonMark.

The other case which comes to my mind is linking e.g. lead images in a newsletter. There may be images linking to blog posts or some other landing pages. This kinda starts falling into a structured content case and most likely should not be done inside the editor. But it's a case in which one will want to link the image manually if it's not handled by the system. If it's handled by the system, then e.g. the system may decide to use a block-level <a> to wrap the image and some additional text which follows it (the entire section of the content) which might give a better result.

So, what are the other scenarios in which the user will want to link an image which is a part of the content?

Reinmar avatar Dec 17 '17 21:12 Reinmar

I agree with you, @Reinmar, for images that are photographs. But there are other types of images too, including line art and text. Examples include logos and highly stylized calls to action. I'd say that images that mostly consist of (stylized) text are quite likely to be turned into links.

davidpolberger avatar Dec 17 '17 21:12 davidpolberger

Similarly, an image that is e.g. a thumbnail for an article is frequently linked to the article itself.

mwadams avatar Jan 09 '18 14:01 mwadams

I got the need for an image to be linking to a product. An ad on a banner.

My current idea, is to use <map><area></map>, which can placed after the <img>. Instead of using a surrounding <a> element, which has already a meaning in CK.

I'm cloning the <figcaption> plugin for this. It's far from perfect. As the area must have coords, and I want my images to be responsive. I also need to generate an id for the <map>, referenced on <img>.

Here is an exemple of result : (as plain html) https://codepen.io/long-lazuli/pen/644a741b5574b452dea02e1d61591aa1?editors=1100

long-lazuli avatar Jan 28 '18 11:01 long-lazuli

We're using ckeditor to send email newsletters. Some newsletters will contain videos and we'd like add an image of the player and surround it with an href to the actual video.

When pasting the newsletter contents (from Wordpress) into ckeditor ("@ckeditor/ckeditor5-build-classic": "10.0.0") the links on the images are removed.

What are the ways of working around this behavior?

stnor avatar May 15 '18 09:05 stnor

@Reinmar Is there any way for me to workaround or add this feature using a plugin or otherwise? We're in dire need.

Thanks!

stnor avatar May 21 '18 10:05 stnor

I asked @jodator to take a look on this. Sadly, the image plugin is one of the more complicated ones and there's no quick recipe how to add conversion for links in them. But to unblock you guys, we'll at least try now to find a way how such conversion could be added (either by a separate plugin or by modifying the image plugin).

Reinmar avatar May 21 '18 13:05 Reinmar

Excellent, much appreciated!

stnor avatar May 21 '18 13:05 stnor

@stnor & @Reinmar So far I've managed to preserve existing links that wraps <img> or <figure>.

I've tested this on "caption" manual test from image plugin. It works with CKEditor's Image & Link plugins. It preserves links and allows to edit images. It does not allow to edit those links and ImageStyle plugin stopped to work on those images also.

I think that one way to enhance this code is to change its behavior so it will convert whole link with image so it will not create <figure> and will not create a widget for such images. But to propose that I'd need to know if that is what you need :) as right now

<a href=""><img src=""></a>

will be transformed to:

<a href=""><figure><img src=""></figure></a>

The current implementation is below:

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '@ckeditor/ckeditor5-typing/src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Image from '../../src/image';
import ImageCaption from '../../src/imagecaption';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
import ImageToolbar from '../../src/imagetoolbar';
import ImageStyle from '../../src/imagestyle';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import List from '@ckeditor/ckeditor5-list/src/list';

import Link from '@ckeditor/ckeditor5-link/src/link';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Range from '../../../ckeditor5-engine/src/view/range';
import Position from '../../../ckeditor5-engine/src/view/position';

class ImageLink extends Plugin {
	init() {
		const editor = this.editor;

		editor.model.schema.extend( 'image', { allowAttributes: [ 'href' ] } );

		editor.conversion.for( 'upcast' ).add( upcastLink() );
		editor.conversion.for( 'upcast' ).add( upcastImageLink( 'img' ) );
		editor.conversion.for( 'upcast' ).add( upcastImageLink( 'figure' ) );
		editor.conversion.for( 'downcast' ).add( downcastImageLink() );
	}
}

ClassicEditor
	.create( document.querySelector( '#editor' ), {
		plugins: [
			Enter, Typing, Paragraph, Heading, Image, ImageToolbar, Link, ImageCaption,
			Undo, Clipboard, ImageStyle, Bold, Italic, Heading, List, ImageLink
		],
		toolbar: [ 'heading', '|', 'undo', 'redo', 'bold', 'italic', 'bulletedList', 'numberedList', 'link' ],
		image: {
			toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
		}
	} )
	.then( editor => {
		window.editor = editor;
	} )
	.catch( err => {
		console.error( err.stack );
	} );

/**
 * Returns converter for links that wraps <img> or <figure> elements.
 *
 * @returns {Function}
 */
function upcastLink() {
	return dispatcher => {
		dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
			const viewLink = data.viewItem;

			const imageInLink = Array.from( viewLink.getChildren() ).find( child => child.name === 'img' || child.name === 'figure' );

			if ( imageInLink ) {
				// There's an image (or figure) inside an <a> element - we consume it so it won't be picked up by Link plugin.
				const consumableAttributes = { attributes: [ 'href' ] };

				// Consume the link so the default one will not convert it to $text attribute.
				if ( !conversionApi.consumable.test( viewLink, consumableAttributes ) ) {
					// Might be consumed by something else - ie other converter with priority=highest - a standard check.
					return;
				}

				// Consume 'href' attribute from link element.
				conversionApi.consumable.consume( viewLink, consumableAttributes );
			}
		}, { priority: 'high' } );
	};
}

function upcastImageLink( elementName ) {
	return dispatcher => {
		dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => {
			const viewImage = data.viewItem;
			const parent = viewImage.parent;

			// Check only <img>/<figure> that are direct children of a link.
			if ( parent.name === 'a' ) {
				const modelImage = data.modelCursor.nodeBefore;
				const linkHref = parent.getAttribute( 'href' );

				if ( modelImage && linkHref ) {
					// Set the href attribute from link element on model image element.
					conversionApi.writer.setAttribute( 'href', linkHref, modelImage );
				}
			}
		}, { priority: 'normal' } );
	};
}

function downcastImageLink() {
	return dispatcher => {
		dispatcher.on( 'attribute:href:image', ( evt, data, conversionApi ) => {
			const href = data.attributeNewValue;
			// The image will be already converted - so it will be present in the view.
			const viewImage = conversionApi.mapper.toViewElement( data.item );

			// Below will wrap already converted image by newly created link element.

			// 1. Create empty link element.
			const linkElement = conversionApi.writer.createContainerElement( 'a', { href } );

			// 2. Insert link before associated image.
			conversionApi.writer.insert( Position.createBefore( viewImage ), linkElement );

			// 3. Move whole converted image to a link.
			conversionApi.writer.move( Range.createOn( viewImage ), new Position( linkElement, 0 ) );
		}, { priority: 'normal' } );
	};
}

jodator avatar May 22 '18 15:05 jodator

Cool! :)

Do you think it would be possible to render <a> elements inside the <figure> (like in https://sdk.ckeditor.com/samples/image2.html) so only <img> is wrapped.

Reinmar avatar May 22 '18 16:05 Reinmar

@Reinmar Yeah - actually it was a bit harder to make it wrap <figure/> instead of an <image/>. AFAIR I've used there AttributeAlement and conversionApi.writer.wrap() to wrap image with ''.

Hi. Thanks for this. However, I cant get this to work for me. I added some logging in the dispatchers and two are invoked (upcastLink and upcastImageLink) when I paste in the contents.

When is the downcast method supposed to run?

As you can see I'm using angularjs and synching the model on 'change'.

My code:

import CkEditor from "@ckeditor/ckeditor5-build-classic";

export default class CkEditorDirective {

  constructor() {
    this.restrict = 'A';
    this.require = 'ngModel';
  }

  static create() {
    return new CkEditorDirective();
  }

  link(scope, elem, attr, ngModel) {
    CkEditor.create(elem[0]).then((editor) => {

      function upcastLink() {
        return dispatcher => {
          dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
            console.log('upcastLink', evt, data, conversionApi)

            const viewLink = data.viewItem;

            const imageInLink = Array.from( viewLink.getChildren() ).find( child => child.name === 'img' || child.name === 'figure' );

            if ( imageInLink ) {
              // There's an image (or figure) inside an <a> element - we consume it so it won't be picked up by Link plugin.
              const consumableAttributes = { attributes: [ 'href' ] };

              // Consume the link so the default one will not convert it to $text attribute.
              if ( !conversionApi.consumable.test( viewLink, consumableAttributes ) ) {
                // Might be consumed by something else - ie other converter with priority=highest - a standard check.
                return;
              }

              // Consume 'href' attribute from link element.
              conversionApi.consumable.consume( viewLink, consumableAttributes );
            }
          }, { priority: 'high' } );
        };
      }

      function upcastImageLink( elementName ) {
        return dispatcher => {
          dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => {
            console.log('upcastImageLink', evt, data, conversionApi)
            const viewImage = data.viewItem;
            const parent = viewImage.parent;

            // Check only <img>/<figure> that are direct children of a link.
            if ( parent.name === 'a' ) {
              const modelImage = data.modelCursor.nodeBefore;
              const linkHref = parent.getAttribute( 'href' );

              if ( modelImage && linkHref ) {
                // Set the href attribute from link element on model image element.
                conversionApi.writer.setAttribute( 'href', linkHref, modelImage );
              }
            }
          }, { priority: 'normal' } );
        };
      }

      function downcastImageLink() {
        return dispatcher => {
          dispatcher.on( 'attribute:href:image', ( evt, data, conversionApi ) => {
            console.log('downcastImageLink', evt, data, conversionApi)
            const href = data.attributeNewValue;
            // The image will be already converted - so it will be present in the view.
            const viewImage = conversionApi.mapper.toViewElement( data.item );

            // Below will wrap already converted image by newly created link element.

            // 1. Create empty link element.
            const linkElement = conversionApi.writer.createContainerElement( 'a', { href } );

            // 2. Insert link before associated image.
            conversionApi.writer.insert( Position.createBefore( viewImage ), linkElement );

            // 3. Move whole converted image to a link.
            conversionApi.writer.move( Range.createOn( viewImage ), new Position( linkElement, 0 ) );
          }, { priority: 'normal' } );
        };
      }


      editor.model.schema.extend( 'image', { allowAttributes: [ 'href' ] } );
      editor.conversion.for( 'upcast' ).add( upcastLink() );
      editor.conversion.for( 'upcast' ).add( upcastImageLink( 'img' ) );
      editor.conversion.for( 'upcast' ).add( upcastImageLink( 'figure' ) );
      editor.conversion.for( 'downcast' ).add( downcastImageLink() );


      editor.model.document.on('change', () => {
        scope.$apply(() => {
          ngModel.$setViewValue(editor.getData());
        });
      });

      ngModel.$render = () => {
        editor.setData(ngModel.$modelValue);
      };

      scope.$on('$destroy', () => {
        editor.destroy();
      });
    });
  }
}

Pasted contents (from wordpress, wysiwyg) source:

Test of link...

<a href="https://nomp.se"><img class="aligncenter size-large wp-image-3525" src="https://blog.nomp.se/wp-content/uploads/2018/05/gdpr-1024x445.png" alt="" width="640" height="278" /></a>

stnor avatar May 23 '18 09:05 stnor

screen shot 2018-05-23 at 11 42 33

stnor avatar May 23 '18 09:05 stnor

@stnor The code provided by me must be implemented as a CKEditor plugin in order to properly extend upcast (from view to the model) and downcast (from model to the view) conversions.

You'll need a custom build for that.

jodator avatar May 23 '18 10:05 jodator

@jodator ok, thanks! i'll check that out.

stnor avatar May 23 '18 11:05 stnor

I created a new factory for CkEditor in my project:

import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import Typing from '@ckeditor/ckeditor5-typing/src/typing';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import Heading from '@ckeditor/ckeditor5-heading/src/heading';
import Image from '@ckeditor/ckeditor5-image/src/image';
import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption';
import Undo from '@ckeditor/ckeditor5-undo/src/undo';
import Clipboard from '@ckeditor/ckeditor5-clipboard/src/clipboard';
import ImageToolbar from '@ckeditor/ckeditor5-image/src/imagetoolbar';
import ImageStyle from '@ckeditor/ckeditor5-image/src/imagestyle';
import Bold from '@ckeditor/ckeditor5-basic-styles/src/bold';
import Italic from '@ckeditor/ckeditor5-basic-styles/src/italic';
import List from '@ckeditor/ckeditor5-list/src/list';
import Link from '@ckeditor/ckeditor5-link/src/link';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import Range from '@ckeditor/ckeditor5-engine/src/view/range';
import Position from '@ckeditor/ckeditor5-engine/src/view/position';

export default class CustomCkEditorFactory {

  static create(element) {
    return ClassicEditor
        .create( element, {
          plugins: [
            Enter, Typing, Paragraph, Heading, Image, ImageToolbar, Link, ImageCaption,
            Undo, Clipboard, ImageStyle, Bold, Italic, Heading, List, ImageLink
          ],
          toolbar: [ 'heading', '|', 'undo', 'redo', 'bold', 'italic', 'bulletedList', 'numberedList', 'link' ],
          image: {
            toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
          }
        } );
  }
}

class ImageLink extends Plugin {
  init() {
    console.log('Plugin in init()')
    const editor = this.editor;
    editor.model.schema.extend( 'image', { allowAttributes: [ 'href' ] } );
    editor.conversion.for( 'upcast' ).add( upcastLink() );
    editor.conversion.for( 'upcast' ).add( upcastImageLink( 'img' ) );
    editor.conversion.for( 'upcast' ).add( upcastImageLink( 'figure' ) );
    editor.conversion.for( 'downcast' ).add( downcastImageLink() );
  }
}

/**
 * Returns converter for links that wraps <img> or <figure> elements.
 *
 * @returns {Function}
 */
function upcastLink() {
  return dispatcher => {
    dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
      console.log('upcastLink')
      const viewLink = data.viewItem;

      const imageInLink = Array.from( viewLink.getChildren() ).find( child => child.name === 'img' || child.name === 'figure' );

      if ( imageInLink ) {
        // There's an image (or figure) inside an <a> element - we consume it so it won't be picked up by Link plugin.
        const consumableAttributes = { attributes: [ 'href' ] };

        // Consume the link so the default one will not convert it to $text attribute.
        if ( !conversionApi.consumable.test( viewLink, consumableAttributes ) ) {
          // Might be consumed by something else - ie other converter with priority=highest - a standard check.
          return;
        }

        // Consume 'href' attribute from link element.
        conversionApi.consumable.consume( viewLink, consumableAttributes );
      }
    }, { priority: 'high' } );
  };
}

function upcastImageLink( elementName ) {
  return dispatcher => {
    dispatcher.on( `element:${ elementName }`, ( evt, data, conversionApi ) => {
      console.log('upcastImageLink')

      const viewImage = data.viewItem;
      const parent = viewImage.parent;

      // Check only <img>/<figure> that are direct children of a link.
      if ( parent.name === 'a' ) {
        const modelImage = data.modelCursor.nodeBefore;
        const linkHref = parent.getAttribute( 'href' );

        if ( modelImage && linkHref ) {
          // Set the href attribute from link element on model image element.
          conversionApi.writer.setAttribute( 'href', linkHref, modelImage );
        }
      }
    }, { priority: 'normal' } );
  };
}

function downcastImageLink() {
  return dispatcher => {
    dispatcher.on( 'attribute:href:image', ( evt, data, conversionApi ) => {
      console.log('downcastImageLink')

      const href = data.attributeNewValue;
      // The image will be already converted - so it will be present in the view.
      const viewImage = conversionApi.mapper.toViewElement( data.item );

      // Below will wrap already converted image by newly created link element.

      // 1. Create empty link element.
      const linkElement = conversionApi.writer.createContainerElement( 'a', { href } );

      // 2. Insert link before associated image.
      conversionApi.writer.insert( Position.createBefore( viewImage ), linkElement );

      // 3. Move whole converted image to a link.
      conversionApi.writer.move( Range.createOn( viewImage ), new Position( linkElement, 0 ) );
    }, { priority: 'normal' } );
  };
}

This produced an error when registering the plugin: "Class constructor Plugin cannot be invoked without 'new'", as in #649

So I copied the constructor from the Plugin class and removed the inheritance, as a workaround described in #649.

Unfortunately the outcome is the same as before, downcast is not run, even though the "ImageLink" is among the registered plugins.

ยดยดยดยด CustomCkEditorFactory.js:118 Plugin in init() CustomCkEditorFactory.js:141 upcastLink CustomCkEditorFactory.js:168 upcastImageLink

Any ideas?

stnor avatar May 23 '18 12:05 stnor

I just managed somehow to get downcast to run once, but I am not sure how I made that happen...

stnor avatar May 23 '18 12:05 stnor