mirror of
https://github.com/rio-labs/rio.git
synced 2026-02-12 00:28:32 -06:00
Merge pull request #34 from 16amattice/dev
Add get_clipboard and set_clipboard to session.
This commit is contained in:
1
RioTest
Submodule
1
RioTest
Submodule
Submodule RioTest added at facc657762
@@ -6,6 +6,9 @@ import {
|
||||
registerFont,
|
||||
closeSession,
|
||||
setTitle,
|
||||
setClipboard,
|
||||
getClipboard,
|
||||
ClipboardError,
|
||||
} from './rpcFunctions';
|
||||
import { AsyncQueue, commitCss } from './utils';
|
||||
|
||||
@@ -367,6 +370,39 @@ export async function processMessageReturnResponse(
|
||||
response = null;
|
||||
break;
|
||||
|
||||
case 'setClipboard':
|
||||
try {
|
||||
await setClipboard(message.params.text);
|
||||
response = null;
|
||||
} catch (e) {
|
||||
response = e.toString();
|
||||
responseIsError = true;
|
||||
if (e instanceof ClipboardError) {
|
||||
console.warn(`ClipboardError: ${e.message}`);
|
||||
} else {
|
||||
console.warn(
|
||||
`Uncaught exception in \`setClipboard\`: ${e}`
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'getClipboard':
|
||||
try {
|
||||
response = await getClipboard();
|
||||
} catch (e) {
|
||||
response = e.toString();
|
||||
responseIsError = true;
|
||||
if (e instanceof ClipboardError) {
|
||||
console.warn(`ClipboardError: ${e.message}`);
|
||||
} else {
|
||||
console.warn(
|
||||
`Uncaught exception in \`getClipboard\`: ${e}`
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
// Invalid method
|
||||
throw `Encountered unknown RPC method: ${message.method}`;
|
||||
|
||||
@@ -132,3 +132,34 @@ export function setTitle(title: string): void {
|
||||
export function closeSession(): void {
|
||||
window.close(); // TODO: What if the browser doesn't allow this?
|
||||
}
|
||||
|
||||
export class ClipboardError extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = this.constructor.name;
|
||||
}
|
||||
}
|
||||
|
||||
export async function setClipboard(text: string): Promise<void> {
|
||||
if (!navigator.clipboard) {
|
||||
throw new ClipboardError('Clipboard API is not available');
|
||||
}
|
||||
try {
|
||||
await navigator.clipboard.writeText(text);
|
||||
} catch (error) {
|
||||
console.warn(`Failed to set clipboard content: ${error}`);
|
||||
throw new ClipboardError(`Failed to set clipboard content: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getClipboard(): Promise<string> {
|
||||
if (!navigator.clipboard) {
|
||||
throw new ClipboardError('Clipboard API is not available');
|
||||
}
|
||||
try {
|
||||
return await navigator.clipboard.readText();
|
||||
} catch (error) {
|
||||
console.warn(`Failed to get clipboard content: ${error}`);
|
||||
throw new ClipboardError(`Failed to get clipboard content: ${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +122,19 @@ export class TimeoutError extends Error {
|
||||
|
||||
/// Copies the given text to the clipboard
|
||||
export function copyToClipboard(text: string): void {
|
||||
if (navigator.clipboard) {
|
||||
navigator.clipboard.writeText(text).catch((error) => {
|
||||
console.warn(
|
||||
`Failed to set clipboard content using navigator.clipboard: ${error}`
|
||||
);
|
||||
fallbackCopyToClipboard(text);
|
||||
});
|
||||
} else {
|
||||
fallbackCopyToClipboard(text);
|
||||
}
|
||||
}
|
||||
|
||||
function fallbackCopyToClipboard(text: string): void {
|
||||
const textArea = document.createElement('textarea');
|
||||
textArea.value = text;
|
||||
|
||||
|
||||
@@ -67,6 +67,19 @@ class WontSerialize(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class ClipboardError(Exception):
|
||||
"""
|
||||
Exception raised for errors related to clipboard operations.
|
||||
"""
|
||||
|
||||
def __init__(self, message: str):
|
||||
super().__init__(message)
|
||||
|
||||
@property
|
||||
def message(self) -> str:
|
||||
return self.args[0]
|
||||
|
||||
|
||||
async def dummy_send_message(message: Jsonable) -> None:
|
||||
raise NotImplementedError() # pragma: no cover
|
||||
|
||||
@@ -2451,3 +2464,45 @@ a.remove();
|
||||
self._call_event_handler(callback, component, refresh=True),
|
||||
name="`on_on_window_size_change` event handler",
|
||||
)
|
||||
|
||||
@unicall.remote(
|
||||
name="setClipboard",
|
||||
parameter_format="dict",
|
||||
await_response=False,
|
||||
)
|
||||
async def _remote_set_clipboard(self, text: str) -> None:
|
||||
raise NotImplementedError # pragma: no cover
|
||||
|
||||
@unicall.remote(
|
||||
name="getClipboard",
|
||||
parameter_format="dict",
|
||||
await_response=True,
|
||||
)
|
||||
async def _remote_get_clipboard(self) -> str:
|
||||
raise NotImplementedError # pragma: no cover
|
||||
|
||||
async def set_clipboard(self, text: str) -> None:
|
||||
"""
|
||||
Set the client's clipboard to the given text.
|
||||
|
||||
## Parameters
|
||||
|
||||
`text`: The text to set on the clipboard.
|
||||
"""
|
||||
try:
|
||||
await self._remote_set_clipboard(text)
|
||||
except Exception as e:
|
||||
raise ClipboardError(f"Failed to set clipboard content: {str(e)}")
|
||||
|
||||
async def get_clipboard(self) -> str:
|
||||
"""
|
||||
Get the current text from the client's clipboard.
|
||||
|
||||
## Returns
|
||||
|
||||
The text currently on the clipboard.
|
||||
"""
|
||||
try:
|
||||
return await self._remote_get_clipboard()
|
||||
except Exception as e:
|
||||
raise ClipboardError(f"Failed to get clipboard content: {str(e)}")
|
||||
|
||||
Reference in New Issue
Block a user