Skip to content

Commit 89eb285

Browse files
committed
share button
1 parent d5d5ba4 commit 89eb285

File tree

2 files changed

+49
-17
lines changed

2 files changed

+49
-17
lines changed

src/app/App.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,26 @@ export function App() {
4545
DM Playground
4646
</h1>
4747
</div>
48-
<button
49-
type="button"
50-
aria-label="Settings"
51-
onClick={() => setShowSettings(true)}
52-
className="rounded-md border border-slate-700 bg-slate-900/80 px-2 py-1 text-sm font-semibold text-slate-200 hover:border-slate-500"
53-
>
54-
⚙️
55-
</button>
48+
<div className="flex items-center gap-2">
49+
<button
50+
type="button"
51+
aria-label="Share"
52+
onClick={() =>
53+
window.dispatchEvent(new CustomEvent('requestShare'))
54+
}
55+
className="rounded-md border border-slate-700 bg-slate-900/80 px-2 py-1 text-sm font-semibold text-slate-200 hover:border-slate-500"
56+
>
57+
🔗 Share Code
58+
</button>
59+
<button
60+
type="button"
61+
aria-label="Settings"
62+
onClick={() => setShowSettings(true)}
63+
className="rounded-md border border-slate-700 bg-slate-900/80 px-2 py-1 text-sm font-semibold text-slate-200 hover:border-slate-500"
64+
>
65+
⚙️
66+
</button>
67+
</div>
5668
</header>
5769
<div className="flex-1 min-h-0">
5870
<ErrorBoundary>

src/app/panels/EditorPanel.tsx

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,21 @@ import { useTheme } from '../theme/useTheme'
77

88
const DEFAULT_CODE = `/world/New()\n world.log << "meow";\n ..()\n eval("")\n shutdown()\n`
99

10-
const wrapTemplate = (code: string) => `// DM Playground\n\n${code}\n`
11-
1210
const getSeededCode = () => {
1311
const params = new URLSearchParams(window.location.search)
1412
const encoded = params.get('code')
1513
if (encoded) {
1614
try {
17-
return wrapTemplate(Base64.decode(encoded))
15+
return Base64.decode(encoded)
1816
} catch {
19-
return wrapTemplate(DEFAULT_CODE)
17+
return DEFAULT_CODE
2018
}
2119
}
22-
return wrapTemplate(DEFAULT_CODE)
20+
return DEFAULT_CODE
2321
}
2422

2523
export function EditorPanel() {
26-
const [value, setValue] = useState(() => getSeededCode())
24+
const [currentCode, setCurrentCode] = useState(() => getSeededCode())
2725
const [, setStatus] = useState<'running' | 'idle'>('idle')
2826
const [activeByond, setActiveByond] = useState<string | null>(() =>
2927
byondService.getActiveVersion()
@@ -49,18 +47,40 @@ export function EditorPanel() {
4947
return () => byondService.removeEventListener('active', handleActive)
5048
}, [])
5149

50+
useEffect(() => {
51+
const handleRequestShare = async () => {
52+
const encoded = Base64.encode(currentCode)
53+
const url = `${window.location.origin}${window.location.pathname}?code=${encodeURIComponent(
54+
encoded
55+
)}`
56+
try {
57+
await navigator.clipboard.writeText(url)
58+
window.alert('Share link copied to clipboard')
59+
} catch {
60+
window.prompt('Copy this link', url)
61+
}
62+
}
63+
64+
window.addEventListener('requestShare', handleRequestShare as EventListener)
65+
return () =>
66+
window.removeEventListener(
67+
'requestShare',
68+
handleRequestShare as EventListener
69+
)
70+
}, [currentCode])
71+
5272
const handleRun = () => {
5373
if (!activeByond) {
5474
return
5575
}
56-
void executorService.executeImmediate(value)
76+
void executorService.executeImmediate(currentCode)
5777
}
5878

5979
return (
6080
<div className="flex h-full min-h-0 flex-col gap-3">
6181
<Editor
62-
value={value}
63-
onChange={setValue}
82+
value={currentCode}
83+
onChange={setCurrentCode}
6484
onRun={handleRun}
6585
themeId={themeId}
6686
/>

0 commit comments

Comments
 (0)