diff --git a/assets/js/hooks/index.ts b/assets/js/hooks/index.ts index 0c7212d3..2bc13a15 100644 --- a/assets/js/hooks/index.ts +++ b/assets/js/hooks/index.ts @@ -5,9 +5,19 @@ export const Hooks = { outline: { selector: '.node:not([data-processed="true"]', mounted() { + this.el.addEventListener("toggle_collapse", ({ detail }) => { + const item = getItemById(detail.uuid); + item!.toggleAttribute("data-collapsed"); + const collapsedStatus = localStorage.getItem(this.el.id) || "{}"; + const collapsed = JSON.parse(collapsedStatus); + collapsed[detail.uuid] = !collapsed[detail.uuid]; + localStorage.setItem(this.el.id, JSON.stringify(collapsed)); + }); + const collapsedStatus = localStorage.getItem(this.el.id) || "{}"; + const collapsed = JSON.parse(collapsedStatus); const nodes = this.el.querySelectorAll(this.selector); nodes.forEach((item: HTMLDivElement) => { - const { parent_id, prev_id } = getNodeByItem(item); + const { uuid, parent_id, prev_id } = getNodeByItem(item); const parentNode = getItemById(parent_id); const prevNode = getItemById(prev_id); @@ -16,7 +26,7 @@ export const Hooks = { } else if (parentNode) { parentNode.querySelector(".children")!.append(item); } - + item.toggleAttribute("data-collapsed", !!collapsed[uuid]); setAttribute(item, "processed", true); }); }, diff --git a/lib/radiator_web/live/outline_component.ex b/lib/radiator_web/live/outline_component.ex index f276195f..046424c0 100644 --- a/lib/radiator_web/live/outline_component.ex +++ b/lib/radiator_web/live/outline_component.ex @@ -98,11 +98,6 @@ defmodule RadiatorWeb.OutlineComponent do |> reply(:noreply) end - def handle_event("toggle_collapse", %{"uuid" => _uuid}, socket) do - socket - |> reply(:noreply) - end - def handle_event("keydown", %{"key" => key, "uuid" => uuid, "value" => ""}, socket) when key in ["Backspace", "Delete", "Meta"] do user_id = socket.assigns.user_id diff --git a/lib/radiator_web/live/outline_component.html.heex b/lib/radiator_web/live/outline_component.html.heex index ed76cbad..b552dfbe 100644 --- a/lib/radiator_web/live/outline_component.html.heex +++ b/lib/radiator_web/live/outline_component.html.heex @@ -1,14 +1,13 @@
-
+