Make sure the code-init is robust when local storage is empty
Plus fix local storage for old code
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import { useContext, Suspense, lazy } from 'react'
|
import { useContext, Suspense, lazy } from 'react'
|
||||||
import { IdeContext } from 'src/components/IdeToolbarNew'
|
import { IdeContext } from 'src/components/IdeToolbarNew'
|
||||||
import { codeStorageKey } from 'src/helpers/hooks/useIdeState'
|
import { makeCodeStoreKey } from 'src/helpers/hooks/useIdeState'
|
||||||
import { requestRender } from 'src/helpers/hooks/useIdeState'
|
import { requestRender } from 'src/helpers/hooks/useIdeState'
|
||||||
const Editor = lazy(() => import('@monaco-editor/react'))
|
const Editor = lazy(() => import('@monaco-editor/react'))
|
||||||
|
|
||||||
@@ -38,7 +38,7 @@ const IdeEditor = () => {
|
|||||||
camera: state.camera,
|
camera: state.camera,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
localStorage.setItem(codeStorageKey, state.code)
|
localStorage.setItem(makeCodeStoreKey(state.ideType), state.code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const loading = (
|
const loading = (
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createContext, useEffect } from 'react'
|
import { createContext, useEffect } from 'react'
|
||||||
import IdeContainer from 'src/components/IdeContainer'
|
import IdeContainer from 'src/components/IdeContainer'
|
||||||
import { isBrowser } from '@redwoodjs/prerender/browserUtils'
|
import { isBrowser } from '@redwoodjs/prerender/browserUtils'
|
||||||
import { useIdeState, codeStorageKey } from 'src/helpers/hooks/useIdeState'
|
import { useIdeState, makeCodeStoreKey } from 'src/helpers/hooks/useIdeState'
|
||||||
import { copyTextToClipboard } from 'src/helpers/clipboard'
|
import { copyTextToClipboard } from 'src/helpers/clipboard'
|
||||||
import { requestRender } from 'src/helpers/hooks/useIdeState'
|
import { requestRender } from 'src/helpers/hooks/useIdeState'
|
||||||
import { encode, decode } from 'src/helpers/compress'
|
import { encode, decode } from 'src/helpers/compress'
|
||||||
@@ -74,7 +74,7 @@ const IdeToolbarNew = ({ cadPackage }) => {
|
|||||||
camera: state.camera,
|
camera: state.camera,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
localStorage.setItem(codeStorageKey, state.code)
|
localStorage.setItem(makeCodeStoreKey(state.ideType), state.code)
|
||||||
}
|
}
|
||||||
function handleMakeLink() {
|
function handleMakeLink() {
|
||||||
if (isBrowser) {
|
if (isBrowser) {
|
||||||
@@ -83,8 +83,8 @@ const IdeToolbarNew = ({ cadPackage }) => {
|
|||||||
copyTextToClipboard(window.location.href)
|
copyTextToClipboard(window.location.href)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const PullTitleFromFirstLine = (code) => {
|
const PullTitleFromFirstLine = (code = '') => {
|
||||||
const firstLine = code.split('\n').filter(identity)[0]
|
const firstLine = code.split('\n').filter(identity)[0] || ''
|
||||||
if (!(firstLine.startsWith('//') || firstLine.startsWith('#'))) {
|
if (!(firstLine.startsWith('//') || firstLine.startsWith('#'))) {
|
||||||
return 'object.stl'
|
return 'object.stl'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { STLLoader } from 'three/examples/jsm/loaders/STLLoader'
|
|||||||
|
|
||||||
export const lambdaBaseURL =
|
export const lambdaBaseURL =
|
||||||
process.env.CAD_LAMBDA_BASE_URL ||
|
process.env.CAD_LAMBDA_BASE_URL ||
|
||||||
'https://2inlbple1b.execute-api.us-east-1.amazonaws.com/prod2'
|
'https://oxt2p7ddgj.execute-api.us-east-1.amazonaws.com/prod'
|
||||||
|
|
||||||
export const stlToGeometry = (url) =>
|
export const stlToGeometry = (url) =>
|
||||||
new Promise((resolve, reject) => {
|
new Promise((resolve, reject) => {
|
||||||
|
|||||||
@@ -44,11 +44,12 @@ show_object(result)
|
|||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const codeStorageKey = 'Last-editor-code'
|
const codeStorageKey = 'Last-editor-code'
|
||||||
|
export const makeCodeStoreKey = (ideType) => `${codeStorageKey}-${ideType}`
|
||||||
let mutableState = null
|
let mutableState = null
|
||||||
|
|
||||||
export const useIdeState = () => {
|
export const useIdeState = () => {
|
||||||
const code = localStorage.getItem(codeStorageKey) || initCodeMap.openscad
|
const code = ''
|
||||||
const initialState = {
|
const initialState = {
|
||||||
ideType: 'INIT',
|
ideType: 'INIT',
|
||||||
consoleMessages: [
|
consoleMessages: [
|
||||||
@@ -78,7 +79,10 @@ export const useIdeState = () => {
|
|||||||
case 'initIde':
|
case 'initIde':
|
||||||
return {
|
return {
|
||||||
...state,
|
...state,
|
||||||
code: initCodeMap[payload.cadPackage] || initCodeMap.openscad,
|
code:
|
||||||
|
localStorage.getItem(makeCodeStoreKey(payload.cadPackage)) ||
|
||||||
|
initCodeMap[payload.cadPackage] ||
|
||||||
|
'',
|
||||||
ideType: payload.cadPackage,
|
ideType: payload.cadPackage,
|
||||||
}
|
}
|
||||||
case 'updateCode':
|
case 'updateCode':
|
||||||
|
|||||||
Reference in New Issue
Block a user