Przeglądaj źródła

Basic implementation.

Kamil Piechaczek 5 lat temu
rodzic
commit
b76b4e1524

+ 18 - 1
packages/ckeditor5-image/src/image/utils.js

@@ -113,11 +113,28 @@ export function isImageAllowed( model ) {
  * Assuming that image is always a first child of a widget (ie. `figureView.getChild( 0 )`) is unsafe as other features might
  * inject their own elements to the widget.
  *
+ * The `<img>` can be wrapped to other elements, e.g. `<a>`. Nested check required.
+ *
  * @param {module:engine/view/element~Element} figureView
  * @returns {module:engine/view/element~Element}
  */
 export function getViewImgFromWidget( figureView ) {
-	return Array.from( figureView.getChildren() ).find( viewChild => viewChild.is( 'img' ) );
+	// TODO: Add a test for that change.
+	const figureChildren = [];
+
+	for ( const figureChild of figureView.getChildren() ) {
+		figureChildren.push( figureChild );
+
+		for ( const nestedChild of figureChild.getChildren() ) {
+			figureChildren.push( nestedChild );
+		}
+	}
+
+	return figureChildren.find( viewChild => {
+		if ( viewChild.is( 'img' ) ) {
+			return true;
+		}
+	} );
 }
 
 // Checks if image is allowed by schema in optimal insertion parent.

+ 28 - 0
packages/ckeditor5-link/src/linkimage.js

@@ -0,0 +1,28 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module link/linkimage
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import LinkImageEditing from './linkimageediting';
+import LinkImageUI from './linkimageui';
+
+export default class LinkImage extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ LinkImageEditing, LinkImageUI ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'LinkImage';
+	}
+}

+ 122 - 0
packages/ckeditor5-link/src/linkimageediting.js

@@ -0,0 +1,122 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module link/linkimageediting
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import Image from '@ckeditor/ckeditor5-image/src/image';
+import LinkEditing from './linkediting';
+
+export default class LinkImageEditing extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ Image, LinkEditing ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'LinkImageEditing';
+	}
+
+	init() {
+		const editor = this.editor;
+
+		editor.model.schema.extend( 'image', { allowAttributes: [ 'linkHref' ] } );
+
+		editor.conversion.for( 'upcast' ).add( this._upcastLink() );
+		editor.conversion.for( 'upcast' ).add( this._upcastImageLink( 'img' ) );
+		editor.conversion.for( 'upcast' ).add( this._upcastImageLink( 'figure' ) );
+		editor.conversion.for( 'downcast' ).add( this._downcastImageLink() );
+	}
+
+	/**
+	 * Returns a converter that consumes the 'href' attribute if a link contains an image.
+	 *
+	 * @private
+	 * @returns {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' );
+
+				if ( imageInLink ) {
+					// There's an image inside an <a> element - we consume it so it won't be picked up by the 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 - i.e. other converter with priority=highest - a standard check.
+						return;
+					}
+
+					// Consume 'linkHref' attribute from link element.
+					conversionApi.consumable.consume( viewLink, consumableAttributes );
+				}
+			}, { priority: 'high' } );
+		};
+	}
+
+	/**
+	 * Returns a converter for links that wraps the `<img>` element.
+	 *
+	 * @private
+	 * @param {String} elementName Name of the element to upcast.
+	 * @returns {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 linkHref attribute from link element on model image element.
+						conversionApi.writer.setAttribute( 'linkHref', linkHref, modelImage );
+					}
+				}
+			} );
+		};
+	}
+
+	/**
+	 * Return a converter that adds the `<a>` element to data.
+	 *
+	 * @private
+	 * @returns {Function}
+	 */
+	_downcastImageLink() {
+		return dispatcher => {
+			dispatcher.on( 'attribute:linkHref:image', ( evt, data, conversionApi ) => {
+				// 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.
+				const writer = conversionApi.writer;
+
+				// 1. Create an empty link element.
+				const linkElement = writer.createContainerElement( 'a', { href: data.attributeNewValue } );
+
+				// 2. Insert link inside the associated image.
+				writer.insert( writer.createPositionAt( viewImage, 0 ), linkElement );
+
+				// 3. Move the image to the link.
+				writer.move( writer.createRangeOn( viewImage.getChild( 1 ) ), writer.createPositionAt( linkElement, 0 ) );
+			} );
+		};
+	}
+}

+ 31 - 0
packages/ckeditor5-link/src/linkimageui.js

@@ -0,0 +1,31 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/**
+ * @module link/linkimageui
+ */
+
+import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
+import Image from '@ckeditor/ckeditor5-image/src/image';
+import LinkUI from './linkui';
+
+export default class LinkImageUI extends Plugin {
+	/**
+	 * @inheritDoc
+	 */
+	static get requires() {
+		return [ Image, LinkUI ];
+	}
+
+	/**
+	 * @inheritDoc
+	 */
+	static get pluginName() {
+		return 'LinkImageUI';
+	}
+
+	init() {
+	}
+}

+ 11 - 0
packages/ckeditor5-link/tests/manual/linkimage.html

@@ -0,0 +1,11 @@
+<div id="editor">
+	<figure class="image">
+		<a href="http://localhost">
+			<img alt="bar" src="sample.jpg">
+		</a>
+		<figcaption>CKEditor logo - caption</figcaption>
+	</figure>
+	<a href="http://localhost">
+		<img alt="bar" src="sample.jpg">
+	</a>
+</div>

+ 49 - 0
packages/ckeditor5-link/tests/manual/linkimage.js

@@ -0,0 +1,49 @@
+/**
+ * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
+ */
+
+/* globals console, window, document */
+
+import ClassicEditor from '@ckeditor/ckeditor5-editor-classic/src/classiceditor';
+import ArticlePluginSet from '@ckeditor/ckeditor5-core/tests/_utils/articlepluginset';
+import LinkImage from '../../src/linkimage';
+
+ClassicEditor
+	.create( document.querySelector( '#editor' ), {
+		plugins: [ ArticlePluginSet, LinkImage ],
+		toolbar: [
+			'heading',
+			'|',
+			'bold',
+			'italic',
+			'link',
+			'bulletedList',
+			'numberedList',
+			'|',
+			'outdent',
+			'indent',
+			'|',
+			'blockQuote',
+			'insertTable',
+			'mediaEmbed',
+			'undo',
+			'redo'
+		],
+		image: {
+			toolbar: [ 'imageStyle:full', 'imageStyle:side', '|', 'imageTextAlternative' ]
+		},
+		table: {
+			contentToolbar: [
+				'tableColumn',
+				'tableRow',
+				'mergeTableCells'
+			]
+		}
+	} )
+	.then( editor => {
+		window.editor = editor;
+	} )
+	.catch( err => {
+		console.error( err.stack );
+	} );

+ 3 - 0
packages/ckeditor5-link/tests/manual/linkimage.md

@@ -0,0 +1,3 @@
+# Linking Image
+
+TODO.

BIN
packages/ckeditor5-link/tests/manual/sample.jpg