From 2cbbf8ad5f42409689aa3cfcf100f13c30d50766 Mon Sep 17 00:00:00 2001 From: sorax Date: Tue, 21 May 2024 22:33:40 +0200 Subject: [PATCH] prepare move & indent nodes --- assets/js/hooks/events/listener.ts | 50 ++++++++++----------- lib/radiator/outline/event_consumer.ex | 21 ++++++++- lib/radiator_web/live/episode_live/index.ex | 16 ++++++- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/assets/js/hooks/events/listener.ts b/assets/js/hooks/events/listener.ts index aae5c102..7323af38 100644 --- a/assets/js/hooks/events/listener.ts +++ b/assets/js/hooks/events/listener.ts @@ -127,30 +127,30 @@ export function keydown(event: KeyboardEvent) { this.pushEvent("delete_node", nextNode); break; - // case "Tab": - // event.preventDefault(); - - // if (event.shiftKey) { - // if (node.parent_id) { - // // outdent - // node.prev_id = node.parent_id; - // node.parent_id = undefined; - // updateItem(node, container); - - // focusItem(item); - // this.pushEvent("update_node", node); - // } - // } else { - // if (node.prev_id) { - // // indent - // node.parent_id = node.prev_id; - // node.prev_id = undefined; // TODO: prev_id should be the id of the last child of the parent node - // updateItem(node, container); - - // focusItem(item); - // this.pushEvent("update_node", node); - // } - // } - // break; + case "Tab": + event.preventDefault(); + + if (event.shiftKey) { + // outdent + // if (node.parent_id) { + // node.prev_id = node.parent_id; + // node.parent_id = undefined; + // updateItem(node, container); + + // focusItem(item); + // this.pushEvent("update_node", node); + // } + } else { + // indent + if (node.prev_id) { + node.parent_id = node.prev_id; + node.prev_id = undefined; // TODO: prev_id should be the id of the last child of the parent node + updateItem(node, container); + + // focusItem(item); + this.pushEvent("move_node", node); + } + } + break; } } diff --git a/lib/radiator/outline/event_consumer.ex b/lib/radiator/outline/event_consumer.ex index b5dbc2db..42ff058c 100644 --- a/lib/radiator/outline/event_consumer.ex +++ b/lib/radiator/outline/event_consumer.ex @@ -5,7 +5,14 @@ defmodule Radiator.Outline.EventConsumer do alias Radiator.EventStore alias Radiator.Outline - alias Radiator.Outline.Command.{ChangeNodeContentCommand, DeleteNodeCommand, InsertNodeCommand} + + alias Radiator.Outline.Command.{ + ChangeNodeContentCommand, + DeleteNodeCommand, + InsertNodeCommand, + MoveNodeCommand + } + alias Radiator.Outline.Dispatch alias Radiator.Outline.Event.{NodeContentChangedEvent, NodeDeletedEvent, NodeInsertedEvent} alias Radiator.Outline.NodeRepository @@ -41,6 +48,18 @@ defmodule Radiator.Outline.EventConsumer do |> handle_change_node_content_result(command) end + defp process_command( + %MoveNodeCommand{ + node_id: _node_id, + parent_node_id: _parent_node_id, + prev_node_id: _prev_node_id + } = _command + ) do + # node_id + # |> Outline.update_node_content(content) + # |> handle_change_node_content_result(command) + end + defp process_command(%DeleteNodeCommand{node_id: node_id} = command) do case NodeRepository.get_node(node_id) do nil -> Logger.error("Could not remove node. Node not found.") diff --git a/lib/radiator_web/live/episode_live/index.ex b/lib/radiator_web/live/episode_live/index.ex index 9fc481f1..2f50d3a1 100644 --- a/lib/radiator_web/live/episode_live/index.ex +++ b/lib/radiator_web/live/episode_live/index.ex @@ -58,7 +58,6 @@ defmodule RadiatorWeb.EpisodeLive.Index do |> reply(:noreply) end - # seperate update content & move node in frontent/js def handle_event("update_node", %{"uuid" => uuid, "content" => content}, socket) do user = socket.assigns.current_user @@ -68,6 +67,21 @@ defmodule RadiatorWeb.EpisodeLive.Index do |> reply(:noreply) end + def handle_event("move_node", %{"uuid" => uuid} = node, socket) do + user = socket.assigns.current_user + + Dispatch.move_node( + uuid, + node["parent_id"], + node["prev_id"], + user.id, + generate_event_id(socket.id) + ) + + socket + |> reply(:noreply) + end + def handle_event("delete_node", %{"uuid" => uuid}, socket) do user = socket.assigns.current_user