add webui
This commit is contained in:
253
webui/static/app.js
Normal file
253
webui/static/app.js
Normal file
@@ -0,0 +1,253 @@
|
||||
(function () {
|
||||
const fileInput = document.getElementById('fileInput');
|
||||
const preview = document.getElementById('preview');
|
||||
const previewPlaceholder = document.getElementById('previewPlaceholder');
|
||||
const statusFile = document.getElementById('statusFile');
|
||||
const statusPlotter = document.getElementById('statusPlotter');
|
||||
const printMessage = document.getElementById('printMessage');
|
||||
const scaleDims = document.getElementById('scaleDims');
|
||||
const plotterInfo = {
|
||||
port: document.getElementById('plotterPort'),
|
||||
baud: document.getElementById('plotterBaud'),
|
||||
winsizeUnits: document.getElementById('plotterWinsizeUnits'),
|
||||
winsizeMm: document.getElementById('plotterWinsizeMm'),
|
||||
bounds: document.getElementById('plotterBounds')
|
||||
};
|
||||
const buttons = {
|
||||
flip: document.getElementById('btnFlip'),
|
||||
rotate90: document.getElementById('btnRotate90'),
|
||||
rotate180: document.getElementById('btnRotate180'),
|
||||
scaleUp: document.getElementById('btnScaleUp'),
|
||||
scaleDown: document.getElementById('btnScaleDown'),
|
||||
centralize: document.getElementById('btnCentralize'),
|
||||
scaleToBounds: document.getElementById('btnScaleToBounds'),
|
||||
print: document.getElementById('btnPrint'),
|
||||
checkPlotter: document.getElementById('btnCheckPlotter')
|
||||
};
|
||||
|
||||
function setMessage(el, text, type) {
|
||||
el.textContent = text || '';
|
||||
el.className = 'message' + (type ? ' ' + type : '');
|
||||
}
|
||||
|
||||
function updatePreview() {
|
||||
const w = Math.min(800, window.innerWidth - 80);
|
||||
const h = Math.min(600, window.innerHeight - 120);
|
||||
preview.src = '/api/svg?width=' + w + '&height=' + h + '&scale=1&_=' + Date.now();
|
||||
}
|
||||
|
||||
function updateDimensions() {
|
||||
if (!scaleDims) return;
|
||||
fetch('/api/dimensions')
|
||||
.then(function (r) {
|
||||
if (!r.ok) throw new Error();
|
||||
return r.json();
|
||||
})
|
||||
.then(function (d) {
|
||||
scaleDims.innerHTML = d.width + ' × ' + d.height + ' <span class="unit">(≈ ' + d.width_mm + ' × ' + d.height_mm + ' mm)</span>';
|
||||
})
|
||||
.catch(function () {
|
||||
scaleDims.innerHTML = '— × — <span class="unit">(≈ — × — mm)</span>';
|
||||
});
|
||||
}
|
||||
|
||||
function updatePlotterInfo() {
|
||||
if (!plotterInfo.port) return;
|
||||
fetch('/api/plotter_info')
|
||||
.then(function (r) { return r.ok ? r.json() : Promise.reject(); })
|
||||
.then(function (d) {
|
||||
plotterInfo.port.textContent = d.port || '—';
|
||||
plotterInfo.baud.textContent = d.baudrate ? d.baudrate + ' baud' : '—';
|
||||
if (d.winsize_units) {
|
||||
plotterInfo.winsizeUnits.textContent = d.winsize_units.width + ' × ' + d.winsize_units.height;
|
||||
} else {
|
||||
plotterInfo.winsizeUnits.textContent = '— × —';
|
||||
}
|
||||
if (d.winsize_mm) {
|
||||
plotterInfo.winsizeMm.textContent = d.winsize_mm.width + ' × ' + d.winsize_mm.height + ' mm';
|
||||
} else {
|
||||
plotterInfo.winsizeMm.textContent = '— × —';
|
||||
}
|
||||
if (d.boundaries) {
|
||||
var b = d.boundaries;
|
||||
plotterInfo.bounds.textContent = b.xmin + ',' + b.ymin + ' … ' + b.xmax + ',' + b.ymax;
|
||||
} else {
|
||||
plotterInfo.bounds.textContent = '—';
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
plotterInfo.port.textContent = '—';
|
||||
plotterInfo.baud.textContent = '—';
|
||||
plotterInfo.winsizeUnits.textContent = '— × —';
|
||||
plotterInfo.winsizeMm.textContent = '— × —';
|
||||
plotterInfo.bounds.textContent = '—';
|
||||
});
|
||||
}
|
||||
|
||||
function setLoaded(loaded) {
|
||||
if (loaded) {
|
||||
preview.classList.add('loaded');
|
||||
previewPlaceholder.classList.add('hidden');
|
||||
Object.keys(buttons).forEach(function (k) {
|
||||
var b = buttons[k];
|
||||
if (b) b.disabled = false;
|
||||
});
|
||||
} else {
|
||||
preview.classList.remove('loaded');
|
||||
previewPlaceholder.classList.remove('hidden');
|
||||
preview.src = '';
|
||||
Object.keys(buttons).forEach(function (k) {
|
||||
var b = buttons[k];
|
||||
if (b) b.disabled = true;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function refreshStatus() {
|
||||
fetch('/api/status')
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (data) {
|
||||
statusFile.textContent = data.has_file ? (data.filename || 'File loaded') : 'No file';
|
||||
statusPlotter.textContent = 'Plotter: ' + (data.plotter_ready ? 'Ready' : 'Not connected');
|
||||
statusPlotter.classList.toggle('ready', data.plotter_ready);
|
||||
statusPlotter.classList.toggle('not-ready', !data.plotter_ready);
|
||||
updatePlotterInfo();
|
||||
if (data.has_file) {
|
||||
setLoaded(true);
|
||||
updatePreview();
|
||||
updateDimensions();
|
||||
} else {
|
||||
setLoaded(false);
|
||||
if (scaleDims) scaleDims.innerHTML = '— × — <span class="unit">(≈ — × — mm)</span>';
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
statusFile.textContent = 'No file';
|
||||
statusPlotter.textContent = 'Plotter: —';
|
||||
setLoaded(false);
|
||||
if (scaleDims) scaleDims.innerHTML = '— × — <span class="unit">(≈ — × — mm)</span>';
|
||||
});
|
||||
}
|
||||
|
||||
fileInput.addEventListener('change', function () {
|
||||
const file = this.files && this.files[0];
|
||||
if (!file) return;
|
||||
preview.src = '';
|
||||
preview.classList.remove('loaded');
|
||||
previewPlaceholder.classList.remove('hidden');
|
||||
previewPlaceholder.textContent = 'Loading…';
|
||||
setMessage(printMessage, '');
|
||||
const form = new FormData();
|
||||
form.append('file', file);
|
||||
fetch('/api/upload', {
|
||||
method: 'POST',
|
||||
body: form
|
||||
})
|
||||
.then(function (r) {
|
||||
if (!r.ok) return r.json().then(function (d) { throw new Error(d.error || 'Upload failed'); });
|
||||
return r.json();
|
||||
})
|
||||
.then(function () {
|
||||
previewPlaceholder.textContent = 'Upload an HPGL file to preview';
|
||||
refreshStatus();
|
||||
})
|
||||
.catch(function (e) {
|
||||
previewPlaceholder.textContent = 'Upload an HPGL file to preview';
|
||||
setMessage(printMessage, e.message || 'Upload failed', 'error');
|
||||
refreshStatus();
|
||||
});
|
||||
});
|
||||
|
||||
function transform(action, payload) {
|
||||
setMessage(printMessage, '');
|
||||
var body = payload || { action: action };
|
||||
fetch('/api/transform', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(body)
|
||||
})
|
||||
.then(function (r) {
|
||||
if (!r.ok) return r.json().then(function (d) { throw new Error(d.error || 'Transform failed'); });
|
||||
return r.json();
|
||||
})
|
||||
.then(function () {
|
||||
updatePreview();
|
||||
updateDimensions();
|
||||
})
|
||||
.catch(function (e) {
|
||||
setMessage(printMessage, e.message || 'Transform failed', 'error');
|
||||
});
|
||||
}
|
||||
|
||||
buttons.flip.addEventListener('click', function () { transform('flip'); });
|
||||
buttons.rotate90.addEventListener('click', function () { transform('rotate', { action: 'rotate', angle: 90 }); });
|
||||
buttons.rotate180.addEventListener('click', function () { transform('rotate', { action: 'rotate', angle: 180 }); });
|
||||
buttons.scaleUp.addEventListener('click', function () { transform('scale', { action: 'scale', factor: 1.25 }); });
|
||||
buttons.scaleDown.addEventListener('click', function () { transform('scale', { action: 'scale', factor: 0.8 }); });
|
||||
buttons.centralize.addEventListener('click', function () { transform('centralize'); });
|
||||
if (buttons.scaleToBounds) {
|
||||
buttons.scaleToBounds.addEventListener('click', function () {
|
||||
setMessage(printMessage, '');
|
||||
fetch('/api/status')
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (data) {
|
||||
if (!data.plotter_ready) {
|
||||
setMessage(printMessage, 'Plotter not connected.', 'error');
|
||||
return;
|
||||
}
|
||||
transform('scale_to_bounds', { action: 'scale_to_bounds' });
|
||||
})
|
||||
.catch(function () {
|
||||
setMessage(printMessage, 'Plotter not connected.', 'error');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
buttons.print.addEventListener('click', function () {
|
||||
setMessage(printMessage, 'Sending to plotter…');
|
||||
fetch('/api/print', { method: 'POST' })
|
||||
.then(function (r) {
|
||||
return r.json().then(function (data) {
|
||||
if (!r.ok) throw new Error(data.error || 'Print failed');
|
||||
return data;
|
||||
});
|
||||
})
|
||||
.then(function () {
|
||||
setMessage(printMessage, 'Sent to plotter.', 'success');
|
||||
})
|
||||
.catch(function (e) {
|
||||
setMessage(printMessage, e.message || 'Print failed', 'error');
|
||||
});
|
||||
});
|
||||
|
||||
if (buttons.checkPlotter) {
|
||||
buttons.checkPlotter.addEventListener('click', function () {
|
||||
setMessage(printMessage, 'Checking plotter…');
|
||||
fetch('/api/status')
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (data) {
|
||||
statusFile.textContent = data.has_file ? (data.filename || 'File loaded') : 'No file';
|
||||
statusPlotter.textContent = 'Plotter: ' + (data.plotter_ready ? 'Ready' : 'Not connected');
|
||||
statusPlotter.classList.toggle('ready', data.plotter_ready);
|
||||
statusPlotter.classList.toggle('not-ready', !data.plotter_ready);
|
||||
updatePlotterInfo();
|
||||
setMessage(printMessage, data.plotter_ready ? 'Plotter: Ready' : 'Plotter: Not connected', data.plotter_ready ? 'success' : 'error');
|
||||
})
|
||||
.catch(function () {
|
||||
statusPlotter.textContent = 'Plotter: —';
|
||||
statusPlotter.classList.remove('ready');
|
||||
statusPlotter.classList.add('not-ready');
|
||||
updatePlotterInfo();
|
||||
setMessage(printMessage, 'Plotter: Not connected', 'error');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
preview.addEventListener('load', function () {
|
||||
previewPlaceholder.classList.add('hidden');
|
||||
});
|
||||
|
||||
refreshStatus();
|
||||
updatePlotterInfo();
|
||||
setInterval(updatePlotterInfo, 10000);
|
||||
})();
|
||||
305
webui/static/style.css
Normal file
305
webui/static/style.css
Normal file
@@ -0,0 +1,305 @@
|
||||
:root {
|
||||
--bg: #ffffff;
|
||||
--surface: #f5f5f6;
|
||||
--border: #d1d5db;
|
||||
--text: #1f2937;
|
||||
--muted: #6b7280;
|
||||
--accent: #2563eb;
|
||||
--accent-hover: #1d4ed8;
|
||||
--danger: #dc2626;
|
||||
--success: #16a34a;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
font-family: system-ui, -apple-system, sans-serif;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.app {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 1rem;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
margin-bottom: 1rem;
|
||||
padding-bottom: 0.75rem;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
.header h1 {
|
||||
margin: 0;
|
||||
font-size: 1.5rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.status {
|
||||
display: flex;
|
||||
gap: 1rem;
|
||||
font-size: 0.875rem;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.status-plotter.ready {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.status-plotter.not-ready {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.main {
|
||||
display: grid;
|
||||
grid-template-columns: 260px 1fr;
|
||||
gap: 1.5rem;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
@media (max-width: 700px) {
|
||||
.main {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.upload-section {
|
||||
padding: 1rem;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.upload-label {
|
||||
display: block;
|
||||
font-weight: 500;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.file-input {
|
||||
display: block;
|
||||
width: 100%;
|
||||
padding: 0.5rem;
|
||||
font-size: 0.875rem;
|
||||
background: #fff;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.file-input::file-selector-button {
|
||||
margin-right: 0.5rem;
|
||||
padding: 0.25rem 0.5rem;
|
||||
background: var(--accent);
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.hint {
|
||||
margin: 0.5rem 0 0;
|
||||
font-size: 0.75rem;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.transform-section h2,
|
||||
.print-section h2,
|
||||
.scale-section h2 {
|
||||
margin: 0 0 0.5rem;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 600;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.scale-section {
|
||||
padding: 1rem;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.scale-section .dims {
|
||||
color: var(--text);
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.scale-section .unit {
|
||||
color: var(--muted);
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
.transform-section {
|
||||
padding: 1rem;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.btn-group {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 0.5rem 0.75rem;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
transition: background 0.15s, border-color 0.15s;
|
||||
}
|
||||
|
||||
.btn:hover:not(:disabled) {
|
||||
background: var(--border);
|
||||
border-color: var(--accent);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-print {
|
||||
width: 100%;
|
||||
padding: 0.75rem 1rem;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
background: var(--accent);
|
||||
border: none;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.plotter-info {
|
||||
margin-top: 1rem;
|
||||
padding-top: 1rem;
|
||||
border-top: 1px solid var(--border);
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.plotter-info-row {
|
||||
margin: 0.25rem 0;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.plotter-info-row strong {
|
||||
color: var(--muted);
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.btn-print:hover:not(:disabled) {
|
||||
background: var(--accent-hover);
|
||||
}
|
||||
|
||||
.btn-check-plotter {
|
||||
width: 100%;
|
||||
margin-top: 0.5rem;
|
||||
padding: 0.5rem 0.75rem;
|
||||
font-size: 0.875rem;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
color: var(--text);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.btn-check-plotter:hover {
|
||||
background: var(--border);
|
||||
border-color: var(--accent);
|
||||
}
|
||||
|
||||
.print-section {
|
||||
padding: 1rem;
|
||||
background: var(--surface);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.message {
|
||||
margin: 0.5rem 0 0;
|
||||
font-size: 0.875rem;
|
||||
min-height: 1.25rem;
|
||||
}
|
||||
|
||||
.message.error {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
.message.success {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.preview-section {
|
||||
min-height: 400px;
|
||||
background: #fafafa;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.preview-wrap {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
min-height: 400px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.preview {
|
||||
max-width: 100%;
|
||||
max-height: 70vh;
|
||||
object-fit: contain;
|
||||
display: none;
|
||||
}
|
||||
|
||||
.preview.loaded {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.preview-placeholder {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
color: var(--muted);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.preview-placeholder.hidden {
|
||||
display: none;
|
||||
}
|
||||
Reference in New Issue
Block a user