Update editing translation

This commit is contained in:
Thelonius Kort
2023-01-09 21:35:35 +01:00
parent 71e6a8da60
commit 33ed533a1a
10 changed files with 111 additions and 18 deletions

View File

@ -1,6 +1,6 @@
defmodule Outlook.InternalTree do
alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic}
alias Outlook.InternalTree.{Html,Modifiers,RawInternalBasic,InternalTree}
alias Outlook.HtmlPreparations.HtmlPreparation
def render_html(tree) do
@ -26,4 +26,13 @@ defmodule Outlook.InternalTree do
|> RawInternalBasic.set_split_markers()
|> RawInternalBasic.partition_to_tunits()
end
def garnish(tree, options) do
options = Enum.reduce(
~w(el_ids el_names tu_ids)a,
options,
fn prop, opts -> Map.put_new(opts, prop, []) end
)
InternalTree.garnish(tree, options)
end
end

View File

@ -0,0 +1,70 @@
defmodule Outlook.InternalTree.InternalTree do
alias Outlook.InternalTree.InternalNode
alias Outlook.InternalTree.TranslationUnit
def garnish([%TranslationUnit{} = node | rest], %{tunits: _} = options) do
[ set_attributes(node, options.tunits, options.tu_ids)
| garnish(rest, options) ]
end
def garnish([%TranslationUnit{} = node | rest], options) do
[ node | garnish(rest, options) ]
end
def garnish([%InternalNode{type: :element} = node | rest], %{elements: _} = options) do
node = set_attributes(node, options.elements, options.el_ids, options.el_names)
[ %InternalNode{node |
content: garnish(node.content, options)
} | garnish(rest, options) ]
end
def garnish([%InternalNode{type: :element} = node | rest], options) do
[ %InternalNode{node |
content: garnish(node.content, options)
} | garnish(rest, options) ]
end
def garnish([node | rest], options) do
[ node | garnish(rest, options) ]
end
def garnish([], _), do: []
defp set_attributes(node, atts, ids, []) do
set_attributes(node, atts, ids)
end
defp set_attributes(node, atts, ids, names) do
if node.name in names do
set_attributes(node, atts, [])
else
set_attributes(node, atts, ids)
end
end
defp set_attributes(node, atts, []) do
set_attributes(node, atts)
end
defp set_attributes(node, atts, ids) do
if node.uuid in ids do
set_attributes(node, atts)
else
node
end
end
defp set_attributes(node, atts) do
attributes = Map.get(atts, :atts, %{})
attributes = if Map.has_key?(atts, :phx) do
# TODO: for all keys in atts.phx create a respective entry
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)
end
%{node | eph: Map.put(node.eph, :attributes, attributes)}
end
end

View File

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

View File

@ -23,7 +23,7 @@ defmodule Outlook.Translations.TranslationUnitsMap do
def dump(tumap) when is_map(tumap) do
serialized_map = for {key, val} <- tumap do
{key, Jason.encode!(val)}
{key, Jason.encode!(val |> Map.delete(:eph))}
end
|> Enum.into(%{})
{:ok, serialized_map}