holder + scad: SCAD source editor on /holder + new /scad universal renderer
Phase 2 — SCAD editor on /holder
================================
Some adjustments need more control than the parameter form gives
(e.g. tweaking the .scad logic itself). A collapsible CodeMirror-based
editor now slides up from the bottom of the viewport when you click
the "</> Source" button at the bottom-right.
- holder.html / holder.css: editor panel, toggle button, Reset and
Close buttons. CodeMirror 5 loaded from cdnjs (single CSS + JS
pair, clike mode for syntax highlighting since OpenSCAD is C-like).
- holder-app.js: lazy-initialises the editor on first show, fetches
bundled source from /api/holder/source, tracks whether the editor
content has been modified ("modified" tag in the panel title).
When the editor is visible AND content differs from bundled,
_doRender() switches from /api/holder/render -> /api/scad/render
with {source, params}. Cell-count cap doesn't apply in that mode
(the source may not even use the holder schema).
Phase 3 — Universal OpenSCAD playground at /scad
================================================
A standalone page for rendering arbitrary OpenSCAD. Paste code, click
Render (or Ctrl+Enter), see STL in the same Three.js viewer the
holder uses. Useful for prototyping new generators before wiring
them into a parameter form.
- New page: static/scad.html + scad.css + js/scad-app.js. Reuses
holder-viewer.js (the Three.js scene module is generic enough).
- "Load example" populates a 6-hole rounded bracket so new users
can verify the renderer works in two clicks.
- Same progress UI (elapsed timer + indeterminate bar + Cancel
via AbortController) as /holder.
- Top nav now has Holder / Busbars / SCAD on every page.
Backend (shared by phase 2 & 3)
================================
- holder.py: split _run_openscad() out of render_stl(). New
render_source(source, params=None) writes the source to a temp
file and renders it; enforces MAX_SOURCE_BYTES (default 512 KB)
and RENDER_TIMEOUT but skips the cell-count cap. New bundled_source()
returns the hex_cell.scad text for editor pre-population.
- app.py: GET /api/holder/source returns the bundled .scad text.
POST /api/scad/render takes {source, params?} and returns STL +
X-Holder-Dimensions header. ValueError -> 400, RuntimeError -> 500.
_stl_response() factored out so both render endpoints emit the same
headers consistently.
This commit is contained in:
+64
-2
@@ -23,6 +23,9 @@ const state = {
|
||||
dimensions: null, // last ECHO dims dict from X-Holder-Dimensions
|
||||
maxBox: null, // {w, d, h} of assembled-battery bounding box
|
||||
enclosure: null, // {w, d, h} of user-defined case (or null)
|
||||
scadEditor: null, // CodeMirror instance (lazy-initialised on first show)
|
||||
bundledSource: null, // text fetched from /api/holder/source
|
||||
scadVisible: false,
|
||||
};
|
||||
|
||||
const COLOR_MAX_BOX = 0x4a9eff; // blue
|
||||
@@ -209,10 +212,16 @@ async function _doRender() {
|
||||
}, 100);
|
||||
|
||||
try {
|
||||
const res = await fetch("/api/holder/render", {
|
||||
const useCustomSource = state.scadVisible && state.scadEditor &&
|
||||
state.scadEditor.getValue() !== state.bundledSource;
|
||||
const url = useCustomSource ? "/api/scad/render" : "/api/holder/render";
|
||||
const body = useCustomSource
|
||||
? { source: state.scadEditor.getValue(), params: state.params }
|
||||
: { params: state.params };
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ params: state.params }),
|
||||
body: JSON.stringify(body),
|
||||
signal: state.abortCtrl.signal,
|
||||
});
|
||||
if (!res.ok) {
|
||||
@@ -348,6 +357,59 @@ function _updateFitCheck() {
|
||||
}
|
||||
}
|
||||
|
||||
// ----- SCAD editor ----------------------------------------------------------
|
||||
|
||||
async function _ensureBundledSource() {
|
||||
if (state.bundledSource !== null) return;
|
||||
const r = await fetch("/api/holder/source");
|
||||
if (!r.ok) throw new Error(`source fetch failed: ${r.statusText}`);
|
||||
state.bundledSource = await r.text();
|
||||
}
|
||||
|
||||
function _updateScadDirtyTag() {
|
||||
if (!state.scadEditor) return;
|
||||
const dirty = state.scadEditor.getValue() !== state.bundledSource;
|
||||
$("scad-dirty-tag").hidden = !dirty;
|
||||
}
|
||||
|
||||
async function _showScadEditor() {
|
||||
await _ensureBundledSource();
|
||||
$("scad-panel").hidden = false;
|
||||
state.scadVisible = true;
|
||||
if (!state.scadEditor) {
|
||||
state.scadEditor = window.CodeMirror($("scad-editor"), {
|
||||
value: state.bundledSource,
|
||||
mode: "text/x-csrc", // closest built-in mode for OpenSCAD
|
||||
theme: "dracula",
|
||||
lineNumbers: true,
|
||||
indentUnit: 4,
|
||||
tabSize: 4,
|
||||
lineWrapping: false,
|
||||
viewportMargin: Infinity, // render whole document so CM fills the panel
|
||||
});
|
||||
state.scadEditor.on("change", _updateScadDirtyTag);
|
||||
} else {
|
||||
state.scadEditor.refresh();
|
||||
}
|
||||
_updateScadDirtyTag();
|
||||
}
|
||||
|
||||
function _hideScadEditor() {
|
||||
$("scad-panel").hidden = true;
|
||||
state.scadVisible = false;
|
||||
}
|
||||
|
||||
$("btn-scad-toggle").addEventListener("click", () => {
|
||||
if (state.scadVisible) _hideScadEditor();
|
||||
else _showScadEditor().catch((e) => alert("Couldn't load SCAD: " + e.message));
|
||||
});
|
||||
$("btn-scad-close").addEventListener("click", _hideScadEditor);
|
||||
$("btn-scad-reset").addEventListener("click", () => {
|
||||
if (!state.scadEditor || state.bundledSource == null) return;
|
||||
state.scadEditor.setValue(state.bundledSource);
|
||||
_updateScadDirtyTag();
|
||||
});
|
||||
|
||||
// ----- buttons --------------------------------------------------------------
|
||||
|
||||
$("show-max-box").addEventListener("change", _updateFitCheck);
|
||||
|
||||
Reference in New Issue
Block a user