Skip to content

Commit

Permalink
Merge branch 'main' into pr/101
Browse files Browse the repository at this point in the history
  • Loading branch information
hrgdavor committed Mar 15, 2024
2 parents e6c683d + e92fda4 commit a02e890
Show file tree
Hide file tree
Showing 34 changed files with 1,000 additions and 274 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ If you want to discuss jscad or jscadui you can also join us on discord: https:/

Most of the things are work in progres, but some parts are pretty ready to be used

- [packages/html-gizmo](./packages/html-gizmo) - a gizmo to display current camera direction
- [packages/html-gizmo](./packages/html-gizmo) - [![npm version](https://badge.fury.io/js/@jscadui%2Fhtml-gizmo.svg)](https://www.npmjs.com/package/@jscadui%2Fhtml-gizmo) a gizmo to display current camera direction
- [file-format/3mf-export](./file-format/3mf-export) - [![npm version](https://badge.fury.io/js/@jscadui%2F3mf-export.svg)](https://www.npmjs.com/package/@jscadui%2F3mf-export) 3mf-export (also used by mynifold)
- [packages/postmessage](./packages/postmessage) - [![npm version](https://badge.fury.io/js/@jscadui%2Fpostmessage.svg)](https://www.npmjs.com/package/@jscadui%2Fpostmessage) postMessage quality of life improvement

# demo
[jscad.app](https://jscad.app) is a nice demo and our attempt at making a an improved version of [openjscad.xyz](https://openjscad.xyz).
Expand Down
8 changes: 4 additions & 4 deletions apps/cardboard-cutter/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ function save(blob, filename) {
}

function exportModel(format) {
sendCmd('exportData', { format }).then(({ data }) => {
sendCmd('exportData', [{ format }]).then(({ data }) => {
console.log('save', fileToRun + '.stl', data)
save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl')
})
Expand All @@ -123,12 +123,12 @@ const { sendCmd, sendNotify } = initMessaging(worker, handlers)

const paramChangeCallback = params => {
console.log('params', params)
sendCmd('runMain', { params })
sendCmd('runMain', [{ params }])
}

export const runScript = file => {
fileToRun = file.replace(/.*\//, '').replace(/\..*/, '')
sendCmd('runScript', { url: file }).then(result => {
sendCmd('runScript', [{ url: file }]).then(result => {
console.log('result', result)
genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback })
})
Expand All @@ -141,5 +141,5 @@ export const initEngine = async (THREE, elem, workerOptions) => {
updateFromCtrl(ctrl)
setTheme(theme)

await sendCmd('init', workerOptions)
await sendCmd('init', [workerOptions])
}
68 changes: 37 additions & 31 deletions apps/engine-test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JscadToCommon } from '@jscadui/format-jscad'
import { Gizmo } from '@jscadui/html-gizmo'
import { OrbitControl, OrbitState, closerAngle, getCommonRotCombined } from '@jscadui/orbit'
import { genParams } from '@jscadui/params'
import { initMessaging } from '@jscadui/postmessage'
import { initMessaging, messageProxy } from '@jscadui/postmessage'
import { makeAxes, makeGrid } from '@jscadui/scene'
import * as themes from '@jscadui/themes'

Expand All @@ -16,6 +16,8 @@ import { availableEngines, availableEnginesList } from './src/availableEngines'
import { CurrentUrl } from './src/currentUrl'
import { EngineState } from './src/engineState'

/** @typedef {import('@jscadui/worker').JscadWorker} JscadWorker*/

const theme = themes.light
const { subtract } = booleans
const { translate } = transforms
Expand Down Expand Up @@ -122,11 +124,11 @@ document.body.ondrop = async ev => {

if (!sw) await initFs()
showDrop(false)
sendCmd('clearTempCache', {})
workerApi.clearTempCache()
const { alias, script } = await fileDropped(sw, files)
projectName = sw.projectName
if (alias.length) {
sendNotify('init', { alias })
workerApi.init({ alias })
}
runScript({ url: sw.fileToRun, base: sw.base })
} catch (error) {
Expand Down Expand Up @@ -167,42 +169,60 @@ function save(blob, filename) {
}

function exportModel(format) {
sendCmd('exportData', { format }).then(({ data }) => {
workerApi.exportData({ format }).then(({ data }) => {
console.log('save', fileToRun + '.stl', data)
save(new Blob([data], { type: 'text/plain' }), fileToRun + '.stl')
}).catch(setError)
}
window.exportModel = exportModel

const paramChangeCallback = async params => {
console.log('params changed', params)
let result = await workerApi.runMain({ params })
handlers.entities(result)
}

const runScript = async ({script, url = './index.js', base, root}) => {
const result = await workerApi.runScript({ script, url, base, root })
console.log('result', result)
genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback })
handlers.entities(result)
}

/** @type {JscadWorker} */
const worker = new Worker('./build/bundle.worker.js')
const workerApi = messageProxy(worker, handlers, { onJobCount: trackJobs })
const handlers = {
entities: ({ entities }) => {
if (!(entities instanceof Array)) entities = [entities]
setViewerScene((model = entities))
setError(undefined)
},
}
const { sendCmd, sendNotify } = initMessaging(worker, handlers)

const spinner = byId('spinner')
async function sendCmdAndSpin(method, params){
spinner.style.display = 'block'
try{
return await sendCmd(method, params)
}catch(error){
setError(error)
throw error
}finally{
let firstJobTimer

function trackJobs(jobs) {
if (jobs === 1) {
// do not show spinner for fast renders
firstJobTimer = setTimeout(() => {
spinner.style.display = 'block'
}, 300)
}
if (jobs === 0) {
clearTimeout(firstJobTimer)
spinner.style.display = 'none'
}
}

sendCmdAndSpin('init', {
await workerApi.init({
bundles: {
'@jscad/modeling': toUrl('./build/bundle.jscad_modeling.js'),
},
}).then(()=>{
runScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives
})

runScript({script:`const { sphere, geodesicSphere } = require('@jscad/modeling').primitives
const { translate, scale } = require('@jscad/modeling').transforms
const main = () => [
Expand All @@ -220,27 +240,13 @@ sendCmdAndSpin('init', {
module.exports = { main }`
})
})

const paramChangeCallback = async params => {
console.log('params changed', params)
let result = await sendCmdAndSpin('runMain', { params })
handlers.entities(result)
}

const runScript = async ({script, url = './index.js', base, root}) => {
const result = await sendCmdAndSpin('runScript', { script, url, base, root })
console.log('result', result)
genParams({ target: byId('paramsDiv'), params: result.def || {}, callback: paramChangeCallback })
handlers.entities(result)
}

let sw
async function initFs() {
sw = await registerServiceWorker('bundle.fs-serviceworker.js?prefix=/swfs/')
sw.defProjectName = 'jscad'
sw.onfileschange = files => {
sendNotify('clearFileCache', { files })
workerApi.clearFileCache({ files })
if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base })
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/engine-test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"allowJs": true,
"skipLibCheck": true,
"moduleResolution": "node16"
},
"exclude": ["node_modules", "build_dev", "build", "dist"]
Expand Down
9 changes: 9 additions & 0 deletions apps/jscad-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ To start the local development server, go to the `apps/jscad-web` directory and
npm start
```

this will start the dev server without generating jscad docs, which is like ok 99% of time.

to start dev server that also has docs run

```
npm run start:full
```


## Deployment

To start the production server run:
Expand Down
14 changes: 7 additions & 7 deletions apps/jscad-web/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {serve} from './serve.js'
import { buildBundle, buildOne } from './src_build/esbuildUtil.js'

// *************** read parameters **********************
const { dev, port = 5120, serve:serveBuild=false } = parseArgs()
const { dev, port = 5120, serve:serveBuild=false, skipDocs=false } = parseArgs()
const watch = dev
const outDir = dev ? 'build_dev' : 'build'
const docsDir = 'jscad/docs'
// if docs dir does not exist, then clone jscad and run `npm run docs` to generate it
if (!existsSync(docsDir)) {
if (!skipDocs &&!existsSync(docsDir)) {
console.log('generating docs')
if (!existsSync('jscad')) {
// TODO: faster to fetch https://github.com/jscad/OpenJSCAD.org/archive/refs/heads/master.zip
Expand All @@ -29,16 +29,16 @@ mkdirSync(outDir, { recursive: true })
copyTask('static', outDir, { include: [], exclude: [], watch, filters: [] })
copyTask('examples', outDir+'/examples', { include: [], exclude: [], watch, filters: [] })
//in dev mode dont try to sync docs, just copy the first time
if(!(dev & existsSync(outDir + "/docs"))){
if(!skipDocs && !(dev & existsSync(outDir + "/docs"))){
// this task is heavy
copyTask(docsDir, outDir + "/docs", { include: [], exclude: [], watch:false, filters: [] })
}

/**************************** BUILD JS that is static *************/
await buildBundle(outDir + '/build', 'bundle.threejs.js', { globalName: 'THREE' })
await buildBundle(outDir + '/build', 'bundle.jscad_modeling.js', { format: 'cjs' })
await buildBundle(outDir + '/build', 'bundle.jscad_io.js', { format:'cjs' })
await buildBundle(outDir + '/build', 'bundle.jscadui.transform-babel.js', { globalName: 'jscadui_transform_babel' })
await buildBundle(outDir + '/build', 'bundle.threejs.js', { globalName: 'THREE', skipExisting: dev })
await buildBundle(outDir + '/build', 'bundle.jscad_modeling.js', { format: 'cjs', skipExisting: dev })
await buildBundle(outDir + '/build', 'bundle.jscad_io.js', { format:'cjs', skipExisting: dev })
await buildBundle(outDir + '/build', 'bundle.jscadui.transform-babel.js', { globalName: 'jscadui_transform_babel', skipExisting: dev })

/**************************** BUILD JS THAT can change and watch if in dev mode *************/
await buildOne('src_bundle', outDir + '/build', 'bundle.worker.js', watch, { format: 'iife' })
Expand Down
Loading

0 comments on commit a02e890

Please sign in to comment.