Skip to content

Commit

Permalink
wip indent & outdent
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Sep 3, 2024
1 parent 767af30 commit 8341b59
Showing 1 changed file with 61 additions and 20 deletions.
81 changes: 61 additions & 20 deletions lib/radiator_web/live/outline_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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, %{}))
Expand Down Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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))
Expand All @@ -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

0 comments on commit 8341b59

Please sign in to comment.