better newline support

This commit is contained in:
Gavin McDonald
2025-05-12 16:22:10 -04:00
parent c4b4c22310
commit 914138b221
5 changed files with 38 additions and 259 deletions

View File

@@ -48,10 +48,10 @@ defmodule LabelmakerWeb.Home do
preview_background =
case assigns.preview_bg do
"r" -> "bg-[linear-gradient(to_right,_black_25%,_white_75%)]"
"b" -> "bg-[linear-gradient(to_bottom,_black_25%,_white_75%)]"
"r" -> "bg-[linear-gradient(to_right,_black_33%,_white_67%)]"
"b" -> "bg-[linear-gradient(to_bottom,_black_33%,_white_67%)]"
"c" -> "bg-[#{assigns.color}]"
_ -> "bg-[linear-gradient(to_right,_black_25%,_white_75%)]"
_ -> "bg-[linear-gradient(to_right,_black_33%,_white_67%)]"
end
~H"""
@@ -103,6 +103,7 @@ defmodule LabelmakerWeb.Home do
<div>
<label for="label" class="block text-sm font-medium">Label</label>
<input
phx-hook="EnterToNewline"
type="text"
id="label"
name="label"
@@ -110,6 +111,9 @@ defmodule LabelmakerWeb.Home do
placeholder="Enter text"
class={"mt-1 block w-full rounded border border-gray-300 px-3 py-2 text-fg-light dark:text-fg-dark dark:border-gray-600 focus:ring-primary dark:placeholder-gray-400/50 transition duration-300 #{if @label_too_long, do: "bg-danger", else: "bg-secondary-light dark:bg-secondary-dark"}"}
/>
<p class="text-xs text-gray-600 dark:text-gray-400 m-0 ml-1">
<code>\n</code> or &LT;Enter&GT; for newlines
</p>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 gap-4">

View File

@@ -1,74 +0,0 @@
defmodule LabelmakerWeb.Label do
use LabelmakerWeb, :live_view
alias LabelmakerWeb.Constants
@label_dir Path.join(:code.priv_dir(:labelmaker), "static/labels")
File.mkdir_p!(@label_dir)
@defaults %{
"label" => "Labelmaker",
"font" => "Helvetica",
"color" => "black",
"size" => "24"
}
@permitted_keys Map.keys(@defaults)
def mount(params, _session, socket) do
options =
@defaults
|> Map.merge(params)
|> Map.take(@permitted_keys)
filename =
options
|> inspect()
|> (fn str -> :crypto.hash(:sha256, str) end).()
|> Base.encode16(case: :lower)
filename = filename <> ".png"
filepath = Path.join(@label_dir, filename)
unless File.exists?(filepath) do
generate_image(options, filepath)
end
{:ok,
assign(socket,
label: options["label"],
image_path: ~p"/labels/#{filename}"
)}
end
def render(assigns) do
IO.inspect(Constants.color_count())
IO.inspect(Constants.colors())
IO.inspect(Constants.font_count())
IO.inspect(Constants.fonts())
~H"""
<div>
<h1>Label: {@label}</h1>
<img src={@image_path} alt="" />
</div>
"""
end
defp generate_image(options, filepath) do
args = [
"-background",
"none",
"-fill",
options["color"],
"-pointsize",
options["size"],
"-font",
options["font"],
"label:#{options["label"]}",
filepath
]
{_, 0} = System.cmd("magick", args)
end
end