Add the ability to link to text resource for IDE #328

Merged
Irev-Dev merged 1 commits from kurt/327 into main 2021-05-27 22:59:52 +02:00
4 changed files with 52 additions and 2 deletions

View File

@@ -2,7 +2,7 @@
# [C a d H u b](https://cadhub.xyz)
[![Netlify Status](https://api.netlify.com/api/v1/badges/77f37543-e54a-4723-8136-157c0221ec27/deploy-status)](https://app.netlify.com/sites/cadhubxyz/deploys)
<!-- [![Netlify Status](https://api.netlify.com/api/v1/badges/77f37543-e54a-4723-8136-157c0221ec27/deploy-status)](https://app.netlify.com/sites/cadhubxyz/deploys) -->
Irev-Dev commented 2021-05-27 22:54:46 +02:00 (Migrated from github.com)
Review

Removing because more than often it displays as failed, when what really happened is build was cancel because there was a change to the docs not the app so the build finished early. Not sure if this can be sorted out or not.

Removing because more than often it displays as failed, when what really happened is build was cancel because there was a change to the docs not the app so the build finished early. Not sure if this can be sorted out or not.
Let's help Code-CAD reach its [full potential!](https://cadhub.xyz) We're making a ~~cad~~hub for the Code-CAD community, think of it as model-repository crossed with a live editor. We have an integration with the excellent [cascadeStudio](https://zalo.github.io/CascadeStudio/) with [more coming soon](https://github.com/Irev-Dev/curated-code-cad).

View File

@@ -26,6 +26,7 @@
"golden-layout": "^1.5.9",
"gotrue-js": "^0.9.27",
"jquery": "^3.5.1",
"lodash": "^4.17.21",
"monaco-editor": "^0.20.0",
"monaco-editor-webpack-plugin": "^1.9.1",
"netlify-identity-widget": "^1.9.1",
@@ -45,6 +46,7 @@
"three": "^0.118.3"
},
"devDependencies": {
"@types/lodash": "^4.14.170",
"autoprefixer": "^10.2.5",
"html-webpack-plugin": "^4.5.0",
"opentype.js": "^1.3.3",

View File

@@ -5,18 +5,34 @@ import { useIdeState, codeStorageKey } from 'src/helpers/hooks/useIdeState'
import { copyTextToClipboard } from 'src/helpers/clipboard'
import { requestRender } from 'src/helpers/hooks/useIdeState'
import { encode, decode } from 'src/helpers/compress'
import { flow } from 'lodash/fp'
export const githubSafe = (url) =>
url.includes('github.com')
? url
.replace('github.com', 'raw.githubusercontent.com')
.replace('/blob/', '/')
: url
const prepareEncodedUrl = flow(decodeURIComponent, githubSafe)
Irev-Dev commented 2021-05-27 22:58:41 +02:00 (Migrated from github.com)
Review

Trying to ease my way into some functional concepts. This is just a helper that makes a function the equivalent of

url => githubSafe(decodeURIComponent(url))

Not really needed in this case with only two functions, but the idea is one of the dis-incentives of making small easy to understand functions is that chaining them is pain to write and read a(b(c(input))) so you just make one abc(input).

Trying to ease my way into some functional concepts. This is just a helper that makes a function the equivalent of ``` url => githubSafe(decodeURIComponent(url)) ``` Not really needed in this case with only two functions, but the idea is one of the dis-incentives of making small easy to understand functions is that chaining them is pain to write and read `a(b(c(input)))` so you just make one `abc(input)`.
export const IdeContext = createContext()
const IdeToolbarNew = ({ cadPackage }) => {
const [state, thunkDispatch] = useIdeState()
const scriptKey = 'encoded_script'
const scriptKeyV2 = 'encoded_script_v2'
const fetchText = 'fetch_text_v1'
useEffect(() => {
thunkDispatch({
type: 'initIde',
payload: { cadPackage },
})
// load code from hash if it's there
const triggerRender = () =>
setTimeout(() => {
// definitely a little hacky, timeout with no delay is just to push it into the next event loop.
handleRender()
})
let hash
if (isBrowser) {
hash = window.location.hash
@@ -25,12 +41,23 @@ const IdeToolbarNew = ({ cadPackage }) => {
if (key === scriptKey) {
const script = atob(encodedScript)
thunkDispatch({ type: 'updateCode', payload: script })
triggerRender()
} else if (key === scriptKeyV2) {
const script = decode(encodedScript)
thunkDispatch({ type: 'updateCode', payload: script })
triggerRender()
} else if (key === fetchText) {
const url = prepareEncodedUrl(encodedScript)
fetch(url).then((response) =>
response.text().then((script) => {
thunkDispatch({ type: 'updateCode', payload: script })
triggerRender()
})
)
} else {
triggerRender()
}
window.location.hash = ''
setTimeout(() => handleRender()) // definitely a little hacky, timeout with no delay is just to push it into the next event loop.
}, [cadPackage])
function handleRender() {
thunkDispatch((dispatch, getState) => {

View File

@@ -0,0 +1,21 @@
// import { githubSafe } from './IdeToolbarNew.js'
// TODO jest doesn't like ECMAScript modules and is failing further down in the tree because three ES modules
Irev-Dev commented 2021-05-27 22:52:52 +02:00 (Migrated from github.com)
Review

I need to sort out testing :(

I need to sort out testing :(
// Need to update config
describe('githubSafe', () => {
const rawUrl =
'https://raw.githubusercontent.com/aaevan/openscad_objects/main/fire_tablet_bottom_corner.scad'
it('It transforms non-raw url to raw urls', () => {
expect(1).toBe(1)
})
// it('It transforms non-raw url to raw urls', () => {
// expect(
// githubSafe(
// 'https://github.com/aaevan/openscad_objects/blob/main/fire_tablet_bottom_corner.scad'
// )
// ).toBe(rawUrl)
// })
// it('It leaves raw urls untouched', () => {
// expect(githubSafe(rawUrl)).toBe(rawUrl)
// })
})