Skip to content

Commit

Permalink
feat: ⭐ Show dialog while multiple clipboard type avalible
Browse files Browse the repository at this point in the history
  • Loading branch information
telesoho committed Sep 16, 2024
1 parent 101198a commit 4af38b6
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 9 deletions.
31 changes: 30 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
47 changes: 41 additions & 6 deletions src/paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,48 @@ class Paster {
return false;
}

static async selectClipboardType(
type: Set<xclip.ClipboardType> | xclip.ClipboardType
): Promise<xclip.ClipboardType> {
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) {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4af38b6

Please sign in to comment.