Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
leandrocp committed Dec 2, 2024
1 parent a55ce63 commit f0f7f61
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 63 deletions.
53 changes: 29 additions & 24 deletions lib/beacon/live_admin/visual_editor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,33 @@ defmodule Beacon.LiveAdmin.VisualEditor do
def find_element(_page, _path), do: nil

defp find_ast_element(nodes, path) do
case String.split(path, ".") do
case resolve_path(path) do
[] ->
nil

parts ->
parts =
parts
|> Enum.reduce([], fn
"", acc ->
acc
find_ast_element_recursive(nodes, parts)
end
end

part, acc ->
[String.to_integer(part) | acc]
end)
|> Enum.reverse()
defp resolve_path(path) when is_binary(path) do
case String.split(path, ".") do
[] ->
[]

find_ast_element_recursive(nodes, parts)
parts ->
parts
|> Enum.reduce([], fn
"", acc ->
acc

part, acc ->
[String.to_integer(part) | acc]
end)
|> Enum.reverse()
end
rescue
_ -> nil
_ -> []
end

defp find_ast_element_recursive(nodes, [index | []]), do: Enum.at(nodes, index)
Expand All @@ -45,29 +52,27 @@ defmodule Beacon.LiveAdmin.VisualEditor do
end

# FIXME: update "root" node
def update_node(nodes, path, attrs, deleted_attributes) do
indices = String.split(path, ".") |> Enum.map(&String.to_integer/1)
update_node_recursive(nodes, indices, attrs, deleted_attributes)
def update_node(nodes, path, attrs, deleted_attrs) do
path = resolve_path(path)
update_node_recursive(nodes, path, attrs, deleted_attrs)
end

defp update_node(node, attrs, deleted_attributes) do
defp update_node(node, attrs, deleted_attrs) do
new_attrs =
node["attrs"]
|> Map.merge(attrs)
|> Map.drop(deleted_attributes)
|> Map.drop(deleted_attrs)

%{node | "attrs" => new_attrs}
end

defp update_node_recursive(nodes, [index], attrs, deleted_attributes) do
nodes
|> List.update_at(index, fn node -> update_node(node, attrs, deleted_attributes) end)
defp update_node_recursive(nodes, [index], attrs, deleted_attrs) do
List.update_at(nodes, index, fn node -> update_node(node, attrs, deleted_attrs) end)
end

defp update_node_recursive(nodes, [index | rest], attrs, deleted_attributes) do
nodes
|> List.update_at(index, fn node ->
%{node | "content" => update_node_recursive(node["content"], rest, attrs, deleted_attributes)}
defp update_node_recursive(nodes, [index | rest], attrs, deleted_attrs) do
List.update_at(nodes, index, fn node ->
%{node | "content" => update_node_recursive(node["content"], rest, attrs, deleted_attrs)}
end)
end

Expand Down
53 changes: 15 additions & 38 deletions priv/static/beacon_live_admin.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion priv/static/beacon_live_admin.min.css

Large diffs are not rendered by default.

0 comments on commit f0f7f61

Please sign in to comment.