2025-06-05 21:29:07 +02:00
|
|
|
<script lang="ts">
|
|
|
|
|
import type { FSNavigator } from "./FSNavigator";
|
2026-07-30 17:07:46 +02:00
|
|
|
import { fs_node_icon, fs_share_hotlink_url, fs_share_url, fs_update, type FSNode, type FSPermissions } from "lib/FilesystemAPI";
|
2025-10-13 16:05:50 +02:00
|
|
|
import { copy_text } from "util/Util";
|
2025-06-05 21:29:07 +02:00
|
|
|
import CopyButton from "layout/CopyButton.svelte";
|
|
|
|
|
import Dialog from "layout/Dialog.svelte";
|
2025-06-12 11:32:56 +02:00
|
|
|
import { fade } from "svelte/transition";
|
2025-06-05 21:29:07 +02:00
|
|
|
|
2025-10-13 16:05:50 +02:00
|
|
|
let { nav }: {
|
|
|
|
|
nav: FSNavigator;
|
|
|
|
|
} = $props();
|
2025-06-05 21:29:07 +02:00
|
|
|
|
|
|
|
|
let path: FSNode[]
|
2025-10-13 16:05:50 +02:00
|
|
|
let base: FSNode = $state()
|
|
|
|
|
let toast = $state("")
|
|
|
|
|
let share_url = $state("")
|
|
|
|
|
let direct_share_url = $state("")
|
|
|
|
|
let is_parent = $state(false)
|
|
|
|
|
let parent_node: FSNode = $state()
|
2025-06-05 21:29:07 +02:00
|
|
|
|
2025-10-13 16:05:50 +02:00
|
|
|
let dialog: Dialog = $state()
|
2025-06-05 21:29:07 +02:00
|
|
|
export const open = async (e: MouseEvent, p: FSNode[]) => {
|
|
|
|
|
path = p
|
|
|
|
|
base = path[path.length-1]
|
|
|
|
|
share_url = fs_share_url(path)
|
|
|
|
|
if (share_url === "") {
|
|
|
|
|
// File is not public, make public
|
|
|
|
|
await make_public()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await share()
|
|
|
|
|
|
|
|
|
|
if (e.target instanceof HTMLButtonElement) {
|
|
|
|
|
dialog.open(e.target.getBoundingClientRect())
|
|
|
|
|
} else {
|
|
|
|
|
dialog.open((e.target as HTMLElement).parentElement.getBoundingClientRect())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const make_public = async () => {
|
2025-10-13 23:20:42 +02:00
|
|
|
if (!base.is_shared()) {
|
2025-09-24 15:37:57 +02:00
|
|
|
base = await fs_update(
|
|
|
|
|
base.path,
|
|
|
|
|
{link_permissions: {read: true} as FSPermissions},
|
|
|
|
|
)
|
|
|
|
|
await nav.reload()
|
2025-06-05 21:29:07 +02:00
|
|
|
|
2025-09-24 15:37:57 +02:00
|
|
|
// Insert the new FSNode into the path
|
|
|
|
|
path[path.length-1] = base
|
|
|
|
|
}
|
2025-06-05 21:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const share = async () => {
|
|
|
|
|
// If the base node does not have a public ID then the file is shared
|
|
|
|
|
// through a parent node. In that case we ask the user if it was their
|
|
|
|
|
// intention to share the parent directory
|
|
|
|
|
is_parent = base.id === undefined
|
|
|
|
|
if (is_parent) {
|
|
|
|
|
// Walk path backwards looking for the last public ID
|
|
|
|
|
for (let i = path.length - 1; i >= 0; i--) {
|
|
|
|
|
if (path[i].id !== undefined && path[i].id !== "me") {
|
|
|
|
|
parent_node = path[i]
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
share_url = fs_share_url(path)
|
|
|
|
|
direct_share_url = fs_share_hotlink_url(path)
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await navigator.share({
|
|
|
|
|
title: base.name,
|
|
|
|
|
text: "I would like to share '" + base.name + "' with you",
|
|
|
|
|
url: share_url,
|
|
|
|
|
})
|
|
|
|
|
} catch(_) {
|
|
|
|
|
if (copy_text(share_url)) {
|
|
|
|
|
toast = "Link copied to clipboard"
|
|
|
|
|
setTimeout(() => {toast = ""}, 10000)
|
|
|
|
|
} else {
|
|
|
|
|
alert("Could not copy text")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<Dialog bind:this={dialog}>
|
|
|
|
|
<div class="dialog_inner">
|
2025-06-12 11:32:56 +02:00
|
|
|
{#if toast !== ""}
|
|
|
|
|
<div class="highlight_green" transition:fade>{toast}</div>
|
|
|
|
|
<div class="separator" transition:fade></div>
|
2025-06-05 21:29:07 +02:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
<div>Sharing link</div>
|
|
|
|
|
<div class="link_copy">
|
|
|
|
|
<div class="button_container">
|
|
|
|
|
<CopyButton text={share_url}>Copy</CopyButton>
|
|
|
|
|
</div>
|
|
|
|
|
<a href="{share_url}">{share_url}</a>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="separator"></div>
|
|
|
|
|
<div>Direct sharing link (hotlink)</div>
|
|
|
|
|
<div class="link_copy">
|
|
|
|
|
<div class="button_container">
|
|
|
|
|
<CopyButton text={direct_share_url}>Copy</CopyButton>
|
|
|
|
|
</div>
|
|
|
|
|
<a href="{direct_share_url}">{direct_share_url}</a>
|
|
|
|
|
</div>
|
2025-06-12 11:32:56 +02:00
|
|
|
|
|
|
|
|
{#if is_parent}
|
|
|
|
|
<div class="separator"></div>
|
|
|
|
|
<div>
|
|
|
|
|
This link also gives access to
|
|
|
|
|
<img src={fs_node_icon(parent_node, 64, 64)} class="node_icon" alt="icon"/>
|
|
|
|
|
{parent_node.name}
|
|
|
|
|
<br/>
|
2025-10-13 16:05:50 +02:00
|
|
|
<button onclick={async e => {await make_public(); await share()}} style="display: inline;">
|
2025-06-12 11:32:56 +02:00
|
|
|
Only share
|
|
|
|
|
<img src={fs_node_icon(base, 64, 64)} class="node_icon" alt="icon"/>
|
|
|
|
|
{base.name}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2025-06-05 21:29:07 +02:00
|
|
|
</div>
|
|
|
|
|
</Dialog>
|
|
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.dialog_inner {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
text-align: center;
|
2025-06-12 11:32:56 +02:00
|
|
|
max-width: 40em;
|
2025-06-05 21:29:07 +02:00
|
|
|
}
|
|
|
|
|
.link_copy {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: row;
|
|
|
|
|
align-items: center;
|
|
|
|
|
gap: 0.5em;
|
|
|
|
|
}
|
|
|
|
|
.link_copy > .button_container {
|
|
|
|
|
flex: 0 0 auto;
|
|
|
|
|
}
|
|
|
|
|
.link_copy > a {
|
|
|
|
|
flex: 1 1 auto;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
overflow-x: hidden;
|
|
|
|
|
}
|
|
|
|
|
.node_icon {
|
|
|
|
|
width: 1.5em;
|
|
|
|
|
height: 1.5em;
|
|
|
|
|
vertical-align: middle;
|
|
|
|
|
}
|
|
|
|
|
.separator {
|
|
|
|
|
height: 1px;
|
|
|
|
|
border: none;
|
|
|
|
|
background-color: var(--separator);
|
2025-06-12 11:32:56 +02:00
|
|
|
margin-top: 0.5em;
|
|
|
|
|
margin-bottom: 0.5em;
|
2025-06-05 21:29:07 +02:00
|
|
|
}
|
|
|
|
|
</style>
|