Encode code into url to make sharing snippets easy

resolves #253
This commit is contained in:
Kurt Hutten
2021-03-16 20:26:58 +11:00
parent 03ce3530c1
commit 01bc76e09a
3 changed files with 73 additions and 5 deletions

View File

@@ -1,10 +1,30 @@
import { useContext } from 'react'
import { useContext, useEffect } from 'react'
import { isBrowser } from '@redwoodjs/prerender/browserUtils'
import { IdeContext } from 'src/components/IdeToolbarNew'
import Editor from '@monaco-editor/react'
const IdeEditor = () => {
const { state, dispatch } = useContext(IdeContext)
const scriptKey = 'encoded_script'
useEffect(() => {
// load code from hash if it's there
let hash
if (isBrowser) {
hash = window.location.hash
}
const [key, scriptBase64] = hash.slice(1).split('=')
if (key === scriptKey) {
const script = atob(scriptBase64)
dispatch({ type: 'updateCode', payload: script })
}
}, [])
useEffect(() => {
if (isBrowser) {
window.location.hash = ''
}
}, [state.code])
function handleCodeChange(value, _event) {
dispatch({ type: 'updateCode', payload: value })
}