Replace uuids with "nanoid"s

This commit is contained in:
Thelonius Kort
2023-01-11 19:01:28 +01:00
parent 881a8ee094
commit 403116cd08
16 changed files with 38 additions and 33 deletions

View File

@ -127,3 +127,7 @@ config :phoenix, :json_library, Jason
import_config "#{config_env()}.exs" import_config "#{config_env()}.exs"
config :floki, :html_parser, Floki.HTMLParser.FastHtml config :floki, :html_parser, Floki.HTMLParser.FastHtml
config :nanoid,
size: 12,
alphabet: "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

View File

@ -27,7 +27,7 @@ defmodule Outlook.Articles.InternalTree do
defp from_json([%{status: _} = node | rest]) do defp from_json([%{status: _} = node | rest]) do
[ %TranslationUnit{ [ %TranslationUnit{
status: String.to_atom(node.status), status: String.to_atom(node.status),
uuid: node.uuid, nid: node.nid,
content: node.content content: node.content
} | from_json(rest) ] } | from_json(rest) ]
end end
@ -37,7 +37,7 @@ defmodule Outlook.Articles.InternalTree do
name: node.name, name: node.name,
attributes: node.attributes, attributes: node.attributes,
type: String.to_atom(node.type), type: String.to_atom(node.type),
uuid: node.uuid, nid: node.nid,
content: from_json(node.content) content: from_json(node.content)
} | from_json(rest) ] } | from_json(rest) ]
end end
@ -47,7 +47,7 @@ defmodule Outlook.Articles.InternalTree do
name: node.name, name: node.name,
attributes: node.attributes, attributes: node.attributes,
type: String.to_atom(node.type), type: String.to_atom(node.type),
uuid: node.uuid, nid: node.nid,
content: node.content content: node.content
} | from_json(rest) ] } | from_json(rest) ]
end end

View File

@ -1,5 +1,5 @@
defmodule Outlook.HtmlPreparations.HtmlPreparation do defmodule Outlook.HtmlPreparations.HtmlPreparation do
import Ecto.UUID, only: [generate: 0] import Nanoid, only: [generate: 0]
alias Outlook.InternalTree.InternalNode alias Outlook.InternalTree.InternalNode
@ -22,7 +22,7 @@ defmodule Outlook.HtmlPreparations.HtmlPreparation do
name: tag, name: tag,
attributes: clean_atts_to_map(attributes), attributes: clean_atts_to_map(attributes),
type: :element, type: :element,
uuid: generate(), nid: generate(),
content: floki_to_internal(content) content: floki_to_internal(content)
} | floki_to_internal(rest) ] } | floki_to_internal(rest) ]
end end
@ -30,7 +30,7 @@ defmodule Outlook.HtmlPreparations.HtmlPreparation do
def floki_to_internal [ "" <> textnode | rest ] do def floki_to_internal [ "" <> textnode | rest ] do
[ %InternalNode{ [ %InternalNode{
type: :text, type: :text,
uuid: generate(), nid: generate(),
content: textnode content: textnode
} | floki_to_internal(rest) ] } | floki_to_internal(rest) ]
end end
@ -38,7 +38,7 @@ defmodule Outlook.HtmlPreparations.HtmlPreparation do
def floki_to_internal [ {:comment, comment} | rest ] do def floki_to_internal [ {:comment, comment} | rest ] do
[ %InternalNode{ [ %InternalNode{
type: :comment, type: :comment,
uuid: generate(), nid: generate(),
content: comment content: comment
} | floki_to_internal(rest) ] } | floki_to_internal(rest) ]
end end

View File

@ -15,9 +15,9 @@ defmodule Outlook.InternalTree do
end end
require Logger require Logger
def apply_modifier(tree, modifier, uuids, opts \\ %{}) do def apply_modifier(tree, modifier, nids, opts \\ %{}) do
# Logger.info modifier # Logger.info modifier
Modifiers.traverse_tree(tree, modifier, uuids, opts) Modifiers.traverse_tree(tree, modifier, nids, opts)
end end
def partition_text(tree) do def partition_text(tree) do

View File

@ -39,13 +39,13 @@ defmodule Outlook.InternalTree.Html do
end end
def to_html([%TranslationUnit{} = tunit | rest]) do def to_html([%TranslationUnit{} = tunit | rest]) do
~s(<span class="tunit" uuid="#{tunit.uuid}" tu-status="#{tunit.status}">#{tunit.content}</span>) <> to_html(rest) ~s(<span class="tunit" nid="#{tunit.nid}" tu-status="#{tunit.status}">#{tunit.content}</span>) <> to_html(rest)
end end
def to_html([]), do: "" def to_html([]), do: ""
def to_html_preview([ %InternalNode{type: :element} = node | rest], target_id) do def to_html_preview([ %InternalNode{type: :element} = node | rest], target_id) do
attr_string = Map.put(node.attributes, :uuid, node.uuid) attr_string = Map.put(node.attributes, :nid, node.nid)
|> Enum.map_join(" ", fn {k,v} -> "#{k}=\"#{v}\"" end) |> Enum.map_join(" ", fn {k,v} -> "#{k}=\"#{v}\"" end)
"<#{node.name} #{attr_string}>" <> "<#{node.name} #{attr_string}>" <>
to_html_preview(node.content, target_id) <> to_html_preview(node.content, target_id) <>
@ -54,16 +54,16 @@ defmodule Outlook.InternalTree.Html do
end end
def to_html_preview([ %InternalNode{type: :text} = node | rest], target_id) do def to_html_preview([ %InternalNode{type: :text} = node | rest], target_id) do
~s(<span uuid="#{node.uuid}">#{node.content}</span>) <> to_html_preview(rest, target_id) ~s(<span nid="#{node.nid}">#{node.content}</span>) <> to_html_preview(rest, target_id)
end end
def to_html_preview([ %InternalNode{type: :comment} = node | rest], target_id) do def to_html_preview([ %InternalNode{type: :comment} = node | rest], target_id) do
~s(<span uuid="#{node.uuid}"><!--#{node.content}--></span>) <> to_html_preview(rest, target_id) ~s(<span nid="#{node.nid}"><!--#{node.content}--></span>) <> to_html_preview(rest, target_id)
end end
def to_html_preview([ %TranslationUnit{} = tunit | rest], target_id) do def to_html_preview([ %TranslationUnit{} = tunit | rest], target_id) do
~s|<span class="tunit" uuid="#{tunit.uuid}" tu-status="#{tunit.status}" phx-click="select_current_tunit" ~s|<span class="tunit" nid="#{tunit.nid}" tu-status="#{tunit.status}" phx-click="select_current_tunit"
phx-value-uuid="#{tunit.uuid}" phx-target="#{target_id}">#{tunit.content}</span>| <> to_html_preview(rest, target_id) phx-value-nid="#{tunit.nid}" phx-target="#{target_id}">#{tunit.content}</span>| <> to_html_preview(rest, target_id)
end end
def to_html_preview([], _target_id), do: "" def to_html_preview([], _target_id), do: ""

View File

@ -1,4 +1,4 @@
defmodule Outlook.InternalTree.InternalNode do defmodule Outlook.InternalTree.InternalNode do
@derive Jason.Encoder @derive Jason.Encoder
defstruct name: "", attributes: %{}, type: :atom, uuid: "", content: [], eph: %{} defstruct name: "", attributes: %{}, type: :atom, nid: "", content: [], eph: %{}
end end

View File

@ -49,7 +49,7 @@ defmodule Outlook.InternalTree.InternalTree do
end end
defp set_attributes(node, atts, ids) do defp set_attributes(node, atts, ids) do
if node.uuid in ids do if node.nid in ids do
set_attributes(node, atts) set_attributes(node, atts)
else else
node node
@ -63,7 +63,7 @@ defmodule Outlook.InternalTree.InternalTree do
Map.put(attributes, "phx-click",atts.phx.click) Map.put(attributes, "phx-click",atts.phx.click)
# TODO: only convert to string if present # TODO: only convert to string if present
|> Map.put("phx-target", atts.phx.target |> to_string) |> Map.put("phx-target", atts.phx.target |> to_string)
|> Map.put("phx-value-uuid", node.uuid) |> Map.put("phx-value-nid", node.nid)
end end
%{node | eph: Map.put(node.eph, :attributes, attributes)} %{node | eph: Map.put(node.eph, :attributes, attributes)}
end end

View File

@ -4,7 +4,6 @@ defmodule Outlook.InternalTree.RawInternalBasic do
Html and before splitting textnodes into %TranslationUnit{}s. Html and before splitting textnodes into %TranslationUnit{}s.
""" """
alias Ecto.UUID
alias Outlook.InternalTree.InternalNode alias Outlook.InternalTree.InternalNode
alias Outlook.InternalTree.TranslationUnit alias Outlook.InternalTree.TranslationUnit
alias Outlook.InternalTree.Html alias Outlook.InternalTree.Html
@ -59,7 +58,7 @@ defmodule Outlook.InternalTree.RawInternalBasic do
%TranslationUnit{ %TranslationUnit{
content: Html.to_html(sentence), content: Html.to_html(sentence),
status: :untranslated, status: :untranslated,
uuid: UUID.generate() nid: Nanoid.generate()
} }
end end
) )

View File

@ -1,4 +1,4 @@
defmodule Outlook.InternalTree.TranslationUnit do defmodule Outlook.InternalTree.TranslationUnit do
@derive Jason.Encoder @derive Jason.Encoder
defstruct status: :atom, uuid: "", content: "", eph: %{} defstruct status: :atom, nid: "", content: "", eph: %{}
end end

View File

@ -5,7 +5,7 @@ defmodule Outlook.Translations.Basic do
def internal_tree_to_tunit_map(tree) do def internal_tree_to_tunit_map(tree) do
collect_translation_units(tree) collect_translation_units(tree)
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end) |> Enum.map(fn tunit -> {tunit.nid, tunit} end)
|> Enum.into(%{}) |> Enum.into(%{})
end end

View File

@ -89,12 +89,12 @@ defmodule Outlook.Translators do
|> Floki.find("span.tunit") |> Floki.find("span.tunit")
|> Enum.map(fn {_,atts,cont} -> |> Enum.map(fn {_,atts,cont} ->
%TranslationUnit{ %TranslationUnit{
uuid: Enum.find(atts, fn {k,_} -> k == "uuid" end) |> Tuple.to_list |> Enum.at(1), nid: Enum.find(atts, fn {k,_} -> k == "nid" end) |> Tuple.to_list |> Enum.at(1),
content: Floki.raw_html(cont), content: Floki.raw_html(cont),
status: :untranslated status: :untranslated
} }
end) end)
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end) |> Enum.map(fn tunit -> {tunit.nid, tunit} end)
|> Enum.into(%{}) |> Enum.into(%{})
case Enum.sort(Map.keys(tunit_map)) == Enum.sort(tunit_ids) do case Enum.sort(Map.keys(tunit_map)) == Enum.sort(tunit_ids) do

View File

@ -19,7 +19,7 @@ defmodule OutlookWeb.HtmlDocComponent do
def dnode(%{node: %{status: status}} = assigns) do def dnode(%{node: %{status: status}} = assigns) do
~H""" ~H"""
<.dynamic_tag name="span" class="tunit" uuid={@node.uuid} {Map.get(@node.eph, :attributes, %{})}> <.dynamic_tag name="span" class="tunit" nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}>
<%= @node.content |> raw %> <%= @node.content |> raw %>
</.dynamic_tag> </.dynamic_tag>
""" """
@ -27,7 +27,7 @@ defmodule OutlookWeb.HtmlDocComponent do
def dnode(assigns) when assigns.node.type == :element do def dnode(assigns) when assigns.node.type == :element do
~H""" ~H"""
<.dynamic_tag name={@node.name} uuid={@node.uuid} {Map.get(@node.eph, :attributes, %{})}> <.dynamic_tag name={@node.name} nid={@node.nid} {Map.get(@node.eph, :attributes, %{})}>
<%= for child_node <- @node.content do %> <%= for child_node <- @node.content do %>
<.dnode node={child_node} /> <.dnode node={child_node} />
<% end %> <% end %>

View File

@ -33,7 +33,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_element(assigns) do def tree_element(assigns) do
~H""" ~H"""
<div uuid={@node.uuid} phx-click={JS.push("select", value: %{uuid: @node.uuid})}> <div nid={@node.nid} phx-click={JS.push("select", value: %{nid: @node.nid})}>
<%= "#{String.duplicate("  ", @level)}<#{@node.name}>" %> <%= "#{String.duplicate("  ", @level)}<#{@node.name}>" %>
</div> </div>
""" """
@ -41,7 +41,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_text(assigns) do def tree_text(assigns) do
~H""" ~H"""
<div uuid={@node.uuid} phx-click={JS.push("select", value: %{uuid: @node.uuid})}> <div nid={@node.nid} phx-click={JS.push("select", value: %{nid: @node.nid})}>
<%= "#{String.duplicate("  ", @level)}\"#{String.slice(@node.content, 0, 15)}...\"\n" %> <%= "#{String.duplicate("  ", @level)}\"#{String.slice(@node.content, 0, 15)}...\"\n" %>
</div> </div>
""" """
@ -49,7 +49,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_comment(assigns) do def tree_comment(assigns) do
~H""" ~H"""
<div uuid={@node.uuid} phx-click={JS.push("select", value: %{uuid: @node.uuid})} title={@node.content}> <div nid={@node.nid} phx-click={JS.push("select", value: %{nid: @node.nid})} title={@node.content}>
<%= "#{String.duplicate("  ", @level)}<!-- #{String.slice(@node.content, 0, 15)}...-->\n" %> <%= "#{String.duplicate("  ", @level)}<!-- #{String.slice(@node.content, 0, 15)}...-->\n" %>
</div> </div>
""" """

View File

@ -105,11 +105,11 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
{:noreply, socket |> assign(:current_tunit, tunit)} {:noreply, socket |> assign(:current_tunit, tunit)}
end end
def handle_event("select_current_tunit", %{"uuid" => uuid}, socket) do def handle_event("select_current_tunit", %{"nid" => nid}, socket) do
{:noreply, {:noreply,
socket socket
|> update_translation_with_current_tunit(socket.assigns.current_tunit.status) |> update_translation_with_current_tunit(socket.assigns.current_tunit.status)
|> assign(:current_tunit, socket.assigns.translation_content[uuid])} |> assign(:current_tunit, socket.assigns.translation_content[nid])}
end end
@doc "updating on browser events" @doc "updating on browser events"
@ -124,7 +124,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
defp update_current_tunit(socket, _) do defp update_current_tunit(socket, _) do
assign(socket, assign(socket,
:current_tunit, :current_tunit,
socket.assigns.translation_content[socket.assigns.current_tunit.uuid]) socket.assigns.translation_content[socket.assigns.current_tunit.nid])
end end
defp assign_article_tree(socket, translation) do defp assign_article_tree(socket, translation) do
@ -140,7 +140,7 @@ defmodule OutlookWeb.TranslationLive.FormComponent do
socket socket
|> assign(:translation_content, |> assign(:translation_content,
socket.assigns.translation_content socket.assigns.translation_content
|> Map.put(socket.assigns.current_tunit.uuid, socket.assigns.current_tunit)) |> Map.put(socket.assigns.current_tunit.nid, socket.assigns.current_tunit))
end end
defp save_translation(socket, :edit, translation_params) do defp save_translation(socket, :edit, translation_params) do

View File

@ -55,6 +55,7 @@ defmodule Outlook.MixProject do
{:ecto_psql_extras, "~> 0.6", only: :dev}, {:ecto_psql_extras, "~> 0.6", only: :dev},
{:fast_html, "~> 2.0"}, {:fast_html, "~> 2.0"},
{:httpoison, "~> 1.8"}, {:httpoison, "~> 1.8"},
{:nanoid, "~> 2.0.5"},
] ]
end end

View File

@ -30,6 +30,7 @@
"mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"}, "mime": {:hex, :mime, "2.0.3", "3676436d3d1f7b81b5a2d2bd8405f412c677558c81b1c92be58c00562bb59095", [:mix], [], "hexpm", "27a30bf0db44d25eecba73755acf4068cbfe26a4372f9eb3e4ea3a45956bff6b"},
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"}, "mint": {:hex, :mint, "1.4.2", "50330223429a6e1260b2ca5415f69b0ab086141bc76dc2fbf34d7c389a6675b2", [:mix], [{:castore, "~> 0.1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:hpax, "~> 0.1.1", [hex: :hpax, repo: "hexpm", optional: false]}], "hexpm", "ce75a5bbcc59b4d7d8d70f8b2fc284b1751ffb35c7b6a6302b5192f8ab4ddd80"},
"nanoid": {:hex, :nanoid, "2.0.5", "1d2948d8967ef2d948a58c3fef02385040bd9823fc6394bd604b8d98e5516b22", [:mix], [], "hexpm", "956e8876321104da72aa48770539ff26b36b744cd26753ec8e7a8a37e53d5f58"},
"nimble_options": {:hex, :nimble_options, "0.5.2", "42703307b924880f8c08d97719da7472673391905f528259915782bb346e0a1b", [:mix], [], "hexpm", "4da7f904b915fd71db549bcdc25f8d56f378ef7ae07dc1d372cbe72ba950dce0"}, "nimble_options": {:hex, :nimble_options, "0.5.2", "42703307b924880f8c08d97719da7472673391905f528259915782bb346e0a1b", [:mix], [], "hexpm", "4da7f904b915fd71db549bcdc25f8d56f378ef7ae07dc1d372cbe72ba950dce0"},
"nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"}, "nimble_pool": {:hex, :nimble_pool, "0.2.6", "91f2f4c357da4c4a0a548286c84a3a28004f68f05609b4534526871a22053cde", [:mix], [], "hexpm", "1c715055095d3f2705c4e236c18b618420a35490da94149ff8b580a2144f653f"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"}, "parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},