Skip to content

Commit

Permalink
Paste (#1231)
Browse files Browse the repository at this point in the history
* update fixBrowserBehavior.ts: Get base64 content of a file ont PASTE event via FileReader. Not reflected into EDITOR yet.

* update fixBrowserBehavior.ts: Get base64 content of a file ont PASTE event via FileReader. And reflect it in the EDITOR and HTML PREVIWER.

* update fixBrowserBehavior.ts:  Add target file name as TITTLE of IMAGE.

Co-authored-by: ConanJordan <1042583309@qq.com>
  • Loading branch information
ConanJordan and ConanJordan committed May 16, 2022
1 parent 42086cf commit 9e0ede3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/ts/util/fixBrowserBehavior.ts
Expand Up @@ -1260,6 +1260,7 @@ export const paste = async (vditor: IVditor, event: (ClipboardEvent | DragEvent)
let textHTML;
let textPlain;
let files;

if ("clipboardData" in event) {
textHTML = event.clipboardData.getData("text/html");
textPlain = event.clipboardData.getData("text/plain");
Expand Down Expand Up @@ -1415,6 +1416,28 @@ export const paste = async (vditor: IVditor, event: (ClipboardEvent | DragEvent)
vditor.outline.render(vditor);
} else if (files.length > 0 && (vditor.options.upload.url || vditor.options.upload.handler)) {
await uploadFiles(vditor, files);
} else if (files.length > 0 && (!vditor.options.upload.url || !vditor.options.upload.handler)) {
var fileReader = new FileReader();
var file: File;
if ("clipboardData" in event) {
files = event.clipboardData.files;
file = files[0];
} else {
if (event.dataTransfer.types.includes("Files")) {
files = event.dataTransfer.items;
file = files[0].getAsFile();
}
}
fileReader.readAsDataURL(file);
fileReader.onload = function() {
// Get the base64 format content of target file.
var result = fileReader.result;
//console.log("This is an image.", result);
// Image in Markdown:![title or file name](dataurl)
// Put it in a newline.
var png = "\r![" + file.name + "](" + result.toString() + ")\r";
insertHTML(vditor.lute.Md2VditorIRDOM(png), vditor);
}
} else if (textPlain.trim() !== "" && files.length === 0) {
if (vditor.currentMode === "ir") {
renderers.Md2VditorIRDOM = {renderLinkDest};
Expand Down

0 comments on commit 9e0ede3

Please sign in to comment.