|
| 1 | +<script lang="ts"> |
| 2 | + import { single_tag_delimiter, active_image, tagCounts, sortedTags } from '$lib/stores'; |
| 3 | +
|
| 4 | + let custom_css_classes = ''; |
| 5 | +
|
| 6 | + let tag_filter_string = ''; |
| 7 | + $: tag_white_list = tag_filter_string |
| 8 | + .split('|') |
| 9 | + .filter((tag) => tag != '' && !tag.startsWith('-')) |
| 10 | + .map((tag) => { |
| 11 | + if (tag.startsWith('^') || tag.endsWith('$')) { |
| 12 | + return new RegExp(tag); |
| 13 | + } |
| 14 | + return tag; |
| 15 | + }); |
| 16 | + $: tag_black_list = tag_filter_string |
| 17 | + .split('|') |
| 18 | + .filter((tag) => tag != '' && tag.startsWith('-')) |
| 19 | + .map((tag) => { |
| 20 | + tag = tag.slice(1); |
| 21 | + if (tag.startsWith('^') || tag.endsWith('$')) { |
| 22 | + return new RegExp(tag); |
| 23 | + } |
| 24 | + return tag; |
| 25 | + }); |
| 26 | +
|
| 27 | + $: fitting_tags = $sortedTags |
| 28 | + .filter((tag) => { |
| 29 | + // Or when the filter string is empty |
| 30 | + if (tag_filter_string == '') return true; |
| 31 | + // Only return if any keyword in the white list is in the tag |
| 32 | + return ( |
| 33 | + tag_white_list.some((keyword) => { |
| 34 | + if (keyword instanceof RegExp) { |
| 35 | + return keyword.test(tag); |
| 36 | + } |
| 37 | + return tag.includes(keyword); |
| 38 | + }) && |
| 39 | + !tag_black_list.some((keyword) => { |
| 40 | + if (keyword instanceof RegExp) { |
| 41 | + return keyword.test(tag); |
| 42 | + } |
| 43 | + return tag.includes(keyword); |
| 44 | + }) |
| 45 | + ); |
| 46 | + }) |
| 47 | + .slice(0, 50) |
| 48 | + .sort((a, b) => a.localeCompare(b)); |
| 49 | +</script> |
| 50 | + |
| 51 | +<hr class="my-2" /> |
| 52 | +<div> |
| 53 | + <label class="label"> |
| 54 | + <span>Single Tag Delimiter</span> |
| 55 | + <input |
| 56 | + class="input variant-form-material" |
| 57 | + type="search" |
| 58 | + placeholder="Single Tag Delimiter" |
| 59 | + title="Single Tag Delimiter" |
| 60 | + on:keydown={(event) => { |
| 61 | + event.stopPropagation(); |
| 62 | + }} |
| 63 | + bind:value={$single_tag_delimiter} |
| 64 | + /> |
| 65 | + </label> |
| 66 | +</div> |
| 67 | +<div> |
| 68 | + <label class="label"> |
| 69 | + <span>Filter tags</span> |
| 70 | + <input |
| 71 | + class="input variant-form-material" |
| 72 | + type="search" |
| 73 | + placeholder="Filter tags" |
| 74 | + title="Filter tags" |
| 75 | + on:keydown={(event) => { |
| 76 | + event.stopPropagation(); |
| 77 | + }} |
| 78 | + bind:value={tag_filter_string} |
| 79 | + /> |
| 80 | + </label> |
| 81 | +</div> |
| 82 | +<div class="overflow-y-scroll pl-1"> |
| 83 | + {#each fitting_tags as tag} |
| 84 | + <label class="flex items-center space-x-2"> |
| 85 | + <input |
| 86 | + class="checkbox" |
| 87 | + type="checkbox" |
| 88 | + checked={($active_image?.caption.split($single_tag_delimiter) || []) |
| 89 | + .map((tag) => tag.trim() + $single_tag_delimiter) |
| 90 | + .includes(tag)} |
| 91 | + on:click={(event) => { |
| 92 | + if ($active_image) { |
| 93 | + if ( |
| 94 | + ($active_image?.caption.split($single_tag_delimiter) || []) |
| 95 | + .map((tag) => tag.trim() + $single_tag_delimiter) |
| 96 | + .includes(tag) |
| 97 | + ) { |
| 98 | + $active_image.caption = $active_image.caption.replace(new RegExp(`${tag} ?`), ''); |
| 99 | + } else { |
| 100 | + console.log('Adding tag ' + tag); |
| 101 | + $active_image.caption = tag + ' ' + $active_image.caption; |
| 102 | + $active_image = $active_image; |
| 103 | + } |
| 104 | + } |
| 105 | + }} |
| 106 | + /> |
| 107 | + <div class="tag-display" data-tag={tag}> |
| 108 | + {tag} |
| 109 | + <div class="inline text-gray opacity-60">{$tagCounts[tag]}</div> |
| 110 | + </div> |
| 111 | + </label> |
| 112 | + {/each} |
| 113 | +</div> |
| 114 | +<hr class="my-2" /> |
| 115 | + |
| 116 | +<svelte:head> |
| 117 | + <!-- Dynamically insert a custom css style tag --> |
| 118 | + <svelte:element this={'style'} type="text/css">{custom_css_classes}</svelte:element> |
| 119 | +</svelte:head> |
0 commit comments