From 97a65ffd296f5709ebdd2079e0d83349bbcbf3fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Sat, 18 Mar 2023 14:09:13 +0100 Subject: [PATCH] Try clipboard pasting --- src/lib/components/CodeInput.svelte | 2 +- src/lib/components/Urlnput.svelte | 15 ++++++++++++++- src/lib/utils/{copy.ts => clipboard.ts} | 4 ++++ src/routes/settings/+page.svelte | 19 ++++++++++++++++++- 4 files changed, 37 insertions(+), 3 deletions(-) rename src/lib/utils/{copy.ts => clipboard.ts} (75%) diff --git a/src/lib/components/CodeInput.svelte b/src/lib/components/CodeInput.svelte index 90c2741..13b74cf 100644 --- a/src/lib/components/CodeInput.svelte +++ b/src/lib/components/CodeInput.svelte @@ -2,7 +2,7 @@ import { invoke } from '@tauri-apps/api/tauri'; import * as settings from '$lib/utils/settings'; import { Status, type Response } from '$lib/types/api'; - import { copyIfEnabled } from '$lib/utils/copy'; + import { copyIfEnabled } from '$lib/utils/clipboard'; let inputCode = ''; let output = ''; diff --git a/src/lib/components/Urlnput.svelte b/src/lib/components/Urlnput.svelte index a973a54..aea3786 100644 --- a/src/lib/components/Urlnput.svelte +++ b/src/lib/components/Urlnput.svelte @@ -2,12 +2,25 @@ import { invoke } from '@tauri-apps/api/tauri'; import * as settings from '$lib/utils/settings'; import { Status, type Response } from '$lib/types/api'; - import { copyIfEnabled } from '$lib/utils/copy'; + import { copyIfEnabled } from '$lib/utils/clipboard'; + import { onMount } from 'svelte'; let inputUrl = ''; let output = ''; let code = ''; + onMount(async () => { + if (await settings.get('pasteOnLoad')) { + console.debug('pasteOnLoad is enabled'); + const clipboardData = await navigator.clipboard.readText(); + console.debug('clipboard data:', clipboardData); + try { + new URL(clipboardData); + inputUrl = clipboardData; + } catch (e) {} + } + }); + async function create() { const endpoint = await settings.get('endpoint'); const response: Response = await invoke('create_clip_cmd', { diff --git a/src/lib/utils/copy.ts b/src/lib/utils/clipboard.ts similarity index 75% rename from src/lib/utils/copy.ts rename to src/lib/utils/clipboard.ts index 0cc27a8..cd8ef2a 100644 --- a/src/lib/utils/copy.ts +++ b/src/lib/utils/clipboard.ts @@ -4,6 +4,10 @@ export const copyToClipboard = async (text: string) => { navigator.clipboard.writeText(text); } +export const readFromClipboard = async () => { + return await navigator.clipboard.readText(); +} + export const copyIfEnabled = async (text: string) => { if (await settings.get('copyToClipboard')) { await copyToClipboard(text); diff --git a/src/routes/settings/+page.svelte b/src/routes/settings/+page.svelte index 47a8ea8..60f0a1d 100644 --- a/src/routes/settings/+page.svelte +++ b/src/routes/settings/+page.svelte @@ -2,7 +2,9 @@ import { get, set } from '$lib/utils/settings'; let endpoint = 'interclip.app'; - let copyToClipboard = true; + let copyToClipboard = false; + let pasteOnLoad = false; + let output = ''; const save = () => { @@ -22,6 +24,7 @@ console.debug('Loading settings'); endpoint = (await get('endpoint')) ?? 'interclip.app'; copyToClipboard = (await get('copyToClipboard')) ?? true; + pasteOnLoad = (await get('pasteOnLoad')) ?? true; console.debug('Settings loaded', { endpoint, copyToClipboard }); })(); @@ -51,6 +54,20 @@ }} /> + {#if output}

{output}

{/if}