diff --git a/src/line-diff.js b/src/line-diff.js
new file mode 100644
index 0000000..16b995e
--- /dev/null
+++ b/src/line-diff.js
@@ -0,0 +1,185 @@
+// Line diff: Myers diff over string arrays, with common prefix/suffix trimming
+// and a bounded edit distance so pathological inputs fall back to a block replace.
+
+const DIFF_MAX_EDIT_DISTANCE = 2000;
+
+// Myers diff on a[aStart..aEnd) vs b[bStart..bEnd). Returns an array of
+// 'same' | 'del' | 'add' ops in forward order, or null if the edit distance
+// exceeds DIFF_MAX_EDIT_DISTANCE.
+function _diffMyersOps(a, aStart, aEnd, b, bStart, bEnd) {
+ const n = aEnd - aStart;
+ const m = bEnd - bStart;
+ const maxD = Math.min(n + m, DIFF_MAX_EDIT_DISTANCE);
+ const size = 2 * maxD + 3;
+ const mid = maxD + 1;
+ const v = new Int32Array(size);
+ const snapshots = [];
+ let found = -1;
+
+ for (let d = 0; d <= maxD && found < 0; d++) {
+ for (let k = -d; k <= d; k += 2) {
+ let x;
+ if (k === -d || (k !== d && v[mid + k - 1] < v[mid + k + 1])) x = v[mid + k + 1];
+ else x = v[mid + k - 1] + 1;
+ let y = x - k;
+ while (x < n && y < m && a[aStart + x] === b[bStart + y]) { x++; y++; }
+ v[mid + k] = x;
+ if (x >= n && y >= m) { found = d; break; }
+ }
+ if (found < 0) snapshots.push(v.slice(mid - d, mid + d + 1));
+ }
+ if (found < 0) return null;
+
+ const ops = [];
+ let x = n;
+ let y = m;
+ for (let d = found; d > 0; d--) {
+ const prev = snapshots[d - 1];
+ const k = x - y;
+ const pAt = (kk) => prev[kk + d - 1];
+ const prevK = (k === -d || (k !== d && pAt(k - 1) < pAt(k + 1))) ? k + 1 : k - 1;
+ const prevX = pAt(prevK);
+ const prevY = prevX - prevK;
+ while (x > prevX && y > prevY) { ops.push('same'); x--; y--; }
+ ops.push(x === prevX ? 'add' : 'del');
+ x = prevX;
+ y = prevY;
+ }
+ while (x > 0 && y > 0) { ops.push('same'); x--; y--; }
+ return ops.reverse();
+}
+
+function _pushBlockReplaceOps(ops, delCount, addCount) {
+ for (let i = 0; i < delCount; i++) ops.push('del');
+ for (let i = 0; i < addCount; i++) ops.push('add');
+}
+
+// Lines occurring exactly once in both ranges, as {b, a} pairs that form the
+// longest increasing subsequence (patience-style anchors), in forward order.
+function _uniqueAnchors(before, bs, be, after, as, ae) {
+ const info = new Map(); // line -> { bCount, aCount, b, a }
+ for (let i = bs; i < be; i++) {
+ const e = info.get(before[i]);
+ if (e) e.bCount++; else info.set(before[i], { bCount: 1, aCount: 0, b: i, a: -1 });
+ }
+ for (let j = as; j < ae; j++) {
+ const e = info.get(after[j]);
+ if (!e) continue;
+ e.aCount++;
+ e.a = j;
+ }
+ const pairs = [];
+ for (const e of info.values()) {
+ if (e.bCount === 1 && e.aCount === 1) pairs.push(e);
+ }
+ if (pairs.length === 0) return [];
+ pairs.sort((x, y) => x.b - y.b);
+ // LIS on .a using tails array + predecessor links
+ const tails = [];
+ const prev = new Int32Array(pairs.length).fill(-1);
+ for (let i = 0; i < pairs.length; i++) {
+ let lo = 0;
+ let hi = tails.length;
+ while (lo < hi) {
+ const m = (lo + hi) >> 1;
+ if (pairs[tails[m]].a < pairs[i].a) lo = m + 1; else hi = m;
+ }
+ if (lo > 0) prev[i] = tails[lo - 1];
+ tails[lo] = i;
+ }
+ const out = [];
+ for (let i = tails[tails.length - 1]; i !== -1; i = prev[i]) out.push(pairs[i]);
+ return out.reverse();
+}
+
+// Appends ops for before[bs..be) vs after[as..ae). Trims common ends, tries
+// Myers, then anchors on unique lines and recurses into the gaps; only a gap
+// with no anchors and over the Myers cap becomes a block replace.
+function _diffRangeOps(before, bs, be, after, as, ae, ops) {
+ while (bs < be && as < ae && before[bs] === after[as]) { ops.push('same'); bs++; as++; }
+ let tail = 0;
+ while (be > bs && ae > as && before[be - 1] === after[ae - 1]) { be--; ae--; tail++; }
+ const delCount = be - bs;
+ const addCount = ae - as;
+ if (delCount === 0 || addCount === 0) {
+ _pushBlockReplaceOps(ops, delCount, addCount);
+ } else {
+ const myers = _diffMyersOps(before, bs, be, after, as, ae);
+ if (myers) {
+ for (let i = 0; i < myers.length; i++) ops.push(myers[i]);
+ } else {
+ const anchors = _uniqueAnchors(before, bs, be, after, as, ae);
+ if (anchors.length === 0) {
+ _pushBlockReplaceOps(ops, delCount, addCount);
+ } else {
+ let pb = bs;
+ let pa = as;
+ for (const anc of anchors) {
+ _diffRangeOps(before, pb, anc.b, after, pa, anc.a, ops);
+ ops.push('same');
+ pb = anc.b + 1;
+ pa = anc.a + 1;
+ }
+ _diffRangeOps(before, pb, be, after, pa, ae, ops);
+ }
+ }
+ }
+ for (let i = 0; i < tail; i++) ops.push('same');
+}
+
+export function diffLines(before, after) {
+ const midOps = [];
+ _diffRangeOps(before, 0, before.length, after, 0, after.length, midOps);
+
+ const rows = [];
+ const hunks = [];
+ let bi = 0;
+ let ai = 0;
+
+ let i = 0;
+ while (i < midOps.length) {
+ if (midOps[i] === 'same') {
+ rows.push({ kind: 'same', beforeIdx: bi++, afterIdx: ai++ });
+ i++;
+ continue;
+ }
+ // Contiguous changed run: emit all dels first, then all adds.
+ let dels = 0;
+ let adds = 0;
+ while (i < midOps.length && midOps[i] !== 'same') {
+ if (midOps[i] === 'del') dels++; else adds++;
+ i++;
+ }
+ const startRow = rows.length;
+ for (let j = 0; j < dels; j++) rows.push({ kind: 'del', beforeIdx: bi++, afterIdx: null });
+ for (let j = 0; j < adds; j++) rows.push({ kind: 'add', beforeIdx: null, afterIdx: ai++ });
+ hunks.push({ startRow, endRow: rows.length - 1 });
+ }
+
+ return { rows, hunks };
+}
+
+export function changedLineSets(rows) {
+ const removedBefore = new Set();
+ const addedAfter = new Set();
+ for (const row of rows) {
+ if (row.kind === 'del') removedBefore.add(row.beforeIdx);
+ else if (row.kind === 'add') addedAfter.add(row.afterIdx);
+ }
+ return { removedBefore, addedAfter };
+}
+
+// layers sorted by startLine. Returns the number of the last layer whose
+// startLine <= lineIndex (the first layer for lines before it), or null.
+export function layerForLine(layers, lineIndex) {
+ if (!layers || layers.length === 0) return null;
+ let lo = 0;
+ let hi = layers.length - 1;
+ let found = 0;
+ while (lo <= hi) {
+ const midIdx = (lo + hi) >> 1;
+ if (layers[midIdx].startLine <= lineIndex) { found = midIdx; lo = midIdx + 1; }
+ else hi = midIdx - 1;
+ }
+ return layers[found].number;
+}
diff --git a/src/review.js b/src/review.js
new file mode 100644
index 0000000..074d25f
--- /dev/null
+++ b/src/review.js
@@ -0,0 +1,347 @@
+// Before/After export review: aligned, virtualized two-column diff of the
+// original file vs. what export will write. Opened first on every export.
+
+let originalLines = null;
+
+const REVIEW_ROW_HEIGHT = 18;
+const REVIEW_BUFFER_ROWS = 20;
+const REVIEW_CONTEXT_ROWS = 3;
+const REVIEW_MAX_CELL_CHARS = 400;
+
+let reviewState = { rows: [], hunks: [], currentHunk: -1, beforeLines: [], afterLines: [] };
+let reviewIsOpen = false;
+let reviewRenderPending = false;
+
+// Bright magenta: distinct from every motion-type color and from both theme backgrounds
+const REVIEW_HIGHLIGHT_COLOR = [1.0, 0.1, 0.7];
+const REVIEW_LARGE_FILE_LINES = 50000;
+
+// 3D panes. Viewers are created once; parsers are replaced per open so a stale
+// parse can never mutate the parser a live viewer is reading.
+let reviewSession = 0;
+let reviewSides = null;
+let reviewBeforeCache = null; // { lines, parser } so reopening skips re-parsing the original
+
+function reviewEl(id) {
+ return document.getElementById(id);
+}
+
+function reviewCellHtml(kind, wantKind, idx, lines) {
+ // wantKind is the kind that this side shows as changed ('del' left, 'add' right)
+ if (idx === null || idx === undefined) return '
';
+ const cls = kind === wantKind ? ' review-' + kind : '';
+ const text = lines[idx] || '';
+ const shown = text.length > REVIEW_MAX_CELL_CHARS ? text.slice(0, REVIEW_MAX_CELL_CHARS) + '...' : text;
+ return `
${idx + 1} ${syntaxHighlight(shown)}
`;
+}
+
+function reviewRenderRows() {
+ reviewRenderPending = false;
+ if (!reviewIsOpen) return;
+ const list = reviewEl('reviewList');
+ const body = reviewEl('reviewRows');
+ const rows = reviewState.rows;
+ const first = Math.max(0, Math.floor(list.scrollTop / REVIEW_ROW_HEIGHT) - REVIEW_BUFFER_ROWS);
+ const last = Math.min(rows.length, Math.ceil((list.scrollTop + list.clientHeight) / REVIEW_ROW_HEIGHT) + REVIEW_BUFFER_ROWS);
+ const parts = [];
+ for (let i = first; i < last; i++) {
+ const r = rows[i];
+ parts.push('
' +
+ reviewCellHtml(r.kind, 'del', r.beforeIdx, reviewState.beforeLines) +
+ reviewCellHtml(r.kind, 'add', r.afterIdx, reviewState.afterLines) +
+ '
');
+ }
+ body.style.transform = `translateY(${first * REVIEW_ROW_HEIGHT}px)`;
+ body.innerHTML = parts.join('');
+}
+
+function reviewScheduleRender() {
+ if (reviewRenderPending) return;
+ reviewRenderPending = true;
+ requestAnimationFrame(reviewRenderRows);
+}
+
+function reviewUpdateHeader() {
+ const total = reviewState.hunks.length;
+ const counter = reviewEl('reviewCounter');
+ counter.textContent = total === 0 ? 'No changes' : `Change ${reviewState.currentHunk + 1} of ${total}`;
+ reviewEl('reviewPrevBtn').disabled = total === 0 || reviewState.currentHunk <= 0;
+ reviewEl('reviewNextBtn').disabled = total === 0 || reviewState.currentHunk >= total - 1;
+}
+
+// ----- 3D panes -----
+
+function reviewEnsureSides() {
+ if (reviewSides) return reviewSides;
+ const make = (suffix) => ({
+ viewer: new GcodeViewer3D('reviewCanvas' + suffix, { parser: new GcodeParser(), review: true }),
+ label: reviewEl('reviewLabel' + suffix),
+ status: reviewEl('reviewStatus' + suffix),
+ ready: false,
+ });
+ reviewSides = { before: make('Before'), after: make('After') };
+ window.addEventListener('resize', () => {
+ if (!reviewIsOpen) return;
+ for (const side of [reviewSides.before, reviewSides.after]) {
+ side.viewer.resize();
+ }
+ reviewRenderViews();
+ });
+ return reviewSides;
+}
+
+// First line of the hunk on one side; for a pure insertion/deletion, the adjacent
+// unchanged row's index on that side so the layer is still correct for that file.
+function reviewHunkLine(hunk, key) {
+ const rows = reviewState.rows;
+ for (let i = hunk.startRow; i <= hunk.endRow; i++) {
+ if (rows[i][key] !== null) return rows[i][key];
+ }
+ for (const i of [hunk.startRow - 1, hunk.endRow + 1]) {
+ if (i >= 0 && i < rows.length && rows[i].kind === 'same') return rows[i][key];
+ }
+ return null;
+}
+
+// Blank the canvas so a frame from an earlier review never shows under a new label
+function reviewClearSide(side) {
+ const gl = side.viewer.gl;
+ if (!gl) return;
+ const isDark = document.documentElement.getAttribute('data-theme') !== 'light';
+ gl.clearColor(isDark ? 0.067 : 0.878, isDark ? 0.071 : 0.882, isDark ? 0.078 : 0.894, 1.0);
+ gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
+}
+
+function reviewLastLayerNumber(p) {
+ return p.layers.length ? p.layers[p.layers.length - 1].number : null;
+}
+
+function reviewRenderSide(side, lineIdx, fallbackLayer) {
+ if (!side.ready) return;
+ const p = side.viewer.parser;
+ let layerNum = lineIdx === null ? null : layerForLine(p.layers, lineIdx);
+ if (layerNum === null) layerNum = fallbackLayer;
+ if (layerNum === null || p.layers.length === 0) {
+ side.label.textContent = 'No layers';
+ reviewClearSide(side);
+ return;
+ }
+ const layer = p.getLayerByNumber(layerNum);
+ side.label.textContent = `Layer ${layerNum}` + (layer && layer.zHeight ? ` (Z ${Number(layer.zHeight).toFixed(2)})` : '');
+ side.viewer.maxVisibleLayer = layerNum;
+ side.viewer.render(layerNum);
+ return layerNum;
+}
+
+function reviewRenderViews() {
+ if (!reviewIsOpen || !reviewSides) return;
+ const { before, after } = reviewSides;
+ const hunk = reviewState.hunks[reviewState.currentHunk];
+ const beforeIdx = hunk ? reviewHunkLine(hunk, 'beforeIdx') : null;
+ const afterIdx = hunk ? reviewHunkLine(hunk, 'afterIdx') : null;
+ const bl = before.ready && beforeIdx !== null ? layerForLine(before.viewer.parser.layers, beforeIdx) : null;
+ const al = after.ready && afterIdx !== null ? layerForLine(after.viewer.parser.layers, afterIdx) : null;
+ if (!hunk) {
+ // No changes: show the top of the finished part on each side
+ reviewRenderSide(before, null, before.ready ? reviewLastLayerNumber(before.viewer.parser) : null);
+ reviewRenderSide(after, null, after.ready ? reviewLastLayerNumber(after.viewer.parser) : null);
+ return;
+ }
+ // With no line on a side at all, fall back to the other side's layer number
+ reviewRenderSide(before, beforeIdx, al);
+ reviewRenderSide(after, afterIdx, bl);
+}
+
+function reviewSideReady(side) {
+ side.ready = true;
+ side.status.textContent = '';
+ side.viewer.resize();
+ side.viewer.fitBounds();
+ reviewRenderViews();
+}
+
+async function reviewParseSide(side, lines, session, highlightSet, cachedParser) {
+ side.ready = false;
+ side.label.textContent = '';
+ side.viewer.clearBuffers();
+ side.viewer.setHighlight(highlightSet, REVIEW_HIGHLIGHT_COLOR);
+ // Empty placeholder until parsing finishes, so the viewer never caches half-parsed geometry
+ side.viewer.parser = new GcodeParser();
+ const p = cachedParser || new GcodeParser();
+ if (!cachedParser) {
+ const large = lines.length > REVIEW_LARGE_FILE_LINES;
+ if (large) side.status.textContent = 'Parsing... 0%';
+ try {
+ await p.parseAsync(lines.join('\n'), 'review', pct => {
+ if (large && session === reviewSession) side.status.textContent = `Parsing... ${(pct * 100).toFixed(0)}%`;
+ });
+ } catch (err) {
+ console.error('Review parse failed:', err);
+ if (session === reviewSession) side.status.textContent = '3D view unavailable';
+ return null;
+ }
+ }
+ if (session !== reviewSession || !reviewIsOpen) return null;
+ side.viewer.parser = p;
+ return p;
+}
+
+async function reviewStartViews(session, beforeLines, afterLines, sets) {
+ const sides = reviewEnsureSides();
+ for (const side of [sides.before, sides.after]) {
+ side.viewer.resize();
+ side.label.textContent = '';
+ side.status.textContent = 'Parsing...';
+ side.ready = false;
+ reviewClearSide(side);
+ }
+ const cached = reviewBeforeCache && reviewBeforeCache.lines === beforeLines ? reviewBeforeCache.parser : null;
+ // Sides are independent: a failure on one must not block or hide the other
+ await Promise.all([
+ reviewRunSide(sides.before, beforeLines, session, sets.removedBefore, cached, (p) => {
+ if (!cached) reviewBeforeCache = { lines: beforeLines, parser: p };
+ }),
+ reviewRunSide(sides.after, afterLines, session, sets.addedAfter, null, null),
+ ]);
+}
+
+async function reviewRunSide(side, lines, session, highlightSet, cachedParser, onParsed) {
+ try {
+ const p = await reviewParseSide(side, lines, session, highlightSet, cachedParser);
+ if (!p) return; // stale session, or parse failure already reported
+ if (onParsed) onParsed(p);
+ reviewSideReady(side);
+ } catch (err) {
+ console.error('Review view failed:', err);
+ if (session === reviewSession) side.status.textContent = '3D view unavailable';
+ }
+}
+
+function reviewGoToHunk(index) {
+ const total = reviewState.hunks.length;
+ if (total === 0) return;
+ reviewState.currentHunk = Math.max(0, Math.min(total - 1, index));
+ const hunk = reviewState.hunks[reviewState.currentHunk];
+ const list = reviewEl('reviewList');
+ list.scrollTop = Math.max(0, (hunk.startRow - REVIEW_CONTEXT_ROWS) * REVIEW_ROW_HEIGHT);
+ reviewUpdateHeader();
+ reviewRenderRows();
+ reviewRenderViews();
+}
+
+function openReview() {
+ if (reviewIsOpen) return;
+ if (!originalLines) {
+ showToast('Load a valid G-code file before exporting', 'warning');
+ return;
+ }
+ if (reparsing || loadInProgress) {
+ showToast('Wait for the file to finish loading before exporting', 'warning');
+ return;
+ }
+ const afterLines = modifier.applyAll(parser.lines, parser);
+ const beforeLines = originalLines;
+ const recoveryMod = modifier.modifications.find(m => m.type === 'recovery');
+ const exportBaseName = parser.fileName.replace(/\.gcode$/i, '');
+ const exportSuffix = recoveryMod ? `_recovery_L${recoveryMod.resumeLayer}` : '_modified';
+ const { rows, hunks } = diffLines(beforeLines, afterLines);
+ reviewState = { rows, hunks, currentHunk: hunks.length ? 0 : -1, beforeLines, afterLines, exportBaseName, exportSuffix };
+ reviewIsOpen = true;
+
+ reviewEl('reviewOverlay').classList.add('active');
+ reviewEl('reviewSpacer').style.height = (rows.length * REVIEW_ROW_HEIGHT) + 'px';
+ reviewEl('reviewList').scrollTop = 0;
+ reviewUpdateHeader();
+ reviewRenderRows();
+ if (hunks.length) reviewGoToHunk(0);
+ reviewEl('reviewContinueBtn').focus();
+ reviewStartViews(++reviewSession, beforeLines, afterLines, changedLineSets(rows));
+}
+
+function closeReview() {
+ if (!reviewIsOpen) return;
+ reviewIsOpen = false;
+ reviewSession++; // invalidate any parse still running
+ if (reviewSides) {
+ for (const side of [reviewSides.before, reviewSides.after]) {
+ side.ready = false;
+ side.label.textContent = '';
+ side.status.textContent = '';
+ side.viewer.parser = new GcodeParser();
+ side.viewer.setHighlight(null);
+ }
+ }
+ reviewEl('reviewOverlay').classList.remove('active');
+ reviewEl('reviewRows').innerHTML = '';
+ reviewState = { rows: [], hunks: [], currentHunk: -1, beforeLines: [], afterLines: [] };
+}
+
+function reviewNext() {
+ if (reviewIsOpen) reviewGoToHunk(reviewState.currentHunk + 1);
+}
+
+function reviewPrev() {
+ if (reviewIsOpen) reviewGoToHunk(reviewState.currentHunk - 1);
+}
+
+function continueExport() {
+ if (!reviewIsOpen) return;
+ const { afterLines, exportBaseName, exportSuffix } = reviewState;
+ closeReview();
+ performExport(afterLines, exportBaseName, exportSuffix);
+}
+
+// ----- Keyboard -----
+
+const REVIEW_BUTTON_IDS = ['reviewPrevBtn', 'reviewNextBtn', 'reviewBackBtn', 'reviewContinueBtn'];
+const REVIEW_KEY_SCROLL_ROWS = 3;
+
+function reviewTrapTab(e) {
+ const buttons = REVIEW_BUTTON_IDS.map(reviewEl).filter(b => b && !b.disabled);
+ if (buttons.length === 0) return;
+ const at = buttons.indexOf(document.activeElement);
+ let next;
+ if (at === -1) next = e.shiftKey ? buttons.length - 1 : 0;
+ else next = (at + (e.shiftKey ? -1 : 1) + buttons.length) % buttons.length;
+ buttons[next].focus();
+}
+
+function reviewScrollListByKey(key) {
+ const list = reviewEl('reviewList');
+ const page = Math.max(REVIEW_ROW_HEIGHT, list.clientHeight - 2 * REVIEW_ROW_HEIGHT);
+ const step = REVIEW_ROW_HEIGHT * REVIEW_KEY_SCROLL_ROWS;
+ if (key === 'ArrowUp') list.scrollTop -= step;
+ else if (key === 'ArrowDown') list.scrollTop += step;
+ else if (key === 'PageUp') list.scrollTop -= page;
+ else if (key === 'PageDown') list.scrollTop += page;
+ else if (key === 'Home') list.scrollTop = 0;
+ else if (key === 'End') list.scrollTop = list.scrollHeight;
+}
+
+// Key handling while the review is open. Only review navigation works; every
+// other key is cancelled so nothing can change the file under review.
+function reviewHandleKey(e) {
+ const mod = e.ctrlKey || e.metaKey;
+ if (e.key === 'Escape') { e.preventDefault(); closeReview(); return; }
+ if (e.key === 'Tab') { e.preventDefault(); reviewTrapTab(e); return; }
+ if (mod && !e.altKey && (e.key === 'c' || e.key === 'C' || e.key === 'a' || e.key === 'A')) return;
+ if ((e.key === 'Enter' || e.key === ' ') && !mod && !e.altKey) {
+ const t = e.target;
+ if (t && t.tagName === 'BUTTON' && t.closest && t.closest('#reviewOverlay')) return; // native activation
+ }
+ if (!mod && !e.altKey && ['ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End'].includes(e.key)) {
+ reviewScrollListByKey(e.key);
+ }
+ e.preventDefault();
+}
+
+// While the review is open a dropped file must not load, and must not make the
+// browser navigate away to it either.
+function reviewBlockDrag(e) {
+ if (!reviewIsOpen) return;
+ e.preventDefault();
+ e.stopPropagation();
+ if (e.type === 'drop') showToast('Close the export review before loading another file', 'warning');
+}
+window.addEventListener('dragover', reviewBlockDrag, true);
+window.addEventListener('drop', reviewBlockDrag, true);
diff --git a/src/ui.js b/src/ui.js
index c668a10..fdc3f99 100644
--- a/src/ui.js
+++ b/src/ui.js
@@ -1027,117 +1027,134 @@ function showOnboardHint(key, targetId, text) {
}
function loadFile(file) {
+ if (reviewIsOpen) {
+ showToast('Close the export review before loading another file', 'warning');
+ return;
+ }
const isBgcode = file.name.toLowerCase().endsWith('.bgcode');
const reader = new FileReader();
+ loadInProgress = true;
+ reader.onerror = () => { loadInProgress = false; };
+ reader.onabort = () => { loadInProgress = false; };
reader.onload = async (e) => {
- let text;
- if (isBgcode) {
- try {
- text = await decodeBgcode(e.target.result);
- } catch (err) {
- document.getElementById('parseProgress').style.display = 'none';
- document.getElementById('gcodePreview').innerHTML =
- `
Failed to decode bgcode file: ${err.message}
`;
- return;
- }
- } else {
- text = e.target.result;
+ try {
+ await loadFileFromReader(e, file, isBgcode);
+ } finally {
+ loadInProgress = false;
}
+ };
+ if (isBgcode) reader.readAsArrayBuffer(file);
+ else reader.readAsText(file);
+}
- const progressEl = document.getElementById('parseProgress');
- const barEl = document.getElementById('parseBar');
- const labelEl = document.getElementById('parseLabel');
- const lineCount = (text.match(/\n/g) || []).length;
-
- if (lineCount > 50000) {
- progressEl.style.display = '';
- labelEl.textContent = 'Parsing...';
- barEl.style.width = '0%';
+async function loadFileFromReader(e, file, isBgcode) {
+ let text;
+ if (isBgcode) {
+ try {
+ text = await decodeBgcode(e.target.result);
+ } catch (err) {
+ document.getElementById('parseProgress').style.display = 'none';
+ document.getElementById('gcodePreview').innerHTML =
+ `
Failed to decode bgcode file: ${err.message}
`;
+ return;
}
+ } else {
+ text = e.target.result;
+ }
- await parser.parseAsync(text, file.name, pct => {
- barEl.style.width = (pct * 100).toFixed(0) + '%';
- labelEl.textContent = `Parsing... ${(pct * 100).toFixed(0)}%`;
- });
+ const progressEl = document.getElementById('parseProgress');
+ const barEl = document.getElementById('parseBar');
+ const labelEl = document.getElementById('parseLabel');
+ const lineCount = (text.match(/\n/g) || []).length;
- progressEl.style.display = 'none';
+ if (lineCount > 50000) {
+ progressEl.style.display = '';
+ labelEl.textContent = 'Parsing...';
+ barEl.style.width = '0%';
+ }
- if (parser.layers.length === 0) {
- document.getElementById('gcodePreview').innerHTML =
- '
No layers detected in this file. It may not be a valid G-code file, or the slicer format is not yet supported.
';
- document.getElementById('parseProgress').style.display = 'none';
- return;
- }
- if (parser.skippedLines > 0) {
- showToast(parser.skippedLines + ' lines skipped during parsing', 'warning');
- }
+ originalLines = null;
+ await parser.parseAsync(text, file.name, pct => {
+ barEl.style.width = (pct * 100).toFixed(0) + '%';
+ labelEl.textContent = `Parsing... ${(pct * 100).toFixed(0)}%`;
+ });
- holeDetector.clearCache();
- viewer.clearBuffers();
- insertManager.clear();
- modifier.modifications = [];
- undoStack.push(modifier.modifications);
- holeDetectMode = false;
- document.getElementById('holeDetectToggle').classList.remove('active');
- document.getElementById('viewerCanvas').classList.remove('hole-mode');
- pauseSelectMode = false;
- selectedMove = null;
- hoveredMove = null;
- layerRangeStart = null;
- layerRangeEnd = null;
- document.getElementById('pauseSelectToggle').classList.remove('active');
- document.getElementById('viewerCanvas').style.cursor = '';
+ progressEl.style.display = 'none';
- // Update UI
- document.getElementById('fileName').textContent = file.name;
- const slicerName = parser.slicerType !== 'unknown' ? ` · ${parser.slicerType}` : '';
- document.getElementById('fileMeta').textContent =
- `${parser.layers.length} layers · ${parser.lines.length.toLocaleString()} lines${slicerName}`;
- document.getElementById('fileInfo').style.display = 'inline';
- document.getElementById('exportBtn').style.display = 'inline-flex';
- dropZone.innerHTML = `
Replace file `;
+ if (parser.layers.length === 0) {
+ document.getElementById('gcodePreview').innerHTML =
+ '
No layers detected in this file. It may not be a valid G-code file, or the slicer format is not yet supported.
';
+ document.getElementById('parseProgress').style.display = 'none';
+ return;
+ }
+ originalLines = parser.lines.slice();
+ if (parser.skippedLines > 0) {
+ showToast(parser.skippedLines + ' lines skipped during parsing', 'warning');
+ }
- renderLayerList();
- // Setup viewer
- viewer.resize();
- viewer.fitBounds();
- updateSlider();
- buildSections();
- initMotionTypeState();
- // Analyze motion (infer profile from parsed lines, then analyze)
- const inferredProfile = MotionAnalyzer.inferProfile(parser.lines);
- Object.assign(motionAnalyzer.profile, inferredProfile);
- motionAnalyzer.analyzeAllLayers(parser.layerMoves);
- // Populate printer profile panel with inferred values
- document.getElementById('profileAccel').value = motionAnalyzer.profile.acceleration;
- document.getElementById('profileJerk').value = motionAnalyzer.profile.jerk;
- document.getElementById('profileFirmware').value = motionAnalyzer.profile.firmwareType;
-
- // Run eager analysis (motion + flow only; others run on demand)
- runEagerAnalysis();
-
- // Update analysis panel material dropdown
- const inferredMaterial = inferMaterial(parser.lines);
- const materialSelect = document.getElementById('analysisMaterialType');
- if (materialSelect) materialSelect.value = inferredMaterial;
- const inferredLabel = document.getElementById('analysisMaterialInferred');
- if (inferredLabel) inferredLabel.textContent = '(inferred: ' + inferredMaterial + ')';
- const printerInfo = document.getElementById('analysisPrinterInfo');
- if (printerInfo) printerInfo.textContent = motionAnalyzer.profile.firmwareType + ' (inferred)';
-
- renderMotionLegend();
- if (parser.layers.length > 0) selectLayer(parser.layers[0].number);
- else renderFullPreview();
-
- showOnboardHint('visual', 'viewVisualBtn', 'Try the 3D Visual view');
-
- // Trigger guided tour on first file load
- if (!localStorage.getItem('gcode_tour_completed')) {
- setTimeout(function() { Onboarding.start(); }, 600);
- }
- };
- if (isBgcode) reader.readAsArrayBuffer(file);
- else reader.readAsText(file);
+ holeDetector.clearCache();
+ viewer.clearBuffers();
+ insertManager.clear();
+ modifier.modifications = [];
+ undoStack.push(modifier.modifications);
+ holeDetectMode = false;
+ document.getElementById('holeDetectToggle').classList.remove('active');
+ document.getElementById('viewerCanvas').classList.remove('hole-mode');
+ pauseSelectMode = false;
+ selectedMove = null;
+ hoveredMove = null;
+ layerRangeStart = null;
+ layerRangeEnd = null;
+ document.getElementById('pauseSelectToggle').classList.remove('active');
+ document.getElementById('viewerCanvas').style.cursor = '';
+
+ // Update UI
+ document.getElementById('fileName').textContent = file.name;
+ const slicerName = parser.slicerType !== 'unknown' ? ` · ${parser.slicerType}` : '';
+ document.getElementById('fileMeta').textContent =
+ `${parser.layers.length} layers · ${parser.lines.length.toLocaleString()} lines${slicerName}`;
+ document.getElementById('fileInfo').style.display = 'inline';
+ document.getElementById('exportBtn').style.display = 'inline-flex';
+ dropZone.innerHTML = `
Replace file `;
+
+ renderLayerList();
+ // Setup viewer
+ viewer.resize();
+ viewer.fitBounds();
+ updateSlider();
+ buildSections();
+ initMotionTypeState();
+ // Analyze motion (infer profile from parsed lines, then analyze)
+ const inferredProfile = MotionAnalyzer.inferProfile(parser.lines);
+ Object.assign(motionAnalyzer.profile, inferredProfile);
+ motionAnalyzer.analyzeAllLayers(parser.layerMoves);
+ // Populate printer profile panel with inferred values
+ document.getElementById('profileAccel').value = motionAnalyzer.profile.acceleration;
+ document.getElementById('profileJerk').value = motionAnalyzer.profile.jerk;
+ document.getElementById('profileFirmware').value = motionAnalyzer.profile.firmwareType;
+
+ // Run eager analysis (motion + flow only; others run on demand)
+ runEagerAnalysis();
+
+ // Update analysis panel material dropdown
+ const inferredMaterial = inferMaterial(parser.lines);
+ const materialSelect = document.getElementById('analysisMaterialType');
+ if (materialSelect) materialSelect.value = inferredMaterial;
+ const inferredLabel = document.getElementById('analysisMaterialInferred');
+ if (inferredLabel) inferredLabel.textContent = '(inferred: ' + inferredMaterial + ')';
+ const printerInfo = document.getElementById('analysisPrinterInfo');
+ if (printerInfo) printerInfo.textContent = motionAnalyzer.profile.firmwareType + ' (inferred)';
+
+ renderMotionLegend();
+ if (parser.layers.length > 0) selectLayer(parser.layers[0].number);
+ else renderFullPreview();
+
+ showOnboardHint('visual', 'viewVisualBtn', 'Try the 3D Visual view');
+
+ // Trigger guided tour on first file load
+ if (!localStorage.getItem('gcode_tour_completed')) {
+ setTimeout(function() { Onboarding.start(); }, 600);
+ }
}
// ===== LAYER LIST =====
@@ -3668,15 +3685,14 @@ function addInsertsForSelection() {
// ===== EXPORT =====
function exportGcode() {
if (parser.lines.length === 0) return;
+ openReview();
+}
- const modifiedLines = modifier.applyAll(parser.lines, parser);
- const text = modifiedLines.join('\n');
+// Writes exactly the lines/name the review showed; never re-derives from live state.
+function performExport(lines, baseName, suffix) {
+ const text = lines.join('\n');
const blob = new Blob([text], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
-
- const baseName = parser.fileName.replace(/\.gcode$/i, '');
- const recoveryMod = modifier.modifications.find(m => m.type === 'recovery');
- const suffix = recoveryMod ? `_recovery_L${recoveryMod.resumeLayer}` : '_modified';
const a = document.createElement('a');
a.href = url;
a.download = baseName + suffix + '.gcode';
diff --git a/src/viewer3d.js b/src/viewer3d.js
index 406d401..7a0e946 100644
--- a/src/viewer3d.js
+++ b/src/viewer3d.js
@@ -1,7 +1,12 @@
// ===== GCODE VIEWER (old Canvas 2D removed — now using GcodeViewer3D) =====
export class GcodeViewer3D {
- constructor(canvasId) {
+ constructor(canvasId, opts = {}) {
+ // Own parser (e.g. before/after review viewers); null falls back to the global parser
+ this.parser = opts.parser || null;
+ // Review mode: motion-type colors only, no global overlays, no edit interaction
+ this.review = !!opts.review;
+ this._highlight = null; // { lines: Set
, color: [r, g, b] } or null
this.canvas = document.getElementById(canvasId);
this.gl = this.canvas.getContext('webgl2', { preserveDrawingBuffer: true });
if (!this.gl) {
@@ -52,6 +57,17 @@ export class GcodeViewer3D {
this._setupInteraction();
}
+ // Parser backing this viewer (own parser if given, else the global one)
+ _p() {
+ return this.parser || parser;
+ }
+
+ // Mark moves whose lineIndex is in lineIndices with color; null clears highlighting
+ setHighlight(lineIndices, color) {
+ this._highlight = lineIndices && lineIndices.size > 0 ? { lines: lineIndices, color } : null;
+ this.clearBuffers();
+ }
+
// --- Shader source ---
static VS = `#version 300 es
uniform mat4 u_mvp;
@@ -151,10 +167,10 @@ export class GcodeViewer3D {
}
_getMVP() {
- const b = parser.bounds;
+ const b = this._p().bounds;
const cx = (b.minX + b.maxX) / 2;
const cy = (b.minY + b.maxY) / 2;
- const maxLayer = parser.layers[parser.layers.length - 1];
+ const maxLayer = this._p().layers[this._p().layers.length - 1];
const maxZ = maxLayer ? (maxLayer.zHeight || 0) : 0;
const cz = maxZ / 2;
@@ -278,13 +294,13 @@ export class GcodeViewer3D {
if (this.layerBuffers.has(layerNum)) return this.layerBuffers.get(layerNum);
const gl = this.gl;
- const moves = parser.layerMoves[layerNum];
+ const moves = this._p().layerMoves[layerNum];
if (!moves || moves.length === 0) {
this.layerBuffers.set(layerNum, null);
return null;
}
- const layer = parser.getLayerByNumber(layerNum);
+ const layer = this._p().getLayerByNumber(layerNum);
const z = layer?.zHeight || 0;
const halfW = 0.2; // half ribbon width in mm
@@ -294,7 +310,7 @@ export class GcodeViewer3D {
const travelVerts = [];
// Per-move offset tracking for simulation
const offsets = [];
- const isHeatmap = colorMode !== 'motion-type';
+ const isHeatmap = !this.review && colorMode !== 'motion-type';
const heatStats = isHeatmap ? getHeatmapLayerStats(layerNum) : null;
const overlayDef = isHeatmap && typeof analysisManager !== 'undefined'
? analysisManager.getSupportedOverlays().find(o => o.id === colorMode)
@@ -306,7 +322,9 @@ export class GcodeViewer3D {
// Skip hidden motion types (in motion-type mode) or non-extrusions (in heatmap mode)
const moveTypeUpper = move.extrude ? move.type.toUpperCase() : 'TRAVEL';
const normalizedMoveType = MOTION_TYPE_ALIASES[moveTypeUpper] || moveTypeUpper;
- if (!isHeatmap && motionTypeVisibility[normalizedMoveType] === false) {
+ // Review mode: a highlighted (changed) move is always shown, even if its type is hidden
+ const isHighlighted = this._highlight !== null && this._highlight.lines.has(move.lineIndex);
+ if (!isHeatmap && !isHighlighted && motionTypeVisibility[normalizedMoveType] === false) {
offsets.push({ triStart: ribbonVerts.length / 7, triCount: 0, lineStart: travelVerts.length / 7, lineCount: 0 });
continue;
}
@@ -315,7 +333,7 @@ export class GcodeViewer3D {
const lineStart = travelVerts.length / 7;
if (move.extrude) {
- const color = isHeatmap
+ const color = isHighlighted ? this._highlight.color : isHeatmap
? this._getHeatmapColor(getHeatmapValue(move, layerNum, moveIndex), heatStats.min, heatStats.max, invertColor)
: this._getTypeColor(move.type);
const dx = move.x2 - move.x1;
@@ -347,10 +365,10 @@ export class GcodeViewer3D {
}
} else {
// Travel move as a line (use custom color if set)
- const tc = this._getTypeColor('TRAVEL');
+ const tc = isHighlighted ? this._highlight.color : this._getTypeColor('TRAVEL');
travelVerts.push(
- move.x1, move.y1, z, tc[0], tc[1], tc[2], 0.3,
- move.x2, move.y2, z, tc[0], tc[1], tc[2], 0.3,
+ move.x1, move.y1, z, tc[0], tc[1], tc[2], isHighlighted ? 1.0 : 0.3,
+ move.x2, move.y2, z, tc[0], tc[1], tc[2], isHighlighted ? 1.0 : 0.3,
);
}
@@ -421,7 +439,7 @@ export class GcodeViewer3D {
if (!compareMoves || compareMoves.length === 0) return;
// Use the current layer's Z for overlay alignment so both layers appear at the same height
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = layer ? layer.zHeight || 0 : 0;
const halfW = 0.2;
const tint = LayerComparison.compareTint;
@@ -528,7 +546,7 @@ export class GcodeViewer3D {
// Get grid dimensions from any available warp grid (dimensions are identical
// across all layers since the grid is computed from global extrusion bounds).
- const layerNums = Object.keys(parser.layerMoves).map(Number).sort((a, b) => a - b);
+ const layerNums = Object.keys(this._p().layerMoves).map(Number).sort((a, b) => a - b);
let refGrid = thermalEngine.getWarpGrid(layerNum);
if (!refGrid) {
for (const ln of layerNums) {
@@ -651,7 +669,7 @@ export class GcodeViewer3D {
if (cumCount[i] > 0) avgWarp[i] = cumWarpSum[i] / cumCount[i];
}
- const layerInfo = parser.layers.find(l => l.number === ln);
+ const layerInfo = this._p().layers.find(l => l.number === ln);
const z = layerInfo ? (layerInfo.zHeight || 0) : 0;
// Build vertex grid: each corner averages warp from up to 4 surrounding cells
@@ -775,7 +793,7 @@ export class GcodeViewer3D {
_drawGrid(mvp) {
const gl = this.gl;
- const b = parser.bounds;
+ const b = this._p().bounds;
const verts = [];
const step = 10; // 10mm grid
const isLight = document.documentElement.getAttribute('data-theme') === 'light';
@@ -809,7 +827,7 @@ export class GcodeViewer3D {
render(layerNum) {
const gl = this.gl;
- if (this._broken || !this._w || !this._h || !parser.bounds) return;
+ if (this._broken || !this._w || !this._h || !this._p().bounds) return;
this.currentLayer = layerNum;
@@ -910,7 +928,7 @@ export class GcodeViewer3D {
}
// Draw leading highlight on the active move
- const moves = parser.layerMoves[ln];
+ const moves = this._p().layerMoves[ln];
if (moves && this.simMoveIndex < moves.length) {
const activeMove = moves[this.simMoveIndex];
if (activeMove) {
@@ -925,8 +943,8 @@ export class GcodeViewer3D {
} else {
// Normal rendering (no simulation)
- const compareActive = typeof LayerComparison !== 'undefined' && LayerComparison.active;
- const inRange = typeof layerRangeStart !== 'undefined' && layerRangeStart !== null &&
+ const compareActive = !this.review && typeof LayerComparison !== 'undefined' && LayerComparison.active;
+ const inRange = !this.review && typeof layerRangeStart !== 'undefined' && layerRangeStart !== null &&
typeof layerRangeEnd !== 'undefined' && layerRangeEnd !== null &&
ln >= layerRangeStart && ln <= layerRangeEnd;
const layerAlpha = isCurrent ? (compareActive ? 0.5 : -1.0) : (inRange ? -1.0 : dimAlpha);
@@ -954,6 +972,12 @@ export class GcodeViewer3D {
}
}
+ // Review viewers show only the toolpath; skip the global overlays
+ if (this.review) {
+ this._drawClipPlaneQuad(mvp);
+ return;
+ }
+
// Draw comparison overlay layer
this._drawCompareLayer(mvp);
@@ -974,10 +998,10 @@ export class GcodeViewer3D {
FlowArrows.setZoomLevel(this.cam.zoom);
- const moves = parser.layerMoves[this.currentLayer];
+ const moves = this._p().layerMoves[this.currentLayer];
if (!moves || moves.length === 0) return;
- const layer = parser.layers[this.currentLayer];
+ const layer = this._p().layers[this.currentLayer];
const z = layer ? layer.zHeight || 0 : 0;
// Determine color function based on current color mode
@@ -1026,13 +1050,13 @@ export class GcodeViewer3D {
_drawModMarkers(mvp) {
const gl = this.gl;
- const b = parser.bounds;
+ const b = this._p().bounds;
const mods = modifier.modifications.filter(m => m.layer !== Infinity && m.layer !== 'end');
if (mods.length === 0) return;
const verts = [];
for (const mod of mods) {
- const layer = parser.getLayerByNumber(mod.layer);
+ const layer = this._p().getLayerByNumber(mod.layer);
if (!layer || layer.zHeight == null) continue;
const z = layer.zHeight + 0.05;
@@ -1087,7 +1111,7 @@ export class GcodeViewer3D {
if (selected.length === 0) return;
// Draw highlight at the currently viewed layer's Z height
- const currentLayerData = parser.getLayerByNumber(this.currentLayer);
+ const currentLayerData = this._p().getLayerByNumber(this.currentLayer);
if (!currentLayerData || currentLayerData.zHeight == null) return;
const z = currentLayerData.zHeight + 0.2;
@@ -1215,7 +1239,7 @@ export class GcodeViewer3D {
}
findNearestMove(worldX, worldY, layerNum) {
- const moves = parser.layerMoves[layerNum];
+ const moves = this._p().layerMoves[layerNum];
if (!moves || moves.length === 0) return null;
let best = null, bestDist = Infinity;
for (const move of moves) {
@@ -1239,7 +1263,7 @@ export class GcodeViewer3D {
_drawMeasurement(mvp) {
if (!measureMode || MeasureTool.points.length === 0) return;
const gl = this.gl;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const layerZ = layer?.zHeight || 0;
const data = MeasureTool.getDrawData(layerZ);
if (!data) return;
@@ -1263,7 +1287,7 @@ export class GcodeViewer3D {
_drawMoveHighlight(mvp, move, color, alpha, lineOffset, crosshairSize) {
const gl = this.gl;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = (layer?.zHeight || 0) + 0.1;
const c = [...color.slice(0, 3), alpha];
@@ -1396,7 +1420,7 @@ export class GcodeViewer3D {
}
_getMovesWithSameLineIndex(move, layerNum) {
- const moves = parser.layerMoves[layerNum];
+ const moves = this._p().layerMoves[layerNum];
if (!moves) return [move];
const target = move.lineIndex;
const matches = moves.filter(m => m.lineIndex === target);
@@ -1429,8 +1453,8 @@ export class GcodeViewer3D {
const bz = nx * ty - ny * tx;
// Size the quad to cover the print bounds
- const b = parser.bounds;
- const maxLayer = parser.layers[parser.layers.length - 1];
+ const b = this._p().bounds;
+ const maxLayer = this._p().layers[this._p().layers.length - 1];
const maxZ = maxLayer ? (maxLayer.zHeight || 0) : 0;
const size = Math.max(b.maxX - b.minX, b.maxY - b.minY, maxZ || 50) * 1.2;
@@ -1517,12 +1541,12 @@ export class GcodeViewer3D {
c.addEventListener('contextmenu', e => {
e.preventDefault();
- if (typeof ContextMenu === 'undefined') return;
+ if (this.review || typeof ContextMenu === 'undefined') return;
const rect = c.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
- const layer = parser.layers[this.currentLayer];
+ const layer = this._p().layers[this.currentLayer];
const z = layer ? layer.zHeight || 0 : 0;
const pt = this.screenToLayerPoint(sx, sy, z);
@@ -1572,7 +1596,7 @@ export class GcodeViewer3D {
// Hover preview for pause select mode
let hoverRafPending = false;
c.addEventListener('mousemove', e => {
- if (!pauseSelectMode || this._dragging) return;
+ if (this.review || !pauseSelectMode || this._dragging) return;
if (hoverRafPending) return;
hoverRafPending = true;
requestAnimationFrame(() => {
@@ -1581,7 +1605,7 @@ export class GcodeViewer3D {
const rect = c.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = layer?.zHeight || 0;
const pt = this.screenToLayerPoint(sx, sy, z);
if (!pt) { if (hoveredMove) { hoveredMove = null; this.render(this.currentLayer); } return; }
@@ -1596,7 +1620,7 @@ export class GcodeViewer3D {
// Hover preview for edit mode
let editHoverRaf = false;
c.addEventListener('mousemove', e => {
- if (!editMode || this._dragging) return;
+ if (this.review || !editMode || this._dragging) return;
if (editHoverRaf) return;
editHoverRaf = true;
requestAnimationFrame(() => {
@@ -1605,7 +1629,7 @@ export class GcodeViewer3D {
const rect = c.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = layer?.zHeight || 0;
const pt = this.screenToLayerPoint(sx, sy, z);
if (!pt) {
@@ -1624,7 +1648,7 @@ export class GcodeViewer3D {
let heatmapTip = null;
let heatmapHoverRaf = false;
c.addEventListener('mousemove', e => {
- if (colorMode === 'motion-type' || pauseSelectMode || this._dragging) {
+ if (this.review || colorMode === 'motion-type' || pauseSelectMode || this._dragging) {
if (heatmapTip) heatmapTip.classList.remove('visible');
return;
}
@@ -1641,13 +1665,13 @@ export class GcodeViewer3D {
}
const rect = c.getBoundingClientRect();
const sx = mx - rect.left, sy = my - rect.top;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = layer?.zHeight || 0;
const pt = this.screenToLayerPoint(sx, sy, z);
if (!pt) { heatmapTip.classList.remove('visible'); return; }
const move = this.findNearestMove(pt.x, pt.y, this.currentLayer);
if (!move) { heatmapTip.classList.remove('visible'); return; }
- const moves = parser.layerMoves[this.currentLayer];
+ const moves = this._p().layerMoves[this.currentLayer];
const moveIndex = moves ? moves.indexOf(move) : -1;
if (colorMode === 'actual-speed' || colorMode === 'speed-delta') {
const result = motionAnalyzer.getResult(this.currentLayer, moveIndex);
@@ -1687,7 +1711,7 @@ export class GcodeViewer3D {
// Stats hover — active when visual view is showing and no special mode is active
let statsHoverRaf = false;
c.addEventListener('mousemove', e => {
- if (this._dragging) return;
+ if (this.review || this._dragging) return;
if (pauseSelectMode || editMode) return;
if (statsHoverRaf) return;
statsHoverRaf = true;
@@ -1698,7 +1722,7 @@ export class GcodeViewer3D {
const rect = c.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
if (!layer) return;
const z = layer.zHeight || 0;
const pt = this.screenToLayerPoint(sx, sy, z);
@@ -1717,7 +1741,7 @@ export class GcodeViewer3D {
dist = Math.hypot(pt.x - (move.x1 + t * dx), pt.y - (move.y1 + t * dy));
}
if (dist < 2) {
- const moves = parser.layerMoves[this.currentLayer];
+ const moves = this._p().layerMoves[this.currentLayer];
const moveIndex = moves ? moves.indexOf(move) : -1;
StatsOverlay.showHoverInfo(move, this.currentLayer, moveIndex);
} else {
@@ -1727,6 +1751,7 @@ export class GcodeViewer3D {
});
c.addEventListener('mouseleave', () => {
+ if (this.review) return;
if (editHoveredMove) {
editHoveredMove = null;
if (editMode) this.render(this.currentLayer);
@@ -1817,11 +1842,11 @@ export class GcodeViewer3D {
c.addEventListener('click', e => {
- if (this._mouseMoved) return;
+ if (this.review || this._mouseMoved) return;
const rect = c.getBoundingClientRect();
const sx = e.clientX - rect.left;
const sy = e.clientY - rect.top;
- const layer = parser.getLayerByNumber(this.currentLayer);
+ const layer = this._p().getLayerByNumber(this.currentLayer);
const z = layer?.zHeight || 0;
// Measurement mode click
diff --git a/test/line-diff.test.js b/test/line-diff.test.js
new file mode 100644
index 0000000..bf0a038
--- /dev/null
+++ b/test/line-diff.test.js
@@ -0,0 +1,131 @@
+import { describe, it } from 'node:test';
+import assert from 'node:assert';
+import { diffLines, changedLineSets, layerForLine } from '../src/line-diff.js';
+
+const kinds = (rows) => rows.map((r) => r.kind);
+
+describe('diffLines', () => {
+ it('identical arrays produce no hunks', () => {
+ const a = ['a', 'b', 'c'];
+ const { rows, hunks } = diffLines(a, [...a]);
+ assert.strictEqual(hunks.length, 0);
+ assert.ok(rows.every((r) => r.kind === 'same'));
+ });
+
+ it('inserted line is one add row and one hunk', () => {
+ const { rows, hunks } = diffLines(['a', 'b'], ['a', 'X', 'b']);
+ assert.deepStrictEqual(kinds(rows), ['same', 'add', 'same']);
+ assert.strictEqual(rows[1].afterIdx, 1);
+ assert.strictEqual(rows[1].beforeIdx, null);
+ assert.deepStrictEqual(hunks, [{ startRow: 1, endRow: 1 }]);
+ });
+
+ it('edited line is del then add in a single hunk', () => {
+ const { rows, hunks } = diffLines(['a', 'b', 'c'], ['a', 'B', 'c']);
+ assert.deepStrictEqual(kinds(rows), ['same', 'del', 'add', 'same']);
+ assert.strictEqual(rows[1].afterIdx, null);
+ assert.deepStrictEqual(hunks, [{ startRow: 1, endRow: 2 }]);
+ });
+
+ it('removed first line and appended last line', () => {
+ const { rows, hunks } = diffLines(['a', 'b'], ['b', 'c']);
+ assert.deepStrictEqual(kinds(rows), ['del', 'same', 'add']);
+ assert.strictEqual(hunks.length, 2);
+ });
+
+ it('trailing empty line is handled', () => {
+ const { rows } = diffLines(['a', ''], ['a', '', 'x']);
+ const adds = rows.filter((r) => r.kind === 'add');
+ assert.strictEqual(adds.length, 1);
+ assert.strictEqual(adds[0].afterIdx, 2);
+ });
+});
+
+describe('changedLineSets', () => {
+ it('maps rows to indices', () => {
+ const { rows } = diffLines(['a', 'b', 'c'], ['a', 'B', 'c']);
+ const { removedBefore, addedAfter } = changedLineSets(rows);
+ assert.deepStrictEqual(removedBefore, new Set([1]));
+ assert.deepStrictEqual(addedAfter, new Set([1]));
+ });
+});
+
+describe('layerForLine', () => {
+ const layers = [{ number: 0, startLine: 10 }, { number: 1, startLine: 50 }];
+
+ it('maps lines to the layer containing them', () => {
+ assert.strictEqual(layerForLine(layers, 5), 0);
+ assert.strictEqual(layerForLine(layers, 10), 0);
+ assert.strictEqual(layerForLine(layers, 49), 0);
+ assert.strictEqual(layerForLine(layers, 50), 1);
+ assert.strictEqual(layerForLine(layers, 9999), 1);
+ });
+
+ it('returns null for no layers', () => {
+ assert.strictEqual(layerForLine([], 3), null);
+ });
+});
+
+describe('diffLines anchoring', () => {
+ it('recovery-shaped diff with rewritten lines keeps most unchanged lines as same', () => {
+ const before = [];
+ for (let i = 0; i < 500; i++) before.push('; header ' + i);
+ for (let i = 0; i < 100000; i++) before.push('G1 X' + i + ' Y' + i + ' E0.1');
+ const remaining = before.slice(500 + 60000);
+ const after = before.slice(0, 500).concat(remaining.map((l, i) => (i % 25 === 0 ? l + ' Z9.9' : l)));
+ const t0 = performance.now();
+ const { rows, hunks } = diffLines(before, after);
+ const elapsed = performance.now() - t0;
+ const same = rows.filter((r) => r.kind === 'same').length;
+ assert.ok(hunks.length > 1, 'hunks ' + hunks.length);
+ assert.ok(same > remaining.length * 0.9, 'same ' + same);
+ assert.ok(elapsed < 4000, 'took ' + elapsed + 'ms');
+ });
+
+ it('edit distance beyond the cap falls back to block replace with consistent indices', () => {
+ const before = ['start'];
+ const after = ['start'];
+ for (let i = 0; i < 2500; i++) { before.push('b' + (i % 3)); after.push('a' + (i % 3)); }
+ before.push('end');
+ after.push('end');
+ const { rows, hunks } = diffLines(before, after);
+ assert.strictEqual(hunks.length, 1);
+ let bi = 0;
+ let ai = 0;
+ for (const r of rows) {
+ if (r.kind === 'same') { assert.strictEqual(r.beforeIdx, bi++); assert.strictEqual(r.afterIdx, ai++); }
+ else if (r.kind === 'del') { assert.strictEqual(r.beforeIdx, bi++); assert.strictEqual(r.afterIdx, null); }
+ else { assert.strictEqual(r.beforeIdx, null); assert.strictEqual(r.afterIdx, ai++); }
+ }
+ assert.strictEqual(bi, before.length);
+ assert.strictEqual(ai, after.length);
+ });
+});
+
+describe('diffLines performance', () => {
+ it('200k lines with 50 inserted completes under 1500ms', () => {
+ const before = [];
+ for (let i = 0; i < 200000; i++) before.push('G1 X' + i);
+ const after = [];
+ for (let i = 0; i < before.length; i++) {
+ if (i % 4000 === 100) after.push('; inserted ' + i);
+ after.push(before[i]);
+ }
+ const t0 = performance.now();
+ const { rows } = diffLines(before, after);
+ const elapsed = performance.now() - t0;
+ assert.strictEqual(rows.filter((r) => r.kind === 'add').length, 50);
+ assert.ok(elapsed < 1500, 'took ' + elapsed + 'ms');
+ });
+
+ it('recovery-shaped diff (60000-line prefix removed from 100000 lines) completes under 4000ms and yields 60000 del rows', () => {
+ const before = [];
+ for (let i = 0; i < 100000; i++) before.push('G1 X' + i);
+ const after = before.slice(60000);
+ const t0 = performance.now();
+ const { rows } = diffLines(before, after);
+ const elapsed = performance.now() - t0;
+ assert.strictEqual(rows.filter((r) => r.kind === 'del').length, 60000);
+ assert.ok(elapsed < 4000, 'took ' + elapsed + 'ms');
+ });
+});