diff --git a/inventory/static/css/components/draw.css b/inventory/static/css/components/draw.css index c225373..d221157 100644 --- a/inventory/static/css/components/draw.css +++ b/inventory/static/css/components/draw.css @@ -45,6 +45,27 @@ opacity: .95; } +.grid-widget[data-mode="viewer"] { + display: inline-block; + height: auto; + min-width: 0; +} + +.grid-widget[data-mode="viewer"] .grid-wrap { + flex: 0 0 auto; + min-height: 0; + width: fit-content; + overflow: visible; +} + +.grid-widget[data-mode="viewer"] [data-grid]{ + position: relative; + width: auto; + height: auto; + inset: auto; + cursor: default; +} + /* WIDE: 1 row */ @container (min-width: 750px) { .grid-widget [data-toolbar].toolbar { diff --git a/inventory/static/js/components/draw.js b/inventory/static/js/components/draw.js index 53f664c..ec515f1 100644 --- a/inventory/static/js/components/draw.js +++ b/inventory/static/js/components/draw.js @@ -293,6 +293,24 @@ function initGridWidget(root, opts = {}) { saveDoc({ version: Number(d.version) || 1, cellSize, shapes }); } + if (mode !== 'editor') { + const b = getShapesBounds(shapes); + + const padCells = 0.5; + + const wCells = b ? (b.maxX - b.minX + padCells * 2) : 10; + const hCells = b ? (b.maxY - b.minY + padCells * 2) : 10; + + const wPx = Math.ceil(wCells * cellSize); + const wPy = Math.ceil(hCells * cellSize); + + gridEl.style.width = `${wPx}px`; + gridEl.style.height = `${wPy}px`; + + gridWrapEl.style.width = `${wPx}px`; + gridWrapEl.style.height = `${wPy}px`; + } + resizeAndSetupCanvas(); } @@ -442,6 +460,37 @@ function initGridWidget(root, opts = {}) { window.activeGridWidget = api; }, { capture: true }); + function getShapesBounds(shapes) { + let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity; + + const expand = (x1, y1, x2, y2) => { + minX = Math.min(minX, x1); + minY = Math.min(minY, y1); + maxX = Math.max(maxX, x2); + maxY = Math.max(maxY, y2); + }; + + for (const s of shapes) { + if (!s) continue; + + if (s.type === 'rect' || s.type === 'ellipse') { + expand(s.x, s.y, s.x + s.w, s.y + s.h); + } else if (s.type === 'line') { + expand( + Math.min(s.x1, s.x2), Math.min(s.y1, s.y2), + Math.max(s.x1, s.x2), Math.max(s.y1, s.y2) + ); + } else if (s.type === 'path') { + const pts = (s.renderPoints?.length >= 2) ? s.renderPoints : s.points; + if (!pts?.length) continue; + for (const p of pts) expand(p.x, p.y, p.x, p.y); + } + } + + if (!Number.isFinite(minX)) return null; + return { minX, minY, maxX, maxY }; + } + function setGrid() { const type = getActiveType(); diff --git a/inventory/templates/components/draw.html b/inventory/templates/components/draw.html index 97ddcfe..bdce7d8 100644 --- a/inventory/templates/components/draw.html +++ b/inventory/templates/components/draw.html @@ -16,6 +16,18 @@ + + + - - - diff --git a/inventory/templates/testing.html b/inventory/templates/testing.html index 9d78a17..ea2c866 100644 --- a/inventory/templates/testing.html +++ b/inventory/templates/testing.html @@ -144,9 +144,11 @@ } {% endset %}