@@ -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 })
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import IdeContainer from 'src/components/IdeContainer'
|
||||
import { createContext } from 'react'
|
||||
import IdeContainer from 'src/components/IdeContainer'
|
||||
import { isBrowser } from '@redwoodjs/prerender/browserUtils'
|
||||
import { useIdeState } from 'src/helpers/hooks/useIdeState'
|
||||
import { copyTextToClipboard } from 'src/helpers/clipboard'
|
||||
|
||||
export const IdeContext = createContext()
|
||||
|
||||
const IdeToolbarNew = () => {
|
||||
const [state, dispatch] = useIdeState()
|
||||
function setIdeType(ide) {
|
||||
@@ -12,12 +13,19 @@ const IdeToolbarNew = () => {
|
||||
function handleRender() {
|
||||
dispatch({ type: 'render', payload: { code: state.code } })
|
||||
}
|
||||
function handleMakeLink() {
|
||||
if (isBrowser) {
|
||||
const scriptBase64 = btoa(state.code)
|
||||
window.location.hash = `encoded_script=${scriptBase64}`
|
||||
copyTextToClipboard(window.location.href)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<IdeContext.Provider value={{ state, dispatch }}>
|
||||
<div className="p-8 border-2">
|
||||
<nav className="flex">
|
||||
<button
|
||||
{/* <button
|
||||
onClick={() => setIdeType('openCascade')}
|
||||
className="p-2 br-2 border-2 m-2 bg-blue-200"
|
||||
>
|
||||
@@ -28,10 +36,13 @@ const IdeToolbarNew = () => {
|
||||
className="p-2 br-2 border-2 m-2 bg-indigo-200"
|
||||
>
|
||||
Switch to OpenSCAD
|
||||
</button>
|
||||
</button> */}
|
||||
<button onClick={handleRender} className="p-2 br-2 border-2 m-2">
|
||||
Render
|
||||
</button>
|
||||
<button onClick={handleMakeLink} className="p-2 br-2 border-2 m-2">
|
||||
Copy link
|
||||
</button>
|
||||
</nav>
|
||||
<IdeContainer />
|
||||
</div>
|
||||
|
||||
37
web/src/helpers/clipboard.js
Normal file
37
web/src/helpers/clipboard.js
Normal file
@@ -0,0 +1,37 @@
|
||||
function fallbackCopyTextToClipboard(text) {
|
||||
var textArea = document.createElement('textarea')
|
||||
textArea.value = text
|
||||
|
||||
// Avoid scrolling to bottom
|
||||
textArea.style.top = '0'
|
||||
textArea.style.left = '0'
|
||||
textArea.style.position = 'fixed'
|
||||
|
||||
document.body.appendChild(textArea)
|
||||
textArea.focus()
|
||||
textArea.select()
|
||||
|
||||
try {
|
||||
var successful = document.execCommand('copy')
|
||||
var msg = successful ? 'successful' : 'unsuccessful'
|
||||
console.log('Fallback: Copying text command was ' + msg)
|
||||
} catch (err) {
|
||||
console.error('Fallback: Oops, unable to copy', err)
|
||||
}
|
||||
|
||||
document.body.removeChild(textArea)
|
||||
}
|
||||
export function copyTextToClipboard(text) {
|
||||
if (!navigator.clipboard) {
|
||||
fallbackCopyTextToClipboard(text)
|
||||
return
|
||||
}
|
||||
navigator.clipboard.writeText(text).then(
|
||||
function () {
|
||||
console.log('Async: Copying to clipboard was successful!')
|
||||
},
|
||||
function (err) {
|
||||
console.error('Async: Could not copy text: ', err)
|
||||
}
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user