Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const FILES = [
'e-repair.js',
'parser.js',
'modifier.js',
'line-diff.js',
'gcode-dictionary.js',
'material-profiles.js', // NEW - no deps
'motion-analyzer.js',
Expand All @@ -33,6 +34,7 @@ const FILES = [
'context-menu.js',
'pa-tuner.js',
'viewer3d.js',
'review.js',
'tooltips.js',
'command-palette.js',
'onboarding.js',
Expand Down
943 changes: 794 additions & 149 deletions gcode-modifier.html

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ let layerRangeEnd = null;
let crossSectionActive = false;
let crossSectionFlipped = false;
let reparsing = false;
let loadInProgress = false; // true from file pick until loadFile finishes (any exit path)
const _storedColor = localStorage.getItem('gcode_highlight_color');
let highlightColor = /^#[0-9a-fA-F]{6}$/.test(_storedColor) ? _storedColor : '#ff3333';

Expand Down Expand Up @@ -448,6 +449,12 @@ document.addEventListener('DOMContentLoaded', () => {
window.addEventListener('keydown', e => {
const isInput = e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT';

// Export review overlay: Esc closes without exporting; see reviewHandleKey: only review navigation and copy/select keys work, everything else is swallowed so the reviewed file cannot change
if (reviewIsOpen) {
reviewHandleKey(e);
return;
}

// Undo/redo (works even in inputs)
if ((e.ctrlKey || e.metaKey) && e.key === 'z' && !e.shiftKey) { e.preventDefault(); performUndo(); return; }
if ((e.ctrlKey || e.metaKey) && (e.key === 'y' || (e.key === 'z' && e.shiftKey))) { e.preventDefault(); performRedo(); return; }
Expand Down
63 changes: 63 additions & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,38 @@
.shortcuts-card kbd { background: var(--surface2); border: 1px solid var(--border); border-radius: 4px; padding: 2px 6px; font-family: var(--mono); font-size: 11px; }
.shortcuts-card .close-btn { float: right; background: none; border: none; color: var(--text-dim); font-size: 18px; cursor: pointer; }

/* REVIEW OVERLAY */
.review-overlay { display: none; position: fixed; inset: 0; background: var(--bg); z-index: 150; flex-direction: column; }
.review-overlay.active { display: flex; }
.review-header { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.review-title { font-size: 15px; font-weight: 700; color: var(--text); }
.review-counter { font-size: 13px; font-weight: 600; color: var(--accent); font-family: var(--mono); min-width: 110px; }
.review-spacer-flex { flex: 1; }
.review-btn { padding: 6px 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface2); color: var(--text); font-size: 12px; font-weight: 600; cursor: pointer; }
.review-btn:hover:not(:disabled) { border-color: var(--accent); }
.review-btn:disabled { opacity: .4; cursor: default; }
.review-btn-primary { background: var(--accent); border-color: var(--accent); color: #000; }
.review-cols-head, .review-views { display: grid; grid-template-columns: 1fr 1fr; }
.review-col-title { padding: 6px 12px; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); background: var(--surface); border-bottom: 1px solid var(--border); }
.review-col-title + .review-col-title { border-left: 1px solid var(--border); }
.review-view { flex: none; height: 38vh; min-height: 180px; max-height: 55vh; background: var(--surface2); border-bottom: 1px solid var(--border); position: relative; overflow: hidden; }
.review-view + .review-view { border-left: 1px solid var(--border); }
.review-view canvas { position: absolute; inset: 0; display: block; }
.review-view-label { position: absolute; top: 6px; left: 8px; padding: 2px 8px; border-radius: 4px; background: var(--surface); border: 1px solid var(--border); font-size: 11px; font-weight: 700; font-family: var(--mono); color: var(--text); pointer-events: none; }
.review-view-status { position: absolute; bottom: 6px; left: 8px; font-size: 11px; color: var(--text-dim); pointer-events: none; }
.review-list { min-height: 120px; }
.review-list { flex: 1; overflow: auto; position: relative; font-family: var(--mono); font-size: 12px; }
.review-spacer { position: relative; width: 100%; }
.review-rows { position: absolute; left: 0; right: 0; top: 0; }
.review-row { display: grid; grid-template-columns: 1fr 1fr; height: 18px; line-height: 18px; }
.review-cell { display: flex; overflow: hidden; white-space: pre; color: var(--text); }
.review-cell + .review-cell { border-left: 1px solid var(--border); }
.review-ln { flex: 0 0 56px; padding-right: 8px; text-align: right; color: var(--text-dim); user-select: none; }
.review-text { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.review-filler { background: var(--surface2); }
.review-add { background: color-mix(in srgb, var(--green) 22%, transparent); }
.review-del { background: color-mix(in srgb, var(--red) 22%, transparent); }

/* RESPONSIVE */
@media (max-width: 1100px) {
.panel-left { width: 200px; min-width: 160px; }
Expand Down Expand Up @@ -1744,6 +1776,37 @@ <h3>Keyboard Shortcuts</h3>
</div>
</div>

<div class="review-overlay" id="reviewOverlay" role="dialog" aria-modal="true" aria-label="Review changes before export">
<div class="review-header">
<span class="review-title">Review changes</span>
<span class="review-counter" id="reviewCounter">No changes</span>
<button class="review-btn" id="reviewPrevBtn" onclick="reviewPrev()">Previous</button>
<button class="review-btn" id="reviewNextBtn" onclick="reviewNext()">Next</button>
<span class="review-spacer-flex"></span>
<button class="review-btn" id="reviewBackBtn" onclick="closeReview()">Back</button>
<button class="review-btn review-btn-primary" id="reviewContinueBtn" onclick="continueExport()">Continue export</button>
</div>
<div class="review-cols-head">
<div class="review-col-title">Before (as loaded)</div>
<div class="review-col-title">After (exported)</div>
</div>
<div class="review-views">
<div class="review-view" id="reviewViewBefore">
<canvas id="reviewCanvasBefore"></canvas>
<span class="review-view-label" id="reviewLabelBefore"></span>
<span class="review-view-status" id="reviewStatusBefore"></span>
</div>
<div class="review-view" id="reviewViewAfter">
<canvas id="reviewCanvasAfter"></canvas>
<span class="review-view-label" id="reviewLabelAfter"></span>
<span class="review-view-status" id="reviewStatusAfter"></span>
</div>
</div>
<div class="review-list" id="reviewList" onscroll="reviewScheduleRender()">
<div class="review-spacer" id="reviewSpacer"><div class="review-rows" id="reviewRows"></div></div>
</div>
</div>

<div id="commandPalette" class="command-palette-overlay" style="display:none;">
<div class="command-palette-card">
<input type="text" id="cmdPaletteInput" placeholder="Type a command..." autocomplete="off" spellcheck="false" />
Expand Down
185 changes: 185 additions & 0 deletions src/line-diff.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading
Loading