|
|
@@ -11,11 +11,8 @@ import { isImageWidget } from './utils.js';
|
|
|
const WIDGET_SELECTED_CLASS_NAME = 'ck-widget_selected';
|
|
|
|
|
|
/**
|
|
|
- * Returns model to view selection converter. If model selection is placed around image widget:
|
|
|
- *
|
|
|
- * [<image src="..." alt="..."></image>]
|
|
|
- *
|
|
|
- * then it will be converted into fake selection.
|
|
|
+ * Returns model to view selection converter. This converter is used after default selection conversion is made.
|
|
|
+ * It creates fake view selection when {@link engine.view.Selection#getSelectedElement} returns instance of image widget.
|
|
|
*
|
|
|
* @param {Function} t {@link utils.Locale#t Locale#t function} used to translate default fake selection's label.
|
|
|
* @returns {Function}
|
|
|
@@ -27,16 +24,18 @@ export function modelToViewSelection( t ) {
|
|
|
const viewSelection = conversionApi.viewSelection;
|
|
|
const selectedElement = viewSelection.getSelectedElement();
|
|
|
|
|
|
- if ( !selectedElement || !isImageWidget( selectedElement ) ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
+ // Remove selected class from previously selected widget.
|
|
|
if ( previouslySelected && previouslySelected.hasClass( WIDGET_SELECTED_CLASS_NAME ) ) {
|
|
|
previouslySelected.removeClass( WIDGET_SELECTED_CLASS_NAME );
|
|
|
}
|
|
|
|
|
|
+ if ( !selectedElement || !isImageWidget( selectedElement ) ) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
let fakeSelectionLabel = t( 'image widget' );
|
|
|
- const altText = selectedElement.getAttribute( 'alt' );
|
|
|
+ const imgElement = selectedElement.getChild( 0 );
|
|
|
+ const altText = imgElement.getAttribute( 'alt' );
|
|
|
|
|
|
if ( altText ) {
|
|
|
fakeSelectionLabel = `${ altText } ${ fakeSelectionLabel }`;
|
|
|
@@ -84,26 +83,25 @@ export function viewToModelImage() {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Check if 'src' attribute can be converted.
|
|
|
- if ( !viewImg.hasAttribute( 'src' ) || !consumable.consume( viewImg, { attributes: [ 'src' ] } ) ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // Check if 'alt' attribute can be converted.
|
|
|
- if ( !viewImg.hasAttribute( 'alt' ) || !consumable.consume( viewImg, { attributes: [ 'alt' ] } ) ) {
|
|
|
+ // Consume img element.
|
|
|
+ if ( !consumable.consume( viewImg, { name: true } ) ) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- // Consume img element.
|
|
|
- if ( !consumable.consume( viewImg, { name: true } ) ) {
|
|
|
+ // Check if 'src' attribute can be converted - it is required attribute.
|
|
|
+ if ( !consumable.consume( viewImg, { attribute: [ 'src' ] } ) ) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// Create model element.
|
|
|
const modelImage = new ModelElement( 'image' );
|
|
|
|
|
|
+ // Not required attribute - convert if present.
|
|
|
+ if ( consumable.consume( viewImg, { attribute: [ 'alt' ] } ) ) {
|
|
|
+ modelImage.setAttribute( 'alt', viewImg.getAttribute( 'alt' ) );
|
|
|
+ }
|
|
|
+
|
|
|
modelImage.setAttribute( 'src', viewImg.getAttribute( 'src' ) );
|
|
|
- modelImage.setAttribute( 'alt', viewImg.getAttribute( 'alt' ) );
|
|
|
|
|
|
data.output = modelImage;
|
|
|
};
|
|
|
@@ -119,9 +117,12 @@ export function viewToModelImage() {
|
|
|
*/
|
|
|
export function modelToViewImage( modelElement ) {
|
|
|
const viewImg = new ViewEmptyElement( 'img', {
|
|
|
- src: modelElement.getAttribute( 'src' ),
|
|
|
- alt: modelElement.getAttribute( 'alt' )
|
|
|
+ src: modelElement.getAttribute( 'src' )
|
|
|
} );
|
|
|
|
|
|
+ if ( modelElement.hasAttribute( 'alt' ) ) {
|
|
|
+ viewImg.setAttribute( 'alt', modelElement.getAttribute( 'alt' ) );
|
|
|
+ }
|
|
|
+
|
|
|
return new ViewContainerElement( 'figure', { class: 'image' }, viewImg );
|
|
|
}
|