Files
fnx_web/svelte/src/filesystem/EditWindow.svelte

207 lines
5.0 KiB
Svelte
Raw Normal View History

2023-05-11 19:07:29 +02:00
<script>
import { fs_delete_all, fs_rename, fs_update } from "./FilesystemAPI";
import Modal from "../util/Modal.svelte";
2023-05-25 21:36:59 +02:00
import { createEventDispatcher } from "svelte";
2023-11-16 12:17:36 +01:00
import Button from "../layout/Button.svelte";
import BrandingOptions from "./BrandingOptions.svelte";
2024-02-16 11:49:38 +01:00
import PathLink from "./util/PathLink.svelte";
2023-05-25 21:36:59 +02:00
let dispatch = createEventDispatcher()
2023-05-11 19:07:29 +02:00
2023-05-17 22:46:27 +02:00
export let fs_navigator
2023-05-11 19:07:29 +02:00
let file = {
path: "",
name: "",
id: "",
mode_octal: "",
properties: {},
2023-05-11 19:07:29 +02:00
};
2024-02-16 11:49:38 +01:00
$: is_root_dir = file.path === "/"+file.id
export let visible
2023-05-25 14:47:36 +02:00
export const edit = (f, oae = false, t = "file") => {
file = f
open_after_edit = oae
tab = t
2023-05-25 14:47:36 +02:00
console.log("Editing file", file)
2023-05-11 19:07:29 +02:00
file_name = file.name
shared = !(file.id === undefined || file.id === "")
if (file.properties === undefined) {
file.properties = {}
}
branding_enabled = file.properties.branding_enabled === "true"
visible = true
2023-05-11 19:07:29 +02:00
}
let tab = "file"
2023-05-25 14:47:36 +02:00
let open_after_edit = false
2023-05-11 19:07:29 +02:00
let file_name = ""
let shared = false
let branding_enabled = false
let branding_colors
let branding_fields = [
"brand_input_color",
"brand_highlight_color",
"brand_danger_color",
"brand_background_color",
"brand_body_color",
"brand_card_color",
"brand_header_image",
"brand_header_link",
"brand_footer_image",
"brand_footer_link",
"brand_background_image",
]
2023-05-11 19:07:29 +02:00
const save = async () => {
console.debug("Saving file", file.path)
2023-05-11 19:07:29 +02:00
try {
2023-05-25 21:36:59 +02:00
dispatch("loading", true)
let opts = {shared: shared}
opts.branding_enabled = branding_enabled ? "true" : ""
if (branding_enabled && file.properties) {
for (let field of branding_fields) {
if (file.properties[field] !== undefined) {
console.log("setting", field, "to", file.properties[field])
opts[field] = file.properties[field]
}
}
}
await fs_update(file.path, opts)
2023-05-11 19:07:29 +02:00
if (file_name !== file.name) {
let parent = file.path.slice(0, -file.name.length)
console.log("Moving", file.path, "to", parent+file_name)
await fs_rename(file.path, parent+file_name)
file.path = parent+file_name
2023-05-11 19:07:29 +02:00
}
} catch (err) {
if (err.message) {
alert(err.message)
} else {
console.error(err)
alert(err)
}
2023-05-11 19:07:29 +02:00
return
2023-05-25 21:36:59 +02:00
} finally {
dispatch("loading", false)
2023-05-11 19:07:29 +02:00
}
2023-05-25 14:47:36 +02:00
if (open_after_edit) {
fs_navigator.navigate(file.path, false)
} else {
fs_navigator.reload()
}
}
2023-11-16 12:17:36 +01:00
const delete_file = async e => {
e.preventDefault()
try {
2023-05-25 21:36:59 +02:00
dispatch("loading", true)
2023-06-08 14:18:24 +02:00
await fs_delete_all(file.path)
} catch (err) {
console.error(err)
alert(err)
return
2023-05-25 21:36:59 +02:00
} finally {
dispatch("loading", false)
}
2023-05-11 19:07:29 +02:00
2023-05-25 14:47:36 +02:00
if (open_after_edit) {
fs_navigator.navigate(file.path, false)
} else {
fs_navigator.reload()
}
visible = false
}
2023-05-11 19:07:29 +02:00
</script>
2024-02-16 11:49:38 +01:00
<Modal bind:visible={visible} title="Edit {file.name}" width="700px" form="edit_form">
<div class="tab_bar">
<button class:button_highlight={tab === "file"} on:click={() => tab = "file"}>
<i class="icon">edit</i>
2024-02-16 11:49:38 +01:00
Properties
</button>
<button class:button_highlight={tab === "share"} on:click={() => tab = "share"}>
<i class="icon">share</i>
Sharing
</button>
<button class:button_highlight={tab === "branding"} on:click={() => tab = "branding"}>
<i class="icon">palette</i>
Branding
</button>
</div>
2024-02-16 11:49:38 +01:00
<form id="edit_form" on:submit|preventDefault={save}></form>
{#if tab === "file"}
<div class="tab_content">
2024-02-16 11:49:38 +01:00
<h2>File settings</h2>
{#if is_root_dir}
<div class="highlight_yellow">
Filesystem root cannot be renamed. If this shared directory
is in
<PathLink nav={fs_navigator} path="/me">your filesystem</PathLink>
you can rename it from there
</div>
{/if}
<div class="form_grid">
<label for="file_name">Name</label>
<input form="edit_form" bind:value={file_name} id="file_name" type="text" class="form_input" disabled={is_root_dir}/>
</div>
<h2>Delete</h2>
<p>
Delete this file or directory. If this is a directory then all
subfiles will be deleted as well. This action cannot be undone.
</p>
<Button click={delete_file} red icon="delete" label="Delete" style="align-self: flex-start;"/>
</div>
{:else if tab === "share"}
<div class="tab_content">
2024-02-16 11:49:38 +01:00
<h2>Share this file/directory</h2>
<p>
When a file or directory is shared it can be accessed
through a unique link. You can get the URL with the 'Copy
link' button on the toolbar, or share the link with the
'Share' button. If you share a directory all the files
within the directory are also accessible from the link.
</p>
<div>
2024-02-16 11:49:38 +01:00
<input form="edit_form" bind:checked={shared} id="shared" type="checkbox" class="form_input"/>
<label for="shared">Share this file or directory</label>
</div>
</div>
{:else if tab === "branding"}
<div class="tab_content">
<BrandingOptions bind:enabled={branding_enabled} bind:colors={branding_colors} file={file} on:style_change/>
</div>
{/if}
2023-05-11 19:07:29 +02:00
</Modal>
<style>
.tab_bar {
border-bottom: 2px solid var(--separator);
}
.tab_content {
display: flex;
flex-direction: column;
padding: 8px;
}
2024-02-16 11:49:38 +01:00
.form_grid {
display: grid;
grid-template-columns: 1fr 10fr;
align-items: center;
}
</style>