From 6ab8ce406941ff3daca749002cda9e043f6576df Mon Sep 17 00:00:00 2001 From: Yaro Kasear Date: Wed, 7 Jan 2026 10:03:17 -0600 Subject: [PATCH] Viewer is working, in theory! --- inventory/static/js/components/draw.js | 37 ++++-- inventory/templates/components/draw.html | 20 +++- inventory/templates/testing.html | 139 +++++++++++++++++++++++ 3 files changed, 184 insertions(+), 12 deletions(-) diff --git a/inventory/static/js/components/draw.js b/inventory/static/js/components/draw.js index 7342ce9..53f664c 100644 --- a/inventory/static/js/components/draw.js +++ b/inventory/static/js/components/draw.js @@ -299,8 +299,26 @@ function initGridWidget(root, opts = {}) { resizeAndSetupCanvas(); redrawAll(); + // ✅ viewer mode still needs to respond to layout/resize if (mode !== 'editor') { - return { setDoc, redraw: redrawAll }; + const ro = new ResizeObserver(() => resizeAndSetupCanvas()); + ro.observe(gridEl); + ro.observe(gridWrapEl); + + // optional, but helps with viewport changes + window.addEventListener('resize', resizeAndSetupCanvas, { passive: true }); + + // also optional: if it's inside tabs/collapses, kick one extra redraw + requestAnimationFrame(() => resizeAndSetupCanvas()); + + return { + setDoc, + redraw: redrawAll, + destroy() { + ro.disconnect(); + window.removeEventListener('resize', resizeAndSetupCanvas); + } + }; } const MAX_HISTORY = 100; @@ -344,6 +362,12 @@ function initGridWidget(root, opts = {}) { let currentStrokeOpacity = clamp01(strokeOpacityEl?.value ?? 1, 1); let currentStrokeWidth = Number(strokeWidthEl?.value ?? 0.12) || 0.12; + selectedColor = colorEl?.value || '#000000'; + if (dotSVGEl) { + const circle = dotSVGEl.querySelector('circle'); + circle?.setAttribute('fill', selectedColor); + } + let currentShape = null; const history = [structuredClone(shapes)]; let historyIndex = 0 @@ -887,15 +911,6 @@ function initGridWidget(root, opts = {}) { const simplified = simplifyRDP(coarse, epsilon); if (simplified.length >= 2) { - const renderPoints = catmullRomResample(simplified, { - alpha: 0.5, - samplesPerSeg: 10, - maxSamplesPerSeg: 40, - minSamplesPerSeg: 6, - closed: false, - maxOutputPoints: 4000 // also fix your option name, see below - }); - finalShape = { type: 'path', points: simplified, @@ -922,7 +937,7 @@ function initGridWidget(root, opts = {}) { if (finalShape) commit([...shapes, finalShape]); currentShape = null; - renderAllWithPreview(null); // clean final render + renderAllWithPreview(null); } gridEl.addEventListener('pointerup', finishPointer); diff --git a/inventory/templates/components/draw.html b/inventory/templates/components/draw.html index ac23e28..97ddcfe 100644 --- a/inventory/templates/components/draw.html +++ b/inventory/templates/components/draw.html @@ -244,4 +244,22 @@ {% endmacro %} {% macro viewWidget(uid, json) %} -{% endmacro %} +
+
+ + + + + +
+
+ +
+
+ + +
+{% endmacro %} \ No newline at end of file diff --git a/inventory/templates/testing.html b/inventory/templates/testing.html index 713f4d2..9d78a17 100644 --- a/inventory/templates/testing.html +++ b/inventory/templates/testing.html @@ -7,10 +7,149 @@ {% endblock %} {% block main %} +{% set jsonImage %} +{ + "version": 1, + "cellSize": 8, + "shapes": [ + { + "type": "ellipse", + "x": 5, + "y": 5, + "w": 15, + "h": 15, + "color": "#ffff00", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 5, + "y": 5, + "w": 15, + "h": 15, + "color": "#000000", + "fill": false, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 8, + "y": 9, + "w": 3, + "h": 3, + "color": "#ffffff", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 14, + "y": 9, + "w": 3, + "h": 3, + "color": "#ffffff", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 8, + "y": 9, + "w": 3, + "h": 3, + "color": "#000000", + "fill": false, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 14, + "y": 9, + "w": 3, + "h": 3, + "color": "#000000", + "fill": false, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 9, + "y": 10, + "w": 1, + "h": 1, + "color": "#000000", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 15, + "y": 10, + "w": 1, + "h": 1, + "color": "#000000", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "ellipse", + "x": 11, + "y": 13, + "w": 3, + "h": 5, + "color": "#000000", + "fill": true, + "fillOpacity": 1, + "strokeOpacity": 1, + "strokeWidth": 0.12 + }, + { + "type": "line", + "x1": 7, + "y1": 9, + "x2": 10, + "y2": 8, + "color": "#000000", + "strokeWidth": 0.12, + "strokeOpacity": 1 + }, + { + "type": "line", + "x1": 15, + "y1": 8, + "x2": 18, + "y2": 9, + "color": "#000000", + "strokeWidth": 0.12, + "strokeOpacity": 1 + } + ] +} +{% endset %}
{{ draw.drawWidget('test1') }}
+
+ {{ draw.viewWidget('test2', jsonImage) }} +
{% endblock %}