From 4af38b695fb53cb9991738d19876f12525cfb398 Mon Sep 17 00:00:00 2001 From: telesoho Date: Mon, 16 Sep 2024 21:07:45 +0800 Subject: [PATCH] feat: :star: Show dialog while multiple clipboard type avalible --- package.json | 31 ++++++++++++++++++++++++++++++- src/extension.ts | 2 +- src/paster.ts | 47 +++++++++++++++++++++++++++++++++++++++++------ xclip | 2 +- 4 files changed, 73 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index a1a3d97..b9123c4 100644 --- a/package.json +++ b/package.json @@ -223,6 +223,35 @@ "default": "encodeSpaceOnly", "description": "Encode path to URL-encode format. Options: none, encodeURI, encodeSpaceOnly" }, + "MarkdownPaste.autoSelectClipboardType": { + "type": "string", + "enum": [ + "always", + "never", + "html&text" + ], + "scope": "resource", + "description": "Auto select clipboard type while multiple clipboard types are available.", + "default": "html&text" + }, + "MarkdownPaste.autoSelectClipboardTypePriority": { + "type": "array", + "scope": "resource", + "items": { + "type": "string", + "enum": [ + "image", + "html", + "text" + ] + }, + "description": "Rules for lang paste.", + "default": [ + "image", + "html", + "text" + ] + }, "MarkdownPaste.lang_rules": { "type": "array", "scope": "resource", @@ -338,7 +367,7 @@ "openai": "^4.61.0", "shelljs": "^0.8.5", "turndown": "^7.1.2", - "xclip": "^1.0.5" + "xclip": "^1.0.6" }, "devDependencies": { "@types/glob": "^7.1.3", diff --git a/src/extension.ts b/src/extension.ts index eefd54e..ed84bd4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -60,7 +60,7 @@ export function activate(context: vscode.ExtensionContext) { ); context.subscriptions.push( vscode.commands.registerCommand("telesoho.MarkdownPaste", () => { - Paster.pasteText(); + Paster.paste(); }) ); context.subscriptions.push( diff --git a/src/paster.ts b/src/paster.ts index 4a3f46b..afaf492 100644 --- a/src/paster.ts +++ b/src/paster.ts @@ -48,17 +48,48 @@ class Paster { return false; } + static async selectClipboardType( + type: Set | xclip.ClipboardType + ): Promise { + if (!(type instanceof Set)) { + return type; + } + if (this.config.autoSelectClipboardType == "always") { + const priorityOrdering = this.config.autoSelectClipboardTypePriority; + for (const theType of priorityOrdering) + if (type.has(theType)) return theType; + return xclip.ClipboardType.Unknown; + } + if ( + this.config.autoSelectClipboardType == "never" || + (this.config.autoSelectClipboardType == "html&text" && + type.has(xclip.ClipboardType.Image)) + ) { + const selected = await vscode.window.showInformationMessage( + "There are multiple types of content in your clipboard. Which one do you want to use?", + { + modal: true, + }, + ...Array.from(type) + ); + if (selected) { + return selected; + } + return xclip.ClipboardType.Unknown; + } + } + /** * Paste text */ - public static async pasteText() { + public static async paste() { const shell = xclip.getShell(); const cb = shell.getClipboard(); - const ctx_type = await cb.getContentType(); + const ctx_type = await this.selectClipboardType(await cb.getContentType()); - let enableHtmlConverter = Paster.getConfig().enableHtmlConverter; - let enableRulesForHtml = Paster.getConfig().enableRulesForHtml; - let turndownOptions = Paster.getConfig().turndownOptions; + let enableHtmlConverter = this.config.enableHtmlConverter; + let enableRulesForHtml = this.config.enableRulesForHtml; + let turndownOptions = this.config.turndownOptions; Logger.log("Clipboard Type:", ctx_type); switch (ctx_type) { @@ -112,7 +143,7 @@ class Paster { public static async pasteDownload() { const shell = xclip.getShell(); const cb = shell.getClipboard(); - const ctx_type = await cb.getContentType(); + const ctx_type = await this.selectClipboardType(await cb.getContentType()); Logger.log("Clipboard Type:", ctx_type); switch (ctx_type) { case xclip.ClipboardType.Html: @@ -158,6 +189,10 @@ class Paster { return vscode.workspace.getConfiguration("MarkdownPaste", fileUri); } + static get config() { + return Paster.getConfig(); + } + /** * Generate different Markdown content based on the value entered. * for example: diff --git a/xclip b/xclip index 026f367..2b33e98 160000 --- a/xclip +++ b/xclip @@ -1 +1 @@ -Subproject commit 026f367b892571bcce206a05891a93ba18cb8595 +Subproject commit 2b33e98b8540d3c227ce568351c21ea96580b0aa