defmodule LiveBookWeb.CellComponent do use LiveBookWeb, :live_component def render(assigns) do ~L"""
" id="cell-<%= @cell.id %>" phx-hook="Cell" data-cell-id="<%= @cell.id %>" data-type="<%= @cell.type %>" data-focused="<%= @focused %>" data-expanded="<%= @expanded %>"> <%= render_cell_content(assigns) %>
""" end def render_cell_content(%{cell: %{type: :markdown}} = assigns) do ~L""" <%= if @focused do %>
<% end %>
"> <%= render_editor(@cell, @cell_info) %>
<%= render_markdown_content_placeholder(@cell.source) %>
""" end def render_cell_content(%{cell: %{type: :elixir}} = assigns) do ~L""" <%= if @focused do %>
<% end %> <%= render_editor(@cell, @cell_info, show_status: true) %> <%= if @cell.outputs != [] do %>
<%= render_outputs(@cell.outputs) %>
<% end %> """ end defp render_editor(cell, cell_info, opts \\ []) do show_status = Keyword.get(opts, :show_status, false) assigns = %{cell: cell, cell_info: cell_info, show_status: show_status} ~L"""
<%= render_editor_content_placeholder(@cell.source) %>
<%= if @show_status do %>
<%= render_cell_status(@cell_info) %>
<% end %>
""" end # The whole page has to load and then hooks are mounded. # There may be a tiny delay before the markdown is rendered # or and editors are mounted, so show neat placeholders immediately. defp render_markdown_content_placeholder("" = _content) do assigns = %{} ~L"""
""" end defp render_markdown_content_placeholder(_content) do assigns = %{} ~L"""
""" end defp render_editor_content_placeholder("" = _content) do assigns = %{} ~L"""
""" end defp render_editor_content_placeholder(_content) do assigns = %{} ~L"""
""" end defp render_outputs(outputs) do assigns = %{outputs: outputs} ~L"""
<%= for output <- Enum.reverse(@outputs) do %>
<%= render_output(output) %>
<% end %>
""" end defp render_output(output) when is_binary(output) do assigns = %{output: output} ~L"""
<%= @output %>
""" end defp render_output({:inspect_html, inspected_html}) do assigns = %{inspected_html: inspected_html} ~L"""
<%= raw @inspected_html %>
""" end defp render_output({:error, formatted}) do assigns = %{formatted: formatted} ~L"""
<%= @formatted %>
""" end defp render_cell_status(%{evaluation_status: :evaluating}) do assigns = %{} ~L"""
Evaluating
""" end defp render_cell_status(%{evaluation_status: :queued}) do assigns = %{} ~L"""
Queued
""" end defp render_cell_status(%{validity_status: :evaluated}) do assigns = %{} ~L"""
Evaluated
""" end defp render_cell_status(%{validity_status: :stale}) do assigns = %{} ~L"""
Stale
""" end defp render_cell_status(_), do: nil end