THIS IS NOT AN ISSUE: HACK to Paste Image directly from clipboard
Open
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 2.9k
- Forks
- 201
- Avg merge
- 12m
- Merged PRs (30d)
- 1
Description
First of all, thank you very much for your excellent work.
Prepare your server
2 routes :
- Updload image /upload/new (formdata with the attribut "image" is used in this present case)
- server store image
- response (json):
{url: "[url to retrive image calling getImage], alt: "image name alternative"}
- get image /getImage/:imagename (for example)
UI
The process is as follows:
- copy the image to the clipboard
- focus on the markdown editor
- paste
- the manager tries to identify the context; if it is an image, it sends the image to the server
- the server returns the url from which the image can be accessed from the server
- the handler finishes by adding the context of the markdow :
image to the clipboard and the contents of the clipboard are pasted into the markdown editor - the markdown editor is refreshed and renders the image by calling the provided url
- +++
With React, build your component
[...]
// Image upload to server
const imageUploadHandler = async (
image: File
): Promise<{ alt: string; url: string } | null> => {
if (image && image.size === 0) return null;
const formData = new FormData();
formData.append("image", image);
//formData.append("what else you want", "your value");
const response = await fetch('/upload/new', {
method: 'POST',
body: formData
})
return (await response.json()) as { alt: string, url: string }
};
const handlePaste = async (event: React.ClipboardEvent<HTMLDivElement>) => {
// Access the clipboard data using event.clipboardData
const clipboardData = event.clipboardData;
// only if clipboard payload is file
if (clipboardData.files.length === 1) {
const myfile = clipboardData.files[0] as File;
// you could perform some test: image size, type mime ....
const url = await imageUploadHandler(myfile);
event.preventDefault();
if (url) {
// change clipboard payload,
// document execCommand is obsolete, you could replace with navigator.clipboard API but some browser
// accept write() method only if the connexion is secure (https)...
document.execCommand(
"insertText",
false,
`\n`
);
} else {
document.execCommand(
"insertText",
false,
"ERROR Image has not been stored on server"
);
}
}
};
return (
<div className={`MarkDownEditor`}>
<MDEditor
[...]
// because the MDEditor interface is an extends of textarea props
// you can use all the methods provided by this interface
// e.g. add a method for dragging and dropping a file...
onPaste={handlePaste}
[...]
/>
</div>
);
[...]
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the MDEditor component and its onPaste handler, using the proposed clipboard and imageUploadHandler flow as the behavior reference. Implement image detection, upload through /upload/new, and insertion of the returned Markdown image, then verify that the editor renders the image from the returned URL.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- frontend
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100