ModelList.svelte 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script lang="ts">
  2. import Sortable from 'sortablejs';
  3. import { createEventDispatcher, getContext, onMount } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { models } from '$lib/stores';
  6. import Tooltip from '$lib/components/common/Tooltip.svelte';
  7. import EllipsisVertical from '$lib/components/icons/EllipsisVertical.svelte';
  8. export let modelIds = [];
  9. let sortable = null;
  10. let modelListElement = null;
  11. const positionChangeHandler = () => {
  12. const modelList = Array.from(modelListElement.children).map((child) =>
  13. child.id.replace('model-item-', '')
  14. );
  15. modelIds = modelList;
  16. };
  17. $: if (modelIds) {
  18. init();
  19. }
  20. const init = () => {
  21. if (sortable) {
  22. sortable.destroy();
  23. }
  24. if (modelListElement) {
  25. sortable = Sortable.create(modelListElement, {
  26. animation: 150,
  27. handle: '.item-handle',
  28. onUpdate: async (event) => {
  29. positionChangeHandler();
  30. }
  31. });
  32. }
  33. };
  34. </script>
  35. {#if modelIds.length > 0}
  36. <div class="flex flex-col -translate-x-1" bind:this={modelListElement}>
  37. {#each modelIds as modelId, modelIdx (modelId)}
  38. <div class=" flex gap-2 w-full justify-between items-center" id="model-item-{modelId}">
  39. <Tooltip content={modelId} placement="top-start">
  40. <div class="flex items-center gap-1">
  41. <EllipsisVertical className="size-4 cursor-move item-handle" />
  42. <div class=" text-sm flex-1 py-1 rounded-lg">
  43. {#if $models.find((model) => model.id === modelId)}
  44. {$models.find((model) => model.id === modelId).name}
  45. {:else}
  46. {modelId}
  47. {/if}
  48. </div>
  49. </div>
  50. </Tooltip>
  51. </div>
  52. {/each}
  53. </div>
  54. {:else}
  55. <div class="text-gray-500 text-xs text-center py-2">
  56. {$i18n.t('No models found')}
  57. </div>
  58. {/if}