From 56ae6baf2b5d5fe8aa1af08442004ed8e2c1847a Mon Sep 17 00:00:00 2001 From: sevenflash Date: Thu, 1 Sep 2022 20:12:58 +0800 Subject: [PATCH] Commit at September 1 --- ea-scripts/Grid Selected Images.md | 51 ++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/ea-scripts/Grid Selected Images.md b/ea-scripts/Grid Selected Images.md index 290701b..7b89988 100644 --- a/ea-scripts/Grid Selected Images.md +++ b/ea-scripts/Grid Selected Images.md @@ -7,21 +7,52 @@ This script arranges selected images into compact grid view, removing gaps in-be */ try { - const els = ea.getViewSelectedElements(); + let els = ea.getViewSelectedElements().filter(el => el.type == 'image'); + + new Notice(els.length); + + if (els.length == 0) throw new Error('No image elements selected'); const bounds = ea.getBoundingBox(els); const { topX, topY, width, height } = bounds; - const topLeftEl = els.reduce((prev, curr) => prev.x + prev.y < curr.x + curr.y ? prev : curr); + els.sort((a, b) => a.x + a.y < b.x + b.y); - const rows = width / topLeftEl.width; + const areaAvailable = width * height; + + let elWidth = els[0].width; + let elHeight = els[0].height; + + if (elWidth * elHeight > areaAvailable) { + while (elWidth * elHeight > areaAvailable) { + elWidth /= 1.1; + elHeight /= 1.1; + } + } else if (elWidth * elHeight < areaAvailable) { + while (elWidth * elHeight > areaAvailable) { + elWidth *= 1.1; + elHeight *= 1.1; + } + } + + const rows = (width - elWidth) / elWidth; let row = 0, column = 0; - for (let i = 0; i < els.length; i++) { - els[i].x = topX + (topLeftEl.width * row); - els[i].y = topY + (topLeftEl.height * column); - els[i].width = topLeftEl.width; - els[i].height = topLeftEl.height; + for (const element of els) { + element.x = topX + (elWidth * row); + element.y = topY + (elHeight * column); + + if (element.width > elWidth) { + while (element.width >= elWidth) { + element.width /= 1.1; + element.height /= 1.1; + } + } else if (element.width < elWidth) { + while (element.width <= elWidth) { + element.width *= 1.1; + element.height *= 1.1; + } + } row++; if (row > rows) { @@ -32,5 +63,5 @@ try { ea.addElementsToView(false, true, true); } catch (err) { - new Notice(err.toString()) -} + _ = new Notice(err.toString()) +} \ No newline at end of file