Skip to content

Commit

Permalink
Enable opening images copied to clipboard from MS Word or Google Docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hbl917070 committed Apr 11, 2024
1 parent 69d06a5 commit b4c5f74
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 5 deletions.
17 changes: 16 additions & 1 deletion Tiefsee/Lib/ClipboardLib.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.IO;
using System.IO;
using System.Text;

namespace Tiefsee;
Expand All @@ -12,6 +12,17 @@ public class ClipboardLib {
/// <returns></returns>
public ClipboardContent GetClipboardContent(int maxTextLength = 5000) {
try {

// html
if (Clipboard.ContainsText(TextDataFormat.Html)) {
string html = Clipboard.GetText(TextDataFormat.Html);
return new() {
Type = "html",
Data = html
};
}

// 圖片
if (Clipboard.ContainsImage()) {
using (var ms = new MemoryStream()) {
Clipboard.GetImage().Save(ms, System.Drawing.Imaging.ImageFormat.Png);
Expand All @@ -21,6 +32,8 @@ public ClipboardContent GetClipboardContent(int maxTextLength = 5000) {
};
}
}

// 檔案
if (Clipboard.ContainsFileDropList()) {
var files = Clipboard.GetFileDropList();
if (files.Count > 0) {
Expand All @@ -33,6 +46,8 @@ public ClipboardContent GetClipboardContent(int maxTextLength = 5000) {
}
}
}

// 文字
if (Clipboard.ContainsText()) {

string oldText = Clipboard.GetText();
Expand Down
5 changes: 5 additions & 0 deletions Www/lang/langData.js
Original file line number Diff line number Diff line change
Expand Up @@ -1770,6 +1770,11 @@ var langData = {
"en": "File size exceeded limit",
"ja": "ファイルサイズが制限を超えた",
},
downloadImages: {
"zh-TW": "此操作將下載 {n} 張圖片,是否繼續?",
"en": "This operation will download {n} images, do you want to continue?",
"ja": "{n} 枚の画像をダウンロードしますか?",
},
//#endregion

//#region 文字編輯器
Expand Down
84 changes: 80 additions & 4 deletions Www/ts/MainWindow/Script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1124,21 +1124,98 @@ class ScriptOpen {
// if (M.fileLoad.getIsBulkView()) { return; }

let clipboardContent = await WebAPI.getClipboardContent();
console.log(clipboardContent);

if (clipboardContent.Type == "url") {
let showErr = () => {
Toast.show(this.M.i18n.t("msg.cannotOpenClipboard"), 1000 * 3); // 無法開啟剪貼簿的內容
}

if (clipboardContent.Type == "html") {

// 解析 <img src="url">
let parser = new DOMParser();
let doc = parser.parseFromString(clipboardContent.Data, "text/html");
let arImg = doc.querySelectorAll("img");

// 如果是來自 word 的資料,圖片標籤為 <v:imagedata src="file:///C:/Users/u1/AppData/Local/Temp/msohtmlclip1/01/clip_image001.png"/>
if (arImg.length === 0) {
arImg = doc.querySelectorAll("v\\:imagedata");
}

// 取得所有圖片的 src,並去除重複
let arSrc = [];
for (let i = 0; i < arImg.length; i++) {
let src = arImg[i].getAttribute("src");
if (src === null) { continue; }
if (arSrc.indexOf(src) === -1) {
arSrc.push(src);
}
}

// console.log(arSrc);

let downloadAll = async () => {
let path;
for (let i = 0; i < arSrc.length; i++) {
let src = arSrc[i];
let base64;
if (src.startsWith("http") || src.startsWith("file://")) {
let file = await this.M.downloadFileFromUrl(src);
if (file != null) {
base64 = await Lib.readFileAsDataURL(file);
}
}
else if (src.startsWith("data:image/")) {
base64 = src;
}

if (base64 !== undefined) {
let extension = await Lib.getExtensionFromBase64(base64); // 取得副檔名
if (extension !== "") {
path = await WV_File.Base64ToTempFile(base64, extension);
}
}
}

if (path !== undefined) {
await this.M.fileLoad.loadFile(path); // 重新載入圖片
return;
}
showErr();
}

// 如果圖片數量大於 10,則跳出詢問視窗
if (arSrc.length >= 10) {
this.M.msgbox.show({
type: "txt",
txt: this.M.i18n.t("msg.downloadImages", { n: arSrc.length }), // 此操作將下載 n 張圖片,是否繼續?
funcYes: async (dom: HTMLElement, value: string) => {
this.M.msgbox.close(dom);
await downloadAll();
}
});
} else {
await downloadAll();
}
return;
}

else if (clipboardContent.Type == "url") {
let file = await this.M.downloadFileFromUrl(clipboardContent.Data);
if (file != null) {
let base64 = await Lib.readFileAsDataURL(file);
let extension = await Lib.getExtensionFromBase64(base64); // 取得副檔名
if (extension !== "") {
let path = await WV_File.Base64ToTempFile(base64, extension);
await this.M.fileLoad.loadFile(path); // 下載檔案後,重新載入圖片
return;
}
}
}

else if (clipboardContent.Type == "file" || clipboardContent.Type == "dir") {
await this.M.fileLoad.loadFile(clipboardContent.Data); // 重新載入圖片
return;
}

else if (clipboardContent.Type == "img") {
Expand All @@ -1147,12 +1224,11 @@ class ScriptOpen {
if (extension !== "") {
let path = await WV_File.Base64ToTempFile(base64, extension);
await this.M.fileLoad.loadFile(path); // 重新載入圖片
return;
}
}

else {
Toast.show(this.M.i18n.t("msg.cannotOpenClipboard"), 1000 * 3); // 無法開啟剪貼簿的內容
}
showErr();
}

/** 另開視窗 */
Expand Down

0 comments on commit b4c5f74

Please sign in to comment.