Skip to content

Commit

Permalink
show focus & blur in nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
sorax committed Sep 4, 2024
1 parent 4f5c61d commit d483155
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
20 changes: 17 additions & 3 deletions assets/js/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ export const Hooks = {
outline: {
selector: '.node:not([data-processed="true"]',
mounted() {
this.el.addEventListener("toggle_collapse", ({ detail }) => {
const item = getItemById(detail.uuid);
this.handleEvent("focus", ({ uuid, user_name }) => {
const item = getItemById(uuid);
item!.querySelector(".editing")!.innerHTML = user_name;
});
this.handleEvent("blur", ({ uuid }) => {
const item = getItemById(uuid);
item!.querySelector(".editing")!.innerHTML = "";
});

this.el.addEventListener("toggle_collapse", ({ detail: { uuid } }) => {
const item = getItemById(uuid);
item!.toggleAttribute("data-collapsed");

const collapsedStatus = localStorage.getItem(this.el.id) || "{}";
const collapsed = JSON.parse(collapsedStatus);
collapsed[detail.uuid] = !collapsed[detail.uuid];

collapsed[uuid] = !collapsed[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 { uuid, parent_id, prev_id } = getNodeByItem(item);
Expand All @@ -26,6 +39,7 @@ export const Hooks = {
} else if (parentNode) {
parentNode.querySelector(".children")!.append(item);
}

item.toggleAttribute("data-collapsed", !!collapsed[uuid]);
setAttribute(item, "processed", true);
});
Expand Down
1 change: 1 addition & 0 deletions lib/radiator_web/live/episode_live/index.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
module={RadiatorWeb.OutlineComponent}
episode_id={@selected_episode.id}
user_id={@current_user.id}
user={@current_user}
/>
</section>

Expand Down
12 changes: 10 additions & 2 deletions lib/radiator_web/live/outline_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,21 @@ defmodule RadiatorWeb.OutlineComponent do
end

@impl true
def handle_event("focus", %{"uuid" => _uuid}, socket) do
def handle_event("focus", %{"uuid" => uuid}, socket) do
id = socket.assigns.user_id
name = socket.assigns.user.email |> String.split("@") |> List.first()

socket
|> push_event("focus", %{uuid: uuid, user_id: id, user_name: name})
|> reply(:noreply)
end

def handle_event("blur", %{"uuid" => _uuid}, socket) do
def handle_event("blur", %{"uuid" => uuid}, socket) do
id = socket.assigns.user_id
name = socket.assigns.user.email |> String.split("@") |> List.first()

socket
|> push_event("blur", %{uuid: uuid, user_id: id, user_name: name})
|> reply(:noreply)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/radiator_web/live/outline_component.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
phx-target={@myself}
/>
</OutlineComponents.outline_form>
<div class="absolute top-0 right-0 z-10 px-1 text-xs text-white bg-green-500 rounded editing">
</div>
<div class="ml-4 peer children group-data-[collapsed]:hidden"></div>
<button
class="absolute peer-empty:hidden top-3 left-0 group-data-[collapsed]:-rotate-90 duration-200 z-10"
Expand Down

0 comments on commit d483155

Please sign in to comment.