Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add ability to paste image to Markdown block #73

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

Merged
merged 3 commits into from
Jan 13, 2023
Merged

Conversation

Wattenberger
Copy link
Contributor

Listens for a paste event and adds the base64 image string if it contains an image. It's not ideal (clutters up the code with a long string), but I think helpful enough for now!

Screen.Recording.2023-01-12.at.2.06.04.PM.mov

@Wattenberger Wattenberger requested a review from jaked January 12, 2023 22:10
Copy link

@jaked jaked left a comment

Choose a reason for hiding this comment

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

EditorView.domEventHandlers({
paste(e, view) {
const value = e.clipboardData?.items[0];
if (value.type === "image/png") {
Copy link

Choose a reason for hiding this comment

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

seems like you could handle other image types here without any other 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.

thanks for calling me on my laziness - I wasn't sure how to test with a jpg, but added support for this & "copy image" from a website as well

});
}
};
reader.readAsDataURL(file);
Copy link

Choose a reason for hiding this comment

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

for robustness you could put some reasonable size limit on pasted files, although it's not super clear what "reasonable" is here.

https://stackoverflow.com/questions/695151/data-protocol-url-size-limitations

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good call!

@Wattenberger Wattenberger requested a review from jaked January 13, 2023 17:56
Copy link

@jaked jaked left a comment

Choose a reason for hiding this comment

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

const value = e.clipboardData?.items[0];
const MAX_IMAGE_SIZE = 5000000;
// handle images pasted from the web
if (value && value.type === "text/html") {
Copy link

Choose a reason for hiding this comment

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

nice, I didn't know pasted web images work like this!

from: view.state.selection.main.from,
to: view.state.selection.main.to,
insert: images
.filter((image) => image && image.length < MAX_IMAGE_SIZE)
Copy link

Choose a reason for hiding this comment

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

I guess since this might be a data: URL it is consistent with the other case, but I was surprised at first since in other cases the URL length doesn't correspond to the image size. Maybe rename MAX_IMAGE_SIZE to MAX_URL_SIZE for clarity? Pickiest of nits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, that's a good call

@Wattenberger Wattenberger merged commit 381e84e into main Jan 13, 2023
@Wattenberger Wattenberger deleted the aw/image-paste branch January 13, 2023 18:48
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.

2 participants