Skip to content

Commit

Permalink
feat: open file from url (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
j4k0xb committed Sep 19, 2024
1 parent 24a098d commit a32252a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
25 changes: 16 additions & 9 deletions apps/playground/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<DeobfuscateContextProvider
Expand All @@ -213,6 +217,9 @@ function App() {
onFileOpen={(content) => {
openUntitledTab().setValue(content);
}}
onLoadFromURL={(url) => {
loadFromURL(url).catch(console.error);
}}
onSave={() => {
if (activeTab()) downloadFile(activeTab()!);
}}
Expand Down
11 changes: 11 additions & 0 deletions apps/playground/src/components/menu/Menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import MenuSetting from './MenuSetting';

interface Props {
onFileOpen?: (content: string) => void;
onLoadFromURL?: (url: string) => void;
onSave?: () => void;
onRestore?: (workspace: Workspace) => void;
}
Expand Down Expand Up @@ -48,6 +49,16 @@ export default function Menu(props: Props) {
>
Open File…
</MenuButton>
<MenuButton
onClick={() => {
const url = prompt('Enter URL');
if (url) {
props.onLoadFromURL?.(url);
}
}}
>
Open File From URL…
</MenuButton>
<MenuDropdown title="Open Recent">
<For each={workspaces()} fallback={<li>No recent files</li>}>
{(workspace) => (
Expand Down

1 comment on commit a32252a

@reslear
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

Please sign in to comment.