Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve section management #411

Merged
merged 4 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions assets/css/components.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@
@apply relative inline-block w-14 h-7 mr-2 select-none transition;
}

.switch-button--disabled {
@apply pointer-events-none opacity-50;
}

.switch-button__checkbox {
@apply appearance-none absolute block w-7 h-7 rounded-full bg-gray-400 border-[5px] border-gray-100 cursor-pointer transition-all duration-300;
}
Expand Down
30 changes: 24 additions & 6 deletions assets/js/session/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,10 @@ function handleDocumentKeyDown(hook, event) {
if (cmd && key === "s") {
cancelEvent(event);
saveNotebook(hook);
} else if (keyBuffer.tryMatch(["S", "n"])) {
insertSectionBelowFocusedCell(hook);
} else if (keyBuffer.tryMatch(["S", "N"])) {
jonatanklosko marked this conversation as resolved.
Show resolved Hide resolved
insertSectionAboveFocusedCell(hook);
} else if (keyBuffer.tryMatch(["d", "d"])) {
deleteFocusedCell(hook);
} else if (
Expand Down Expand Up @@ -316,8 +320,6 @@ function handleDocumentKeyDown(hook, event) {
insertCellBelowFocused(hook, "markdown");
} else if (keyBuffer.tryMatch(["M"])) {
insertCellAboveFocused(hook, "markdown");
} else if (keyBuffer.tryMatch(["S"])) {
addSection(hook);
}
}
}
Expand Down Expand Up @@ -642,10 +644,6 @@ function insertCellAboveFocused(hook, type) {
}
}

function addSection(hook) {
hook.pushEvent("add_section", {});
}

function insertFirstCell(hook, type) {
const sectionIds = getSectionIds();

Expand All @@ -658,6 +656,26 @@ function insertFirstCell(hook, type) {
}
}

function insertSectionBelowFocusedCell(hook) {
if (hook.state.focusedCellId) {
hook.pushEvent("insert_section_below_cell", {
cell_id: hook.state.focusedCellId,
});
} else {
hook.pushEvent("insert_section_at_end", {});
}
}

function insertSectionAboveFocusedCell(hook) {
if (hook.state.focusedCellId) {
hook.pushEvent("insert_section_above_cell", {
cell_id: hook.state.focusedCellId,
});
} else {
hook.pushEvent("insert_section_at_start", {});
}
}

function setFocusedCell(hook, cellId, scroll = true) {
hook.state.focusedCellId = cellId;

Expand Down
37 changes: 35 additions & 2 deletions lib/livebook/notebook.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ defmodule Livebook.Notebook do
%{notebook | sections: sections}
end

@doc """
Inserts `section` below the parent section.

Cells below the given index are moved to the newly inserted section.
"""
@spec insert_section_into(t(), Section.id(), non_neg_integer(), Section.t()) :: t()
def insert_section_into(notebook, section_id, index, section) do
{sections_above, [parent_section | sections_below]} =
Enum.split_while(notebook.sections, &(&1.id != section_id))

{cells_above, cells_below} = Enum.split(parent_section.cells, index)

sections =
sections_above ++
[%{parent_section | cells: cells_above}, %{section | cells: cells_below}] ++
sections_below

%{notebook | sections: sections}
end

@doc """
Inserts `cell` at the given `index` within section identified by `section_id`.
"""
Expand All @@ -106,11 +126,24 @@ defmodule Livebook.Notebook do

@doc """
Deletes section with the given id.

All cells are moved to the previous section if present.
"""
@spec delete_section(t(), Section.id()) :: t()
def delete_section(notebook, section_id) do
{_, notebook} = pop_in(notebook, [Access.key(:sections), access_by_id(section_id)])
notebook
sections =
case Enum.split_while(notebook.sections, &(&1.id != section_id)) do
{[], [_section | sections_below]} ->
sections_below

{sections_above, [section | sections_below]} ->
{prev_section, sections_above} = List.pop_at(sections_above, length(sections_above) - 1)

sections_above ++
[%{prev_section | cells: prev_section.cells ++ section.cells} | sections_below]
end

%{notebook | sections: sections}
end

@doc """
Expand Down
24 changes: 19 additions & 5 deletions lib/livebook/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ defmodule Livebook.Session do
GenServer.cast(name(session_id), {:insert_section, self(), index})
end

@doc """
Asynchronously sends section insertion request to the server.
"""
@spec insert_section_into(id(), Section.id(), non_neg_integer()) :: :ok
def insert_section_into(session_id, section_id, index) do
GenServer.cast(name(session_id), {:insert_section_into, self(), section_id, index})
end

@doc """
Asynchronously sends cell insertion request to the server.
"""
Expand All @@ -127,9 +135,9 @@ defmodule Livebook.Session do
@doc """
Asynchronously sends section deletion request to the server.
"""
@spec delete_section(id(), Section.id()) :: :ok
def delete_section(session_id, section_id) do
GenServer.cast(name(session_id), {:delete_section, self(), section_id})
@spec delete_section(id(), Section.id(), boolean()) :: :ok
def delete_section(session_id, section_id, delete_cells) do
GenServer.cast(name(session_id), {:delete_section, self(), section_id, delete_cells})
end

@doc """
Expand Down Expand Up @@ -352,14 +360,20 @@ defmodule Livebook.Session do
{:noreply, handle_operation(state, operation)}
end

def handle_cast({:insert_section_into, client_pid, section_id, index}, state) do
# Include new id in the operation, so it's reproducible
operation = {:insert_section_into, client_pid, section_id, index, Utils.random_id()}
{:noreply, handle_operation(state, operation)}
end

def handle_cast({:insert_cell, client_pid, section_id, index, type}, state) do
# Include new id in the operation, so it's reproducible
operation = {:insert_cell, client_pid, section_id, index, type, Utils.random_id()}
{:noreply, handle_operation(state, operation)}
end

def handle_cast({:delete_section, client_pid, section_id}, state) do
operation = {:delete_section, client_pid, section_id}
def handle_cast({:delete_section, client_pid, section_id, delete_cells}, state) do
operation = {:delete_section, client_pid, section_id, delete_cells}
{:noreply, handle_operation(state, operation)}
end

Expand Down
123 changes: 74 additions & 49 deletions lib/livebook/session/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ defmodule Livebook.Session.Data do

@type operation ::
{:insert_section, pid(), index(), Section.id()}
| {:insert_section_into, pid(), Section.id(), index(), Section.id()}
| {:insert_cell, pid(), Section.id(), index(), Cell.type(), Cell.id()}
| {:delete_section, pid(), Section.id()}
| {:delete_section, pid(), Section.id(), delete_cells :: boolean()}
| {:delete_cell, pid(), Cell.id()}
| {:move_cell, pid(), Cell.id(), offset :: integer()}
| {:move_section, pid(), Section.id(), offset :: integer()}
Expand Down Expand Up @@ -179,6 +180,18 @@ defmodule Livebook.Session.Data do
|> wrap_ok()
end

def apply_operation(data, {:insert_section_into, _client_pid, section_id, index, id}) do
with {:ok, _section} <- Notebook.fetch_section(data.notebook, section_id) do
section = %{Section.new() | id: id}

data
|> with_actions()
|> insert_section_into(section_id, index, section)
|> set_dirty()
|> wrap_ok()
end
end

def apply_operation(data, {:insert_cell, _client_pid, section_id, index, type, id}) do
with {:ok, _section} <- Notebook.fetch_section(data.notebook, section_id) do
cell = %{Cell.new(type) | id: id}
Expand All @@ -191,39 +204,24 @@ defmodule Livebook.Session.Data do
end
end

def apply_operation(data, {:delete_section, _client_pid, id}) do
with {:ok, section} <- Notebook.fetch_section(data.notebook, id) do
def apply_operation(data, {:delete_section, _client_pid, id, delete_cells}) do
with {:ok, section} <- Notebook.fetch_section(data.notebook, id),
true <- section != hd(data.notebook.sections) or delete_cells do
data
|> with_actions()
|> delete_section(section)
|> delete_section(section, delete_cells)
|> set_dirty()
|> wrap_ok()
else
_ -> :error
end
end

def apply_operation(data, {:delete_cell, _client_pid, id}) do
with {:ok, cell, section} <- Notebook.fetch_cell_and_section(data.notebook, id) do
case data.cell_infos[cell.id].evaluation_status do
:evaluating ->
data
|> with_actions()
|> clear_evaluation()
|> add_action({:stop_evaluation, section})

:queued ->
data
|> with_actions()
|> unqueue_cell_evaluation(cell, section)
|> unqueue_dependent_cells_evaluation(cell)
|> mark_dependent_cells_as_stale(cell)

_ ->
data
|> with_actions()
|> mark_dependent_cells_as_stale(cell)
end
|> delete_cell(cell)
|> add_action({:forget_evaluation, cell, section})
data
|> with_actions()
|> delete_cell(cell, section)
|> set_dirty()
|> wrap_ok()
end
Expand Down Expand Up @@ -332,26 +330,14 @@ defmodule Livebook.Session.Data do
end

def apply_operation(data, {:cancel_cell_evaluation, _client_pid, id}) do
with {:ok, cell, section} <- Notebook.fetch_cell_and_section(data.notebook, id) do
case data.cell_infos[cell.id].evaluation_status do
:evaluating ->
data
|> with_actions()
|> clear_evaluation()
|> add_action({:stop_evaluation, section})
|> wrap_ok()

:queued ->
data
|> with_actions()
|> unqueue_cell_evaluation(cell, section)
|> unqueue_dependent_cells_evaluation(cell)
|> mark_dependent_cells_as_stale(cell)
|> wrap_ok()

_ ->
:error
end
with {:ok, cell, section} <- Notebook.fetch_cell_and_section(data.notebook, id),
true <- data.cell_infos[cell.id].evaluation_status in [:evaluating, :queued] do
data
|> with_actions()
|> cancel_cell_evaluation(cell, section)
|> wrap_ok()
else
_ -> :error
end
end

Expand Down Expand Up @@ -491,6 +477,14 @@ defmodule Livebook.Session.Data do
)
end

defp insert_section_into({data, _} = data_actions, section_id, index, section) do
data_actions
|> set!(
notebook: Notebook.insert_section_into(data.notebook, section_id, index, section),
section_infos: Map.put(data.section_infos, section.id, new_section_info())
)
end

defp insert_cell({data, _} = data_actions, section_id, index, cell) do
data_actions
|> set!(
Expand All @@ -499,18 +493,29 @@ defmodule Livebook.Session.Data do
)
end

defp delete_section({data, _} = data_actions, section) do
defp delete_section(data_actions, section, delete_cells) do
{data, _} =
data_actions =
if delete_cells do
data_actions
|> reduce(Enum.reverse(section.cells), &delete_cell(&1, &2, section))
else
data_actions
end

data_actions
|> set!(
notebook: Notebook.delete_section(data.notebook, section.id),
section_infos: Map.delete(data.section_infos, section.id),
deleted_sections: [section | data.deleted_sections]
deleted_sections: [%{section | cells: []} | data.deleted_sections]
)
|> reduce(section.cells, &delete_cell_info/2)
end

defp delete_cell({data, _} = data_actions, cell) do
defp delete_cell({data, _} = data_actions, cell, section) do
data_actions
|> cancel_cell_evaluation(cell, section)
|> mark_dependent_cells_as_stale(cell)
|> add_action({:forget_evaluation, cell, section})
|> set!(
notebook: Notebook.delete_cell(data.notebook, cell.id),
deleted_cells: [cell | data.deleted_cells]
Expand Down Expand Up @@ -630,6 +635,8 @@ defmodule Livebook.Session.Data do
|> set_section_info!(section.id, evaluating_cell_id: nil)
end

defp mark_dependent_cells_as_stale(data_actions, %Cell.Markdown{}), do: data_actions

defp mark_dependent_cells_as_stale({data, _} = data_actions, cell) do
dependent = dependent_cells_with_section(data, cell.id)
mark_cells_as_stale(data_actions, dependent)
Expand Down Expand Up @@ -751,6 +758,24 @@ defmodule Livebook.Session.Data do
end)
end

defp cancel_cell_evaluation({data, _} = data_actions, cell, section) do
case data.cell_infos[cell.id].evaluation_status do
:evaluating ->
data_actions
|> clear_evaluation()
|> add_action({:stop_evaluation, section})

:queued ->
data_actions
|> unqueue_cell_evaluation(cell, section)
|> unqueue_dependent_cells_evaluation(cell)
|> mark_dependent_cells_as_stale(cell)

_ ->
data_actions
end
end

defp unqueue_dependent_cells_evaluation({data, _} = data_actions, cell) do
dependent = dependent_cells_with_section(data, cell.id)
unqueue_cells_evaluation(data_actions, dependent)
Expand Down
11 changes: 8 additions & 3 deletions lib/livebook_web/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,18 @@ defmodule LivebookWeb.Helpers do
@doc """
Renders a checkbox input styled as a switch.
"""
def render_switch(name, checked, label) do
assigns = %{name: name, checked: checked, label: label}
def render_switch(name, checked, label, opts \\ []) do
assigns = %{
name: name,
checked: checked,
label: label,
disabled: Keyword.get(opts, :disabled, false)
}

~L"""
<div class="flex space-x-3 items-center justify-between">
<span class="text-gray-700"><%= @label %></span>
<label class="switch-button">
<label class="switch-button <%= if(@disabled, do: "switch-button--disabled") %>">
<%= tag :input, class: "switch-button__checkbox", type: "checkbox", name: @name, checked: @checked %>
<div class="switch-button__bg"></div>
</label>
Expand Down
Loading