diff --git a/ea-scripts/Fixed horizontal distance between centers.md b/ea-scripts/Fixed horizontal distance between centers.md new file mode 100644 index 0000000..cd037cd --- /dev/null +++ b/ea-scripts/Fixed horizontal distance between centers.md @@ -0,0 +1,72 @@ +/* +![](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-horizontal-distance-between-centers.png) + +This script arranges the selected elements horizontally with a fixed center spacing. + +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 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 +const groups = topGroups.sort((lha,rha) => lha[0].x - rha[0].x); + +for(var i=0; i 0) { + const preGroup = groups[i-1]; + const curGroup = groups[i]; + + const preLeft = Math.min(...preGroup.map(el => el.x)); + const preRight = Math.max(...preGroup.map(el => el.x + el.width)); + const preCenter = preLeft + (preRight - preLeft) / 2; + const curLeft = Math.min(...curGroup.map(el => el.x)); + const curRight = Math.max(...curGroup.map(el => el.x + el.width)); + const curCenter = curLeft + (curRight - curLeft) / 2; + const distanceBetweenCenters = curCenter - preCenter - distance; + + for(const curEl of curGroup) { + curEl.x = curEl.x - distanceBetweenCenters; + } + } +} +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(false, false); diff --git a/ea-scripts/Fixed vertical distance between centers.md b/ea-scripts/Fixed vertical distance between centers.md new file mode 100644 index 0000000..3307ee3 --- /dev/null +++ b/ea-scripts/Fixed vertical distance between centers.md @@ -0,0 +1,74 @@ +/* +![](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-vertical-distance-between-centers.png) + +This script arranges the selected elements vertically with a fixed center spacing. + +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 vertical 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 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 +const groups = topGroups.sort((lha,rha) => lha[0].y - rha[0].y); + +for(var i=0; i 0) { + const preGroup = groups[i-1]; + const curGroup = groups[i]; + + const preTop = Math.min(...preGroup.map(el => el.y)); + const preBottom = Math.max(...preGroup.map(el => el.y + el.height)); + const preCenter = preTop + (preBottom - preTop) / 2; + const curTop = Math.min(...curGroup.map(el => el.y)); + const curBottom = Math.max(...curGroup.map(el => el.y + el.height)); + const curCenter = curTop + (curBottom - curTop) / 2; + const distanceBetweenCenters = curCenter - preCenter - distance; + + for(const curEl of curGroup) { + curEl.y = curEl.y - distanceBetweenCenters; + } + } +} + +ea.copyViewElementsToEAforEditing(elements); +ea.addElementsToView(false, false); \ No newline at end of file diff --git a/ea-scripts/README.md b/ea-scripts/README.md index 6bdb090..f4087a7 100644 --- a/ea-scripts/README.md +++ b/ea-scripts/README.md @@ -32,7 +32,9 @@ Open the script you are interested in and save it to your Obsidian Vault includi |[Expand rectangles horizontally](Expand%20rectangles%20horizontally.md)|This script expands the width of the selected rectangles until they are all the same width.|![](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 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 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)| |[Set Font Family](Set%20Font%20Family.md)|Sets font family of the text block (Virgil, Helvetica, Cascadia). Useful if you want to set a keyboard shortcut for selecting font family.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-font-family.jpg)|[@zsviczian](https://github.com/zsviczian)| |[Set Grid](Set%20Grid.md)|The default grid size in Excalidraw is 20. Currently there is no way to change the grid size via the user interface. This script offers a way to bridge this gap.|![](https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/images/scripts-grid.jpg)|[@zsviczian](https://github.com/zsviczian)| diff --git a/ea-scripts/index.md b/ea-scripts/index.md index 551770d..ad7cb6c 100644 --- a/ea-scripts/index.md +++ b/ea-scripts/index.md @@ -42,7 +42,9 @@ I would love to include your contribution in the script library. If you have a s - [[#Expand rectangles horizontally]] - [[#Expand rectangles vertically keep text centered]] - [[#Expand rectangles vertically]] +- [[#Fixed horizontal distance between centers]] - [[#Fixed spacing]] +- [[#Fixed vertical distance between centers]] - [[#Fixed vertical distance]] - [[#Lighten background color]] - [[#Modify background color opacity]] @@ -161,12 +163,24 @@ https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea ```
Author@1-2-3
SourceFile on GitHub
DescriptionThis script expands the height of the selected rectangles until they are all the same height.
+## Fixed horizontal distance between centers +```excalidraw-script-install +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20horizontal%20distance%20between%20centers.md +``` +
Author@1-2-3
SourceFile on GitHub
DescriptionThis script arranges the selected elements horizontally with a fixed center spacing.
+ ## Fixed spacing ```excalidraw-script-install https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20spacing.md ```
Author@1-2-3
SourceFile on GitHub
DescriptionThe 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.
+## Fixed vertical distance between centers +```excalidraw-script-install +https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20vertical%20distance%20between%20centers.md +``` +
Author@1-2-3
SourceFile on GitHub
DescriptionThis script arranges the selected elements vertically with a fixed center spacing.
+ ## Fixed vertical distance ```excalidraw-script-install https://raw.githubusercontent.com/zsviczian/obsidian-excalidraw-plugin/master/ea-scripts/Fixed%20vertical%20distance.md diff --git a/images/scripts-fixed-horizontal-distance-between-centers.png b/images/scripts-fixed-horizontal-distance-between-centers.png new file mode 100644 index 0000000..ce61acc Binary files /dev/null and b/images/scripts-fixed-horizontal-distance-between-centers.png differ diff --git a/images/scripts-fixed-vertical-distance-between-centers.png b/images/scripts-fixed-vertical-distance-between-centers.png new file mode 100644 index 0000000..2f92795 Binary files /dev/null and b/images/scripts-fixed-vertical-distance-between-centers.png differ