File management improvements (#2131)

This commit is contained in:
Jonatan Kłosko 2023-08-01 20:41:54 +02:00 committed by GitHub
parent e86b14f999
commit f2144106ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 62 additions and 6 deletions

View file

@ -277,6 +277,27 @@ defmodule Livebook.Utils do
|> URI.to_string()
end
@doc """
Infers file name from the given URL.
## Examples
iex> Livebook.Utils.url_basename("https://example.com/data.csv")
"data.csv"
iex> Livebook.Utils.url_basename("https://example.com")
""
"""
@spec url_basename(String.t()) :: String.t()
def url_basename(url) do
uri = URI.parse(url)
(uri.path || "/")
|> String.split("/")
|> List.last()
end
@doc ~S"""
Wraps the given line into lines that fit in `width` characters.

View file

@ -79,7 +79,7 @@ defmodule LivebookWeb.SessionLive.AddFileEntryFileComponent do
phx-target={@myself}
>
<div class="flex flex-col space-y-4">
<.text_field field={f[:name]} label="Name" autocomplete="off" phx-debounce="blur" />
<.text_field field={f[:name]} label="Name" autocomplete="off" phx-debounce="200" />
<.radio_field
field={f[:copy]}
options={[

View file

@ -59,7 +59,7 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUploadComponent do
label="Name"
id="add-file-entry-form-name"
autocomplete="off"
phx-debounce="blur"
phx-debounce="200"
/>
</div>
<div class="mt-6 flex space-x-3">

View file

@ -52,8 +52,21 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUrlComponent do
phx-target={@myself}
>
<div class="flex flex-col space-y-4">
<.text_field field={f[:url]} label="URL" autocomplete="off" phx-debounce="blur" />
<.text_field field={f[:name]} label="Name" autocomplete="off" phx-debounce="blur" />
<.text_field
field={f[:url]}
label="URL"
autocomplete="off"
phx-debounce="200"
phx-blur="url_blur"
phx-target={@myself}
/>
<.text_field
field={f[:name]}
label="Name"
id="add-file-entry-form-name"
autocomplete="off"
phx-debounce="200"
/>
<.radio_field
field={f[:copy]}
options={[
@ -86,6 +99,28 @@ defmodule LivebookWeb.SessionLive.AddFileEntryUrlComponent do
{:noreply, assign(socket, changeset: changeset)}
end
def handle_event("url_blur", %{"value" => url}, socket) do
name = Livebook.Utils.url_basename(url)
socket =
if socket.assigns.changeset.params["name"] == "" and name != "" do
# Emulate input event to make sure validation errors are shown
exec_js(
socket,
JS.dispatch("lb:set_value",
to: "#add-file-entry-form-name",
detail: %{value: name}
)
|> JS.dispatch("input", to: "#add-file-entry-form-name")
|> JS.dispatch("blur", to: "#add-file-entry-form-name")
)
else
socket
end
{:noreply, socket}
end
def handle_event("add", %{"data" => data}, socket) do
data
|> changeset()

View file

@ -110,7 +110,7 @@ defmodule LivebookWeb.SessionLive.FilesListComponent do
<.menu_item>
<a
role="menuitem"
href={~p"/sessions/#{@session.id}/files/download/#{file_entry.name}"}
href={~p"/sessions/#{@session.id}/download/files/#{file_entry.name}"}
>
<.remix_icon icon="download-2-line" />
<span>Download</span>

View file

@ -64,7 +64,7 @@ defmodule LivebookWeb.SessionLive.InsertImageComponent do
label="Name"
id="insert-image-form-name"
autocomplete="off"
phx-debounce="blur"
phx-debounce="200"
/>
</div>
<div class="mt-8 flex justify-end space-x-2">