defmodule LivebookWeb.Output.ControlComponent do use LivebookWeb, :live_component @impl true def mount(socket) do {:ok, assign(socket, keyboard_enabled: false)} end @impl true def render(%{attrs: %{type: :keyboard}} = assigns) do ~H"""
""" end def render(%{attrs: %{type: :button}} = assigns) do ~H"""
""" end def render(%{attrs: %{type: :form}} = assigns) do ~H"""
<.live_component module={LivebookWeb.Output.ControlFormComponent} id={@id} attrs={@attrs} input_values={@input_values} client_id={@client_id} />
""" end def render(assigns) do ~H"""
Unknown control type <%= @attrs.type %>
""" end @impl true def handle_event("toggle_keyboard", %{}, socket) do socket = update(socket, :keyboard_enabled, ¬/1) maybe_report_status(socket) {:noreply, socket} end def handle_event("disable_keyboard", %{}, socket) do socket = if socket.assigns.keyboard_enabled do socket = assign(socket, keyboard_enabled: false) maybe_report_status(socket) socket else socket end {:noreply, socket} end def handle_event("button_click", %{}, socket) do report_event(socket, %{type: :click}) {:noreply, socket} end def handle_event("keydown", %{"key" => key}, socket) do report_event(socket, %{type: :keydown, key: key}) {:noreply, socket} end def handle_event("keyup", %{"key" => key}, socket) do report_event(socket, %{type: :keyup, key: key}) {:noreply, socket} end defp maybe_report_status(socket) do if :status in socket.assigns.attrs.events do report_event(socket, %{type: :status, enabled: socket.assigns.keyboard_enabled}) end end defp report_event(socket, attrs) do topic = socket.assigns.attrs.ref event = Map.merge(%{origin: socket.assigns.client_id}, attrs) send(socket.assigns.attrs.destination, {:event, topic, event}) end end