MCPcopy Create free account
hub / github.com/cifertech/DisplayKit / distributeSelected

Function distributeSelected

app.js:7054–7105  ·  view source on GitHub ↗
(axis)

Source from the content-addressed store, hash-verified

7052alignVCenterBtn.addEventListener("click", () => alignSelected("vcenter"));
7053
7054function distributeSelected(axis) {
7055 const ids = selectedIds && selectedIds.size ? Array.from(selectedIds) : [];
7056 const els = ids.map((id) => elements.find((e) => e.id === id)).filter(Boolean);
7057 if (els.length < 3) return;
7058
7059 if (axis === "h") {
7060 const sorted = els.slice().sort((a, b) => a.x - b.x);
7061 const minX = sorted[0].x;
7062 const maxX = sorted[sorted.length - 1].x + sorted[sorted.length - 1].w;
7063 const totalW = sorted.reduce((s, e) => s + e.w, 0);
7064 const gap = (maxX - minX - totalW) / (sorted.length - 1);
7065 let cursor = minX;
7066 sorted.forEach((el, idx) => {
7067 if (idx === 0) {
7068 cursor = el.x + el.w + gap;
7069 return;
7070 }
7071 if (idx === sorted.length - 1) return; // keep last anchored
7072 el.x = Math.round(cursor);
7073 cursor = el.x + el.w + gap;
7074 });
7075 } else {
7076 const sorted = els.slice().sort((a, b) => a.y - b.y);
7077 const minY = sorted[0].y;
7078 const maxY = sorted[sorted.length - 1].y + sorted[sorted.length - 1].h;
7079 const totalH = sorted.reduce((s, e) => s + e.h, 0);
7080 const gap = (maxY - minY - totalH) / (sorted.length - 1);
7081 let cursor = minY;
7082 sorted.forEach((el, idx) => {
7083 if (idx === 0) {
7084 cursor = el.y + el.h + gap;
7085 return;
7086 }
7087 if (idx === sorted.length - 1) return; // keep last anchored
7088 el.y = Math.round(cursor);
7089 cursor = el.y + el.h + gap;
7090 });
7091 }
7092
7093 if (snapToGrid && gridSize > 0) {
7094 els.forEach((el) => {
7095 el.x = Math.round(el.x / gridSize) * gridSize;
7096 el.y = Math.round(el.y / gridSize) * gridSize;
7097 });
7098 }
7099
7100 updatePropsInputs(false);
7101 renderElements();
7102 renderLayers();
7103 updateCode();
7104 pushHistory();
7105}
7106
7107if (distributeHBtn) distributeHBtn.addEventListener("click", () => distributeSelected("h"));
7108if (distributeVBtn) distributeVBtn.addEventListener("click", () => distributeSelected("v"));

Callers 1

app.jsFile · 0.85

Calls 5

updatePropsInputsFunction · 0.85
renderElementsFunction · 0.85
renderLayersFunction · 0.85
updateCodeFunction · 0.85
pushHistoryFunction · 0.85

Tested by

no test coverage detected