diff --git a/ea-scripts/Expand rectangles horizontally keep text centered.md b/ea-scripts/Expand rectangles horizontally keep text centered.md new file mode 100644 index 0000000..a6fa13d --- /dev/null +++ b/ea-scripts/Expand rectangles horizontally keep text centered.md @@ -0,0 +1,75 @@ +/* + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-download-raw.jpg) + +Download this file and save to your Obsidian Vault including the first line, or open it in "Raw" and copy the entire contents to Obsidian. + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif) + +This script expands the width of the selected rectangles until they are all the same width and keep the text centered. + +See documentation for more details: +https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html + +```javascript +*/ + +const elements = ea.getViewSelectedElements(); +const topGroups = ea.getMaximumGroups(elements); + +const groupWidths = topGroups + .map((g) => + g.reduce( + (pre, cur, i) => { + if (i === 0) { + return { + minLeft: cur.x, + maxRight: cur.x + cur.width, + index: i, + }; + } else { + return { + minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, + maxRight: + cur.x + cur.width > pre.maxRight + ? cur.x + cur.width + : pre.maxRight, + index: i, + }; + } + }, + { minLeft: 0, maxRight: 0 } + ) + ) + .map((r) => { + r.width = r.maxRight - r.minLeft; + return r; + }); + +const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); + +for (var i = 0; i < topGroups.length; i++) { + const rects = topGroups[i] + .filter((el) => el.type === "rectangle") + .sort((lha, rha) => lha.x - rha.x); + const texts = topGroups[i] + .filter((el) => el.type === "text") + .sort((lha, rha) => lha.x - rha.x); + const groupWith = groupWidths[i].width; + if (groupWith < maxGroupWidth) { + const distance = maxGroupWidth - groupWith; + const perRectDistance = distance / rects.length; + for (var j = 0; j < rects.length; j++) { + const rect = rects[j]; + rect.x = rect.x + perRectDistance * j - perRectDistance / 2; + rect.width += perRectDistance; + } + for (var j = 0; j < texts.length; j++) { + const text = texts[j]; + text.x = text.x + perRectDistance * j; + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(); diff --git a/ea-scripts/Expand rectangles horizontally.md b/ea-scripts/Expand rectangles horizontally.md new file mode 100644 index 0000000..4cf9104 --- /dev/null +++ b/ea-scripts/Expand rectangles horizontally.md @@ -0,0 +1,76 @@ +/* + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-download-raw.jpg) + +Download this file and save to your Obsidian Vault including the first line, or open it in "Raw" and copy the entire contents to Obsidian. + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif) + +This script expands the width of the selected rectangles until they are all the same width. + +See documentation for more details: +https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html + +```javascript +*/ + +const elements = ea.getViewSelectedElements(); +const topGroups = ea.getMaximumGroups(elements); + +const groupWidths = topGroups + .map((g) => + g.reduce( + (pre, cur, i) => { + if (i === 0) { + return { + minLeft: cur.x, + maxRight: cur.x + cur.width, + index: i, + }; + } else { + return { + minLeft: cur.x < pre.minLeft ? cur.x : pre.minLeft, + maxRight: + cur.x + cur.width > pre.maxRight + ? cur.x + cur.width + : pre.maxRight, + index: i, + }; + } + }, + { minLeft: 0, maxRight: 0 } + ) + ) + .map((r) => { + r.width = r.maxRight - r.minLeft; + return r; + }); + +const maxGroupWidth = Math.max(...groupWidths.map((g) => g.width)); + +for (var i = 0; i < topGroups.length; i++) { + const rects = topGroups[i] + .filter((el) => el.type === "rectangle") + .sort((lha, rha) => lha.x - rha.x); + const texts = topGroups[i] + .filter((el) => el.type === "text") + .sort((lha, rha) => lha.x - rha.x); + const groupWith = groupWidths[i].width; + if (groupWith < maxGroupWidth) { + const distance = maxGroupWidth - groupWith; + const perRectDistance = distance / rects.length; + for (var j = 0; j < rects.length; j++) { + const rect = rects[j]; + rect.x = rect.x + perRectDistance * j; + rect.width += perRectDistance; + } + for (var j = 0; j < texts.length; j++) { + const text = texts[j]; + text.x = text.x + perRectDistance * j; + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(); + diff --git a/ea-scripts/Expand rectangles vertically keep text centered.md b/ea-scripts/Expand rectangles vertically keep text centered.md new file mode 100644 index 0000000..ec34d73 --- /dev/null +++ b/ea-scripts/Expand rectangles vertically keep text centered.md @@ -0,0 +1,75 @@ +/* + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-download-raw.jpg) + +Download this file and save to your Obsidian Vault including the first line, or open it in "Raw" and copy the entire contents to Obsidian. + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif) + +This script expands the height of the selected rectangles until they are all the same height and keep the text centered. + +See documentation for more details: +https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html + +```javascript +*/ + +const elements = ea.getViewSelectedElements(); +const topGroups = ea.getMaximumGroups(elements); + +const groupHeights = topGroups + .map((g) => + g.reduce( + (pre, cur, i) => { + if (i === 0) { + return { + minTop: cur.y, + maxBottom: cur.y + cur.height, + index: i, + }; + } else { + return { + minTop: cur.y < pre.minTop ? cur.y : pre.minTop, + maxBottom: + cur.y + cur.height > pre.maxBottom + ? cur.y + cur.height + : pre.maxBottom, + index: i, + }; + } + }, + { minTop: 0, maxBottom: 0 } + ) + ) + .map((r) => { + r.height = r.maxBottom - r.minTop; + return r; + }); + +const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); + +for (var i = 0; i < topGroups.length; i++) { + const rects = topGroups[i] + .filter((el) => el.type === "rectangle") + .sort((lha, rha) => lha.y - rha.y); + const texts = topGroups[i] + .filter((el) => el.type === "text") + .sort((lha, rha) => lha.y - rha.y); + const groupWith = groupHeights[i].height; + if (groupWith < maxGroupHeight) { + const distance = maxGroupHeight - groupWith; + const perRectDistance = distance / rects.length; + for (var j = 0; j < rects.length; j++) { + const rect = rects[j]; + rect.y = rect.y + perRectDistance * j - perRectDistance / 2; + rect.height += perRectDistance; + } + for (var j = 0; j < texts.length; j++) { + const text = texts[j]; + text.y = text.y + perRectDistance * j; + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(); diff --git a/ea-scripts/Expand rectangles vertically.md b/ea-scripts/Expand rectangles vertically.md new file mode 100644 index 0000000..ab0c837 --- /dev/null +++ b/ea-scripts/Expand rectangles vertically.md @@ -0,0 +1,73 @@ +/* + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-download-raw.jpg) + +Download this file and save to your Obsidian Vault including the first line, or open it in "Raw" and copy the entire contents to Obsidian. + +![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif) + +This script expands the height of the selected rectangles until they are all the same height. + +```javascript +*/ + +const elements = ea.getViewSelectedElements(); +const topGroups = ea.getMaximumGroups(elements); + +const groupHeights = topGroups + .map((g) => + g.reduce( + (pre, cur, i) => { + if (i === 0) { + return { + minTop: cur.y, + maxBottom: cur.y + cur.height, + index: i, + }; + } else { + return { + minTop: cur.y < pre.minTop ? cur.y : pre.minTop, + maxBottom: + cur.y + cur.height > pre.maxBottom + ? cur.y + cur.height + : pre.maxBottom, + index: i, + }; + } + }, + { minTop: 0, maxBottom: 0 } + ) + ) + .map((r) => { + r.height = r.maxBottom - r.minTop; + return r; + }); + +const maxGroupHeight = Math.max(...groupHeights.map((g) => g.height)); + +for (var i = 0; i < topGroups.length; i++) { + const rects = topGroups[i] + .filter((el) => el.type === "rectangle") + .sort((lha, rha) => lha.y - rha.y); + const texts = topGroups[i] + .filter((el) => el.type === "text") + .sort((lha, rha) => lha.y - rha.y); + const groupWith = groupHeights[i].height; + if (groupWith < maxGroupHeight) { + const distance = maxGroupHeight - groupWith; + const perRectDistance = distance / rects.length; + for (var j = 0; j < rects.length; j++) { + const rect = rects[j]; + rect.y = rect.y + perRectDistance * j; + rect.height += perRectDistance; + } + for (var j = 0; j < texts.length; j++) { + const text = texts[j]; + text.y = text.y + perRectDistance * j; + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(); + diff --git a/images/scripts-expand-rectangles.gif b/images/scripts-expand-rectangles.gif new file mode 100644 index 0000000..c059419 Binary files /dev/null and b/images/scripts-expand-rectangles.gif differ