Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smart crop Assets block powered by DMwOA #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

@anuraggupta228 anuraggupta228 linked an issue Nov 26, 2024 that may be closed by this pull request
@sdmcraft
Copy link
Collaborator

I think we can directly use the srcset functionality of the picture which serves the exact purpose natively. For e.g.

<picture>
  <source srcset="image.jpg?smartcrop=large" media="(min-width: 1024px)">
  <source srcset="image.jpg?smartcrop=medium" media="(min-width: 768px)">
  <source srcset="image.jpg?smartcrop=small">
  <img src="image.jpg" alt="A responsive example">
</picture>

@anuraggupta228
Copy link
Contributor Author

@sdmcraft updated the PR. feel free to review again

@sdmcraft
Copy link
Collaborator

@anuraggupta228 , as we discussed offline, I think it may be worthwhile to make "smartcrop" a block/section/page level modifier instead of a block. And then for each entity (whether page / section / block) having that identifier, decorate its images to use smart crop

// check if smartcrop should be applied by checking page level
// meta tag or class attribute or section level data attribute
const renderImgSmartCrop = window.hlx?.aemassets?.smartCrops
&& (hasDMImageSmartcropMeta() || ele.classList?.contains('dm-image-smartcrop')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
&& (hasDMImageSmartcropMeta() || ele.classList?.contains('dm-image-smartcrop')
&& (hasDMImageSmartcropMeta() || ele.classList?.contains('smartcrop')


function hasDMImageSmartcropMeta() {
const metaTags = document.getElementsByTagName('meta');
const matchingMeta = Array.from(metaTags).find((meta) => meta.name === 'dm-image-smartcrop' && meta.content === 'true');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const matchingMeta = Array.from(metaTags).find((meta) => meta.name === 'dm-image-smartcrop' && meta.content === 'true');
const matchingMeta = Array.from(metaTags).find((meta) => meta.name === 'smartcrop' && meta.content === 'true');

// meta tag or class attribute or section level data attribute
const renderImgSmartCrop = window.hlx?.aemassets?.smartCrops
&& (hasDMImageSmartcropMeta() || ele.classList?.contains('dm-image-smartcrop')
|| ele.getAttribute('data-dm-image-smartcrop') === 'true');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|| ele.getAttribute('data-dm-image-smartcrop') === 'true');
|| ele.getAttribute('data-smartcrop') === 'true');

const extImages = ele.querySelectorAll('a');
extImages.forEach((extImage) => {
if (isExternalImage(extImage, deliveryMarker)) {
const extImageSrc = extImage.getAttribute('href');
const extPicture = createOptimizedPicture(extImageSrc);

// except the samrtcrop param in <img>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// except the samrtcrop param in <img>,
// except the smartcrop param in <img>,


// This would potentially repalce all exisiting picture tag with smartcrop loaded picture tag
// without this check, it would not account for block level smartcrop flag
if (renderImgSmartCrop) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we are doing it too late and call to createOptimizedPicture can be avoided if smart crop is to be done. Can we check renderImgSmartCrop in the beginning itself and invoke

if smartcrop
  createOptimizedPictureWithSmartcrop
else
  createOptimizedPicture

Copy link
Contributor Author

@anuraggupta228 anuraggupta228 Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code for decorateExternalImages() is managed with 2 parts -

decorateExternalImages(ele, deliveryMarker) {
    const renderImgSmartCrop = // checks for renderImgSmartCrop to be render or not;
    
    // PART : 1 (checking with all <a> tag)
    // convert all <a> tag to <picture> tag 

   // PART : 2 (checking with all <picture> tag)
   // when any block/section explicitly call for decorateExternalImages() by setting at section/block level metadata then we'll need to check with all exisiting <picture> tag too in the HTML and recreate it with smartcrop
}

the beahvaiour to note here is that block/section code comes into picture after page load so when any block calls for decorateExternalImages() then by that time all <a> would have been already converted to <picture> tag leaving smartcrop unaccounted as on page load ele remains 'main'.

and I think it won't make much of a diff we all part1 or part2 first. wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Smart Crop Assets with DM OpenAPI
2 participants