defmodule LiveBookWeb.Cell do use LiveBookWeb, :live_component alias LiveBookWeb.Utils def render(assigns) do ~L"""
"> <%= 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({:ok, value}) do inspected = Utils.inspect_as_html(value, pretty: true, width: 100) assigns = %{inspected: inspected} ~L"""
<%= @inspected %>
""" end defp render_output({:error, kind, error, stacktrace}) do formatted = Exception.format(kind, error, stacktrace) 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