EditDocModal.svelte 4.8 KB

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