defmodule LivebookWeb.Output.ImageInputComponent do use LivebookWeb, :live_component @impl true def mount(socket) do {:ok, assign(socket, value: nil)} end @impl true def update(assigns, socket) do {value, assigns} = Map.pop!(assigns, :value) socket = assign(socket, assigns) socket = if value == socket.assigns.value do socket else image_info = if value do %{data: Base.encode64(value.data), height: value.height, width: value.width} end push_event(socket, "image_input_change:#{socket.assigns.id}", %{image_info: image_info}) end {:ok, assign(socket, value: value)} end @impl true def render(assigns) do ~H"""
Drag an image file
<.menu id={"#{@id}-camera-select-menu"} position={:bottom_left}> <:toggle>
<.menu_item>
""" end @impl true def handle_event("change", params, socket) do value = %{ data: Base.decode64!(params["data"]), height: params["height"], width: params["width"], format: socket.assigns.format } send_update(LivebookWeb.Output.InputComponent, id: socket.assigns.input_component_id, event: :change, value: value ) {:noreply, socket} end end