2021-04-02 20:00:49 +08:00
|
|
|
defmodule LivebookWeb.SessionLive.InsertButtonsComponent do
|
2021-03-25 01:37:50 +08:00
|
|
|
use LivebookWeb, :live_component
|
|
|
|
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2022-08-02 21:51:02 +08:00
|
|
|
<div
|
|
|
|
class="relative top-0.5 m-0 flex justify-center"
|
2021-11-17 04:57:10 +08:00
|
|
|
role="toolbar"
|
|
|
|
aria-label="insert new"
|
2022-08-02 21:51:02 +08:00
|
|
|
data-el-insert-buttons
|
|
|
|
>
|
|
|
|
<div class={
|
|
|
|
"w-full absolute z-10 hover:z-[11] #{if(@persistent, do: "opacity-100", else: "opacity-0")} hover:opacity-100 focus-within:opacity-100 flex space-x-2 justify-center items-center"
|
|
|
|
}>
|
|
|
|
<button
|
|
|
|
class="button-base button-small"
|
2021-11-17 04:57:10 +08:00
|
|
|
phx-click="insert_cell_below"
|
2022-03-02 19:48:02 +08:00
|
|
|
phx-value-type="code"
|
2021-07-07 20:32:49 +08:00
|
|
|
phx-value-section_id={@section_id}
|
2021-11-17 04:57:10 +08:00
|
|
|
phx-value-cell_id={@cell_id}
|
2022-08-02 21:51:02 +08:00
|
|
|
>
|
|
|
|
+ Code
|
|
|
|
</button>
|
2022-04-15 03:17:19 +08:00
|
|
|
<.menu id={"#{@id}-block-menu"} position="bottom-left">
|
2022-03-25 02:02:31 +08:00
|
|
|
<:toggle>
|
|
|
|
<button class="button-base button-small">+ Block</button>
|
|
|
|
</:toggle>
|
|
|
|
<:content>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="menu-item text-gray-500"
|
2022-03-25 02:02:31 +08:00
|
|
|
role="menuitem"
|
|
|
|
phx-click="insert_cell_below"
|
|
|
|
phx-value-type="markdown"
|
|
|
|
phx-value-section_id={@section_id}
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-value-cell_id={@cell_id}
|
|
|
|
>
|
2022-03-25 02:02:31 +08:00
|
|
|
<.remix_icon icon="markdown-fill" />
|
|
|
|
<span class="font-medium">Markdown</span>
|
|
|
|
</button>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="menu-item text-gray-500"
|
2022-03-25 02:02:31 +08:00
|
|
|
role="menuitem"
|
|
|
|
phx-click="insert_section_below"
|
|
|
|
phx-value-section_id={@section_id}
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-value-cell_id={@cell_id}
|
|
|
|
>
|
2022-03-25 02:02:31 +08:00
|
|
|
<.remix_icon icon="h-2" />
|
|
|
|
<span class="font-medium">Section</span>
|
|
|
|
</button>
|
2022-04-19 22:51:46 +08:00
|
|
|
<div class="my-2 border-b border-gray-200"></div>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="menu-item text-gray-500"
|
2022-04-19 22:51:46 +08:00
|
|
|
role="menuitem"
|
|
|
|
phx-click="insert_cell_below"
|
|
|
|
phx-value-type="diagram"
|
|
|
|
phx-value-section_id={@section_id}
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-value-cell_id={@cell_id}
|
|
|
|
>
|
|
|
|
<.remix_icon icon="organization-chart" />
|
|
|
|
<span class="font-medium">Diagram</span>
|
|
|
|
</button>
|
2022-03-25 02:02:31 +08:00
|
|
|
</:content>
|
|
|
|
</.menu>
|
2022-03-31 03:55:50 +08:00
|
|
|
<%= cond do %>
|
2022-04-02 02:13:37 +08:00
|
|
|
<% not Livebook.Runtime.connected?(@runtime) -> %>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="button-base button-small"
|
2022-03-31 03:55:50 +08:00
|
|
|
phx-click={
|
|
|
|
with_confirm(
|
|
|
|
JS.push("setup_default_runtime"),
|
|
|
|
title: "Setup runtime",
|
|
|
|
description: ~s'''
|
2022-04-02 02:13:37 +08:00
|
|
|
To see the available smart cells, you need a connected runtime.
|
|
|
|
Do you want to connect and setup the default one?
|
2022-03-31 03:55:50 +08:00
|
|
|
''',
|
|
|
|
confirm_text: "Setup runtime",
|
|
|
|
confirm_icon: "play-line",
|
|
|
|
danger: false
|
|
|
|
)
|
2022-08-02 21:51:02 +08:00
|
|
|
}
|
|
|
|
>
|
|
|
|
+ Smart
|
|
|
|
</button>
|
2022-03-31 03:55:50 +08:00
|
|
|
<% @smart_cell_definitions == [] -> %>
|
|
|
|
<span class="tooltip right" data-tooltip="No smart cells available">
|
|
|
|
<button class="button-base button-small" disabled>+ Smart</button>
|
|
|
|
</span>
|
|
|
|
<% true -> %>
|
2022-04-15 03:17:19 +08:00
|
|
|
<.menu id={"#{@id}-smart-menu"} position="bottom-left">
|
2022-03-31 03:55:50 +08:00
|
|
|
<:toggle>
|
|
|
|
<button class="button-base button-small">+ Smart</button>
|
|
|
|
</:toggle>
|
|
|
|
<:content>
|
|
|
|
<%= for definition <- Enum.sort_by(@smart_cell_definitions, & &1.name) do %>
|
2022-04-05 01:48:57 +08:00
|
|
|
<.smart_cell_insert_button
|
|
|
|
definition={definition}
|
|
|
|
section_id={@section_id}
|
2022-08-02 21:51:02 +08:00
|
|
|
cell_id={@cell_id}
|
|
|
|
/>
|
2022-03-31 03:55:50 +08:00
|
|
|
<% end %>
|
|
|
|
</:content>
|
|
|
|
</.menu>
|
2022-02-28 20:53:33 +08:00
|
|
|
<% end %>
|
2021-03-25 01:37:50 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
2022-03-31 03:55:50 +08:00
|
|
|
|
2022-04-05 01:48:57 +08:00
|
|
|
defp smart_cell_insert_button(%{definition: %{requirement: %{variants: [_, _ | _]}}} = assigns) do
|
|
|
|
~H"""
|
|
|
|
<.submenu>
|
|
|
|
<button class="menu-item text-gray-500" role="menuitem">
|
|
|
|
<span class="font-medium"><%= @definition.name %></span>
|
|
|
|
</button>
|
|
|
|
<:content>
|
|
|
|
<%= for {variant, idx} <- Enum.with_index(@definition.requirement.variants) do %>
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="menu-item text-gray-500"
|
2022-04-05 01:48:57 +08:00
|
|
|
role="menuitem"
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-click={on_smart_cell_click(@definition, idx, @section_id, @cell_id)}
|
|
|
|
>
|
2022-04-05 01:48:57 +08:00
|
|
|
<span class="font-medium"><%= variant.name %></span>
|
|
|
|
</button>
|
|
|
|
<% end %>
|
|
|
|
</:content>
|
|
|
|
</.submenu>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp smart_cell_insert_button(assigns) do
|
|
|
|
~H"""
|
2022-08-02 21:51:02 +08:00
|
|
|
<button
|
|
|
|
class="menu-item text-gray-500"
|
2022-04-05 01:48:57 +08:00
|
|
|
role="menuitem"
|
2022-08-02 21:51:02 +08:00
|
|
|
phx-click={on_smart_cell_click(@definition, 0, @section_id, @cell_id)}
|
|
|
|
>
|
2022-04-05 01:48:57 +08:00
|
|
|
<span class="font-medium"><%= @definition.name %></span>
|
|
|
|
</button>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
|
|
|
defp on_smart_cell_click(%{requirement: nil} = definition, _variant_idx, section_id, cell_id) do
|
2022-03-31 03:55:50 +08:00
|
|
|
insert_smart_cell(definition, section_id, cell_id)
|
|
|
|
end
|
|
|
|
|
2022-04-05 01:48:57 +08:00
|
|
|
defp on_smart_cell_click(%{requirement: %{}} = definition, variant_idx, section_id, cell_id) do
|
2022-04-30 18:26:03 +08:00
|
|
|
variant = Enum.fetch!(definition.requirement.variants, variant_idx)
|
|
|
|
|
2022-03-31 03:55:50 +08:00
|
|
|
with_confirm(
|
2022-04-05 01:48:57 +08:00
|
|
|
JS.push("add_smart_cell_dependencies",
|
|
|
|
value: %{kind: definition.kind, variant_idx: variant_idx}
|
|
|
|
)
|
2022-04-30 18:26:03 +08:00
|
|
|
|> insert_smart_cell(definition, section_id, cell_id)
|
|
|
|
|> JS.push("queue_cells_reevaluation"),
|
|
|
|
title: "Add packages",
|
|
|
|
description:
|
|
|
|
case variant.packages do
|
|
|
|
[%{name: name}] ->
|
|
|
|
~s'''
|
|
|
|
The <span class="font-semibold">“#{definition.name}“</span>
|
|
|
|
smart cell requires the #{code_tag(name)} package. Do you want to add
|
|
|
|
it as a dependency and restart?
|
|
|
|
'''
|
|
|
|
|
|
|
|
packages ->
|
|
|
|
~s'''
|
|
|
|
The <span class="font-semibold">“#{definition.name}“</span>
|
|
|
|
smart cell requires the #{packages |> Enum.map(&code_tag(&1.name)) |> format_items()}
|
|
|
|
packages. Do you want to add them as dependencies and restart?
|
|
|
|
'''
|
|
|
|
end,
|
|
|
|
confirm_text: "Add and restart",
|
2022-03-31 03:55:50 +08:00
|
|
|
confirm_icon: "add-line",
|
2022-04-30 18:26:03 +08:00
|
|
|
danger: false,
|
|
|
|
html: true
|
2022-03-31 03:55:50 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-04-30 18:26:03 +08:00
|
|
|
defp code_tag(text), do: "<code>#{text}</code>"
|
|
|
|
|
2022-03-31 03:55:50 +08:00
|
|
|
defp insert_smart_cell(js \\ %JS{}, definition, section_id, cell_id) do
|
|
|
|
JS.push(js, "insert_cell_below",
|
|
|
|
value: %{
|
|
|
|
type: "smart",
|
|
|
|
kind: definition.kind,
|
|
|
|
section_id: section_id,
|
|
|
|
cell_id: cell_id
|
|
|
|
}
|
|
|
|
)
|
|
|
|
end
|
2021-03-25 01:37:50 +08:00
|
|
|
end
|