Skip to content

Commit

Permalink
Merge pull request #7591 from ckeditor/i/7457
Browse files Browse the repository at this point in the history
Feature (link): Added the icon indication in the top-right corner of the image that informs the image is linked. Closes #7457.

Internal (theme-lark): Added the linked image indication styles.
  • Loading branch information
pomek committed Jul 15, 2020
2 parents de476bb + 40d3e4f commit 9887b7f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/ckeditor5-link/src/linkimage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import LinkImageEditing from './linkimageediting';
import LinkImageUI from './linkimageui';

import '../theme/linkimage.css';

/**
* The `LinkImage` plugin.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/ckeditor5-link/src/linkimageediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
import ImageEditing from '@ckeditor/ckeditor5-image/src/image/imageediting';
import LinkEditing from './linkediting';

import linkIcon from '../theme/icons/link.svg';

/**
* The link image engine feature.
*
Expand Down Expand Up @@ -115,6 +117,14 @@ function downcastImageLink() {
// But we need to check whether the link element exists.
const linkInImage = Array.from( viewFigure.getChildren() ).find( child => child.name === 'a' );

// Create an icon indicator for a linked image.
const linkIconIndicator = writer.createUIElement( 'span', { class: 'ck ck-link-image_icon' }, function( domDocument ) {
const domElement = this.toDomElement( domDocument );
domElement.innerHTML = linkIcon;

return domElement;
} );

// If so, update the attribute if it's defined or remove the entire link if the attribute is empty.
if ( linkInImage ) {
if ( data.attributeNewValue ) {
Expand All @@ -135,6 +145,9 @@ function downcastImageLink() {

// 3. Move the image to the link.
writer.move( writer.createRangeOn( viewFigure.getChild( 1 ) ), writer.createPositionAt( linkElement, 0 ) );

// 4. Inset the linked image icon indicator.
writer.insert( writer.createPositionAt( linkElement, 'end' ), linkIconIndicator );
}
} );
};
Expand Down
23 changes: 20 additions & 3 deletions packages/ckeditor5-link/tests/linkimageediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import normalizeHtml from '@ckeditor/ckeditor5-utils/tests/_utils/normalizehtml'
import { getData as getViewData } from '@ckeditor/ckeditor5-engine/src/dev-utils/view';
import ImageCaptionEditing from '@ckeditor/ckeditor5-image/src/imagecaption/imagecaptionediting';

import linkIcon from '../theme/icons/link.svg';

// We can import the SVG code directly to avoid re-edit, but we have to make sure that the icon code has `</path>` closing tag.
// After cleaning up the icons, the closing tag can be re-parsed and some tests will fail.
const linkIconInditatorElement = '<span class="ck ck-link-image_icon">' + linkIcon + '</span>';

describe( 'LinkImageEditing', () => {
let editor, model, view;

Expand Down Expand Up @@ -48,15 +54,19 @@ describe( 'LinkImageEditing', () => {
setModelData( model, '<image src="/assets/sample.png" alt="alt text" linkHref="http://ckeditor.com"></image>' );

expect( editor.getData() ).to.equal(
'<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" src="/assets/sample.png"></a></figure>'
'<figure class="image"><a href="http://ckeditor.com"><img alt="alt text" src="/assets/sample.png">' +
linkIconInditatorElement +
'</a></figure>'
);
} );

it( 'should convert an image with a link and without alt attribute', () => {
setModelData( model, '<image src="/assets/sample.png" linkHref="http://ckeditor.com"></image>' );

expect( editor.getData() ).to.equal(
'<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png"></a></figure>'
'<figure class="image"><a href="http://ckeditor.com"><img src="/assets/sample.png">' +
linkIconInditatorElement +
'</a></figure>'
);
} );

Expand All @@ -72,6 +82,7 @@ describe( 'LinkImageEditing', () => {
'<figure class="image">' +
'<a href="http://ckeditor.com">' +
'<img sizes="100vw" src="/assets/sample.png" srcset="small.png 148w, big.png 1024w"></img>' +
linkIconInditatorElement +
'</a>' +
'</figure>'
);
Expand Down Expand Up @@ -264,6 +275,8 @@ describe( 'LinkImageEditing', () => {
'<figure class="ck-widget image" contenteditable="false">' +
'<a href="http://ckeditor.com">' +
'<img alt="alt text" src="/assets/sample.png"></img>' +
// Content of the UIElement is skipped here.
'<span class="ck ck-link-image_icon"></span>' +
'</a>' +
'</figure>'
);
Expand All @@ -281,6 +294,8 @@ describe( 'LinkImageEditing', () => {
'<figure class="ck-widget image" contenteditable="false">' +
'<a href="https://ckeditor.com/why-ckeditor/">' +
'<img alt="alt text" src="/assets/sample.png"></img>' +
// Content of the UIElement is skipped here.
'<span class="ck ck-link-image_icon"></span>' +
'</a>' +
'</figure>'
);
Expand All @@ -302,7 +317,7 @@ describe( 'LinkImageEditing', () => {
} );
} );

describe( 'figure > a > img + figcaption', () => {
describe( 'figure > a > img + span + figcaption', () => {
it( 'should convert a link and the caption element', () => {
return VirtualTestEditor
.create( {
Expand All @@ -319,6 +334,8 @@ describe( 'LinkImageEditing', () => {
'<figure class="ck-widget image" contenteditable="false">' +
'<a href="http://ckeditor.com">' +
'<img alt="alt text" src="/assets/sample.png"></img>' +
// Content of the UIElement is skipped here.
'<span class="ck ck-link-image_icon"></span>' +
'</a>' +
'<figcaption class="ck-editor__editable ck-editor__nested-editable" ' +
'contenteditable="true" data-placeholder="Enter image caption">' +
Expand Down
4 changes: 3 additions & 1 deletion packages/ckeditor5-link/tests/manual/linkimage.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Linking Image

Both images should have defined the `linkHref` attribute.
Only the first image should have:
- defined the `linkHref` attribute.
- the linked image icon indicator in the top-right corner (the element should be placed inside the `<a>` element).
2 changes: 1 addition & 1 deletion packages/ckeditor5-link/theme/icons/link.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions packages/ckeditor5-link/theme/linkimage.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

.ck.ck-link-image_icon {
position: absolute;
top: var(--ck-spacing-medium);
right: var(--ck-spacing-medium);
width: 18px;
height: 18px;
padding: 5px;
border-radius: var(--ck-border-radius);

& svg {
fill: currentColor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/

.ck.ck-link-image_icon {
color: hsl(0, 0%, 100%);
background: hsla(0, 0%, 0%, .4);
}

0 comments on commit 9887b7f

Please sign in to comment.