Credo: break up label placement and style-field helpers in canvas edit

7df95b5 · Gabe Morell · 2026-11-04 20:32

5 files +29 -27

Files changed

modified lib/diogramos_web/live/canvas_live/edit.ex
+29 −27
@@ -2171,25 +2171,20 @@ defmodule DiogramosWeb.CanvasLive.Edit do
2171 2171 size = label_size_px(get_in(element, ["style", "label_size"]) || "md")
2172 2172 pad = 8
2173 2173
2174 − {ax, anchor} =
2175 − case pos do
2176 − p when p in ["tl", "ml", "bl"] -> {x + pad, "start"}
2177 − p when p in ["tc", "center", "bc"] -> {x + w / 2, "middle"}
2178 − p when p in ["tr", "mr", "br"] -> {x + w - pad, "end"}
2179 − _ -> {x + w / 2, "middle"}
2180 − end
2181 −
2182 − {ay, baseline} =
2183 − case pos do
2184 − p when p in ["tl", "tc", "tr"] -> {y + pad, "hanging"}
2185 − p when p in ["ml", "center", "mr"] -> {y + h / 2, "middle"}
2186 − p when p in ["bl", "bc", "br"] -> {y + h - pad, "auto"}
2187 − _ -> {y + h / 2, "middle"}
2188 − end
2174 + {ax, anchor} = label_horizontal(pos, x, w, pad)
2175 + {ay, baseline} = label_vertical(pos, y, h, pad)
2189 2176
2190 2177 %{x: ax, y: ay, text_anchor: anchor, baseline: baseline, font_size: size}
2191 2178 end
2192 2179
2180 + defp label_horizontal(pos, x, _w, pad) when pos in ["tl", "ml", "bl"], do: {x + pad, "start"}
2181 + defp label_horizontal(pos, x, w, pad) when pos in ["tr", "mr", "br"], do: {x + w - pad, "end"}
2182 + defp label_horizontal(_pos, x, w, _pad), do: {x + w / 2, "middle"}
2183 +
2184 + defp label_vertical(pos, y, _h, pad) when pos in ["tl", "tc", "tr"], do: {y + pad, "hanging"}
2185 + defp label_vertical(pos, y, h, pad) when pos in ["bl", "bc", "br"], do: {y + h - pad, "auto"}
2186 + defp label_vertical(_pos, y, h, _pad), do: {y + h / 2, "middle"}
2187 +
2193 2188 defp bbox_of(%{"type" => "circle", "cx" => cx, "cy" => cy, "r" => r}),
2194 2189 do: {cx - r, cy - r, 2 * r, 2 * r}
2195 2190
@@ -2695,6 +2690,12 @@ defmodule DiogramosWeb.CanvasLive.Edit do
2695 2690 |> Map.put("dash", get_in(e, ["style", "dash"]) || "solid")
2696 2691 |> Map.put("stroke_width", get_in(e, ["style", "stroke_width"]) || 1.5)
2697 2692 |> Map.put("shadow", get_in(e, ["style", "shadow"]) == true)
2693 + |> with_label_style_fields(e)
2694 + |> with_link_fields(e)
2695 + end
2696 +
2697 + defp with_label_style_fields(params, e) do
2698 + params
2698 2699 |> Map.put(
2699 2700 "label_position",
2700 2701 get_in(e, ["style", "label_position"]) ||
@@ -2705,7 +2706,6 @@ defmodule DiogramosWeb.CanvasLive.Edit do
2705 2706 |> Map.put("label_font_family", get_in(e, ["style", "label_font_family"]) || "sans")
2706 2707 |> Map.put("label_bold", get_in(e, ["style", "label_bold"]) == true)
2707 2708 |> Map.put("label_italic", get_in(e, ["style", "label_italic"]) == true)
2708 − |> with_link_fields(e)
2709 2709 end
2710 2710
2711 2711 defp with_link_fields(params, e) do
@@ -2729,18 +2729,20 @@ defmodule DiogramosWeb.CanvasLive.Edit do
2729 2729 )
2730 2730 end
2731 2731
2732 + @connector_form_defaults %{
2733 + "label" => "",
2734 + "routing" => "orthogonal",
2735 + "dash" => "solid",
2736 + "marker_start" => "none",
2737 + "marker_end" => "arrow",
2738 + "start_gap" => 6,
2739 + "end_gap" => 6,
2740 + "stroke_width" => 2,
2741 + "color" => "base-content"
2742 + }
2743 +
2732 2744 defp connector_form_params(c) do
2733 − %{
2734 − "label" => c["label"] || "",
2735 − "routing" => c["routing"] || "orthogonal",
2736 − "dash" => c["dash"] || "solid",
2737 − "marker_start" => c["marker_start"] || "none",
2738 − "marker_end" => c["marker_end"] || "arrow",
2739 − "start_gap" => c["start_gap"] || 6,
2740 − "end_gap" => c["end_gap"] || 6,
2741 − "stroke_width" => c["stroke_width"] || 2,
2742 − "color" => c["color"] || "base-content"
2743 − }
2745 + Map.new(@connector_form_defaults, fn {key, default} -> {key, c[key] || default} end)
2744 2746 end
2745 2747
2746 2748 defp style_clipboard_compatible?(nil, _), do: false

Parents: c93a87c