open-webui/src/lib/components/common/Folder.svelte

142 lines
3.6 KiB
Svelte
Raw Normal View History

2024-10-15 07:35:14 +00:00
<script>
2024-10-15 09:12:39 +00:00
import { getContext, createEventDispatcher, onMount, onDestroy } from 'svelte';
2024-10-15 07:35:14 +00:00
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
import ChevronDown from '../icons/ChevronDown.svelte';
import ChevronRight from '../icons/ChevronRight.svelte';
import Collapsible from './Collapsible.svelte';
export let open = true;
2024-10-15 09:12:39 +00:00
export let id = '';
2024-10-15 07:35:14 +00:00
export let name = '';
2024-10-15 09:12:39 +00:00
export let collapsible = true;
2024-10-17 04:05:03 +00:00
export let className = '';
2024-10-15 09:12:39 +00:00
let folderElement;
2024-10-17 04:05:03 +00:00
let draggedOver = false;
2024-10-15 09:12:39 +00:00
const onDragOver = (e) => {
e.preventDefault();
2024-10-18 02:45:18 +00:00
e.stopPropagation();
2024-10-17 04:05:03 +00:00
draggedOver = true;
2024-10-15 09:12:39 +00:00
};
const onDrop = (e) => {
e.preventDefault();
2024-10-18 02:45:18 +00:00
e.stopPropagation();
2024-10-15 09:12:39 +00:00
if (folderElement.contains(e.target)) {
console.log('Dropped on the Button');
2024-10-18 03:13:28 +00:00
if (e.dataTransfer.items && e.dataTransfer.items.length > 0) {
// Iterate over all items in the DataTransferItemList use functional programming
for (const item of Array.from(e.dataTransfer.items)) {
// If dropped items aren't files, reject them
if (item.kind === 'file') {
const file = item.getAsFile();
if (file && file.type === 'application/json') {
console.log('Dropped file is a JSON file!');
// Read the JSON file with FileReader
const reader = new FileReader();
reader.onload = async function (event) {
try {
const fileContent = JSON.parse(event.target.result);
console.log('Parsed JSON Content: ', fileContent);
2024-10-18 21:11:13 +00:00
open = true;
2024-10-18 03:13:28 +00:00
dispatch('import', fileContent);
} catch (error) {
console.error('Error parsing JSON file:', error);
}
};
// Start reading the file
reader.readAsText(file);
} else {
console.error('Only JSON file types are supported.');
}
} else {
2024-10-18 21:11:13 +00:00
open = true;
2024-10-18 03:13:28 +00:00
const dataTransfer = e.dataTransfer.getData('text/plain');
const data = JSON.parse(dataTransfer);
2024-10-18 21:11:13 +00:00
2024-10-18 03:13:28 +00:00
console.log(data);
dispatch('drop', data);
}
}
2024-10-17 04:05:03 +00:00
}
draggedOver = false;
2024-10-15 09:12:39 +00:00
}
};
const onDragLeave = (e) => {
e.preventDefault();
2024-10-18 02:45:18 +00:00
e.stopPropagation();
2024-10-17 04:05:03 +00:00
draggedOver = false;
2024-10-15 09:12:39 +00:00
};
onMount(() => {
folderElement.addEventListener('dragover', onDragOver);
folderElement.addEventListener('drop', onDrop);
folderElement.addEventListener('dragleave', onDragLeave);
});
onDestroy(() => {
folderElement.addEventListener('dragover', onDragOver);
folderElement.removeEventListener('drop', onDrop);
folderElement.removeEventListener('dragleave', onDragLeave);
});
2024-10-15 07:35:14 +00:00
</script>
2024-10-17 04:05:03 +00:00
<div bind:this={folderElement} class="relative {className}">
{#if draggedOver}
2024-10-15 07:35:14 +00:00
<div
2024-10-18 21:11:13 +00:00
class="absolute top-0 left-0 w-full h-full rounded-sm bg-[hsla(260,85%,65%,0.1)] bg-opacity-50 dark:bg-opacity-10 z-50 pointer-events-none touch-none"
2024-10-15 09:12:39 +00:00
></div>
{/if}
{#if collapsible}
<Collapsible
bind:open
className="w-full "
buttonClassName="w-full"
on:change={(e) => {
dispatch('change', e.detail);
2024-10-15 07:35:14 +00:00
}}
>
2024-10-15 09:12:39 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
2024-10-17 04:05:03 +00:00
<div class="w-full">
2024-10-15 09:12:39 +00:00
<button
2024-10-15 12:12:56 +00:00
class="w-full py-1.5 px-2 rounded-md flex items-center gap-1.5 text-xs text-gray-500 dark:text-gray-500 font-medium hover:bg-gray-100 dark:hover:bg-gray-900 transition"
2024-10-15 09:12:39 +00:00
>
2024-10-15 12:00:36 +00:00
<div class="text-gray-300 dark:text-gray-600">
2024-10-15 09:12:39 +00:00
{#if open}
<ChevronDown className=" size-3" strokeWidth="2.5" />
{:else}
<ChevronRight className=" size-3" strokeWidth="2.5" />
{/if}
</div>
<div class="translate-y-[0.5px]">
{name}
</div>
</button>
</div>
2024-10-17 04:05:03 +00:00
<div slot="content" class="w-full">
2024-10-15 09:12:39 +00:00
<slot></slot>
</div>
</Collapsible>
{:else}
<slot></slot>
{/if}
2024-10-15 07:35:14 +00:00
</div>