From a32252a6f6c60f161843530f447981c0f5994715 Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Thu, 19 Sep 2024 17:20:39 +0200 Subject: [PATCH] feat: open file from url (#109) --- apps/playground/src/App.tsx | 25 +++++++++++++------- apps/playground/src/components/menu/Menu.tsx | 11 +++++++++ 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/apps/playground/src/App.tsx b/apps/playground/src/App.tsx index ab40e9c4..39efdbcd 100644 --- a/apps/playground/src/App.tsx +++ b/apps/playground/src/App.tsx @@ -183,24 +183,28 @@ function App() { ]); } - (async () => { + async function loadFromURL(url: string) { + const response = await fetch(url).catch(() => + fetch('https://corsproxy.io/?' + encodeURIComponent(url)), + ); + if (response.ok) { + const model = activeTab() || openUntitledTab(); + model.setValue(await response.text()); + } + } + + { const queryParams = new URLSearchParams(location.search); const urlParam = queryParams.get('url'); const codeParam = queryParams.get('code'); if (urlParam !== null) { - const response = await fetch(urlParam).catch(() => - fetch('https://corsproxy.io/?' + encodeURIComponent(urlParam)), - ); - if (response.ok) { - const model = activeTab() || openUntitledTab(); - model.setValue(await response.text()); - } + loadFromURL(urlParam).catch(console.error); } else if (codeParam !== null) { const model = activeTab() || openUntitledTab(); model.setValue(codeParam); } - })().catch(console.error); + } return ( { openUntitledTab().setValue(content); }} + onLoadFromURL={(url) => { + loadFromURL(url).catch(console.error); + }} onSave={() => { if (activeTab()) downloadFile(activeTab()!); }} diff --git a/apps/playground/src/components/menu/Menu.tsx b/apps/playground/src/components/menu/Menu.tsx index 70edbd58..c20d7393 100644 --- a/apps/playground/src/components/menu/Menu.tsx +++ b/apps/playground/src/components/menu/Menu.tsx @@ -10,6 +10,7 @@ import MenuSetting from './MenuSetting'; interface Props { onFileOpen?: (content: string) => void; + onLoadFromURL?: (url: string) => void; onSave?: () => void; onRestore?: (workspace: Workspace) => void; } @@ -48,6 +49,16 @@ export default function Menu(props: Props) { > Open File… + { + const url = prompt('Enter URL'); + if (url) { + props.onLoadFromURL?.(url); + } + }} + > + Open File From URL… + No recent files}> {(workspace) => (