Skip to content

Commit

Permalink
jscad-web: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
platypii committed Sep 20, 2023
1 parent 3b54017 commit 034a0a9
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 60 deletions.
8 changes: 5 additions & 3 deletions apps/jscad-web/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ editor.init(defaultCode, async (script, path) => {
// it is expected if multiple files require same file/module that first time it is loaded
// but for others resolved module is returned
// if not cleared by calling clearFileCache, require will not try to reload the file
await sendCmd('clearFileCache',{files:[path]})
await sendCmd('clearFileCache', { files: [path] })
if (sw.fileToRun) runScript({ url: sw.fileToRun, base: sw.base })
} else {
runScript({ script })
Expand All @@ -217,9 +217,11 @@ remote.init((script) => {
})
exporter.init(exportModel)

try{
try {
await initFs()
}catch(err){setError(err)}
} catch (err) {
setError(err)
}

if ('serviceWorker' in navigator && !navigator.serviceWorker.controller) {
// service workers are disabled on hard-refresh, so need to reload.
Expand Down
29 changes: 15 additions & 14 deletions apps/jscad-web/src/editor.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { EditorView, basicSetup } from "codemirror"
import { javascript } from "@codemirror/lang-javascript"
import { defaultKeymap } from "@codemirror/commands"
import { keymap } from "@codemirror/view"
import { defaultKeymap } from '@codemirror/commands'
import { javascript } from '@codemirror/lang-javascript'
import { keymap } from '@codemirror/view'
import { EditorView, basicSetup } from 'codemirror'

import * as drawer from './drawer.js'

let view
Expand All @@ -17,31 +18,31 @@ const compile = (code, path) => {
if (compileFn) {
compileFn(code, path)
} else {
console.log("not ready to compile")
console.log('not ready to compile')
}
}

export const init = (defaultCode, fn) => {
compileFn = fn
// Initialize codemirror
const editorDiv = document.getElementById("editor-container")
const editorDiv = document.getElementById('editor-container')
view = new EditorView({
extensions: [
basicSetup,
javascript(),
keymap.of([
{
key: "Shift-Enter",
key: 'Shift-Enter',
run: () => compile(view.state.doc.toString(), currentFile),
preventDefault: true
preventDefault: true,
},
{
key: "Mod-s",
key: 'Mod-s',
run: () => compile(view.state.doc.toString(), currentFile),
preventDefault: true
preventDefault: true,
},
...defaultKeymap
])
...defaultKeymap,
]),
],
parent: editorDiv,
})
Expand All @@ -56,7 +57,7 @@ export const init = (defaultCode, fn) => {
})

// Setup file selector
editorFile.addEventListener('click', (e) => {
editorFile.addEventListener('click', () => {
editorNav.classList.toggle('open')
})
// Close file selector on click outside
Expand All @@ -68,7 +69,7 @@ export const init = (defaultCode, fn) => {
}

export const setSource = (source, path = '/index.js') => {
view.dispatch({changes: {from: 0, to: view.state.doc.length, insert: source}})
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: source } })
currentFile = path
}

Expand Down
3 changes: 2 additions & 1 deletion apps/jscad-web/src/engine.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RenderThreejs } from '@jscadui/render-threejs'

// import * as THREE from 'three'

export const init = async () => {
Expand Down Expand Up @@ -28,7 +29,7 @@ const addScript = async (source, module = false) => {
var tag = document.createElement('script')
tag.type = module ? 'module' : 'text/javascript'
tag.src = source
tag.onload = ()=>resolve()
tag.onload = () => resolve()
tag.onerror = err => reject(err)
document.head.append(tag)
})
Expand Down
27 changes: 13 additions & 14 deletions apps/jscad-web/src/exporter.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@

const exportFormats = [
{ name: "stla", extension: "stl", label: "STL (ascii)" },
{ name: "stlb", extension: "stl", label: "STL (binary)" },
{ name: "amf", extension: "amf", label: "AMF" },
{ name: "dxf", extension: "dxf", label: "DXF" },
{ name: "json", extension: "json", label: "JSON" },
{ name: "obj", extension: "obj", label: "OBJ" },
{ name: "x3d", extension: "x3d", label: "X3D" },
{ name: "svg", extension: "svg", label: "SVG" },
{ name: "3mf", extension: "3mf", label: "3MF" },
{ name: 'stla', extension: 'stl', label: 'STL (ascii)' },
{ name: 'stlb', extension: 'stl', label: 'STL (binary)' },
{ name: 'amf', extension: 'amf', label: 'AMF' },
{ name: 'dxf', extension: 'dxf', label: 'DXF' },
{ name: 'json', extension: 'json', label: 'JSON' },
{ name: 'obj', extension: 'obj', label: 'OBJ' },
{ name: 'x3d', extension: 'x3d', label: 'X3D' },
{ name: 'svg', extension: 'svg', label: 'SVG' },
{ name: '3mf', extension: '3mf', label: '3MF' },
]

export const init = (exportFn) => {
populateFormats(exportFormats)
// Bind export buttons
document.getElementById("export-button").addEventListener("click", () => {
document.getElementById('export-button').addEventListener('click', () => {
// Export model in selected format
const formatSelect = document.getElementById("export-format")
const formatSelect = document.getElementById('export-format')
const format = exportFormats.find((f) => f.name === formatSelect.value)
exportFn(format.name, format.extension)
})
}

const populateFormats = (formats) => {
const select = document.getElementById("export-format")
const select = document.getElementById('export-format')
formats.forEach((format) => {
const option = document.createElement("option")
const option = document.createElement('option')
option.value = format.name
option.text = format.label
select.appendChild(option)
Expand Down
28 changes: 14 additions & 14 deletions apps/jscad-web/src/menu.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
import { examples } from './examples.js'

const menu = document.getElementById("menu")
const menu = document.getElementById('menu')

export const init = (loadExample) => {
const button = document.getElementById("menu-button")
const content = document.getElementById("menu-content")
const button = document.getElementById('menu-button')
const content = document.getElementById('menu-content')

// Menu button
button.addEventListener("click", () => {
menu.classList.toggle("open")
button.addEventListener('click', () => {
menu.classList.toggle('open')
})

// Close menu when anything else is clicked
window.addEventListener("click", (e) => {
window.addEventListener('click', (e) => {
if (!button.contains(e.target) && !content.contains(e.target)) {
dismiss()
}
})
window.addEventListener("drop", () => dismiss())
window.addEventListener("dragstart", () => dismiss())
window.addEventListener("dragover", () => dismiss())
window.addEventListener('drop', () => dismiss())
window.addEventListener('dragstart', () => dismiss())
window.addEventListener('dragover', () => dismiss())

// Add examples to menu
const exampleDiv = document.getElementById("examples")
const exampleDiv = document.getElementById('examples')
examples.forEach(({ name, source }) => {
const a = document.createElement("a")
const a = document.createElement('a')
a.innerText = name
a.addEventListener("click", async () => {
a.addEventListener('click', async () => {
console.log(`load example ${name}`)
loadExample(await (await fetch(source)).text())
})
const li = document.createElement("li")
const li = document.createElement('li')
li.appendChild(a)
exampleDiv.appendChild(li)
})
}

const dismiss = () => {
menu.classList.remove("open")
menu.classList.remove('open')
}
3 changes: 1 addition & 2 deletions apps/jscad-web/src/remote.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

export const init = (compileFn, setError) => {
const load = loadFromUrl(compileFn, setError)
load() // on load
Expand Down Expand Up @@ -28,7 +27,7 @@ export const loadFromUrl = (compileFn, setError) => async () => {
*/
const fetchUrl = async (url) => {
// Try to fetch url directly
const res = await fetch(url).catch((err) => {
const res = await fetch(url).catch(() => {
// Failed to fetch directly, try proxy
return fetch(`/remote?url=${url}`)
})
Expand Down
13 changes: 7 additions & 6 deletions apps/jscad-web/src/stacktrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ export const formatStacktrace = (error) => {
// firefox stacktrace:
// main@http://localhost:5120/build/bundle.worker.js line 14 > eval line 1 > eval:13:3
// @http://localhost:5120/build/bundle.worker.js line 14 > eval:1:37
const cleaned = error.stack.split('\n')
.filter((line) => line.includes('eval'))
.map((line) => line.replace(/eval at <anonymous> \(.*?\), /, '')) // chrome
.map((line) => line.replace(/^@/, '<anonymous>@')) // firefox
.map((line) => line.replace(/@http.*?bundle.worker.js.* > eval:/, ' ')) // firefox
.map((line) => line.replace(/^\s*(at )?/, ' at ')) // indent
const cleaned = error.stack
.split('\n')
.filter(line => line.includes('eval'))
.map(line => line.replace(/eval at <anonymous> \(.*?\), /, '')) // chrome
.map(line => line.replace(/^@/, '<anonymous>@')) // firefox
.map(line => line.replace(/@http.*?bundle.worker.js.* > eval:/, ' ')) // firefox
.map(line => line.replace(/^\s*(at )?/, ' at ')) // indent

return [error.message, ...cleaned].join('\n')
}
13 changes: 7 additions & 6 deletions apps/jscad-web/src/welcome.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const welcome = document.getElementById("welcome")
const welcome = document.getElementById('welcome')
let showing = true

export const init = () => {
// hello devs
console.log('Welcome to JSCAD! Like JavaScript and want to help? Join us at https://github.com/jscad/OpenJSCAD.org')
if (!welcome) return
// hide the welcome menu when anything is clicked
window.addEventListener("mousedown", (e) => dismiss(e))
window.addEventListener("click", (e) => dismiss(e))
window.addEventListener("drop", () => dismiss())
window.addEventListener("dragstart", () => dismiss())
window.addEventListener("dragover", () => dismiss())
window.addEventListener('mousedown', dismiss)
window.addEventListener('click', dismiss)
window.addEventListener('drop', dismiss)
window.addEventListener('dragstart', dismiss)
window.addEventListener('dragover', dismiss)
// permanently hide the welcome menu
document.getElementById('welcome-dismiss').addEventListener('click', () => {
localStorage.setItem('welcome.dismissed', true)
dismiss()
Expand Down

0 comments on commit 034a0a9

Please sign in to comment.