EditDocModal.svelte 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { onMount, getContext } from 'svelte';
  5. import { getDocs, tagDocByName, updateDocByName } from '$lib/apis/documents';
  6. import Modal from '../common/Modal.svelte';
  7. import { documents } from '$lib/stores';
  8. import TagInput from '../common/Tags/TagInput.svelte';
  9. import Tags from '../common/Tags.svelte';
  10. import { addTagById } from '$lib/apis/chats';
  11. const i18n = getContext('i18n');
  12. export let show = false;
  13. export let selectedDoc;
  14. let tags = [];
  15. let doc = {
  16. name: '',
  17. title: '',
  18. content: null
  19. };
  20. const submitHandler = async () => {
  21. const res = await updateDocByName(localStorage.token, selectedDoc.name, {
  22. title: doc.title,
  23. name: doc.name
  24. }).catch((error) => {
  25. toast.error(error);
  26. });
  27. if (res) {
  28. show = false;
  29. documents.set(await getDocs(localStorage.token));
  30. }
  31. };
  32. const addTagHandler = async (tagName) => {
  33. if (!tags.find((tag) => tag.name === tagName) && tagName !== '') {
  34. tags = [...tags, { name: tagName }];
  35. await tagDocByName(localStorage.token, doc.name, {
  36. name: doc.name,
  37. tags: tags
  38. });
  39. documents.set(await getDocs(localStorage.token));
  40. } else {
  41. console.log('tag already exists');
  42. }
  43. };
  44. const deleteTagHandler = async (tagName) => {
  45. tags = tags.filter((tag) => tag.name !== tagName);
  46. await tagDocByName(localStorage.token, doc.name, {
  47. name: doc.name,
  48. tags: tags
  49. });
  50. documents.set(await getDocs(localStorage.token));
  51. };
  52. onMount(() => {
  53. if (selectedDoc) {
  54. doc = JSON.parse(JSON.stringify(selectedDoc));
  55. tags = doc?.content?.tags ?? [];
  56. }
  57. });
  58. </script>
  59. <Modal size="sm" bind:show>
  60. <div>
  61. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4">
  62. <div class=" text-lg font-medium self-center">{$i18n.t('Edit Doc')}</div>
  63. <button
  64. class="self-center"
  65. on:click={() => {
  66. show = false;
  67. }}
  68. >
  69. <svg
  70. xmlns="http://www.w3.org/2000/svg"
  71. viewBox="0 0 20 20"
  72. fill="currentColor"
  73. class="w-5 h-5"
  74. >
  75. <path
  76. 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"
  77. />
  78. </svg>
  79. </button>
  80. </div>
  81. <div class="flex flex-col md:flex-row w-full px-5 py-4 md:space-x-4 dark:text-gray-200">
  82. <div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
  83. <form
  84. class="flex flex-col w-full"
  85. on:submit|preventDefault={() => {
  86. submitHandler();
  87. }}
  88. >
  89. <div class=" flex flex-col space-y-1.5">
  90. <div class="flex flex-col w-full">
  91. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name Tag')}</div>
  92. <div class="flex flex-1">
  93. <div
  94. class="bg-gray-200 dark:bg-gray-800 font-bold px-3 py-0.5 border border-r-0 dark:border-gray-800 rounded-l-xl flex items-center"
  95. >
  96. #
  97. </div>
  98. <input
  99. 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"
  100. type="text"
  101. bind:value={doc.name}
  102. autocomplete="off"
  103. required
  104. />
  105. </div>
  106. </div>
  107. <div class="flex flex-col w-full">
  108. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Title')}</div>
  109. <div class="flex-1">
  110. <input
  111. class="w-full rounded-xl py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  112. type="text"
  113. bind:value={doc.title}
  114. autocomplete="off"
  115. required
  116. />
  117. </div>
  118. </div>
  119. <div class="flex flex-col w-full">
  120. <div class=" mb-2 text-xs text-gray-500">{$i18n.t('Tags')}</div>
  121. <Tags {tags} addTag={addTagHandler} deleteTag={deleteTagHandler} />
  122. </div>
  123. </div>
  124. <div class="flex justify-end pt-5 text-sm font-medium">
  125. <button
  126. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  127. type="submit"
  128. >
  129. {$i18n.t('Save')}
  130. </button>
  131. </div>
  132. </form>
  133. </div>
  134. </div>
  135. </div>
  136. </Modal>
  137. <style>
  138. input::-webkit-outer-spin-button,
  139. input::-webkit-inner-spin-button {
  140. /* display: none; <- Crashes Chrome on hover */
  141. -webkit-appearance: none;
  142. margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
  143. }
  144. .tabs::-webkit-scrollbar {
  145. display: none; /* for Chrome, Safari and Opera */
  146. }
  147. .tabs {
  148. -ms-overflow-style: none; /* IE and Edge */
  149. scrollbar-width: none; /* Firefox */
  150. }
  151. input[type='number'] {
  152. -moz-appearance: textfield; /* Firefox */
  153. }
  154. </style>