Skip to content
This repository was archived by the owner on Jun 3, 2026. It is now read-only.

Add ability to paste image to Markdown block - #73

Merged
Wattenberger merged 3 commits into
mainfrom
aw/image-paste
Jan 13, 2023
Merged

Add ability to paste image to Markdown block#73
Wattenberger merged 3 commits into
mainfrom
aw/image-paste

Conversation

@Wattenberger

Copy link
Copy Markdown
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

@jaked jaked left a comment

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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

@jaked jaked left a comment

Copy link
Copy Markdown

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
Copy Markdown

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
Copy Markdown

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
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants