open-webui/src/lib/components/documents/EditDocModal.svelte

182 lines
4.6 KiB
Svelte
Raw Normal View History

2024-01-08 09:49:20 +00:00
<script lang="ts">
2024-03-01 09:18:07 +00:00
import { toast } from 'svelte-sonner';
2024-01-08 09:49:20 +00:00
import dayjs from 'dayjs';
import { onMount, getContext } from 'svelte';
2024-01-08 09:49:20 +00:00
2024-02-03 22:44:49 +00:00
import { getDocs, tagDocByName, updateDocByName } from '$lib/apis/documents';
2024-01-08 09:49:20 +00:00
import Modal from '../common/Modal.svelte';
import { documents } from '$lib/stores';
2024-02-03 22:44:49 +00:00
import TagInput from '../common/Tags/TagInput.svelte';
import Tags from '../common/Tags.svelte';
import { addTagById } from '$lib/apis/chats';
2024-01-08 09:49:20 +00:00
const i18n = getContext('i18n');
2024-01-08 09:49:20 +00:00
export let show = false;
export let selectedDoc;
2024-02-03 22:44:49 +00:00
let tags = [];
2024-01-08 09:49:20 +00:00
let doc = {
name: '',
2024-02-03 22:44:49 +00:00
title: '',
content: null
2024-01-08 09:49:20 +00:00
};
const submitHandler = async () => {
const res = await updateDocByName(localStorage.token, selectedDoc.name, {
title: doc.title,
name: doc.name
}).catch((error) => {
toast.error(error);
});
if (res) {
show = false;
documents.set(await getDocs(localStorage.token));
}
};
2024-02-03 22:44:49 +00:00
const addTagHandler = async (tagName) => {
2024-02-04 01:21:51 +00:00
if (!tags.find((tag) => tag.name === tagName) && tagName !== '') {
2024-02-03 22:44:49 +00:00
tags = [...tags, { name: tagName }];
await tagDocByName(localStorage.token, doc.name, {
name: doc.name,
tags: tags
});
documents.set(await getDocs(localStorage.token));
} else {
console.log('tag already exists');
}
};
const deleteTagHandler = async (tagName) => {
tags = tags.filter((tag) => tag.name !== tagName);
await tagDocByName(localStorage.token, doc.name, {
name: doc.name,
tags: tags
});
documents.set(await getDocs(localStorage.token));
};
2024-01-08 09:49:20 +00:00
onMount(() => {
if (selectedDoc) {
doc = JSON.parse(JSON.stringify(selectedDoc));
2024-02-03 22:44:49 +00:00
tags = doc?.content?.tags ?? [];
2024-01-08 09:49:20 +00:00
}
});
</script>
<Modal size="sm" bind:show>
<div>
2024-05-02 00:55:18 +00:00
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4">
<div class=" text-lg font-medium self-center">{$i18n.t('Edit Doc')}</div>
2024-01-08 09:49:20 +00:00
<button
class="self-center"
on:click={() => {
show = false;
}}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
class="w-5 h-5"
>
<path
d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
/>
</svg>
</button>
</div>
2024-01-08 09:57:09 +00:00
<div class="flex flex-col md:flex-row w-full px-5 py-4 md:space-x-4 dark:text-gray-200">
2024-01-08 09:49:20 +00:00
<div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
<form
class="flex flex-col w-full"
on:submit|preventDefault={() => {
submitHandler();
}}
>
<div class=" flex flex-col space-y-1.5">
<div class="flex flex-col w-full">
<div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name Tag')}</div>
2024-01-08 09:49:20 +00:00
<div class="flex flex-1">
<div
2024-07-08 20:35:25 +00:00
class="bg-gray-200 dark:bg-gray-800 font-semibold px-3 py-0.5 border border-r-0 dark:border-gray-800 rounded-l-xl flex items-center"
2024-01-08 09:49:20 +00:00
>
#
</div>
<input
2024-05-02 00:55:18 +00:00
class="w-full rounded-r-xl py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 disabled:text-gray-500 dark:disabled:text-gray-500 outline-none"
2024-01-08 09:49:20 +00:00
type="text"
bind:value={doc.name}
autocomplete="off"
required
/>
</div>
</div>
<div class="flex flex-col w-full">
<div class=" mb-1 text-xs text-gray-500">{$i18n.t('Title')}</div>
2024-01-08 09:49:20 +00:00
<div class="flex-1">
<input
2024-05-02 00:55:18 +00:00
class="w-full rounded-xl py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
2024-01-08 09:49:20 +00:00
type="text"
bind:value={doc.title}
autocomplete="off"
required
/>
</div>
</div>
2024-02-03 22:44:49 +00:00
<div class="flex flex-col w-full">
2024-05-02 00:55:18 +00:00
<div class=" mb-2 text-xs text-gray-500">{$i18n.t('Tags')}</div>
2024-02-03 22:44:49 +00:00
<Tags {tags} addTag={addTagHandler} deleteTag={deleteTagHandler} />
</div>
2024-01-08 09:49:20 +00:00
</div>
2024-01-08 09:57:09 +00:00
<div class="flex justify-end pt-5 text-sm font-medium">
2024-01-08 09:49:20 +00:00
<button
2024-04-28 01:20:43 +00:00
class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
2024-01-08 09:49:20 +00:00
type="submit"
>
2024-03-04 08:53:56 +00:00
{$i18n.t('Save')}
2024-01-08 09:49:20 +00:00
</button>
</div>
</form>
</div>
</div>
</div>
</Modal>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
/* display: none; <- Crashes Chrome on hover */
-webkit-appearance: none;
margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}
.tabs::-webkit-scrollbar {
display: none; /* for Chrome, Safari and Opera */
}
.tabs {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
input[type='number'] {
-moz-appearance: textfield; /* Firefox */
}
</style>