Skip to content

Commit b857a52

Browse files
committed
Refactor Paste action + Support Markdown editing
1 parent 4d02ddb commit b857a52

File tree

4 files changed

+47
-43
lines changed

4 files changed

+47
-43
lines changed

src/aiChat.jsx

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ import { init } from "#root/src/api/init.js";
1919

2020
import { watch } from "node:fs/promises";
2121

22-
import { current_datetime, formatDate, removePrefix, getFileFromURL } from "./helpers/helper.js";
22+
import { current_datetime, formatDate, removePrefix } from "./helpers/helper.js";
2323
import { plainTextMarkdown } from "./helpers/markdown.js";
2424
import throttle, { AIChatDelayFunction } from "#root/src/helpers/throttle.js";
2525

2626
import { help_action, help_action_panel } from "./helpers/helpPage.jsx";
27+
import { PasteAction } from "./components/actions/pasteAction.jsx";
2728
import { autoCheckForUpdates } from "./helpers/update.jsx";
2829
import { confirmClearData, tryRecoverJSON } from "./helpers/aiChatHelper.jsx";
2930

@@ -579,7 +580,7 @@ export default function Chat({ launchContext }) {
579580
</Form.Dropdown>
580581

581582
<Form.Description title="System Prompt" text="This prompt will be sent to GPT to start the conversation." />
582-
<Form.TextArea id="systemPrompt" defaultValue={chat?.systemPrompt ?? ""} />
583+
<Form.TextArea id="systemPrompt" defaultValue={chat?.systemPrompt ?? ""} enableMarkdown />
583584
</>
584585
);
585586
};
@@ -702,25 +703,7 @@ export default function Chat({ launchContext }) {
702703
}
703704
}}
704705
/>
705-
<Action
706-
title="Paste"
707-
icon={Icon.Clipboard}
708-
onAction={async () => {
709-
let { text, file } = await Clipboard.read();
710-
setInput((oldInput) => {
711-
let newInput = structuredClone(oldInput);
712-
if (file) {
713-
file = getFileFromURL(file);
714-
newInput.files = [...(oldInput.files ?? []), file];
715-
} else {
716-
// Only set the text if there is no file, otherwise it's just the file name
717-
newInput.message += text;
718-
}
719-
return newInput;
720-
});
721-
}}
722-
shortcut={{ modifiers: ["cmd"], key: "v" }}
723-
/>
706+
{PasteAction(setInput)}
724707
</ActionPanel>
725708
}
726709
>
@@ -729,6 +712,7 @@ export default function Chat({ launchContext }) {
729712
title="Message"
730713
value={input.message}
731714
onChange={(message) => setInput({ ...input, message })}
715+
enableMarkdown
732716
/>
733717
<Form.FilePicker
734718
id="files"

src/api/gpt.jsx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
Action,
33
ActionPanel,
4-
Clipboard,
54
confirmAlert,
65
Detail,
76
Form,
@@ -21,13 +20,14 @@ import * as providers from "./providers.js";
2120
import throttle, { AIChatDelayFunction } from "#root/src/helpers/throttle.js";
2221

2322
import { help_action } from "../helpers/helpPage.jsx";
23+
import { PasteAction } from "../components/actions/pasteAction.jsx";
2424
import { autoCheckForUpdates } from "../helpers/update.jsx";
2525

2626
import { init } from "../api/init.js";
2727
import { Message, pairs_to_messages } from "../classes/message.js";
2828
import { Preferences } from "./preferences.js";
2929

30-
import { getFileFromURL, truncate_chat } from "../helpers/helper.js";
30+
import { truncate_chat } from "../helpers/helper.js";
3131
import { plainTextMarkdown } from "../helpers/markdown.js";
3232
import { getFormattedWebResult, systemResponse, web_search_mode, webSystemPrompt } from "./tools/web";
3333

@@ -463,25 +463,7 @@ export default (
463463
await getResponse(prompt, { files: files });
464464
}}
465465
/>
466-
<Action
467-
title="Paste"
468-
icon={Icon.Clipboard}
469-
onAction={async () => {
470-
let { text, file } = await Clipboard.read();
471-
setInput((oldInput) => {
472-
let newInput = structuredClone(oldInput);
473-
if (allowUploadFiles && file) {
474-
file = getFileFromURL(file);
475-
newInput.files = [...(oldInput.files ?? []), file];
476-
} else {
477-
// Only set the text if there is no file, otherwise it's just the file name
478-
newInput.message += text;
479-
}
480-
return newInput;
481-
});
482-
}}
483-
shortcut={{ modifiers: ["cmd"], key: "v" }}
484-
/>
466+
{PasteAction(setInput)}
485467
</ActionPanel>
486468
}
487469
>
@@ -490,6 +472,7 @@ export default (
490472
title={showFormText}
491473
value={input.message}
492474
onChange={(message) => setInput({ ...input, message })}
475+
enableMarkdown
493476
/>
494477
{allowUploadFiles && (
495478
<Form.FilePicker
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Action, Clipboard, Icon, showToast, Toast } from "@raycast/api";
2+
import { getFileFromURL } from "#root/src/helpers/helper.js";
3+
4+
export const PasteAction = (setInput) => {
5+
return (
6+
<Action
7+
title="Paste"
8+
icon={Icon.Clipboard}
9+
onAction={async () => {
10+
// Show a toast if the operation takes some time
11+
let toast = null;
12+
const timeout = setTimeout(() => {
13+
toast = showToast(Toast.Style.Animated, "Pasting");
14+
}, 100);
15+
16+
let { text, file } = await Clipboard.read();
17+
setInput((oldInput) => {
18+
let newInput = structuredClone(oldInput);
19+
if (file) {
20+
file = getFileFromURL(file);
21+
newInput.files = [...(oldInput.files ?? []), file];
22+
} else {
23+
// Only set the text if there is no file, otherwise it's just the file name
24+
newInput.message += text;
25+
}
26+
return newInput;
27+
});
28+
29+
clearTimeout(timeout);
30+
if (toast) {
31+
await (await toast).hide();
32+
}
33+
}}
34+
shortcut={{ modifiers: ["cmd"], key: "v" }}
35+
/>
36+
);
37+
};

src/components/preferences/manageAIPresets.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const ManageAIPresets = () => {
103103
</Form.Dropdown>
104104

105105
<Form.Description title="System Prompt" text="This prompt will be sent to GPT to start the conversation." />
106-
<Form.TextArea id="systemPrompt" defaultValue={preset.systemPrompt} />
106+
<Form.TextArea id="systemPrompt" defaultValue={preset.systemPrompt} enableMarkdown />
107107

108108
<Form.Checkbox id="isDefault" label="Set as Default" defaultValue={preset.isDefault} />
109109
</Form>

0 commit comments

Comments
 (0)