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

202 lines
4.8 KiB
Svelte
Raw Normal View History

2023-05-11 19:07:29 +02:00
<script>
2024-11-19 15:31:51 +01:00
import { fs_rename, fs_update } from "../FilesystemAPI.mjs";
2024-02-16 14:50:34 +01:00
import Modal from "../../util/Modal.svelte";
import BrandingOptions from "./BrandingOptions.svelte";
2024-02-16 14:50:34 +01:00
import { branding_from_node } 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";
2023-05-25 21:36:59 +02:00
export let nav
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 14:50:34 +01:00
let custom_css = ""
2024-02-16 11:49:38 +01:00
export let visible
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, 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
2023-05-11 19:07:29 +02:00
shared = !(file.id === undefined || file.id === "")
if (file.properties === undefined) {
file.properties = {}
}
2024-02-16 14:50:34 +01:00
2024-11-19 15:31:51 +01:00
if (shared && file.link_permissions === undefined) {
// Default to read-only for public links
file.link_permissions = {read: true, write: false, delete: false}
}
branding_enabled = file.properties.branding_enabled === "true"
2024-02-16 14:50:34 +01:00
if (branding_enabled) {
custom_css = branding_from_node(file)
} else {
custom_css = ""
}
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 shared = false
2024-02-16 21:07:01 +01:00
let new_name = ""
2023-05-11 19:07:29 +02:00
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_background_image",
]
2024-02-16 21:07:01 +01:00
const save = async (keep_editing = false) => {
console.debug("Saving file", file.path)
2024-02-16 21:07:01 +01:00
let new_file
2023-05-11 19:07:29 +02:00
try {
nav.set_loading(true)
2024-11-19 15:31:51 +01:00
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]
}
}
}
2024-11-19 15:31:51 +01:00
if (shared && file.link_permissions !== undefined) {
opts.link_permissions = file.link_permissions
}
if (shared && file.user_permissions !== undefined) {
opts.user_permissions = file.user_permissions
}
if (shared && file.password_permissions !== undefined) {
opts.password_permissions = file.password_permissions
}
2024-02-16 21:07:01 +01:00
new_file = await fs_update(file.path, opts)
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 {
nav.set_loading(false)
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
2024-02-16 21:07:01 +01:00
if (keep_editing) {
edit(new_file, open_after_edit)
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">
<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>
2024-11-19 15:31:51 +01:00
{#if shared && $nav.permissions.owner}
<button class:button_highlight={tab === "access"} on:click={() => tab = "access"}>
<i class="icon">key</i>
Access control
</button>
{/if}
<button class:button_highlight={tab === "branding"} on:click={() => tab = "branding"}>
<i class="icon">palette</i>
Branding
</button>
</div>
2024-02-16 21:07:01 +01:00
<form id="edit_form" on:submit|preventDefault={() => save(false)}></form>
<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"}
2024-11-19 15:31:51 +01:00
<SharingOptions bind:file bind:shared on:save={() => save(true)} />
{:else if tab === "access"}
<AccessControl bind:file bind:shared />
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:colors={branding_colors}
2024-02-16 21:07:01 +01:00
bind:file
2024-02-16 14:50:34 +01:00
on:style_change={e => custom_css = branding_from_node(file)}
/>
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>