@@ -7,23 +7,21 @@ import { useTheme } from '../theme/useTheme'
77
88const 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-
1210const 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
2523export 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