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

Function alignSelected

app.js:7001–7045  ·  view source on GitHub ↗
(mode)

Source from the content-addressed store, hash-verified

6999
7000
7001function alignSelected(mode) {
7002 const ids = selectedIds && selectedIds.size ? Array.from(selectedIds) : (selectedId ? [selectedId] : []);
7003 const els = ids.map((id) => elements.find((e) => e.id === id)).filter(Boolean);
7004 if (!els.length) return;
7005
7006 if (els.length === 1) {
7007 const el = els[0];
7008 if (mode === "left") el.x = 0;
7009 else if (mode === "right") el.x = dispWidth - el.w;
7010 else if (mode === "hcenter") el.x = Math.round((dispWidth - el.w) / 2);
7011 else if (mode === "top") el.y = 0;
7012 else if (mode === "bottom") el.y = dispHeight - el.h;
7013 else if (mode === "vcenter") el.y = Math.round((dispHeight - el.h) / 2);
7014 } else {
7015 // Multi-select: align within current selection bounds.
7016 const minX = Math.min(...els.map((e) => e.x));
7017 const minY = Math.min(...els.map((e) => e.y));
7018 const maxX = Math.max(...els.map((e) => e.x + e.w));
7019 const maxY = Math.max(...els.map((e) => e.y + e.h));
7020 const midX = (minX + maxX) / 2;
7021 const midY = (minY + maxY) / 2;
7022
7023 els.forEach((el) => {
7024 if (mode === "left") el.x = Math.round(minX);
7025 else if (mode === "right") el.x = Math.round(maxX - el.w);
7026 else if (mode === "hcenter") el.x = Math.round(midX - el.w / 2);
7027 else if (mode === "top") el.y = Math.round(minY);
7028 else if (mode === "bottom") el.y = Math.round(maxY - el.h);
7029 else if (mode === "vcenter") el.y = Math.round(midY - el.h / 2);
7030 });
7031 }
7032
7033 if (snapToGrid && gridSize > 0) {
7034 els.forEach((el) => {
7035 el.x = Math.round(el.x / gridSize) * gridSize;
7036 el.y = Math.round(el.y / gridSize) * gridSize;
7037 });
7038 }
7039
7040 updatePropsInputs(false);
7041 renderElements();
7042 renderLayers();
7043 updateCode();
7044 pushHistory();
7045}
7046
7047alignLeftBtn.addEventListener("click", () => alignSelected("left"));
7048alignRightBtn.addEventListener("click", () => alignSelected("right"));

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