Skip to content

Commit

Permalink
[Paste as Plain Text] Optional No Prompt after pasting (#13084)
Browse files Browse the repository at this point in the history
* [Paste as Plain Text] Optional No Prompt after pasting

* Update tips style

* Add animated tips when pasting

* Update CHANGELOG.md and optimise images

---------

Co-authored-by: raycastbot <bot@raycast.com>
  • Loading branch information
koinzhang and raycastbot committed Jun 25, 2024
1 parent 008e628 commit 40c3b70
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 56 deletions.
5 changes: 5 additions & 0 deletions extensions/paste-as-plain-text/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Paste as Plain text Changelog

## [Optional No Prompt] - 2024-06-17

- Optional No Prompt after pasting
- Update metadata screenshot

## [Paste as File] - 2024-06-17

- Paste as File, extract file path from the clipboard and paste it as file
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 19 additions & 36 deletions extensions/paste-as-plain-text/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions extensions/paste-as-plain-text/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://www.raycast.com/schemas/extension.json",
"name": "paste-as-plain-text",
"title": "Paste as Plain Text",
"description": "Paste text from the clipboard as any format you want.",
"description": "Paste text from the clipboard as any format.",
"icon": "paste-icon.png",
"author": "koinzhang",
"contributors": [
Expand Down Expand Up @@ -69,14 +69,22 @@
"description": "Replace clipboard with pasted text after pasting",
"required": false,
"default": false
},
{
"name": "showTips",
"label": "Show Tips",
"type": "checkbox",
"description": "Show tips after pasting",
"required": false,
"default": true
}
],
"commands": [
{
"name": "paste-as-plain-text",
"title": "Paste as",

Check warning on line 85 in extensions/paste-as-plain-text/package.json

View workflow job for this annotation

GitHub Actions / Publish

Command's title has to be Title Cased. Expected "Paste As"
"subtitle": "Plain Text",
"description": "Paste text from the clipboard as any format you want.",
"description": "Paste text from the clipboard as any format.",
"mode": "no-view",
"arguments": [
{
Expand Down Expand Up @@ -117,7 +125,7 @@
}
],
"dependencies": {
"@raycast/api": "^1.76.0",
"@raycast/api": "^1.77.1",
"axios": "^1.7.2",
"cheerio": "^1.0.0-rc.12",
"fs-extra": "^11.2.0",
Expand Down
6 changes: 3 additions & 3 deletions extensions/paste-as-plain-text/src/paste-as-plain-text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { captureException, LaunchProps, showHUD } from "@raycast/api";
import { getArgument, isEmpty } from "./utils/common-utils";
import { captureException, LaunchProps, Toast } from "@raycast/api";
import { getArgument, isEmpty, showCustomHUD } from "./utils/common-utils";
import { PasteFormat } from "./types/types";
import { pasteAs } from "./utils/paste-as-utils";

Expand All @@ -15,6 +15,6 @@ export default async (props: LaunchProps<{ arguments: PasteAsArguments }>) => {
} catch (e) {
captureException(e);
console.error(e);
await showHUD("🚨 " + e);
await showCustomHUD({ title: String(e), style: Toast.Style.Failure });
}
};
3 changes: 2 additions & 1 deletion extensions/paste-as-plain-text/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ interface Preferences {
autoFetchTitle: boolean;
cleanLineBreaks: boolean;
replaceClipboard: boolean;
showTips: boolean;
}

export const { trimStart, trimEnd, autoFetchTitle, cleanLineBreaks, replaceClipboard } =
export const { trimStart, trimEnd, autoFetchTitle, cleanLineBreaks, replaceClipboard, showTips } =
getPreferenceValues<Preferences>();

export enum PasteFormat {
Expand Down
21 changes: 17 additions & 4 deletions extensions/paste-as-plain-text/src/utils/common-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { cleanLineBreaks, trimEnd, trimStart } from "../types/types";
import { Cache } from "@raycast/api";
import { cleanLineBreaks, showTips, trimEnd, trimStart } from "../types/types";
import { Cache, showHUD, showToast, Toast } from "@raycast/api";
import axios from "axios";
import cheerio from "cheerio";

Expand Down Expand Up @@ -87,10 +87,23 @@ export async function fetchTitle(url: string) {
const response = await axios.get(url);
const html = response.data;
const $ = cheerio.load(html);
const title = $("title").text();
return title;
return $("title").text();
} catch (error) {
console.error("Error fetching title:", error);
return "";
}
}

export const showCustomHUD = (options: Toast.Options) => {
if (options.style && options.style === Toast.Style.Failure) {
// failure should always show toast
return showToast(options);
} else if (showTips) {
// success or animated should show HUD
if (options.style && options.style === Toast.Style.Animated) {
return showToast(options);
} else {
return showHUD(options.title);
}
}
};
15 changes: 6 additions & 9 deletions extensions/paste-as-plain-text/src/utils/paste-as-utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Clipboard, closeMainWindow, showHUD, updateCommandMetadata } from "@raycast/api";
import { extractNumber, extractUrl, fetchTitle, isEmpty, transform } from "./common-utils";
import { Clipboard, closeMainWindow, Toast, updateCommandMetadata } from "@raycast/api";
import { extractNumber, extractUrl, fetchTitle, isEmpty, showCustomHUD, transform } from "./common-utils";
import { autoFetchTitle, PasteFormat, replaceClipboard } from "../types/types";
import { isJSON, isURL } from "validator";
import fse from "fs-extra";
import { fileURLToPath } from "node:url";

export async function pasteAs(advancedPasteFormat: string) {
await closeMainWindow();
await showCustomHUD({ title: "Pasting...", style: Toast.Style.Animated });
const clipboardText = await Clipboard.readText();
if (!clipboardText || isEmpty(clipboardText)) {
await showHUD("️🚨 No content in clipboard");
await showCustomHUD({ title: "No content in clipboard", style: Toast.Style.Failure });
return;
}

Expand Down Expand Up @@ -50,22 +51,18 @@ export async function pasteAs(advancedPasteFormat: string) {
}
case PasteFormat.File: {
const { file } = await Clipboard.read();
console.log("file " + file);
const oldPasteStr = pasteStr;
if (file) {
pasteStr = file;
}
console.log("oldPasteStr " + oldPasteStr);

try {
pasteStr = fileURLToPath(pasteStr);
} catch (e) {
console.error(e);
}

if (fse.pathExistsSync(pasteStr)) {
console.log("pasteStr " + pasteStr);
realPasteFormatIcon = "📃";
realPasteFormatIcon = "📄";
realPasteFormat = PasteFormat.File;
isPasteAsFile = true;
} else {
Expand Down Expand Up @@ -114,7 +111,7 @@ export async function pasteAs(advancedPasteFormat: string) {
}
}

await showHUD(`${realPasteFormatIcon} Paste as ${realPasteFormat}`);
await showCustomHUD({ title: `${realPasteFormatIcon} Paste as ${realPasteFormat}` });
await updateCommandMetadata({
subtitle: isEmpty(advancedPasteFormat) ? PasteFormat.PLAIN_TEXT : advancedPasteFormat,
});
Expand Down

0 comments on commit 40c3b70

Please sign in to comment.