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

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

View File

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

View File

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

View File

@ -39,13 +39,13 @@ defmodule Outlook.InternalTree.Html do
end
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
def to_html([]), 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)
"<#{node.name} #{attr_string}>" <>
to_html_preview(node.content, target_id) <>
@ -54,16 +54,16 @@ defmodule Outlook.InternalTree.Html do
end
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
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
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"
phx-value-uuid="#{tunit.uuid}" phx-target="#{target_id}">#{tunit.content}</span>| <> to_html_preview(rest, target_id)
~s|<span class="tunit" nid="#{tunit.nid}" tu-status="#{tunit.status}" phx-click="select_current_tunit"
phx-value-nid="#{tunit.nid}" phx-target="#{target_id}">#{tunit.content}</span>| <> to_html_preview(rest, target_id)
end
def to_html_preview([], _target_id), do: ""

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -89,12 +89,12 @@ defmodule Outlook.Translators do
|> Floki.find("span.tunit")
|> Enum.map(fn {_,atts,cont} ->
%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),
status: :untranslated
}
end)
|> Enum.map(fn tunit -> {tunit.uuid, tunit} end)
|> Enum.map(fn tunit -> {tunit.nid, tunit} end)
|> Enum.into(%{})
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
~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 %>
</.dynamic_tag>
"""
@ -27,7 +27,7 @@ defmodule OutlookWeb.HtmlDocComponent do
def dnode(assigns) when assigns.node.type == :element do
~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 %>
<.dnode node={child_node} />
<% end %>

View File

@ -33,7 +33,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_element(assigns) do
~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}>" %>
</div>
"""
@ -41,7 +41,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_text(assigns) do
~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" %>
</div>
"""
@ -49,7 +49,7 @@ defmodule OutlookWeb.HtmlTreeComponent do
def tree_comment(assigns) do
~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" %>
</div>
"""

View File

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