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

Re-introduce lazy loading #2367 #2450

Merged

Conversation

LamJiuFong
Copy link
Contributor

@LamJiuFong LamJiuFong commented Mar 10, 2024

What is the purpose of this pull request?
Fix #2367

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Overview of changes:
Add lazy property to <pic> and <annotate>
Checks if either width or height are given when users want to use lazy loading, a warning will be logged to the console when both are missing

Anything you'd like to highlight/discuss:

Testing instructions:

Proposed commit message: (wrap lines at 72 characters)
Add lazy property to pic and annotate


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

Reviewer checklist:

Indicate the SEMVER impact of the PR:

  • Major (when you make incompatible API changes)
  • Minor (when you add functionality in a backward compatible manner)
  • Patch (when you make backward compatible bug fixes)

At the end of the review, please label the PR with the appropriate label: r.Major, r.Minor, r.Patch.

Breaking change release note preparation (if applicable):

  • To be included in the release note for any feature that is made obsolete/breaking

Give a brief explanation note about:

  • what was the old feature that was made obsolete
  • any replacement feature (if any), and
  • how the author should modify his website to migrate from the old feature to the replacement feature (if possible).

Copy link

codecov bot commented Mar 10, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 49.04%. Comparing base (f0759d3) to head (92b1c36).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2450      +/-   ##
==========================================
+ Coverage   48.97%   49.04%   +0.07%     
==========================================
  Files         124      124              
  Lines        5254     5262       +8     
  Branches     1112     1118       +6     
==========================================
+ Hits         2573     2581       +8     
  Misses       2376     2376              
  Partials      305      305              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@LamJiuFong
Copy link
Contributor Author

Would like to seek for advice on the log warning:

Screenshot 2024-03-10 at 9 55 33 PM

@yucheng11122017
Copy link
Contributor

Would like to seek for advice on the log warning:

Screenshot 2024-03-10 at 9 55 33 PM

What is your confusion?

@LamJiuFong
Copy link
Contributor Author

Would like to seek for advice on the log warning:
Screenshot 2024-03-10 at 9 55 33 PM

What is your confusion?

Not sure if it can be further improved from a user's point of view.

@yucheng11122017
Copy link
Contributor

Would like to seek for advice on the log warning:
Screenshot 2024-03-10 at 9 55 33 PM

What is your confusion?

Not sure if it can be further improved from a user's point of view.

Ah I see. I think it could be better with the file where it came from so users know exactly where the error is.
Apart from that, it seems ok to me.

@LamJiuFong
Copy link
Contributor Author

Would like to seek for advice on the log warning:
Screenshot 2024-03-10 at 9 55 33 PM

What is your confusion?

Not sure if it can be further improved from a user's point of view.

Ah I see. I think it could be better with the file where it came from so users know exactly where the error is. Apart from that, it seems ok to me.

Sure, I will work on it, thanks for the suggestion!

@LamJiuFong
Copy link
Contributor Author

@yucheng11122017 I have changed the warning message to only specify the file and not the code snippet.

Screenshot 2024-03-13 at 10 07 58 PM

This is because I realised we can only get the code snippet if the <pic> or <annotate> is declared as a <variable> due to how we process the source files.

However, I think it is sufficient to only show the file paths since the users can now have an idea on where to do the changes.
What do you think?

@kaixin-hc
Copy link
Contributor

I think having just the filepath but not the code is OK, but can you get the filepath to precede the warning message and be on the same line? Just think its weird that it prints "In ___" afterwards, it looks messy...

@LamJiuFong
Copy link
Contributor Author

LamJiuFong commented Mar 13, 2024

@kaixin-hc thanks for the suggestion! I have modified the output as below, not sure if the use of --- will be too messy. I was thinking of separating the filepath and the output message to make it easier to read?

Screenshot 2024-03-14 at 1 40 05 AM

What do you think?

@kaixin-hc
Copy link
Contributor

lgtm


nodeProcessor.processNode(testNode, new Context(path.resolve(''), [], {}, {}));

const logMessage = consoleSpy.mock.calls[0];
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can write an expect statement like expect(consoleSpy.warn).toHaveBeenCalledWith(undefined); and combine these two

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion!
I am thinking of another way to do it expect(consoleSpy).not.toHaveBeenCalled().
It checks if the warn method is not being called at all

const testCode = '<pic scr="" alt="" width="300" lazy></pic>';
const testNode = parseHTML(testCode)[0] as MbNode;

const consoleSpy = jest.spyOn(logger, 'warn');
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm OK with this I think - though I remember some discussions about spy-on vs mock, where mock tends to be preferred over spying as errors can't be produced from winston.

Here is some additional discussions on stack overflow for your consideration...

And a related issue in markbind #2099 - feel free to weigh in with your thoughts.

Thanks @luminousleek for sharing these links with me!

Copy link
Contributor

Choose a reason for hiding this comment

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

I've got an example of how you might mock the logger in #2463

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the suggestions @kaixin-hc @luminousleek ! Will follow the suggestions and update my code.

@LamJiuFong LamJiuFong changed the title [Request Review for Functionality Code] Re-introduce lazy loading #2367 [Request Review] Re-introduce lazy loading #2367 Mar 14, 2024
Co-authored-by: david <71922282+itsyme@users.noreply.github.com>
@kaixin-hc kaixin-hc added this to the v5.3.1 milestone Mar 15, 2024
@LamJiuFong LamJiuFong changed the title [Request Review] Re-introduce lazy loading #2367 Re-introduce lazy loading #2367 Mar 16, 2024
Copy link
Contributor

@kaixin-hc kaixin-hc left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

Hi @LamJiuFong thanks for the work on this PR!

Personally, I'd prefer not to override the height to maintain the aspect ratio - this would be breaking behaviour in that case. Plus if users want to change the aspect ratio that is their choice as well.
In other words, I think the expected behavior should be: if users enter both height and width, that should be used. Only if either one is missing, do we maintain the height ratio.
wdyt? tagging @kaixin-hc as well

docs/userGuide/syntax/pictures.md Outdated Show resolved Hide resolved
@@ -175,7 +175,6 @@ export class NodeProcessor {

// log warnings for conflicting attributes
if (_.has(warnConflictingAtributesMap, node.name)) { warnConflictingAtributesMap[node.name](node); }

Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't have unnecessary changes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will make the changes!

const imgHeight = renderedImg.naturalHeight;
const imgWidth = renderedImg.naturalWidth;
const aspectRatio = imgWidth / imgHeight;
if (this.hasWidth) { // if width is present, overwrite the height (if any) to maintain aspect ratio
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm if this behavior necessary? I think its ok for a user to adjust the aspect ratio as needed.
This might also be breaking behavior in that case.

@yucheng11122017
Copy link
Contributor

Also another thing discussed by @kaixin-hc and I is the behaviour when the width and height is relative eg. 50% or max-width.

Could you check @LamJiuFong and see if lazy loading will cause issues in that case?

default:
break;
}
} catch (error) {
logger.error(error);
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the newlines were intentional for readability! Perhaps reverting them could prevent any unnecessary change!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, will make the changes!

@LamJiuFong
Copy link
Contributor Author

@yucheng11122017 Thanks for raising them up, I have not thought about the issues you mentioned when I was solving this issue.

Previously, I followed the user guide to enforce the aspect ratio
https://markbind-master.netlify.app/userguide/components/imagesanddiagrams#pictures
But now when I think about it, maybe we should remove this constraint and leave it to the user to decide what they want

According to https://stackoverflow.com/questions/65376615/set-height-of-image-with-100-width-for-cls-cumulative-layout-shift:

Screenshot 2024-03-17 at 2 35 46 PM

it seems like layout shifts won't occur if the widths and heights are relative.

Also, our current implementation of Annotate and Pic doesn't support the case where inputs are proportions eg. width: 50%. I think it is a good idea to support it but I am not sure if it should be tackled altogether in this issue because it looks like a separate feature?

Thanks for commenting!

@yucheng11122017
Copy link
Contributor

Thanks for the detailed response @LamJiuFong

Previously, I followed the user guide to enforce the aspect ratio https://markbind-master.netlify.app/userguide/components/imagesanddiagrams#pictures But now when I think about it, maybe we should remove this constraint and leave it to the user to decide what they want

Ah I see I must have missed this. In that case, I think its ok to continue with this current implementation. We can discuss seperately if we should allow the overriding of aspect ratio in that case. Checking in with @kaixin-hc if you have other opinions?

According to https://stackoverflow.com/questions/65376615/set-height-of-image-with-100-width-for-cls-cumulative-layout-shift:

Screenshot 2024-03-17 at 2 35 46 PM

it seems like layout shifts won't occur if the widths and heights are relative.

Ok great!

Also, our current implementation of Annotate and Pic doesn't support the case where inputs are proportions eg. width: 50%. I think it is a good idea to support it but I am not sure if it should be tackled altogether in this issue because it looks like a separate feature?

Oh hmm I see. I was not aware of this. Thank you for investigating! We can discuss this seperately as well but you can ignore it for now. Thanks!

@kaixin-hc
Copy link
Contributor

kaixin-hc commented Mar 17, 2024

Thanks for investigating!

I'm good to shelve those questions for another issue and merge this first. (But um. Why the checks failing?)

@LamJiuFong
Copy link
Contributor Author

Thanks for the comments!

I'm good to shelve those questions for another issue and merge this first. (But um. Why the checks failing?)

The logger was imported twice after I merged the master branch. I have fixed the issue.

Copy link
Contributor

@yucheng11122017 yucheng11122017 left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for the work

@yucheng11122017 yucheng11122017 merged commit 371228d into MarkBind:master Mar 17, 2024
9 checks passed
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.

Re-introduce lazy loading for pictures
5 participants