diff --git a/ea-scripts/Fixed inner distance.md b/ea-scripts/Fixed inner distance.md new file mode 100644 index 0000000..9a39c03 --- /dev/null +++ b/ea-scripts/Fixed inner distance.md @@ -0,0 +1,126 @@ +/* +![](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-fixed-inner-distance.png) + +This script arranges selected elements and groups with a fixed inner distance. + +Tips: You can use the `Box Selected Elements` and `Dimensions` scripts to create rectangles of the desired size, then use the `Change shape of selected elements` script to convert the rectangles to ellipses, and then use the `Fixed inner distance` script regains a desired inner distance. + +Inspiration: #394 + +See documentation for more details: +https://zsviczian.github.io/obsidian-excalidraw-plugin/ExcalidrawScriptsEngine.html + +```javascript +*/ +if(!ea.verifyMinimumPluginVersion || !ea.verifyMinimumPluginVersion("1.5.21")) { + new Notice("This script requires a newer version of Excalidraw. Please install the latest version."); + return; +} +settings = ea.getScriptSettings(); +//set default values on first run +if(!settings["Default distance"]) { + settings = { + "Prompt for distance?": true, + "Default distance" : { + value: 10, + description: "Fixed horizontal distance between centers" + }, + "Remember last distance?": false + }; + ea.setScriptSettings(settings); +} + +let distanceStr = settings["Default distance"].value.toString(); +const rememberLastDistance = settings["Remember last distance?"]; + +if(settings["Prompt for distance?"]) { + distanceStr = await utils.inputPrompt("distance?","number",distanceStr); +} + +const borders = ["top", "bottom", "left", "right"]; +const fromBorder = await utils.suggester(borders, borders, "from border?"); + +if(!fromBorder) { + return; +} + +const distance = parseInt(distanceStr); +if(isNaN(distance)) { + return; +} +if(rememberLastDistance) { + settings["Default distance"].value = distance; + ea.setScriptSettings(settings); +} +const elements=ea.getViewSelectedElements(); +const topGroups = ea.getMaximumGroups(elements) + .filter(els => !(els.length === 1 && els[0].type ==="arrow")); // ignore individual arrows + +if(topGroups.length <= 1) { + new Notice("At least 2 or more elements or groups should be selected."); + return; +} + +if(fromBorder === 'top') { + const groups = topGroups.sort((lha,rha) => Math.min(...lha.map(t => t.y)) - Math.min(...rha.map(t => t.y))); + const firstGroupTop = Math.min(...groups[0].map(el => el.y)); + + for(var i=0; i 0) { + const curGroup = groups[i]; + const moveDistance = distance * i; + for(const curEl of curGroup) { + curEl.y = firstGroupTop + moveDistance; + } + } + } +} +else if(fromBorder === 'bottom') { + const groups = topGroups.sort((lha,rha) => Math.min(...lha.map(t => t.y + t.height)) - Math.min(...rha.map(t => t.y + t.height))).reverse(); + const firstGroupBottom = Math.max(...groups[0].map(el => el.y + el.height)); + + for(var i=0; i 0) { + const curGroup = groups[i]; + const moveDistance = distance * i; + for(const curEl of curGroup) { + curEl.y = firstGroupBottom - moveDistance - curEl.height; + } + } + } +} +else if(fromBorder === 'left') { + const groups = topGroups.sort((lha,rha) => Math.min(...lha.map(t => t.x)) - Math.min(...rha.map(t => t.x))); + const firstGroupLeft = Math.min(...groups[0].map(el => el.x)); + + for(var i=0; i 0) { + const curGroup = groups[i]; + const moveDistance = distance * i; + for(const curEl of curGroup) { + curEl.x = firstGroupLeft + moveDistance; + } + } + } +} +else if(fromBorder === 'right') { + const groups = topGroups.sort((lha,rha) => Math.min(...lha.map(t => t.x + t.width)) - Math.min(...rha.map(t => t.x + t.width))).reverse(); + const firstGroupRight = Math.max(...groups[0].map(el => el.x + el.width)); + + for(var i=0; i 0) { + const curGroup = groups[i]; + const moveDistance = distance * i; + for(const curEl of curGroup) { + curEl.x = firstGroupRight - moveDistance - curEl.width; + } + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(false, false); diff --git a/ea-scripts/README.md b/ea-scripts/README.md index f4087a7..b39d541 100644 --- a/ea-scripts/README.md +++ b/ea-scripts/README.md @@ -33,6 +33,7 @@ Open the script you are interested in and save it to your Obsidian Vault includi |[Expand rectangles vertically keep text centered](Expand%20rectangles%20vertically%20keep%20text%20centered.md)|This script expands the height of the selected rectangles until they are all the same height and keep the text centered.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif)|[@1-2-3](https://github.com/1-2-3)| |[Expand rectangles vertically](Expand%20rectangles%20vertically.md)|This script expands the height of the selected rectangles until they are all the same height.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-expand-rectangles.gif)|[@1-2-3](https://github.com/1-2-3)| |[Fixed horizontal distance between centers](Fixed%20horizontal%20distance%20between%20centers.md)|This script arranges the selected elements horizontally with a fixed center spacing.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-fixed-horizontal-distance-between-centers.png)|[@1-2-3](https://github.com/1-2-3)| +|[Fixed inner distance](Fixed%20inner%20distance.md)|This script arranges selected elements and groups with a fixed inner distance.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-fixed-inner-distance.png)|[@1-2-3](https://github.com/1-2-3)| |[Fixed spacing](Fixed%20spacing.md)|The script arranges the selected elements horizontally with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. `Fixed spacing` and `Fixed vertical Distance` scripts can save us a lot of time.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-fix-space-demo.png)|[@1-2-3](https://github.com/1-2-3)| |[Fixed vertical distance between centers](Fixed%20vertical%20distance%20between%20centers.md)|This script arranges the selected elements vertically with a fixed center spacing.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-fixed-vertical-distance-between-centers.png)|[@1-2-3](https://github.com/1-2-3)| |[Fixed vertical distance](Fixed%20vertical%20distance.md)|The script arranges the selected elements vertically with a fixed spacing. When we create an architecture diagram or mind map, we often need to arrange a large number of elements in a fixed spacing. `Fixed spacing` and `Fixed vertical Distance` scripts can save us a lot of time.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-fixed-vertical-distance.png)|[@1-2-3](https://github.com/1-2-3)| diff --git a/ea-scripts/index.md b/ea-scripts/index.md index 876d9db..a00dde7 100644 --- a/ea-scripts/index.md +++ b/ea-scripts/index.md @@ -43,6 +43,7 @@ I would love to include your contribution in the script library. If you have a s - [[#Expand rectangles vertically keep text centered]] - [[#Expand rectangles vertically]] - [[#Fixed horizontal distance between centers]] +- [[#Fixed inner distance]] - [[#Fixed spacing]] - [[#Fixed vertical distance between centers]] - [[#Fixed vertical distance]] @@ -169,6 +170,12 @@ https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea ```
Author@1-2-3
SourceFile on GitHub
DescriptionThis script arranges the selected elements horizontally with a fixed center spacing.
+## Fixed inner distance +```excalidraw-script-install +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20inner%20distance.md +``` +
Author@1-2-3
SourceFile on GitHub
DescriptionThis script arranges selected elements and groups with a fixed inner distance.
+ ## Fixed spacing ```excalidraw-script-install https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20spacing.md diff --git a/images/scripts-fixed-inner-distance.png b/images/scripts-fixed-inner-distance.png new file mode 100644 index 0000000..ebe12f3 Binary files /dev/null and b/images/scripts-fixed-inner-distance.png differ