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

184 lines
4.1 KiB
Svelte
Raw Normal View History

2023-05-11 19:07:29 +02:00
<script>
2024-02-16 21:07:01 +01:00
import { fs_rename, fs_update } from "../FilesystemAPI";
2024-02-16 14:50:34 +01:00
import Modal from "../../util/Modal.svelte";
2023-05-25 21:36:59 +02:00
import { createEventDispatcher } from "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";
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 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
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 {
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]
}
}
}
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 {
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-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>
2024-02-16 14:50:34 +01:00
<Modal bind:visible={visible} title="Edit {file.name}" width="700px" 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>
<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
fs_navigator={fs_navigator}
bind:file
bind:new_name
bind:visible
bind:open_after_edit
on:loading
/>
{:else if tab === "share"}
<SharingOptions
bind:file
bind:shared
on:save={() => save(true)}
/>
{: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>