Skip to content

Instantly share code, notes, and snippets.

@christhearchitect
Last active May 16, 2022 20:04
Show Gist options
  • Save christhearchitect/11253eced0d5870314f3661d65b39fc7 to your computer and use it in GitHub Desktop.
Save christhearchitect/11253eced0d5870314f3661d65b39fc7 to your computer and use it in GitHub Desktop.
#jArchi
/*
* Distribute the selected (visual) elements horizontally, vertically, or both (based on response to prompt)
* Notes:
* - the center point of each element is taken as reference for the distribution
* - the overall visual order (left-to-right and top-to-bottom) of the elements is not modified
* - if a non-visual element is part of the selection, the script will crash :-(
* Author: christhearchitect, 2022
* Version: 0.1
*/
console.clear();
console.log("Distribute Elements\n-------------------\n");
var debug = false;
// var curView = $("archimate-diagram-model").first();
// debug ? console.log("// current view =",curView.name):null;
function centerX(e) {
return e.bounds.x + e.bounds.width/2;
};
function centerY(e){
return e.bounds.y + e.bounds.height/2;
}
var action = "9";
while (action<"0" || action>"3") {
action = window.prompt("How do you want to distribute the selected elements ?\n 1 - Horizontally\n 2 - Vertically\n 3 - Both\n 0 - Cancel","0");
};
var sortedSelection = selection.clone();
if (action=="1" || action=="3") {
// Distribute horizontally
console.log("> Distributing horizontally...");
sortedSelection.sort(function horizontalOffset(a,b) {
return centerX(a) - centerX(b);
});
var incrX = (centerX(sortedSelection[selection.length-1]) - centerX(sortedSelection[0])) / (selection.length-1);
var nextX = sortedSelection[0].bounds.x;
for (i=0; i<selection.length; i++){
var elem = sortedSelection[i];
debug ? console.log("//",elem.name,elem.bounds, centerX(elem), nextX):null;
elem.bounds = {x:nextX, width:elem.bounds.width, y:elem.bounds.y, height:elem.bounds.height};
nextX += incrX;
};
};
if (action=="2" || action=="3") {
// Distribute vertically
console.log("> Distributing vertically...")
sortedSelection.sort(function verticalOffset(a,b) {
return centerY(a) - centerY(b);
});
var incrY = (centerY(sortedSelection[selection.length-1]) - centerY(sortedSelection[0])) / (selection.length-1);
var nextY = sortedSelection[0].bounds.y;
for (i=0; i<selection.length; i++){
var elem = sortedSelection[i];
debug ? console.log("//",elem.name,elem.bounds, centerX(elem), nextY):null;
elem.bounds = {x:elem.bounds.x, width:elem.bounds.width, y:nextY, height:elem.bounds.height};
nextY += incrY;
};
};
console.log("\n> Processed",selection.length-1,"elements");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment