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

Fix: Image widget editor fails to calculate image dimensions #618

Merged
merged 9 commits into from
Jul 27, 2023

Conversation

nixterrimus
Copy link
Contributor

@nixterrimus nixterrimus commented Jul 21, 2023

Summary:

This fixes an error where images where getting no size information. In addition it also adds preview information to the image editor pane so that content editors get better visibility into what's going on.

image

I think this gives us two ways to catch image problems before they get out to prod: better code and human intervention.

Issue: https://khanacademy.atlassian.net/browse/LC-821

Test plan:

  • Start storybook and play with the widget

@nixterrimus nixterrimus self-assigned this Jul 21, 2023
@changeset-bot
Copy link

changeset-bot bot commented Jul 21, 2023

🦋 Changeset detected

Latest commit: eccbcb6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@khanacademy/perseus-editor Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@nixterrimus
Copy link
Contributor Author

@jeremywiebe I know you mentioned in the discussion that you wanted to see us Fetch the image and draw it to the canvas. As I've looked at this it doesn't seem unreasonable to use the Image HTML element and get the natural size. I've tried to clean up the code a lot (we don't need to worry about IE now that it's Edge and secretly really chrome). Does this feel reasonable?

I'm still experimenting but I think the problem is that there was an edge case where we didn't set an image size or set an error. I'm removing that case and trying to figure out how it impacts the editor experience.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 21, 2023

Size Change: -177 B (0%)

Total Size: 825 kB

Filename Size Change
packages/math-input/dist/es/index.js 79.3 kB -180 B (0%)
packages/perseus-editor/dist/es/index.js 268 kB +70 B (0%)
packages/perseus/dist/es/index.js 397 kB -67 B (0%)
ℹ️ View Unchanged
Filename Size
packages/kas/dist/es/index.js 38 kB
packages/kmath/dist/es/index.js 4.13 kB
packages/perseus-core/dist/es/index.js 55 B
packages/perseus-error/dist/es/index.js 705 B
packages/perseus-linter/dist/es/index.js 21.2 kB
packages/pure-markdown/dist/es/index.js 3.65 kB
packages/simple-markdown/dist/es/index.js 12.6 kB

compressed-size-action


Util.getImageSize(url, (width, height) => {
if (this._leadingUrl !== url) {
return;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I think this is the source of the errors. If the previous URL doesn't match the current URL then this function fails, shows no error and gives up. The note about why we were doing this was to prevent weirdness but I'll take correctness with some weirdness over a smooth but deceptively incorrect experience.

I'm figuring out how to get this editor running so that I can interactively play with it to confirm that this is the problem.

Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

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

I'll hold off to approve as this is still in draft, but I think the approach looks fine.

}

// Handle the error case
image.onerror = reject;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This seems like another place where we'll shore up this image size calculation process. The old getImageSize didn't attach an onerror.

packages/perseus-editor/src/widgets/image-editor.tsx Outdated Show resolved Hide resolved
onUrlChange: async function (url: string | undefined | null, silent) {
// Check if we've been passed something that looks like a URL
if (!url) {
this.setUrl(url, 0, 0, silent);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking at setUrl, if we get a value that's not a valid URL (even null or undefined), we'll set that value onto the Image.url attribute and the new error handling should handle it? Just want to make sure we aren't burying a new error here.

@coveralls
Copy link
Collaborator

coveralls commented Jul 25, 2023

Coverage Status

coverage: 69.0% (-0.007%) from 69.007% when pulling eccbcb6 on new/image-size-loading into fb6b9c8 on main.

@nixterrimus nixterrimus marked this pull request as ready for review July 26, 2023 21:21
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Jul 26, 2023

Gerald

Required Reviewers
  • @Khan/perseus for changes to .changeset/heavy-deers-give.md, packages/perseus/src/util.ts, packages/perseus-editor/src/widgets/image-editor.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 26, 2023

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (e250cc0) and published it to npm. You
can install it using the tag PR618.

Example:

yarn add @khanacademy/perseus@PR618

@jeremywiebe jeremywiebe requested a review from a team July 26, 2023 22:52
Copy link
Collaborator

@jeremywiebe jeremywiebe left a comment

Choose a reason for hiding this comment

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

Excited to get this fixed. Thanks for adding the image preview, that's a really nice touch!

"@khanacademy/perseus-editor": minor
---

Update image editor so it has a small preview
Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you think about also mentioning the bug fix here?

} catch (error) {
this.setState({
backgroundImageError:
"There was an error loading the image URL",
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we want to provide the error text in this message? For learner-facing I think we'd want more polish, but the source error might give more insight to the content author why the image couldn't load. What do you think?

@nixterrimus nixterrimus merged commit 97d02da into main Jul 27, 2023
9 checks passed
@nixterrimus nixterrimus deleted the new/image-size-loading branch July 27, 2023 21:03
nixterrimus pushed a commit that referenced this pull request Aug 4, 2023
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.

4 participants