2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.SessionLive do
|
|
|
|
use LivebookWeb, :live_view
|
2021-01-08 21:14:26 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
import LivebookWeb.UserHelpers
|
|
|
|
|
2021-04-21 01:34:17 +08:00
|
|
|
alias Livebook.{SessionSupervisor, Session, Delta, Notebook, Runtime}
|
2021-05-21 20:56:25 +08:00
|
|
|
import Livebook.Utils, only: [access_by_id: 1]
|
2021-01-18 05:03:03 +08:00
|
|
|
|
2021-01-08 21:14:26 +08:00
|
|
|
@impl true
|
2021-05-04 02:03:19 +08:00
|
|
|
def mount(%{"id" => session_id}, %{"current_user_id" => current_user_id}, socket) do
|
2021-01-18 05:03:03 +08:00
|
|
|
if SessionSupervisor.session_exists?(session_id) do
|
2021-05-04 02:03:19 +08:00
|
|
|
current_user = build_current_user(current_user_id, socket)
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
data =
|
|
|
|
if connected?(socket) do
|
2021-05-04 02:03:19 +08:00
|
|
|
data = Session.register_client(session_id, self(), current_user)
|
2021-03-04 05:56:28 +08:00
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "sessions:#{session_id}")
|
2021-05-04 02:03:19 +08:00
|
|
|
Phoenix.PubSub.subscribe(Livebook.PubSub, "users:#{current_user_id}")
|
2021-01-18 05:03:03 +08:00
|
|
|
|
|
|
|
data
|
|
|
|
else
|
|
|
|
Session.get_data(session_id)
|
|
|
|
end
|
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
session_pid = Session.get_pid(session_id)
|
|
|
|
|
2021-02-18 20:14:09 +08:00
|
|
|
platform = platform_from_socket(socket)
|
|
|
|
|
2021-03-26 06:29:22 +08:00
|
|
|
{:ok,
|
|
|
|
socket
|
2021-04-22 05:02:09 +08:00
|
|
|
|> assign(
|
|
|
|
platform: platform,
|
|
|
|
session_id: session_id,
|
|
|
|
session_pid: session_pid,
|
2021-05-04 02:03:19 +08:00
|
|
|
current_user: current_user,
|
2021-05-07 22:41:37 +08:00
|
|
|
self: self(),
|
2021-04-22 05:02:09 +08:00
|
|
|
data_view: data_to_view(data)
|
|
|
|
)
|
2021-04-05 00:55:51 +08:00
|
|
|
|> assign_private(data: data)
|
|
|
|
|> allow_upload(:cell_image,
|
|
|
|
accept: ~w(.jpg .jpeg .png .gif),
|
|
|
|
max_entries: 1,
|
|
|
|
max_file_size: 5_000_000
|
|
|
|
)}
|
2021-01-18 05:03:03 +08:00
|
|
|
else
|
2021-02-21 23:54:44 +08:00
|
|
|
{:ok, redirect(socket, to: Routes.home_path(socket, :page))}
|
2021-01-18 05:03:03 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-26 06:29:22 +08:00
|
|
|
# Puts the given assigns in `socket.private`,
|
|
|
|
# to ensure they are not used for rendering.
|
|
|
|
defp assign_private(socket, assigns) do
|
|
|
|
Enum.reduce(assigns, socket, fn {key, value}, socket ->
|
|
|
|
put_in(socket.private[key], value)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-02-18 20:14:09 +08:00
|
|
|
defp platform_from_socket(socket) do
|
|
|
|
with connect_info when connect_info != nil <- get_connect_info(socket),
|
|
|
|
{:ok, user_agent} <- Map.fetch(connect_info, :user_agent) do
|
|
|
|
platform_from_user_agent(user_agent)
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-08 21:14:26 +08:00
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~L"""
|
2021-02-11 19:42:17 +08:00
|
|
|
<div class="flex flex-grow h-full"
|
2021-02-18 22:11:24 +08:00
|
|
|
id="session"
|
2021-03-11 22:28:18 +08:00
|
|
|
data-element="session"
|
|
|
|
phx-hook="Session">
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="w-16 flex flex-col items-center space-y-5 px-3 py-7 bg-gray-900">
|
2021-03-20 21:10:15 +08:00
|
|
|
<%= live_patch to: Routes.home_path(@socket, :page) do %>
|
2021-03-24 00:46:33 +08:00
|
|
|
<img src="/logo.png" height="40" width="40" alt="livebook" />
|
2021-03-20 21:10:15 +08:00
|
|
|
<% end %>
|
2021-03-23 22:27:03 +08:00
|
|
|
<span class="tooltip right distant" aria-label="Sections (ss)">
|
2021-05-04 02:03:19 +08:00
|
|
|
<button class="text-2xl text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center" data-element="sections-list-toggle">
|
2021-03-23 05:15:40 +08:00
|
|
|
<%= remix_icon("booklet-fill") %>
|
|
|
|
</button>
|
|
|
|
</span>
|
2021-05-04 02:03:19 +08:00
|
|
|
<span class="tooltip right distant" aria-label="Connected users (su)">
|
2021-05-07 22:41:37 +08:00
|
|
|
<button class="text-2xl text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center" data-element="clients-list-toggle">
|
2021-05-04 02:03:19 +08:00
|
|
|
<%= remix_icon("group-fill") %>
|
|
|
|
</button>
|
|
|
|
</span>
|
2021-04-22 05:02:09 +08:00
|
|
|
<span class="tooltip right distant" aria-label="Runtime settings (sr)">
|
|
|
|
<%= live_patch to: Routes.session_path(@socket, :runtime_settings, @session_id),
|
|
|
|
class: "text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center #{if(@live_action == :runtime_settings, do: "text-gray-50 bg-gray-700")}" do %>
|
|
|
|
<%= remix_icon("cpu-line", class: "text-2xl") %>
|
2021-03-23 05:15:40 +08:00
|
|
|
<% end %>
|
|
|
|
</span>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex-grow"></div>
|
2021-03-23 22:27:03 +08:00
|
|
|
<span class="tooltip right distant" aria-label="Keyboard shortcuts (?)">
|
2021-03-23 21:10:34 +08:00
|
|
|
<%= live_patch to: Routes.session_path(@socket, :shortcuts, @session_id),
|
2021-04-02 20:54:14 +08:00
|
|
|
class: "text-gray-400 hover:text-gray-50 focus:text-gray-50 rounded-xl h-10 w-10 flex items-center justify-center #{if(@live_action == :shortcuts, do: "text-gray-50 bg-gray-700")}" do %>
|
2021-03-23 21:10:34 +08:00
|
|
|
<%= remix_icon("keyboard-box-fill", class: "text-2xl") %>
|
2021-03-23 05:15:40 +08:00
|
|
|
<% end %>
|
|
|
|
</span>
|
2021-05-04 02:03:19 +08:00
|
|
|
<span class="tooltip right distant" aria-label="User profile">
|
|
|
|
<%= live_patch to: Routes.session_path(@socket, :user, @session_id),
|
|
|
|
class: "text-gray-400 rounded-xl h-8 w-8 flex items-center justify-center" do %>
|
|
|
|
<%= render_user_avatar(@current_user, class: "h-full w-full", text_class: "text-xs") %>
|
|
|
|
<% end %>
|
|
|
|
</span>
|
2021-03-20 21:10:15 +08:00
|
|
|
</div>
|
2021-04-21 23:42:03 +08:00
|
|
|
<div class="flex flex-col h-full w-full max-w-xs absolute z-30 top-0 left-[64px] shadow-xl md:static md:shadow-none overflow-y-auto bg-gray-50 border-r border-gray-100 px-6 py-10"
|
2021-05-04 02:03:19 +08:00
|
|
|
data-element="side-panel">
|
|
|
|
<div data-element="sections-list">
|
|
|
|
<div class="flex-grow flex flex-col">
|
|
|
|
<h3 class="font-semibold text-gray-800 text-lg">
|
|
|
|
Sections
|
|
|
|
</h3>
|
2021-05-07 22:41:37 +08:00
|
|
|
<div class="mt-4 flex flex-col space-y-4">
|
2021-05-04 02:03:19 +08:00
|
|
|
<%= for section_item <- @data_view.sections_items do %>
|
|
|
|
<button class="text-left hover:text-gray-900 text-gray-500"
|
2021-05-07 22:41:37 +08:00
|
|
|
data-element="sections-list-item"
|
2021-05-04 02:03:19 +08:00
|
|
|
data-section-id="<%= section_item.id %>">
|
|
|
|
<%= section_item.name %>
|
|
|
|
</button>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
<button class="mt-8 p-8 py-1 text-gray-500 text-sm font-medium rounded-xl border border-gray-400 border-dashed hover:bg-gray-100 inline-flex items-center justify-center space-x-2"
|
|
|
|
phx-click="add_section" >
|
|
|
|
<%= remix_icon("add-line", class: "text-lg align-center") %>
|
|
|
|
<span>New section</span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-05-07 22:41:37 +08:00
|
|
|
<div data-element="clients-list">
|
2021-05-04 02:03:19 +08:00
|
|
|
<div class="flex-grow flex flex-col">
|
|
|
|
<h3 class="font-semibold text-gray-800 text-lg">
|
|
|
|
Users
|
|
|
|
</h3>
|
|
|
|
<h4 class="font text-gray-500 text-sm my-1">
|
2021-05-07 22:41:37 +08:00
|
|
|
<%= length(@data_view.clients) %> connected
|
2021-05-04 02:03:19 +08:00
|
|
|
</h4>
|
2021-05-07 22:41:37 +08:00
|
|
|
<div class="mt-4 flex flex-col space-y-4">
|
|
|
|
<%= for {client_pid, user} <- @data_view.clients do %>
|
|
|
|
<div class="flex items-center justify-between space-x-2"
|
|
|
|
id="clients-list-item-<%= inspect(client_pid) %>"
|
|
|
|
data-element="clients-list-item"
|
|
|
|
data-client-pid="<%= inspect(client_pid) %>">
|
|
|
|
<button class="flex space-x-2 items-center text-gray-500 hover:text-gray-900 disabled:pointer-events-none"
|
|
|
|
<%= if client_pid == @self, do: "disabled" %>
|
|
|
|
data-element="client-link">
|
|
|
|
<%= render_user_avatar(user, class: "h-7 w-7 flex-shrink-0", text_class: "text-xs") %>
|
|
|
|
<span><%= user.name || "Anonymous" %></span>
|
|
|
|
</button>
|
|
|
|
<%= if client_pid != @self do %>
|
|
|
|
<span class="tooltip left" aria-label="Follow this user"
|
|
|
|
data-element="client-follow-toggle"
|
|
|
|
data-meta="follow">
|
|
|
|
<button class="icon-button">
|
|
|
|
<%= remix_icon("pushpin-line", class: "text-lg") %>
|
|
|
|
</button>
|
|
|
|
</span>
|
|
|
|
<span class="tooltip left" aria-label="Unfollow this user"
|
|
|
|
data-element="client-follow-toggle"
|
|
|
|
data-meta="unfollow">
|
|
|
|
<button class="icon-button">
|
|
|
|
<%= remix_icon("pushpin-fill", class: "text-lg") %>
|
|
|
|
</button>
|
|
|
|
</span>
|
|
|
|
<% end %>
|
2021-05-04 02:03:19 +08:00
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
2021-03-26 00:39:18 +08:00
|
|
|
</div>
|
2021-01-18 05:03:03 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex-grow overflow-y-auto" data-element="notebook">
|
2021-03-26 00:39:18 +08:00
|
|
|
<div class="py-7 px-16 max-w-screen-lg w-full mx-auto">
|
2021-04-22 05:02:09 +08:00
|
|
|
<div class="flex space-x-4 items-center pb-4 mb-6 border-b border-gray-200">
|
|
|
|
<h1 class="flex-grow text-gray-800 font-semibold text-3xl p-1 -ml-1 rounded-lg border border-transparent hover:border-blue-200 focus:border-blue-300"
|
2021-03-11 22:28:18 +08:00
|
|
|
id="notebook-name"
|
2021-03-30 01:50:46 +08:00
|
|
|
data-element="notebook-name"
|
2021-03-11 22:28:18 +08:00
|
|
|
contenteditable
|
|
|
|
spellcheck="false"
|
|
|
|
phx-blur="set_notebook_name"
|
|
|
|
phx-hook="ContentEditable"
|
2021-03-26 06:29:22 +08:00
|
|
|
data-update-attribute="phx-value-name"><%= @data_view.notebook_name %></h1>
|
2021-04-22 05:02:09 +08:00
|
|
|
<div class="relative" id="session-menu" phx-hook="Menu" data-element="menu">
|
|
|
|
<button class="icon-button" data-toggle>
|
|
|
|
<%= remix_icon("more-2-fill", class: "text-xl") %>
|
|
|
|
</button>
|
|
|
|
<div class="menu" data-content>
|
|
|
|
<button class="menu__item text-gray-500"
|
|
|
|
phx-click="fork_session">
|
|
|
|
<%= remix_icon("git-branch-line") %>
|
|
|
|
<span class="font-medium">Fork</span>
|
|
|
|
</button>
|
|
|
|
<%= link to: live_dashboard_process_path(@socket, @session_pid),
|
|
|
|
class: "menu__item text-gray-500",
|
|
|
|
target: "_blank" do %>
|
|
|
|
<%= remix_icon("dashboard-2-line") %>
|
|
|
|
<span class="font-medium">See on Dashboard</span>
|
|
|
|
<% end %>
|
|
|
|
<%= live_patch to: Routes.home_path(@socket, :close_session, @session_id),
|
|
|
|
class: "menu__item text-red-600" do %>
|
|
|
|
<%= remix_icon("close-circle-line") %>
|
|
|
|
<span class="font-medium">Close</span>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-11 22:28:18 +08:00
|
|
|
</div>
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="flex flex-col w-full space-y-16">
|
2021-03-26 06:29:22 +08:00
|
|
|
<%= if @data_view.section_views == [] do %>
|
2021-03-25 01:37:50 +08:00
|
|
|
<div class="flex justify-center">
|
2021-03-26 00:39:18 +08:00
|
|
|
<button class="button button-small"
|
2021-03-25 01:37:50 +08:00
|
|
|
phx-click="insert_section"
|
|
|
|
phx-value-index="0"
|
|
|
|
>+ Section</button>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
2021-03-26 06:29:22 +08:00
|
|
|
<%= for {section_view, index} <- Enum.with_index(@data_view.section_views) do %>
|
2021-04-02 20:00:49 +08:00
|
|
|
<%= live_component @socket, LivebookWeb.SessionLive.SectionComponent,
|
2021-03-26 06:29:22 +08:00
|
|
|
id: section_view.id,
|
2021-03-25 01:37:50 +08:00
|
|
|
index: index,
|
2021-03-11 22:28:18 +08:00
|
|
|
session_id: @session_id,
|
2021-03-26 06:29:22 +08:00
|
|
|
section_view: section_view %>
|
2021-03-11 22:28:18 +08:00
|
|
|
<% end %>
|
2021-03-12 23:40:37 +08:00
|
|
|
<div style="height: 80vh"></div>
|
2021-03-11 22:28:18 +08:00
|
|
|
</div>
|
2021-01-18 05:03:03 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-04-04 18:42:46 +08:00
|
|
|
<div class="fixed bottom-[0.4rem] right-[1.5rem]">
|
2021-04-01 18:56:19 +08:00
|
|
|
<%= live_component @socket, LivebookWeb.SessionLive.IndicatorsComponent,
|
|
|
|
session_id: @session_id,
|
|
|
|
data_view: @data_view %>
|
2021-02-18 20:14:09 +08:00
|
|
|
</div>
|
2021-03-11 22:28:18 +08:00
|
|
|
</div>
|
2021-04-02 20:00:49 +08:00
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
<%= if @live_action == :user do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.UserComponent,
|
|
|
|
id: :user_modal,
|
|
|
|
modal_class: "w-full max-w-sm",
|
|
|
|
user: @current_user,
|
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id) %>
|
|
|
|
<% end %>
|
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
<%= if @live_action == :runtime_settings do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.SessionLive.RuntimeComponent,
|
|
|
|
id: :runtime_settings_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-4xl",
|
2021-04-22 05:02:09 +08:00
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id),
|
|
|
|
session_id: @session_id,
|
|
|
|
runtime: @data_view.runtime %>
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<%= if @live_action == :file_settings do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.SessionLive.PersistenceComponent,
|
|
|
|
id: :runtime_settings_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-4xl",
|
2021-04-02 20:00:49 +08:00
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id),
|
|
|
|
session_id: @session_id,
|
2021-04-22 05:02:09 +08:00
|
|
|
current_path: @data_view.path,
|
|
|
|
path: @data_view.path %>
|
2021-04-02 20:00:49 +08:00
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<%= if @live_action == :shortcuts do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.SessionLive.ShortcutsComponent,
|
|
|
|
id: :shortcuts_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-5xl",
|
2021-04-02 20:00:49 +08:00
|
|
|
platform: @platform,
|
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id) %>
|
|
|
|
<% end %>
|
|
|
|
|
|
|
|
<%= if @live_action == :cell_settings do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.SessionLive.CellSettingsComponent,
|
|
|
|
id: :cell_settings_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-xl",
|
2021-04-02 20:00:49 +08:00
|
|
|
session_id: @session_id,
|
|
|
|
cell: @cell,
|
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id) %>
|
|
|
|
<% end %>
|
2021-04-05 00:55:51 +08:00
|
|
|
|
|
|
|
<%= if @live_action == :cell_upload do %>
|
|
|
|
<%= live_modal @socket, LivebookWeb.SessionLive.CellUploadComponent,
|
|
|
|
id: :cell_upload_modal,
|
2021-04-28 20:28:28 +08:00
|
|
|
modal_class: "w-full max-w-xl",
|
2021-04-05 00:55:51 +08:00
|
|
|
session_id: @session_id,
|
|
|
|
cell: @cell,
|
|
|
|
uploads: @uploads,
|
|
|
|
return_to: Routes.session_path(@socket, :page, @session_id) %>
|
|
|
|
<% end %>
|
2021-01-08 21:14:26 +08:00
|
|
|
"""
|
|
|
|
end
|
2021-01-18 05:03:03 +08:00
|
|
|
|
2021-02-11 19:42:17 +08:00
|
|
|
@impl true
|
2021-03-04 05:23:48 +08:00
|
|
|
def handle_params(%{"cell_id" => cell_id}, _url, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
{:ok, cell, _} = Notebook.fetch_cell_and_section(socket.private.data.notebook, cell_id)
|
2021-03-04 05:23:48 +08:00
|
|
|
{:noreply, assign(socket, cell: cell)}
|
|
|
|
end
|
|
|
|
|
2021-02-11 19:42:17 +08:00
|
|
|
def handle_params(_params, _url, socket) do
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
@impl true
|
2021-05-07 22:41:37 +08:00
|
|
|
def handle_event("session_init", _params, socket) do
|
|
|
|
data = socket.private.data
|
|
|
|
|
|
|
|
payload = %{
|
|
|
|
clients:
|
|
|
|
Enum.map(data.clients_map, fn {client_pid, user_id} ->
|
|
|
|
client_info(client_pid, data.users_map[user_id])
|
|
|
|
end)
|
|
|
|
}
|
|
|
|
|
|
|
|
{:reply, payload, socket}
|
|
|
|
end
|
|
|
|
|
2021-01-30 07:33:04 +08:00
|
|
|
def handle_event("cell_init", %{"cell_id" => cell_id}, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
data = socket.private.data
|
2021-01-30 07:33:04 +08:00
|
|
|
|
|
|
|
case Notebook.fetch_cell_and_section(data.notebook, cell_id) do
|
|
|
|
{:ok, cell, _section} ->
|
|
|
|
payload = %{
|
|
|
|
source: cell.source,
|
|
|
|
revision: data.cell_infos[cell.id].revision
|
|
|
|
}
|
|
|
|
|
|
|
|
{:reply, payload, socket}
|
|
|
|
|
|
|
|
:error ->
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
def handle_event("add_section", _params, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
end_index = length(socket.private.data.notebook.sections)
|
2021-01-18 05:03:03 +08:00
|
|
|
Session.insert_section(socket.assigns.session_id, end_index)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-25 01:37:50 +08:00
|
|
|
def handle_event("insert_section", %{"index" => index}, socket) do
|
|
|
|
index = ensure_integer(index) |> max(0)
|
|
|
|
Session.insert_section(socket.assigns.session_id, index)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
def handle_event("delete_section", %{"section_id" => section_id}, socket) do
|
|
|
|
Session.delete_section(socket.assigns.session_id, section_id)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event(
|
|
|
|
"insert_cell",
|
|
|
|
%{"section_id" => section_id, "index" => index, "type" => type},
|
|
|
|
socket
|
|
|
|
) do
|
2021-03-11 22:28:18 +08:00
|
|
|
index = ensure_integer(index) |> max(0)
|
2021-01-18 05:03:03 +08:00
|
|
|
type = String.to_atom(type)
|
|
|
|
Session.insert_cell(socket.assigns.session_id, section_id, index, type)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("insert_cell_below", %{"cell_id" => cell_id, "type" => type}, socket) do
|
2021-02-04 23:01:59 +08:00
|
|
|
type = String.to_atom(type)
|
2021-03-26 06:29:22 +08:00
|
|
|
insert_cell_next_to(socket, cell_id, type, idx_offset: 1)
|
2021-02-04 23:01:59 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("insert_cell_above", %{"cell_id" => cell_id, "type" => type}, socket) do
|
2021-02-04 23:01:59 +08:00
|
|
|
type = String.to_atom(type)
|
2021-03-26 06:29:22 +08:00
|
|
|
insert_cell_next_to(socket, cell_id, type, idx_offset: 0)
|
2021-02-04 23:01:59 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("delete_cell", %{"cell_id" => cell_id}, socket) do
|
|
|
|
Session.delete_cell(socket.assigns.session_id, cell_id)
|
2021-02-04 23:01:59 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
def handle_event("set_notebook_name", %{"name" => name}, socket) do
|
|
|
|
name = normalize_name(name)
|
|
|
|
Session.set_notebook_name(socket.assigns.session_id, name)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_event("set_section_name", %{"section_id" => section_id, "name" => name}, socket) do
|
|
|
|
name = normalize_name(name)
|
|
|
|
Session.set_section_name(socket.assigns.session_id, section_id, name)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-01-21 20:11:45 +08:00
|
|
|
def handle_event(
|
2021-01-30 07:33:04 +08:00
|
|
|
"apply_cell_delta",
|
2021-01-21 20:11:45 +08:00
|
|
|
%{"cell_id" => cell_id, "delta" => delta, "revision" => revision},
|
|
|
|
socket
|
|
|
|
) do
|
|
|
|
delta = Delta.from_compressed(delta)
|
2021-03-02 15:50:31 +08:00
|
|
|
Session.apply_cell_delta(socket.assigns.session_id, cell_id, delta, revision)
|
2021-01-21 20:11:45 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-02-24 04:20:46 +08:00
|
|
|
def handle_event(
|
|
|
|
"report_cell_revision",
|
|
|
|
%{"cell_id" => cell_id, "revision" => revision},
|
|
|
|
socket
|
|
|
|
) do
|
2021-03-02 15:50:31 +08:00
|
|
|
Session.report_cell_revision(socket.assigns.session_id, cell_id, revision)
|
2021-02-24 04:20:46 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("move_cell", %{"cell_id" => cell_id, "offset" => offset}, socket) do
|
2021-03-02 01:17:24 +08:00
|
|
|
offset = ensure_integer(offset)
|
2021-03-11 22:28:18 +08:00
|
|
|
Session.move_cell(socket.assigns.session_id, cell_id, offset)
|
2021-03-01 20:29:46 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-04-20 18:12:29 +08:00
|
|
|
def handle_event("move_section", %{"section_id" => section_id, "offset" => offset}, socket) do
|
|
|
|
offset = ensure_integer(offset)
|
|
|
|
Session.move_section(socket.assigns.session_id, section_id, offset)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("queue_cell_evaluation", %{"cell_id" => cell_id}, socket) do
|
|
|
|
Session.queue_cell_evaluation(socket.assigns.session_id, cell_id)
|
|
|
|
{:noreply, socket}
|
2021-02-03 02:58:06 +08:00
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("queue_section_cells_evaluation", %{"section_id" => section_id}, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
with {:ok, section} <- Notebook.fetch_section(socket.private.data.notebook, section_id) do
|
2021-03-11 22:28:18 +08:00
|
|
|
for cell <- section.cells, cell.type == :elixir do
|
|
|
|
Session.queue_cell_evaluation(socket.assigns.session_id, cell.id)
|
|
|
|
end
|
2021-02-03 02:58:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("queue_all_cells_evaluation", _params, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
data = socket.private.data
|
2021-02-18 20:14:09 +08:00
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
for {cell, _} <- Notebook.elixir_cells_with_section(data.notebook),
|
|
|
|
data.cell_infos[cell.id].validity_status != :evaluated do
|
|
|
|
Session.queue_cell_evaluation(socket.assigns.session_id, cell.id)
|
2021-02-18 20:14:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("queue_child_cells_evaluation", %{"cell_id" => cell_id}, socket) do
|
|
|
|
with {:ok, cell, _section} <-
|
2021-03-26 06:29:22 +08:00
|
|
|
Notebook.fetch_cell_and_section(socket.private.data.notebook, cell_id) do
|
|
|
|
for {cell, _} <- Notebook.child_cells_with_section(socket.private.data.notebook, cell.id) do
|
2021-02-04 23:01:59 +08:00
|
|
|
Session.queue_cell_evaluation(socket.assigns.session_id, cell.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-03-11 22:28:18 +08:00
|
|
|
def handle_event("cancel_cell_evaluation", %{"cell_id" => cell_id}, socket) do
|
|
|
|
Session.cancel_cell_evaluation(socket.assigns.session_id, cell_id)
|
2021-02-22 21:21:28 +08:00
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-04-01 18:56:19 +08:00
|
|
|
def handle_event("save", %{}, socket) do
|
2021-04-22 05:02:09 +08:00
|
|
|
if socket.private.data.path do
|
|
|
|
Session.save(socket.assigns.session_id)
|
|
|
|
{:noreply, socket}
|
|
|
|
else
|
|
|
|
{:noreply,
|
|
|
|
push_patch(socket,
|
|
|
|
to: Routes.session_path(socket, :file_settings, socket.assigns.session_id)
|
|
|
|
)}
|
|
|
|
end
|
2021-04-01 18:56:19 +08:00
|
|
|
end
|
|
|
|
|
2021-02-18 20:14:09 +08:00
|
|
|
def handle_event("show_shortcuts", %{}, socket) do
|
|
|
|
{:noreply,
|
|
|
|
push_patch(socket, to: Routes.session_path(socket, :shortcuts, socket.assigns.session_id))}
|
|
|
|
end
|
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
def handle_event("show_runtime_settings", %{}, socket) do
|
2021-04-14 18:20:51 +08:00
|
|
|
{:noreply,
|
|
|
|
push_patch(socket,
|
2021-04-22 05:02:09 +08:00
|
|
|
to: Routes.session_path(socket, :runtime_settings, socket.assigns.session_id)
|
2021-04-14 18:20:51 +08:00
|
|
|
)}
|
|
|
|
end
|
|
|
|
|
2021-04-21 01:34:17 +08:00
|
|
|
def handle_event("completion_request", %{"hint" => hint, "cell_id" => cell_id}, socket) do
|
|
|
|
data = socket.private.data
|
|
|
|
|
|
|
|
with {:ok, cell, _section} <- Notebook.fetch_cell_and_section(data.notebook, cell_id) do
|
|
|
|
if data.runtime do
|
|
|
|
prev_ref =
|
|
|
|
case Notebook.parent_cells_with_section(data.notebook, cell.id) do
|
|
|
|
[{parent, _} | _] -> parent.id
|
|
|
|
[] -> nil
|
|
|
|
end
|
|
|
|
|
|
|
|
ref = make_ref()
|
|
|
|
Runtime.request_completion_items(data.runtime, self(), ref, hint, :main, prev_ref)
|
|
|
|
|
|
|
|
{:reply, %{"completion_ref" => inspect(ref)}, socket}
|
|
|
|
else
|
|
|
|
{:reply, %{"completion_ref" => nil}, socket}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
_ -> {:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
def handle_event("fork_session", %{}, socket) do
|
|
|
|
notebook = Notebook.forked(socket.private.data.notebook)
|
|
|
|
%{images_dir: images_dir} = Session.get_summary(socket.assigns.session_id)
|
|
|
|
create_session(socket, notebook: notebook, copy_images_from: images_dir)
|
|
|
|
end
|
|
|
|
|
2021-05-07 22:41:37 +08:00
|
|
|
def handle_event("location_report", report, socket) do
|
|
|
|
Phoenix.PubSub.broadcast_from(
|
|
|
|
Livebook.PubSub,
|
|
|
|
self(),
|
|
|
|
"sessions:#{socket.assigns.session_id}",
|
|
|
|
{:location_report, self(), report}
|
|
|
|
)
|
|
|
|
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
|
2021-04-22 05:02:09 +08:00
|
|
|
defp create_session(socket, opts) do
|
|
|
|
case SessionSupervisor.create_session(opts) do
|
|
|
|
{:ok, id} ->
|
|
|
|
{:noreply, push_redirect(socket, to: Routes.session_path(socket, :page, id))}
|
|
|
|
|
|
|
|
{:error, reason} ->
|
|
|
|
{:noreply, put_flash(socket, :error, "Failed to create a notebook: #{reason}")}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-18 05:03:03 +08:00
|
|
|
@impl true
|
|
|
|
def handle_info({:operation, operation}, socket) do
|
2021-03-26 06:29:22 +08:00
|
|
|
case Session.Data.apply_operation(socket.private.data, operation) do
|
2021-01-21 20:11:45 +08:00
|
|
|
{:ok, data, actions} ->
|
2021-02-04 23:01:59 +08:00
|
|
|
new_socket =
|
|
|
|
socket
|
2021-03-26 06:29:22 +08:00
|
|
|
|> assign_private(data: data)
|
2021-05-21 20:56:25 +08:00
|
|
|
|> assign(data_view: update_data_view(socket.assigns.data_view, data, operation))
|
2021-02-04 23:01:59 +08:00
|
|
|
|> after_operation(socket, operation)
|
|
|
|
|> handle_actions(actions)
|
|
|
|
|
|
|
|
{:noreply, new_socket}
|
2021-01-18 05:03:03 +08:00
|
|
|
|
|
|
|
:error ->
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
|
|
|
end
|
2021-01-21 20:11:45 +08:00
|
|
|
|
2021-02-11 19:42:17 +08:00
|
|
|
def handle_info({:error, error}, socket) do
|
2021-03-30 01:52:06 +08:00
|
|
|
message = error |> to_string() |> upcase_first()
|
2021-02-11 19:42:17 +08:00
|
|
|
|
|
|
|
{:noreply, put_flash(socket, :error, message)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info({:info, info}, socket) do
|
2021-03-30 01:52:06 +08:00
|
|
|
message = info |> to_string() |> upcase_first()
|
2021-02-11 19:42:17 +08:00
|
|
|
|
|
|
|
{:noreply, put_flash(socket, :info, message)}
|
|
|
|
end
|
|
|
|
|
2021-02-22 02:17:14 +08:00
|
|
|
def handle_info(:session_closed, socket) do
|
|
|
|
{:noreply,
|
|
|
|
socket
|
|
|
|
|> put_flash(:info, "Session has been closed")
|
|
|
|
|> push_redirect(to: Routes.home_path(socket, :page))}
|
|
|
|
end
|
|
|
|
|
2021-04-21 01:34:17 +08:00
|
|
|
def handle_info({:completion_response, ref, items}, socket) do
|
|
|
|
payload = %{"completion_ref" => inspect(ref), "items" => items}
|
|
|
|
{:noreply, push_event(socket, "completion_response", payload)}
|
|
|
|
end
|
|
|
|
|
2021-05-04 02:03:19 +08:00
|
|
|
def handle_info(
|
|
|
|
{:user_change, %{id: id} = user},
|
|
|
|
%{assigns: %{current_user: %{id: id}}} = socket
|
|
|
|
) do
|
|
|
|
{:noreply, assign(socket, :current_user, user)}
|
|
|
|
end
|
|
|
|
|
2021-05-07 22:41:37 +08:00
|
|
|
def handle_info({:location_report, client_pid, report}, socket) do
|
|
|
|
report = Map.put(report, :client_pid, inspect(client_pid))
|
|
|
|
{:noreply, push_event(socket, "location_report", report)}
|
|
|
|
end
|
|
|
|
|
2021-02-11 19:42:17 +08:00
|
|
|
def handle_info(_message, socket), do: {:noreply, socket}
|
|
|
|
|
2021-05-07 22:41:37 +08:00
|
|
|
defp after_operation(socket, _prev_socket, {:client_join, client_pid, user}) do
|
|
|
|
push_event(socket, "client_joined", %{client: client_info(client_pid, user)})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp after_operation(socket, _prev_socket, {:client_leave, client_pid}) do
|
|
|
|
push_event(socket, "client_left", %{client_pid: inspect(client_pid)})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp after_operation(socket, _prev_socket, {:update_user, _client_pid, user}) do
|
|
|
|
updated_clients =
|
|
|
|
socket.private.data.clients_map
|
|
|
|
|> Enum.filter(fn {_client_pid, user_id} -> user_id == user.id end)
|
|
|
|
|> Enum.map(fn {client_pid, _user_id} -> client_info(client_pid, user) end)
|
|
|
|
|
|
|
|
push_event(socket, "clients_updated", %{clients: updated_clients})
|
|
|
|
end
|
|
|
|
|
2021-03-02 15:50:31 +08:00
|
|
|
defp after_operation(socket, _prev_socket, {:insert_section, client_pid, _index, section_id}) do
|
|
|
|
if client_pid == self() do
|
2021-03-11 22:28:18 +08:00
|
|
|
push_event(socket, "section_inserted", %{section_id: section_id})
|
2021-03-02 15:50:31 +08:00
|
|
|
else
|
|
|
|
socket
|
|
|
|
end
|
2021-02-04 23:01:59 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:50:31 +08:00
|
|
|
defp after_operation(socket, _prev_socket, {:delete_section, _client_pid, section_id}) do
|
2021-03-11 22:28:18 +08:00
|
|
|
push_event(socket, "section_deleted", %{section_id: section_id})
|
2021-02-04 23:01:59 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:50:31 +08:00
|
|
|
defp after_operation(socket, _prev_socket, {:insert_cell, client_pid, _, _, _, cell_id}) do
|
|
|
|
if client_pid == self() do
|
2021-03-11 22:28:18 +08:00
|
|
|
push_event(socket, "cell_inserted", %{cell_id: cell_id})
|
2021-03-02 15:50:31 +08:00
|
|
|
else
|
|
|
|
socket
|
|
|
|
end
|
2021-02-04 23:01:59 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:50:31 +08:00
|
|
|
defp after_operation(socket, prev_socket, {:delete_cell, _client_pid, cell_id}) do
|
2021-03-11 22:28:18 +08:00
|
|
|
# Find a sibling cell that the client would focus if the deleted cell has focus.
|
|
|
|
sibling_cell_id =
|
2021-03-26 06:29:22 +08:00
|
|
|
case Notebook.fetch_cell_sibling(prev_socket.private.data.notebook, cell_id, 1) do
|
2021-02-04 23:01:59 +08:00
|
|
|
{:ok, next_cell} ->
|
2021-03-11 22:28:18 +08:00
|
|
|
next_cell.id
|
2021-02-04 23:01:59 +08:00
|
|
|
|
|
|
|
:error ->
|
2021-03-26 06:29:22 +08:00
|
|
|
case Notebook.fetch_cell_sibling(prev_socket.private.data.notebook, cell_id, -1) do
|
2021-03-11 22:28:18 +08:00
|
|
|
{:ok, previous_cell} -> previous_cell.id
|
|
|
|
:error -> nil
|
2021-02-04 23:01:59 +08:00
|
|
|
end
|
|
|
|
end
|
2021-03-11 22:28:18 +08:00
|
|
|
|
|
|
|
push_event(socket, "cell_deleted", %{cell_id: cell_id, sibling_cell_id: sibling_cell_id})
|
|
|
|
end
|
|
|
|
|
|
|
|
defp after_operation(socket, _prev_socket, {:move_cell, client_pid, cell_id, _offset}) do
|
|
|
|
if client_pid == self() do
|
|
|
|
push_event(socket, "cell_moved", %{cell_id: cell_id})
|
2021-02-04 23:01:59 +08:00
|
|
|
else
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-21 01:31:23 +08:00
|
|
|
defp after_operation(socket, _prev_socket, {:move_section, client_pid, section_id, _offset}) do
|
|
|
|
if client_pid == self() do
|
|
|
|
push_event(socket, "section_moved", %{section_id: section_id})
|
|
|
|
else
|
|
|
|
socket
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-04 23:01:59 +08:00
|
|
|
defp after_operation(socket, _prev_socket, _operation), do: socket
|
|
|
|
|
|
|
|
defp handle_actions(socket, actions) do
|
|
|
|
Enum.reduce(actions, socket, &handle_action(&2, &1))
|
2021-01-21 20:11:45 +08:00
|
|
|
end
|
|
|
|
|
2021-03-02 15:50:31 +08:00
|
|
|
defp handle_action(socket, {:broadcast_delta, client_pid, cell, delta}) do
|
|
|
|
if client_pid == self() do
|
2021-01-21 20:11:45 +08:00
|
|
|
push_event(socket, "cell_acknowledgement:#{cell.id}", %{})
|
|
|
|
else
|
|
|
|
push_event(socket, "cell_delta:#{cell.id}", %{delta: Delta.to_compressed(delta)})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_action(socket, _action), do: socket
|
2021-01-30 07:33:04 +08:00
|
|
|
|
2021-05-07 22:41:37 +08:00
|
|
|
defp client_info(pid, user) do
|
|
|
|
%{pid: inspect(pid), hex_color: user.hex_color, name: user.name || "Anonymous"}
|
|
|
|
end
|
|
|
|
|
2021-01-30 07:33:04 +08:00
|
|
|
defp normalize_name(name) do
|
|
|
|
name
|
|
|
|
|> String.trim()
|
|
|
|
|> String.replace(~r/\s+/, " ")
|
|
|
|
|> case do
|
|
|
|
"" -> "Untitled"
|
|
|
|
name -> name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-30 01:52:06 +08:00
|
|
|
def upcase_first(string) do
|
|
|
|
{head, tail} = String.split_at(string, 1)
|
|
|
|
String.upcase(head) <> tail
|
|
|
|
end
|
|
|
|
|
2021-03-26 06:29:22 +08:00
|
|
|
defp insert_cell_next_to(socket, cell_id, type, idx_offset: idx_offset) do
|
|
|
|
{:ok, cell, section} = Notebook.fetch_cell_and_section(socket.private.data.notebook, cell_id)
|
2021-03-11 22:28:18 +08:00
|
|
|
index = Enum.find_index(section.cells, &(&1 == cell))
|
2021-03-26 06:29:22 +08:00
|
|
|
Session.insert_cell(socket.assigns.session_id, section.id, index + idx_offset, type)
|
2021-01-30 07:33:04 +08:00
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
|
2021-03-02 01:17:24 +08:00
|
|
|
defp ensure_integer(n) when is_integer(n), do: n
|
|
|
|
defp ensure_integer(n) when is_binary(n), do: String.to_integer(n)
|
2021-03-26 06:29:22 +08:00
|
|
|
|
|
|
|
# Builds view-specific structure of data by cherry-picking
|
|
|
|
# only the relevant attributes.
|
|
|
|
# We then use `@data_view` in the templates and consequently
|
|
|
|
# irrelevant changes to data don't change `@data_view`, so LV doesn't
|
|
|
|
# have to traverse the whole template tree and no diff is sent to the client.
|
|
|
|
defp data_to_view(data) do
|
|
|
|
%{
|
|
|
|
path: data.path,
|
2021-04-01 18:56:19 +08:00
|
|
|
dirty: data.dirty,
|
2021-03-26 06:29:22 +08:00
|
|
|
runtime: data.runtime,
|
2021-04-01 18:56:19 +08:00
|
|
|
global_evaluation_status: global_evaluation_status(data),
|
2021-03-26 06:29:22 +08:00
|
|
|
notebook_name: data.notebook.name,
|
|
|
|
sections_items:
|
|
|
|
for section <- data.notebook.sections do
|
|
|
|
%{id: section.id, name: section.name}
|
|
|
|
end,
|
2021-05-07 22:41:37 +08:00
|
|
|
clients:
|
2021-05-04 02:03:19 +08:00
|
|
|
data.clients_map
|
2021-05-07 22:41:37 +08:00
|
|
|
|> Enum.map(fn {client_pid, user_id} -> {client_pid, data.users_map[user_id]} end)
|
|
|
|
|> Enum.sort_by(fn {_client_pid, user} -> user.name end),
|
2021-05-24 00:22:55 +08:00
|
|
|
section_views: section_views(data.notebook.sections, data)
|
2021-03-26 06:29:22 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-04-01 18:56:19 +08:00
|
|
|
defp global_evaluation_status(data) do
|
|
|
|
cells =
|
|
|
|
data.notebook
|
|
|
|
|> Notebook.elixir_cells_with_section()
|
|
|
|
|> Enum.map(fn {cell, _} -> cell end)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
evaluating = Enum.find(cells, &evaluating?(&1, data)) ->
|
|
|
|
{:evaluating, evaluating.id}
|
|
|
|
|
|
|
|
stale = Enum.find(cells, &stale?(&1, data)) ->
|
|
|
|
{:stale, stale.id}
|
|
|
|
|
|
|
|
evaluated = Enum.find(Enum.reverse(cells), &evaluated?(&1, data)) ->
|
|
|
|
{:evaluated, evaluated.id}
|
|
|
|
|
|
|
|
true ->
|
|
|
|
{:fresh, nil}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp evaluating?(cell, data), do: data.cell_infos[cell.id].evaluation_status == :evaluating
|
|
|
|
|
|
|
|
defp stale?(cell, data), do: data.cell_infos[cell.id].validity_status == :stale
|
|
|
|
|
|
|
|
defp evaluated?(cell, data), do: data.cell_infos[cell.id].validity_status == :evaluated
|
|
|
|
|
2021-05-24 00:22:55 +08:00
|
|
|
defp section_views(sections, data) do
|
|
|
|
sections
|
|
|
|
|> Enum.map(& &1.name)
|
|
|
|
|> names_to_html_ids()
|
|
|
|
|> Enum.zip(sections)
|
|
|
|
|> Enum.map(fn {html_id, section} ->
|
|
|
|
%{
|
|
|
|
id: section.id,
|
|
|
|
html_id: html_id,
|
|
|
|
name: section.name,
|
|
|
|
cell_views: Enum.map(section.cells, &cell_to_view(&1, data))
|
|
|
|
}
|
|
|
|
end)
|
2021-03-26 06:29:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
defp cell_to_view(cell, data) do
|
|
|
|
info = data.cell_infos[cell.id]
|
|
|
|
|
|
|
|
%{
|
|
|
|
id: cell.id,
|
|
|
|
type: cell.type,
|
|
|
|
empty?: cell.source == "",
|
|
|
|
outputs: cell.outputs,
|
|
|
|
validity_status: info.validity_status,
|
|
|
|
evaluation_status: info.evaluation_status,
|
|
|
|
changed?: info.evaluation_digest != nil and info.digest != info.evaluation_digest
|
|
|
|
}
|
|
|
|
end
|
2021-05-21 20:56:25 +08:00
|
|
|
|
|
|
|
# Updates current data_view in response to an operation.
|
|
|
|
# In most cases we simply recompute data_view, but for the
|
|
|
|
# most common ones we only update the relevant parts.
|
|
|
|
defp update_data_view(data_view, data, operation) do
|
|
|
|
case operation do
|
|
|
|
{:report_cell_revision, _pid, _cell_id, _revision} ->
|
|
|
|
data_view
|
|
|
|
|
|
|
|
{:apply_cell_delta, _pid, cell_id, _delta, _revision} ->
|
|
|
|
update_cell_view(data_view, data, cell_id)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
data_to_view(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp update_cell_view(data_view, data, cell_id) do
|
|
|
|
{:ok, cell, section} = Notebook.fetch_cell_and_section(data.notebook, cell_id)
|
|
|
|
cell_view = cell_to_view(cell, data)
|
|
|
|
|
|
|
|
put_in(
|
|
|
|
data_view,
|
|
|
|
[:section_views, access_by_id(section.id), :cell_views, access_by_id(cell.id)],
|
|
|
|
cell_view
|
|
|
|
)
|
|
|
|
end
|
2021-01-08 21:14:26 +08:00
|
|
|
end
|