-
Notifications
You must be signed in to change notification settings - Fork 17
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
Conversation
There was a problem hiding this 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") { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good call!
There was a problem hiding this 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") { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
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