Skip to content
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

支持使用 Base64 显示图片 #1231

Merged
merged 3 commits into from May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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