defmodule LivebookWeb.SessionLive.CellComponent do use LivebookWeb, :live_component @impl true def render(assigns) do ~H"""
<%= render_cell(assigns) %>
""" end defp render_cell(%{cell_view: %{type: :markdown}} = assigns) do ~H"""
<.cell_link_button cell_id={@cell_view.id} /> <%= live_patch to: Routes.session_path(@socket, :cell_upload, @session_id, @cell_view.id), class: "icon-button", aria_label: "insert image" do %> <.remix_icon icon="image-add-line" class="text-xl" /> <% end %> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} />
<.cell_body>
<.editor cell_view={@cell_view} />
<.content_placeholder bg_class="bg-gray-200" empty={@cell_view.empty?} />
""" end defp render_cell(%{cell_view: %{type: :elixir}} = assigns) do ~H"""
<%= if @cell_view.evaluation_status == :ready do %> <%= if @cell_view.validity_status != :fresh and @cell_view.reevaluate_automatically do %> <%= live_patch to: Routes.session_path(@socket, :cell_settings, @session_id, @cell_view.id), class: "text-gray-600 hover:text-gray-800 focus:text-gray-800 flex space-x-1 items-center" do %> <.remix_icon icon="check-line" class="text-xl" /> Reevaluates automatically <% end %> <% else %> <% end %> <% else %> <% end %>
<.cell_link_button cell_id={@cell_view.id} /> <.cell_settings_button cell_id={@cell_view.id} socket={@socket} session_id={@session_id} /> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} />
<.cell_body> <.editor cell_view={@cell_view} /> <%= if @cell_view.outputs != [] do %>
<.outputs cell_view={@cell_view} runtime={@runtime} socket={@socket} />
<% end %> """ end defp render_cell(%{cell_view: %{type: :input}} = assigns) do ~H"""
<.cell_link_button cell_id={@cell_view.id} /> <.cell_settings_button cell_id={@cell_view.id} socket={@socket} session_id={@session_id} /> <.move_cell_up_button cell_id={@cell_view.id} /> <.move_cell_down_button cell_id={@cell_view.id} /> <.delete_cell_button cell_id={@cell_view.id} />
<.cell_body>
<%= @cell_view.name %>
<.cell_input cell_view={@cell_view} /> <%= if @cell_view.error do %>
<%= String.capitalize(@cell_view.error) %>
<% end %>
""" end defp cell_input(%{cell_view: %{input_type: :textarea}} = assigns) do ~H""" """ end defp cell_input(%{cell_view: %{input_type: :range}} = assigns) do ~H"""
<%= @cell_view.props.min %>
<%= @cell_view.props.max %>
""" end defp cell_input(%{cell_view: %{input_type: :select}} = assigns) do ~H"""
""" end defp cell_input(%{cell_view: %{input_type: :checkbox}} = assigns) do ~H"""
<.switch_checkbox data-element="input" name="value" checked={@cell_view.value == "true"} />
""" end defp cell_input(%{cell_view: %{input_type: :password}} = assigns) do ~H""" <.with_password_toggle id={@cell_view.id}> """ end defp cell_input(assigns) do ~H""" """ end defp html_input_type(:password), do: "password" defp html_input_type(:number), do: "number" defp html_input_type(:color), do: "color" defp html_input_type(:range), do: "range" defp html_input_type(:select), do: "select" defp html_input_type(_), do: "text" defp cell_body(assigns) do ~H"""
<%= render_slot(@inner_block) %>
""" end defp cell_link_button(assigns) do ~H""" <.remix_icon icon="link" class="text-xl" /> """ end defp cell_settings_button(assigns) do ~H""" <%= live_patch to: Routes.session_path(@socket, :cell_settings, @session_id, @cell_id), class: "icon-button", aria_label: "cell settings" do %> <.remix_icon icon="settings-3-line" class="text-xl" /> <% end %> """ end defp move_cell_up_button(assigns) do ~H""" """ end defp move_cell_down_button(assigns) do ~H""" """ end defp delete_cell_button(assigns) do ~H""" """ end defp editor(assigns) do ~H"""
<.content_placeholder bg_class="bg-gray-500" empty={@cell_view.empty?} />
<%= if @cell_view.type == :elixir do %>
<.cell_status cell_view={@cell_view} />
<% end %>
""" end # The whole page has to load and then hooks are mounted. # There may be a tiny delay before the markdown is rendered # or editors are mounted, so show neat placeholders immediately. defp content_placeholder(assigns) do ~H""" <%= if @empty do %>
<% else %>
<% end %> """ end defp cell_status(%{cell_view: %{evaluation_status: :evaluating}} = assigns) do ~H""" <.status_indicator circle_class="bg-blue-500" animated_circle_class="bg-blue-400" change_indicator={true}> """ end defp cell_status(%{cell_view: %{evaluation_status: :queued}} = assigns) do ~H""" <.status_indicator circle_class="bg-gray-400" animated_circle_class="bg-gray-300"> Queued """ end defp cell_status(%{cell_view: %{validity_status: :evaluated}} = assigns) do ~H""" <.status_indicator circle_class="bg-green-400" change_indicator={true} tooltip={evaluated_label(@cell_view.evaluation_time_ms)}> Evaluated """ end defp cell_status(%{cell_view: %{validity_status: :stale}} = assigns) do ~H""" <.status_indicator circle_class="bg-yellow-200" change_indicator={true}> Stale """ end defp cell_status(%{cell_view: %{validity_status: :aborted}} = assigns) do ~H""" <.status_indicator circle_class="bg-gray-500"> Aborted """ end defp cell_status(assigns), do: ~H"" defp status_indicator(assigns) do assigns = assigns |> assign_new(:animated_circle_class, fn -> nil end) |> assign_new(:change_indicator, fn -> false end) |> assign_new(:tooltip, fn -> nil end) ~H"""
<%= render_slot(@inner_block) %> <%= if @change_indicator do %> * <% end %>
<%= if @animated_circle_class do %> <% end %>
""" end defp evaluated_label(time_ms) when is_integer(time_ms) do evaluation_time = if time_ms > 100 do seconds = time_ms |> Kernel./(1000) |> Float.floor(1) "#{seconds}s" else "#{time_ms}ms" end "Took " <> evaluation_time end defp evaluated_label(_time_ms), do: nil defp outputs(assigns) do ~H"""
<%= for {output, index} <- @cell_view.outputs |> Enum.reverse() |> Enum.with_index(), output != :ignored do %>
<%= LivebookWeb.Output.render_output(output, %{ id: "cell-#{@cell_view.id}-evaluation#{evaluation_number(@cell_view.evaluation_status, @cell_view.number_of_evaluations)}-output#{index}", socket: @socket, runtime: @runtime }) %>
<% end %>
""" end defp evaluation_number(:evaluating, number_of_evaluations), do: number_of_evaluations + 1 defp evaluation_number(_evaluation_status, number_of_evaluations), do: number_of_evaluations end