Use native JavaScript to build thumbnail clip path

Change-Id: Ib5d734fde88eaa20ec866722af3de37abe694add
This commit is contained in:
Jon Robson 2023-05-12 14:01:23 -07:00
parent 1ee66bbf72
commit 323d770d66
5 changed files with 45 additions and 29 deletions

Binary file not shown.

Binary file not shown.

View file

@ -126,6 +126,18 @@ function createThumbnailImg( url ) {
return img; return img;
} }
/**
* Sets multiple attributes on a node.
*
* @param {HTMLElement} node
* @param {Record<String, String>} attrs
*/
const addAttributes = ( node, attrs ) => {
Object.keys( attrs ).forEach( ( key ) => {
node.setAttribute( key, attrs[ key ] );
} );
};
/** /**
* Creates the SVG image element that represents the thumbnail. * Creates the SVG image element that represents the thumbnail.
* *
@ -141,7 +153,7 @@ function createThumbnailImg( url ) {
* @param {number} thumbnailHeight * @param {number} thumbnailHeight
* @param {number} width * @param {number} width
* @param {number} height * @param {number} height
* @return {JQuery} * @return {HTMLElement}
*/ */
export function createThumbnailSVG( export function createThumbnailSVG(
@ -162,28 +174,31 @@ export function createThumbnailSVG(
line.setAttribute( 'points', points.join( ' ' ) ); line.setAttribute( 'points', points.join( ' ' ) );
line.setAttribute( 'stroke-width', 1 ); line.setAttribute( 'stroke-width', 1 );
const $thumbnailSVGImage = $( document.createElementNS( nsSvg, 'image' ) ); const thumbnailSVGImage = document.createElementNS( nsSvg, 'image' );
$thumbnailSVGImage[ 0 ].setAttributeNS( nsXlink, 'href', url ); thumbnailSVGImage.setAttributeNS( nsXlink, 'href', url );
// The following classes are used here: // The following classes are used here:
// * mwe-popups-is-not-tall // * mwe-popups-is-not-tall
// * mwe-popups-is-tall // * mwe-popups-is-tall
$thumbnailSVGImage thumbnailSVGImage.classList.add( className );
.addClass( className ) addAttributes(
.attr( { thumbnailSVGImage,
{
x, x,
y, y,
width: thumbnailWidth, width: thumbnailWidth,
height: thumbnailHeight height: thumbnailHeight
} ); }
);
const $thumbnail = $( document.createElementNS( nsSvg, 'svg' ) ) const thumbnail = document.createElementNS( nsSvg, 'svg' );
.attr( { addAttributes(
thumbnail, {
xmlns: nsSvg, xmlns: nsSvg,
width, width,
height height
} ) }
.append( $thumbnailSVGImage ); );
thumbnail.appendChild( thumbnailSVGImage );
$thumbnail.append( line ); thumbnail.appendChild( line );
return $thumbnail; return thumbnail;
} }

View file

@ -135,8 +135,8 @@ QUnit.test( 'createPagePreview', ( assert ) => {
assert.strictEqual( preview.hasThumbnail, true, 'Preview has thumbnail.' ); assert.strictEqual( preview.hasThumbnail, true, 'Preview has thumbnail.' );
assert.deepEqual( assert.deepEqual(
preview.thumbnail.el.html(), $( preview.thumbnail.el ).html(),
createThumbnail( model.thumbnail ).el.html(), $( createThumbnail( model.thumbnail ).el ).html(),
'Preview thumbnail is the correct one.' 'Preview thumbnail is the correct one.'
); );
assert.strictEqual( assert.strictEqual(

View file

@ -86,32 +86,32 @@ QUnit.test( 'createThumbnail - tall image element', ( assert ) => {
} ); } );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'x' ), +$( thumbnail.el ).find( 'image' ).attr( 'x' ),
case_.expectedX, case_.expectedX,
`Image element x coordinate is correct. ${case_.message}` `Image element x coordinate is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'y' ), +$( thumbnail.el ).find( 'image' ).attr( 'y' ),
case_.expectedY, case_.expectedY,
`Image element y coordinate is correct. ${case_.message}` `Image element y coordinate is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'width' ), +$( thumbnail.el ).find( 'image' ).attr( 'width' ),
case_.width, case_.width,
`Image element width is correct. ${case_.message}` `Image element width is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'height' ), +$( thumbnail.el ).find( 'image' ).attr( 'height' ),
case_.height, case_.height,
`Image element height is correct. ${case_.message}` `Image element height is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.attr( 'width' ), +$( thumbnail.el ).attr( 'width' ),
case_.expectedSVGWidth, case_.expectedSVGWidth,
`Image SVG width is correct. ${case_.message}` `Image SVG width is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.attr( 'height' ), +$( thumbnail.el ).attr( 'height' ),
case_.expectedSVGHeight, case_.expectedSVGHeight,
`Image SVG height is correct. ${case_.message}` `Image SVG height is correct. ${case_.message}`
); );
@ -208,32 +208,32 @@ QUnit.test( 'createThumbnail - landscape image element', ( assert ) => {
} ); } );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'x' ), +$( thumbnail.el ).find( 'image' ).attr( 'x' ),
case_.expectedX, case_.expectedX,
`Image x coordinate is correct. ${case_.message}` `Image x coordinate is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'y' ), +$( thumbnail.el ).find( 'image' ).attr( 'y' ),
case_.expectedY, case_.expectedY,
`Image y coordinate is correct. ${case_.message}` `Image y coordinate is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'width' ), +$( thumbnail.el ).find( 'image' ).attr( 'width' ),
case_.width, case_.width,
`Image element width is correct. ${case_.message}` `Image element width is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.find( 'image' ).attr( 'height' ), +$( thumbnail.el ).find( 'image' ).attr( 'height' ),
case_.height, case_.height,
`Image element height is correct. ${case_.message}` `Image element height is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.attr( 'width' ), +$( thumbnail.el ).attr( 'width' ),
case_.expectedSVGWidth, case_.expectedSVGWidth,
`Image SVG width is correct. ${case_.message}` `Image SVG width is correct. ${case_.message}`
); );
assert.strictEqual( assert.strictEqual(
+thumbnail.el.attr( 'height' ), +$( thumbnail.el ).attr( 'height' ),
case_.expectedSVGHeight, case_.expectedSVGHeight,
`Image SVG height is correct. ${case_.message}` `Image SVG height is correct. ${case_.message}`
); );
@ -318,9 +318,10 @@ QUnit.test( 'createThumbnailSVG', ( assert ) => {
expectedPoints: '0 0 0 300' expectedPoints: '0 0 0 300'
} }
].forEach( ( { className, expectedPoints, expectedHTML }, i ) => { ].forEach( ( { className, expectedPoints, expectedHTML }, i ) => {
const $thumbnail = createThumbnailSVG( const thumbnail = createThumbnailSVG(
className, url, x, y, thumbnailWidth, thumbnailHeight, className, url, x, y, thumbnailWidth, thumbnailHeight,
width, height ); width, height );
const $thumbnail = $( thumbnail );
// Simplify HTML image test // Simplify HTML image test
const points = $thumbnail.find( 'polyline' ).attr( 'points' ); const points = $thumbnail.find( 'polyline' ).attr( 'points' );