2022-11-19 22:08:00 +08:00
|
|
|
defmodule LivebookWeb.Output.ImageInputComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(socket) do
|
2023-03-21 03:09:41 +08:00
|
|
|
{:ok, assign(socket, value: nil)}
|
2022-11-19 22:08:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
{value, assigns} = Map.pop!(assigns, :value)
|
|
|
|
|
|
|
|
socket = assign(socket, assigns)
|
|
|
|
|
|
|
|
socket =
|
2023-03-21 03:09:41 +08:00
|
|
|
if value == socket.assigns.value do
|
2022-11-19 22:08:00 +08:00
|
|
|
socket
|
|
|
|
else
|
2023-03-21 03:09:41 +08:00
|
|
|
image_info =
|
2022-11-19 22:08:00 +08:00
|
|
|
if value do
|
2023-03-21 03:09:41 +08:00
|
|
|
%{data: Base.encode64(value.data), height: value.height, width: value.width}
|
2022-11-19 22:08:00 +08:00
|
|
|
end
|
|
|
|
|
2023-03-21 03:09:41 +08:00
|
|
|
push_event(socket, "image_input_change:#{socket.assigns.id}", %{image_info: image_info})
|
2022-11-19 22:08:00 +08:00
|
|
|
end
|
|
|
|
|
2023-03-21 03:09:41 +08:00
|
|
|
{:ok, assign(socket, value: value)}
|
2022-11-19 22:08:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
|
|
|
<div
|
|
|
|
id={"#{@id}-root"}
|
2022-12-22 22:06:17 +08:00
|
|
|
class="inline-flex flex-col p-4 border-2 border-dashed border-gray-200 rounded-lg"
|
2022-11-19 22:08:00 +08:00
|
|
|
phx-hook="ImageInput"
|
2022-12-22 22:06:17 +08:00
|
|
|
phx-update="ignore"
|
2022-11-19 22:08:00 +08:00
|
|
|
data-id={@id}
|
2023-01-05 04:44:04 +08:00
|
|
|
data-phx-target={@myself}
|
2022-11-19 22:08:00 +08:00
|
|
|
data-height={@height}
|
|
|
|
data-width={@width}
|
|
|
|
data-format={@format}
|
|
|
|
data-fit={@fit}
|
|
|
|
>
|
2022-12-22 22:06:17 +08:00
|
|
|
<input type="file" data-input class="hidden" name="value" accept="image/*" capture="user" />
|
|
|
|
<div class="flex justify-center" data-preview>
|
|
|
|
<div class="flex justify-center text-gray-500">
|
|
|
|
Drag an image file
|
2022-11-19 22:08:00 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-12-22 22:06:17 +08:00
|
|
|
<div class="hidden flex justify-center" data-camera-preview></div>
|
|
|
|
<div class="mt-4 flex items-center justify-center gap-4">
|
2023-02-23 02:34:54 +08:00
|
|
|
<.menu id={"#{@id}-camera-select-menu"} position={:bottom_left}>
|
2022-12-22 22:06:17 +08:00
|
|
|
<:toggle>
|
|
|
|
<button
|
|
|
|
class="button-base button-gray border-transparent py-2 px-4 inline-flex text-gray-500"
|
|
|
|
data-btn-open-camera
|
|
|
|
>
|
|
|
|
<.remix_icon icon="camera-line" class="text-lg leading-none mr-2" />
|
|
|
|
<span>Open camera</span>
|
|
|
|
</button>
|
|
|
|
</:toggle>
|
2023-02-24 02:07:00 +08:00
|
|
|
<div data-camera-list>
|
|
|
|
<.menu_item>
|
|
|
|
<button role="menuitem" data-camera-id>
|
|
|
|
<span class="font-medium" data-label></span>
|
|
|
|
</button>
|
|
|
|
</.menu_item>
|
|
|
|
</div>
|
2022-12-22 22:06:17 +08:00
|
|
|
</.menu>
|
|
|
|
<button
|
|
|
|
class="hidden button-base button-gray border-transparent py-2 px-4 inline-flex text-gray-500"
|
|
|
|
data-btn-capture-camera
|
|
|
|
>
|
|
|
|
<.remix_icon icon="camera-line" class="text-lg leading-none mr-2" />
|
|
|
|
<span>Take photo</span>
|
|
|
|
</button>
|
|
|
|
<button
|
|
|
|
class="hidden button-base button-gray border-transparent py-2 px-4 inline-flex text-gray-500"
|
|
|
|
data-btn-cancel
|
|
|
|
>
|
|
|
|
<.remix_icon icon="close-circle-line" class="text-lg leading-none mr-2" />
|
|
|
|
<span>Cancel</span>
|
|
|
|
</button>
|
2022-12-29 22:09:09 +08:00
|
|
|
<button
|
|
|
|
class="button-base button-gray border-transparent py-2 px-4 inline-flex text-gray-500"
|
|
|
|
data-btn-upload
|
|
|
|
>
|
|
|
|
<.remix_icon icon="upload-2-line" class="text-lg leading-none mr-2" />
|
|
|
|
<span>Upload</span>
|
|
|
|
</button>
|
2022-12-22 22:06:17 +08:00
|
|
|
</div>
|
2022-11-19 22:08:00 +08:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
2023-01-05 04:44:04 +08:00
|
|
|
|
|
|
|
@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
|
2022-11-19 22:08:00 +08:00
|
|
|
end
|