2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.PathSelectComponent do
|
|
|
|
use LivebookWeb, :live_component
|
2021-02-21 23:54:44 +08:00
|
|
|
|
|
|
|
# The component expects:
|
|
|
|
#
|
|
|
|
# * `path` - the currently entered path
|
|
|
|
# * `running_paths` - the list of notebook paths that are already linked to running sessions
|
2021-02-27 03:53:29 +08:00
|
|
|
# * `extnames` - a list of file extensions that should be shown
|
2021-04-13 04:59:48 +08:00
|
|
|
# * `phx_target` - id of the component to send update events to or nil to send to the parent LV
|
|
|
|
# * `phx_submit` - the event name sent on form submission, use `nil` for no action
|
2021-02-21 23:54:44 +08:00
|
|
|
#
|
|
|
|
# The target receives `set_path` events with `%{"path" => path}` payload.
|
2021-03-20 21:10:15 +08:00
|
|
|
#
|
|
|
|
# Optionally inner block may be passed (e.g. with action buttons)
|
|
|
|
# and it's rendered next to the text input.
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def mount(socket) do
|
|
|
|
inner_block = Map.get(socket.assigns, :inner_block, nil)
|
2021-04-21 18:15:11 +08:00
|
|
|
{:ok, assign(socket, inner_block: inner_block, current_dir: nil)}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def update(assigns, socket) do
|
|
|
|
%{assigns: assigns} = socket = assign(socket, assigns)
|
|
|
|
{dir, basename} = split_path(assigns.path)
|
|
|
|
dir = Path.expand(dir)
|
|
|
|
|
|
|
|
files =
|
|
|
|
if assigns.current_dir != dir do
|
|
|
|
list_files(dir, assigns.extnames, assigns.running_paths)
|
|
|
|
else
|
|
|
|
assigns.files
|
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, assign(socket, files: annotate_matching(files, basename), current_dir: dir)}
|
2021-03-20 21:10:15 +08:00
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
|
|
|
~L"""
|
2021-03-20 21:10:15 +08:00
|
|
|
<div class="h-full flex flex-col">
|
|
|
|
<div class="flex space-x-5 items-center mb-4">
|
|
|
|
<form class="flex-grow"
|
|
|
|
phx-change="set_path"
|
2021-04-13 04:59:48 +08:00
|
|
|
<%= if @phx_submit do %>
|
|
|
|
phx-submit="<%= @phx_submit %>"
|
|
|
|
<% else %>
|
|
|
|
onsubmit="return false"
|
|
|
|
<% end %>
|
|
|
|
<%= if @phx_target, do: "phx-target=#{@phx_target}" %>>
|
2021-03-20 21:10:15 +08:00
|
|
|
<input class="input"
|
|
|
|
id="input-path"
|
|
|
|
phx-hook="FocusOnUpdate"
|
|
|
|
type="text"
|
|
|
|
name="path"
|
|
|
|
placeholder="File"
|
|
|
|
value="<%= @path %>"
|
|
|
|
spellcheck="false"
|
|
|
|
autocomplete="off" />
|
|
|
|
</form>
|
|
|
|
<%= if @inner_block do %>
|
|
|
|
<div>
|
|
|
|
<%= render_block(@inner_block) %>
|
|
|
|
</div>
|
2021-02-21 23:54:44 +08:00
|
|
|
<% end %>
|
|
|
|
</div>
|
2021-04-21 18:34:44 +08:00
|
|
|
<div class="flex-grow -m-1 p-1 overflow-y-auto tiny-scrollbar" tabindex="-1">
|
2021-04-21 18:15:11 +08:00
|
|
|
<%= if highlighting?(@files) do %>
|
2021-04-21 23:42:03 +08:00
|
|
|
<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-2 border-b border-dashed border-grey-200 mb-2 pb-2">
|
2021-04-21 18:15:11 +08:00
|
|
|
<%= for file <- @files, file.highlighted != "" do %>
|
|
|
|
<%= render_file(file, @phx_target) %>
|
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
|
|
|
|
2021-04-21 23:42:03 +08:00
|
|
|
<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 gap-2">
|
2021-04-21 18:15:11 +08:00
|
|
|
<%= for file <- @files, file.highlighted == "" do %>
|
2021-04-13 04:59:48 +08:00
|
|
|
<%= render_file(file, @phx_target) %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<% end %>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-02-21 23:54:44 +08:00
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
defp highlighting?(files) do
|
2021-04-21 18:17:35 +08:00
|
|
|
Enum.any?(files, &(&1.highlighted != ""))
|
2021-04-21 18:15:11 +08:00
|
|
|
end
|
|
|
|
|
2021-04-13 04:59:48 +08:00
|
|
|
defp render_file(file, phx_target) do
|
2021-02-21 23:54:44 +08:00
|
|
|
icon =
|
|
|
|
case file do
|
2021-03-12 18:57:01 +08:00
|
|
|
%{is_running: true} -> "play-circle-line"
|
|
|
|
%{is_dir: true} -> "folder-fill"
|
|
|
|
_ -> "file-code-line"
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
assigns = %{file: file, icon: icon}
|
|
|
|
|
|
|
|
~L"""
|
2021-03-12 18:57:01 +08:00
|
|
|
<button class="flex space-x-2 items-center p-2 rounded-lg hover:bg-gray-100 focus:ring-1 focus:ring-gray-400"
|
2021-02-21 23:54:44 +08:00
|
|
|
phx-click="set_path"
|
2021-04-01 21:49:39 +08:00
|
|
|
phx-value-path="<%= @file.path %>"
|
2021-04-13 04:59:48 +08:00
|
|
|
<%= if phx_target, do: "phx-target=#{phx_target}" %>>
|
2021-02-21 23:54:44 +08:00
|
|
|
<span class="block">
|
2021-03-12 18:57:01 +08:00
|
|
|
<%= remix_icon(@icon, class: "text-xl align-middle #{if(@file.is_running, do: "text-green-300", else: "text-gray-400")}") %>
|
2021-02-21 23:54:44 +08:00
|
|
|
</span>
|
2021-04-01 21:49:39 +08:00
|
|
|
<span class="flex font-medium overflow-hidden overflow-ellipsis whitespace-nowrap <%= if(@file.is_running, do: "text-green-300", else: "text-gray-500") %>">
|
|
|
|
<%= if @file.highlighted != "" do %>
|
|
|
|
<span class="font-medium <%= if(@file.is_running, do: "text-green-400", else: "text-gray-900") %>">
|
|
|
|
<%= @file.highlighted %>
|
|
|
|
</span>
|
|
|
|
<% end %>
|
|
|
|
<span>
|
|
|
|
<%= @file.unhighlighted %>
|
|
|
|
</span>
|
2021-02-21 23:54:44 +08:00
|
|
|
</span>
|
|
|
|
</button>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
defp annotate_matching(files, prefix) do
|
|
|
|
for %{name: name} = file <- files do
|
|
|
|
if String.starts_with?(name, prefix) do
|
|
|
|
%{file | highlighted: prefix, unhighlighted: String.replace_prefix(name, prefix, "")}
|
|
|
|
else
|
|
|
|
%{file | highlighted: "", unhighlighted: name}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-02-21 23:54:44 +08:00
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
defp list_files(dir, extnames, running_paths) do
|
2021-02-21 23:54:44 +08:00
|
|
|
if File.exists?(dir) do
|
|
|
|
file_names =
|
|
|
|
case File.ls(dir) do
|
|
|
|
{:ok, names} -> names
|
|
|
|
{:error, _} -> []
|
|
|
|
end
|
|
|
|
|
|
|
|
file_infos =
|
|
|
|
file_names
|
|
|
|
|> Enum.map(fn name ->
|
2021-04-21 18:15:11 +08:00
|
|
|
file_info(name, Path.join(dir, name), running_paths)
|
2021-02-21 23:54:44 +08:00
|
|
|
end)
|
|
|
|
|> Enum.filter(fn file ->
|
2021-04-01 21:49:39 +08:00
|
|
|
not hidden?(file.name) and (file.is_dir or valid_extension?(file.name, extnames))
|
2021-02-21 23:54:44 +08:00
|
|
|
end)
|
2021-04-01 21:49:39 +08:00
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
parent = Path.dirname(dir)
|
|
|
|
|
2021-04-01 21:49:39 +08:00
|
|
|
file_infos =
|
2021-04-21 18:15:11 +08:00
|
|
|
if parent == dir do
|
2021-04-01 21:49:39 +08:00
|
|
|
file_infos
|
|
|
|
else
|
2021-04-21 18:15:11 +08:00
|
|
|
[file_info("..", parent, running_paths) | file_infos]
|
2021-04-01 21:49:39 +08:00
|
|
|
end
|
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
Enum.sort_by(file_infos, fn file -> {!file.is_dir, file.name} end)
|
2021-02-21 23:54:44 +08:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
defp file_info(name, path, running_paths) do
|
2021-04-01 21:49:39 +08:00
|
|
|
is_dir = File.dir?(path)
|
|
|
|
|
|
|
|
%{
|
|
|
|
name: name,
|
2021-04-21 18:15:11 +08:00
|
|
|
highlighted: "",
|
|
|
|
unhighlighted: name,
|
2021-04-01 21:49:39 +08:00
|
|
|
path: if(is_dir, do: ensure_trailing_slash(path), else: path),
|
|
|
|
is_dir: is_dir,
|
|
|
|
is_running: path in running_paths
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-02-21 23:54:44 +08:00
|
|
|
defp hidden?(filename) do
|
|
|
|
String.starts_with?(filename, ".")
|
|
|
|
end
|
|
|
|
|
2021-02-27 03:53:29 +08:00
|
|
|
defp valid_extension?(filename, extnames) do
|
|
|
|
Path.extname(filename) in extnames
|
2021-02-21 23:54:44 +08:00
|
|
|
end
|
|
|
|
|
2021-04-21 18:15:11 +08:00
|
|
|
# Note: to provide an intuitive behavior when typing the path
|
|
|
|
# we enter a new directory when it has a trailing slash,
|
|
|
|
# so given "/foo/bar" we list files in "foo" and given "/foo/bar/
|
|
|
|
# we list files in "bar".
|
|
|
|
#
|
|
|
|
# The basename is kinda like search within the current directory,
|
|
|
|
# so we highlight files starting with that string.
|
2021-02-21 23:54:44 +08:00
|
|
|
defp split_path(path) do
|
|
|
|
if String.ends_with?(path, "/") do
|
|
|
|
{path, ""}
|
|
|
|
else
|
|
|
|
{Path.dirname(path), Path.basename(path)}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp ensure_trailing_slash(path) do
|
|
|
|
if String.ends_with?(path, "/") do
|
|
|
|
path
|
|
|
|
else
|
|
|
|
path <> "/"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|