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

172 lines
4.3 KiB
Svelte
Raw Normal View History

<script lang="ts">
2026-01-29 23:41:30 +01:00
import { fs_rename, fs_update, type FSNode, type FSPermissions, type NodeOptions } from "lib/FilesystemAPI.svelte";
import Modal from "util/Modal.svelte";
import BrandingOptions from "./BrandingOptions.svelte";
import { branding_from_props } from "./Branding";
2024-02-16 21:07:01 +01:00
import FileOptions from "./FileOptions.svelte";
import SharingOptions from "./SharingOptions.svelte";
2024-11-19 15:31:51 +01:00
import AccessControl from "./AccessControl.svelte";
import type { FSNavigator } from "filesystem/FSNavigator";
import { loading_finish, loading_start } from "lib/Loading";
2023-05-25 21:36:59 +02:00
2025-10-13 16:05:50 +02:00
let file: FSNode = $state({} as FSNode)
let options: NodeOptions = $state({} as NodeOptions)
2023-05-11 19:07:29 +02:00
2025-10-13 16:05:50 +02:00
let custom_css = $state("")
2024-02-16 11:49:38 +01:00
2025-10-13 16:05:50 +02:00
let {
nav,
visible = $bindable()
}: {
nav: FSNavigator;
visible: boolean;
} = $props();
2024-02-16 21:07:01 +01:00
2026-01-29 23:41:30 +01:00
const default_permissions: FSPermissions = {
owner: false, read: false, write: false, delete: false
}
2024-02-16 21:07:01 +01:00
// Open the edit window. Argument 1 is the file to edit, 2 is whether the file
// should be opened after the user finishes editing and 3 is the default tab
// that should be open when the window shows
export const edit = (f: FSNode, oae = false, open_tab = "") => {
2023-05-25 14:47:36 +02:00
file = f
open_after_edit = oae
2024-02-16 21:07:01 +01:00
if (open_tab !== "") {
tab = open_tab
}
2023-05-25 14:47:36 +02:00
console.log("Editing file", file)
2023-05-11 19:07:29 +02:00
2024-02-16 21:07:01 +01:00
// We save the name in a separate field because we need both the original
// name and the new name for the rename operation
new_name = file.name
if (file.properties === undefined) {
options = {} as NodeOptions
} else {
options = file.properties
}
2024-02-16 14:50:34 +01:00
options.custom_domain_name = file.custom_domain_name
2026-01-29 23:41:30 +01:00
options.link_permissions = file.link_permissions === undefined ? default_permissions : file.link_permissions
options.user_permissions = file.user_permissions === undefined ? {} : file.user_permissions
options.password_permissions = file.password_permissions === undefined ? {} : file.password_permissions
2024-11-19 15:31:51 +01:00
branding_enabled = options.branding_enabled === "true"
2024-02-16 14:50:34 +01:00
if (branding_enabled) {
custom_css = branding_from_props(options)
2024-02-16 14:50:34 +01:00
} else {
custom_css = ""
}
visible = true
2023-05-11 19:07:29 +02:00
}
2025-10-13 16:05:50 +02:00
let tab = $state("file")
let open_after_edit = $state(false)
2025-10-13 16:05:50 +02:00
let new_name = $state("")
let branding_enabled = $state(false)
const save = async (e: SubmitEvent) => {
e.preventDefault()
console.debug("Saving file", file.path)
let new_file: FSNode
2023-05-11 19:07:29 +02:00
try {
loading_start()
options.branding_enabled = JSON.stringify(branding_enabled)
2024-11-19 15:31:51 +01:00
new_file = await fs_update(file.path, options)
2023-05-11 19:07:29 +02:00
2024-02-16 21:07:01 +01:00
if (new_name !== file.name) {
2023-05-11 19:07:29 +02:00
let parent = file.path.slice(0, -file.name.length)
2024-02-16 21:07:01 +01:00
console.log("Moving", file.path, "to", parent+new_name)
await fs_rename(file.path, parent+new_name)
file.path = parent+new_name
2023-05-11 19:07:29 +02:00
2024-02-16 21:07:01 +01:00
new_file.name = new_name
new_file.path = file.path
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 {
loading_finish()
2023-05-11 19:07:29 +02:00
}
2023-05-25 14:47:36 +02:00
if (open_after_edit) {
nav.navigate(file.path, false)
2023-05-25 14:47:36 +02:00
} else {
nav.reload()
2023-05-25 14:47:36 +02:00
}
}
2023-05-11 19:07:29 +02:00
</script>
<Modal bind:visible={visible} title="Edit {file.name}" width="800px" form="edit_form" style="color: var(--body_text_color); {custom_css}">
<div class="tab_bar">
2025-10-13 16:05:50 +02:00
<button class:button_highlight={tab === "file"} onclick={() => tab = "file"}>
<i class="icon">edit</i>
2024-02-16 11:49:38 +01:00
Properties
</button>
2025-10-13 16:05:50 +02:00
<button class:button_highlight={tab === "share"} onclick={() => tab = "share"}>
<i class="icon">share</i>
Sharing
</button>
2025-10-09 15:48:23 +02:00
{#if $nav.permissions.owner}
2025-10-13 16:05:50 +02:00
<button class:button_highlight={tab === "access"} onclick={() => tab = "access"}>
2024-11-19 15:31:51 +01:00
<i class="icon">key</i>
Access control
</button>
{/if}
2025-10-13 16:05:50 +02:00
<button class:button_highlight={tab === "branding"} onclick={() => tab = "branding"}>
<i class="icon">palette</i>
Branding
</button>
</div>
<form id="edit_form" onsubmit={save}></form>
2024-02-16 21:07:01 +01:00
<div class="tab_content">
{#if tab === "file"}
<FileOptions
nav={nav}
2024-02-16 21:07:01 +01:00
bind:file
bind:new_name
bind:visible
bind:open_after_edit
/>
{:else if tab === "share"}
2025-10-13 16:05:50 +02:00
<SharingOptions
bind:file
bind:options
/>
2024-11-19 15:31:51 +01:00
{:else if tab === "access"}
<AccessControl bind:options />
2024-02-16 21:07:01 +01:00
{:else if tab === "branding"}
2024-02-16 14:50:34 +01:00
<BrandingOptions
bind:enabled={branding_enabled}
bind:options={options}
2024-02-16 21:07:01 +01:00
bind:file
bind:custom_css
2024-02-16 14:50:34 +01:00
/>
2024-02-16 21:07:01 +01:00
{/if}
</div>
2023-05-11 19:07:29 +02:00
</Modal>
<style>
.tab_bar {
border-bottom: 2px solid var(--separator);
}
.tab_content {
padding: 8px;
}
</style>