2023-08-22 19:21:22 +08:00
|
|
|
defmodule LivebookWeb.Output.PlainTextComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(socket) do
|
2023-10-28 02:49:46 +08:00
|
|
|
{:ok, stream(socket, :chunks, [])}
|
2023-08-22 19:21:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
{text, assigns} = Map.pop(assigns, :text)
|
2023-10-28 02:49:46 +08:00
|
|
|
|
2023-08-22 19:21:22 +08:00
|
|
|
socket = assign(socket, assigns)
|
|
|
|
|
|
|
|
if text do
|
2023-11-16 00:33:43 +08:00
|
|
|
chunk = %{id: Livebook.Utils.random_long_id(), text: text}
|
2023-10-28 02:49:46 +08:00
|
|
|
{:ok, stream_insert(socket, :chunks, chunk)}
|
2023-08-22 19:21:22 +08:00
|
|
|
else
|
|
|
|
{:ok, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
2023-10-28 03:36:19 +08:00
|
|
|
<div
|
|
|
|
id={@id}
|
|
|
|
class="text-gray-700 whitespace-pre-wrap break-words"
|
|
|
|
phx-update="stream"
|
|
|
|
phx-no-format
|
|
|
|
><span
|
2023-10-28 02:49:46 +08:00
|
|
|
:for={{dom_id, chunk}<- @streams.chunks} id={dom_id}><%= chunk.text %></span></div>
|
2023-08-22 19:21:22 +08:00
|
|
|
"""
|
|
|
|
end
|
|
|
|
end
|