diff --git a/lib/radiator_web/live/outline_component.ex b/lib/radiator_web/live/outline_component.ex index 540a6b09..f276195f 100644 --- a/lib/radiator_web/live/outline_component.ex +++ b/lib/radiator_web/live/outline_component.ex @@ -21,7 +21,7 @@ defmodule RadiatorWeb.OutlineComponent do def update(%{event: %NodeInsertedEvent{node: node, next_id: next_id}}, socket) do nodes = case NodeRepository.get_node_if(next_id) do - nil -> [] + nil -> [node] next_node -> [node, next_node] end @@ -40,8 +40,21 @@ defmodule RadiatorWeb.OutlineComponent do |> reply(:ok) end - def update(%{event: %NodeMovedEvent{node_id: _, parent_id: _, prev_id: _}}, socket) do + def update( + %{event: %NodeMovedEvent{node_id: node_id, old_next_id: old_next_id, next_id: next_id}}, + socket + ) do + node = NodeRepository.get_node!(node_id) + old_next_node = NodeRepository.get_node_if(old_next_id) + next_node = NodeRepository.get_node_if(next_id) + + nodes = [node, old_next_node, next_node] |> Enum.reject(&is_nil/1) + node_forms = Enum.map(nodes, &to_change_form(&1, %{})) + + # what about your children ???? + socket + |> stream(:nodes, node_forms) |> reply(:ok) end @@ -52,7 +65,7 @@ defmodule RadiatorWeb.OutlineComponent do nodes = case NodeRepository.get_node_if(next_id) do nil -> children - next_node -> [next_node, children] + next_node -> [next_node | children] end node_forms = Enum.map(nodes, &to_change_form(&1, %{})) @@ -92,23 +105,27 @@ defmodule RadiatorWeb.OutlineComponent do def handle_event("keydown", %{"key" => key, "uuid" => uuid, "value" => ""}, socket) when key in ["Backspace", "Delete", "Meta"] do - node = NodeRepository.get_node!(uuid) - user_id = socket.assigns.user_id Dispatch.delete_node(uuid, user_id, generate_event_id(socket.id)) socket - |> stream_delete(:nodes, to_change_form(node, %{})) + |> stream_delete_by_dom_id(:nodes, "nodes-form-#{uuid}") |> reply(:noreply) end def handle_event( "keydown", - %{"key" => "Tab", "shiftKey" => false, "uuid" => uuid, "prev" => prev_id}, + %{ + "key" => "Tab", + "shiftKey" => false, + "uuid" => uuid, + "prev" => prev_id, + "value" => content + }, socket ) do socket - |> indent(uuid, prev_id) + |> indent(uuid, prev_id, content) |> reply(:noreply) end @@ -119,11 +136,17 @@ defmodule RadiatorWeb.OutlineComponent do def handle_event( "keydown", - %{"key" => "Tab", "shiftKey" => true, "uuid" => uuid, "parent" => parent_id}, + %{ + "key" => "Tab", + "shiftKey" => true, + "uuid" => uuid, + "parent" => parent_id, + "value" => content + }, socket ) do socket - |> outdent(uuid, parent_id) + |> outdent(uuid, parent_id, content) |> reply(:noreply) end @@ -146,26 +169,22 @@ defmodule RadiatorWeb.OutlineComponent do end def handle_event("new", %{"uuid" => uuid, "node" => _params}, socket) do - node = NodeRepository.get_node!(uuid) - new_uuid = Ecto.UUID.generate() user_id = socket.assigns.user_id episode_id = socket.assigns.episode_id params = %{ "uuid" => new_uuid, - "parent_id" => node.parent_id, - "prev_id" => node.uuid, + "parent_id" => nil, + "prev_id" => uuid, "creator_id" => user_id, "episode_id" => episode_id } new_node = %Node{ uuid: new_uuid, - parent_id: node.parent_id, - prev_id: node.uuid, - creator_id: user_id, - episode_id: episode_id + parent_id: nil, + prev_id: uuid } Dispatch.insert_node(params, user_id, generate_event_id(socket.id)) @@ -186,11 +205,33 @@ defmodule RadiatorWeb.OutlineComponent do defp generate_event_id(id), do: Ecto.UUID.generate() <> ":" <> id - defp indent(socket, _uuid, _prev_id) do + defp indent(socket, uuid, prev_id, content) do + node = %Node{ + uuid: uuid, + parent_id: prev_id, + prev_id: nil, + content: content + } + + user_id = socket.assigns.user_id + Dispatch.move_node(uuid, prev_id, nil, user_id, generate_event_id(socket.id)) + socket + |> stream_insert(:nodes, to_change_form(node, %{})) end - defp outdent(socket, _uuid, _parent_id) do + defp outdent(socket, uuid, parent_id, content) do + node = %Node{ + uuid: uuid, + parent_id: nil, + prev_id: parent_id, + content: content + } + + user_id = socket.assigns.user_id + Dispatch.move_node(uuid, nil, parent_id, user_id, generate_event_id(socket.id)) + socket + |> stream_insert(:nodes, to_change_form(node, %{})) end end