Loggest thine Stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0 KiB

  1. <script lang="ts">
  2. import Icon from "../layout/Icon.svelte";
  3. export let value: string[];
  4. export let exclaimMode = false;
  5. let nextTag = "";
  6. function onKey(ev: KeyboardEvent) {
  7. if ((ev.metaKey||ev.ctrlKey) && nextTag === "" && ev.key === "Backspace") {
  8. value = value.slice(0, -1);
  9. }
  10. if ((ev.metaKey||ev.ctrlKey) && nextTag !== "" && ev.key === "Enter") {
  11. value = [...value, nextTag];
  12. nextTag = "";
  13. ev.preventDefault();
  14. }
  15. }
  16. $: while (nextTag.includes(",")) {
  17. const newTags = nextTag.split(",").map(t => t.trim());
  18. value = [...value, ...newTags.slice(0, -1)];
  19. nextTag = newTags[newTags.length - 1];
  20. }
  21. $: value = (value||[]).filter((e, i) => !value.slice(0, i).includes(e));
  22. $: if (exclaimMode) {
  23. value = (value||[]).filter((e, i) => (
  24. e.startsWith("!")
  25. ? !value.slice(i+1).includes(e.slice(1))
  26. : !value.slice(i+1).includes("!"+e)
  27. ));
  28. }
  29. </script>
  30. <div class="tag-input">
  31. {#each value as tag (tag)}
  32. <div class="tag">
  33. <span>{tag}</span>
  34. <span class="comma">,</span>
  35. <span class="x" on:click={() => value = value.filter(v => v !== tag)}>
  36. <Icon name="times" />
  37. </span>
  38. </div>
  39. {/each}
  40. <input placeholder="Add tag (type , or press Ctrl+Enter to add)" on:keyup={onKey} bind:value={nextTag} />
  41. </div>
  42. <style lang="sass">
  43. @import "../../css/colors"
  44. div.tag-input
  45. width: calc(100% - 2ch)
  46. margin-bottom: 1em
  47. margin-top: 0.20em
  48. min-height: 2em
  49. background: $color-entryhalf
  50. color: $color-entry8
  51. border: none
  52. outline: none
  53. resize: vertical
  54. padding: 0.5em 1ch
  55. display: flex
  56. flex-direction: row
  57. flex-wrap: wrap
  58. > div.tag
  59. margin: 0.25em 0.5ch
  60. background-color: $color-entry1
  61. padding: 0.25em 1ch
  62. border-radius: 0.25em
  63. span.x
  64. font-size: 0.75em
  65. line-height: 1em
  66. user-select: none
  67. cursor: pointer
  68. &:hover
  69. color: $color-entry12
  70. span.comma
  71. font-size: 0
  72. > input
  73. margin-bottom: 0.125em
  74. </style>