2022-02-28 20:53:33 +08:00
|
|
|
defmodule LivebookWeb.JSViewComponent do
|
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
2022-03-23 01:25:42 +08:00
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
{:ok,
|
|
|
|
socket
|
|
|
|
|> assign(assigns)
|
|
|
|
|> assign_new(:timeout_message, fn -> "Not available" end)}
|
|
|
|
end
|
|
|
|
|
2022-02-28 20:53:33 +08:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~H"""
|
2022-08-02 21:51:02 +08:00
|
|
|
<div
|
|
|
|
id={"js-output-#{@id}-#{@js_view.ref}"}
|
2022-02-28 20:53:33 +08:00
|
|
|
phx-hook="JSView"
|
|
|
|
phx-update="ignore"
|
|
|
|
data-ref={@js_view.ref}
|
2023-02-23 02:34:54 +08:00
|
|
|
data-assets-base-path={~p"/public/sessions/#{@session_id}/assets/#{@js_view.assets.hash}/"}
|
2022-02-28 20:53:33 +08:00
|
|
|
data-js-path={@js_view.assets.js_path}
|
2023-02-18 08:16:42 +08:00
|
|
|
data-session-token={session_token(@session_id, @client_id)}
|
|
|
|
data-connect-token={connect_token(@js_view.pid)}
|
2022-03-23 01:25:42 +08:00
|
|
|
data-iframe-local-port={LivebookWeb.IframeEndpoint.port()}
|
2022-05-26 08:39:55 +08:00
|
|
|
data-iframe-url={Livebook.Config.iframe_url()}
|
2022-08-02 21:51:02 +08:00
|
|
|
data-timeout-message={@timeout_message}
|
|
|
|
>
|
2022-02-28 20:53:33 +08:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2023-02-18 08:16:42 +08:00
|
|
|
defp session_token(session_id, client_id) do
|
|
|
|
Phoenix.Token.sign(LivebookWeb.Endpoint, "session", %{
|
|
|
|
session_id: session_id,
|
|
|
|
client_id: client_id
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp connect_token(pid) do
|
|
|
|
Phoenix.Token.sign(LivebookWeb.Endpoint, "js-view-connect", %{pid: pid})
|
2022-02-28 20:53:33 +08:00
|
|
|
end
|
|
|
|
end
|