1- import { Action , ActionPanel , Clipboard , Detail , showToast , Toast , getPreferenceValues } from "@raycast/api" ;
2- import { useState , useEffect } from "react" ;
1+ import { Clipboard , showHUD , getPreferenceValues } from "@raycast/api" ;
32import { raycastConverter } from "./raycast-converter.js" ;
43import { ImageHandlingMode } from "../../core/converter.js" ;
54
6- export default function ConvertClipboard ( ) {
7- const [ markdown , setMarkdown ] = useState < string > ( "" ) ;
8- const [ isLoading , setIsLoading ] = useState ( true ) ;
9- const [ error , setError ] = useState < string | null > ( null ) ;
10-
5+ export default async function ConvertClipboard ( ) {
116 const preferences = getPreferenceValues < { imageHandling : ImageHandlingMode } > ( ) ;
127
13- useEffect ( ( ) => {
14- convertClipboard ( ) ;
15- } , [ ] ) ;
16-
17- const convertClipboard = async ( ) => {
18- try {
19- setIsLoading ( true ) ;
20- setError ( null ) ;
21-
22- // Since Raycast doesn't support HTML clipboard reading, we'll enhance the plain text processing
23-
24- const result = await raycastConverter . convertFromClipboard ( {
25- imageHandling : preferences . imageHandling
26- } ) ;
8+ try {
9+ const result = await raycastConverter . convertFromClipboard ( {
10+ imageHandling : preferences . imageHandling
11+ } ) ;
2712
28- setMarkdown ( result ) ;
29-
30- if ( result . trim ( ) ) {
31- await showToast ( {
32- style : Toast . Style . Success ,
33- title : "Converted to Markdown" ,
34- message : "Result copied to clipboard"
35- } ) ;
36-
37- // Copy back to clipboard
38- await Clipboard . copy ( result ) ;
39- } else {
40- await showToast ( {
41- style : Toast . Style . Failure ,
42- title : "No content found" ,
43- message : "No rich text found in clipboard"
44- } ) ;
45- }
46- } catch ( error ) {
47- const errorMessage = error instanceof Error ? error . message : "Unknown error occurred" ;
48- setError ( errorMessage ) ;
49- await showToast ( {
50- style : Toast . Style . Failure ,
51- title : "Conversion failed" ,
52- message : errorMessage
53- } ) ;
54- } finally {
55- setIsLoading ( false ) ;
13+ if ( result . trim ( ) ) {
14+ await Clipboard . copy ( result ) ;
15+ await showHUD ( "✓ Converted to Markdown" ) ;
16+ } else {
17+ await showHUD ( "✗ No rich text found in clipboard" ) ;
5618 }
57- } ;
58-
59- if ( isLoading ) {
60- return < Detail isLoading = { true } markdown = "Converting clipboard content..." /> ;
61- }
62-
63- if ( error ) {
64- return (
65- < Detail
66- markdown = { `# Conversion Error\n\n${ error } ` }
67- actions = {
68- < ActionPanel >
69- < Action title = "Try Again" onAction = { convertClipboard } />
70- </ ActionPanel >
71- }
72- />
73- ) ;
19+ } catch ( error ) {
20+ const errorMessage = error instanceof Error ? error . message : "Unknown error occurred" ;
21+ await showHUD ( `✗ Conversion failed: ${ errorMessage } ` ) ;
7422 }
75-
76- if ( ! markdown . trim ( ) ) {
77- return (
78- < Detail
79- markdown = "# No Content Found\n\nNo rich text content was found in your clipboard. Copy some formatted text (from Word, Google Docs, or a webpage) and try again."
80- actions = {
81- < ActionPanel >
82- < Action title = "Try Again" onAction = { convertClipboard } />
83- </ ActionPanel >
84- }
85- />
86- ) ;
87- }
88-
89- const escapedMarkdown = markdown
90- . replace ( / ` ` ` / g, "`\u200b``" )
91- . replace ( / \r \n / g, "\n" ) ;
92-
93- const detailMarkdown = `# Conversion Result\n\n\`\`\`\n${ escapedMarkdown } \n\`\`\`` ;
94-
95- return (
96- < Detail
97- markdown = { detailMarkdown }
98- actions = {
99- < ActionPanel >
100- < Action title = "Convert Again" onAction = { convertClipboard } />
101- </ ActionPanel >
102- }
103- />
104- ) ;
10523}
0 commit comments