aboutsummaryrefslogtreecommitdiff
path: root/viewer/src/views/layout/left/LayoutTagInput.vue
blob: 9ee6f8a806280437dc658e4af76595fcf01a1d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<!-- ldgallery - A static generator which turns a collection of tagged
--             pictures into a searchable web gallery.
--
-- Copyright (C) 2019-2022  Guillaume FOUET
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as
-- published by the Free Software Foundation, either version 3 of the
-- License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
--
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.
-->

<template>
  <LdInput
    ref="input"
    v-model="search"
    :placeholder="t('tagInput.placeholder')"
    :tabindex="50"
    @focus="(e: FocusEvent) => (e.target as HTMLInputElement).select()"
    @keypress.enter="inputEnter"
    @keydown.backspace="inputBackspace"
  />
  <LdDropdown
    ref="dropdown"
    v-model="showDropdown"
    :list="filteredTags"
    list-key="tagfiltered"
    :tabindex-root="51"
    :class="$style.dropdown"
    :style="dropdownStyle"
    @select="addTag"
    @opening="emit('opening')"
    @closing="cleanSearch(); emit('closing');"
  >
    <template #option="{option}:{option:TagSearch}">
      <div v-text="option.display" />
      <div v-text="option.items.length" />
    </template>
    <template #empty>
      <div
        :class="$style.nomatch"
        v-text="t('tagInput.nomatch')"
      />
    </template>
  </LdDropdown>
</template>

<script setup lang="ts">
import { TagSearch } from '@/@types/tag';
import LdDropdown from '@/components/LdDropdown.vue';
import LdInput from '@/components/LdInput.vue';
import { useIndexFactory } from '@/services/indexFactory';
import { useGalleryStore } from '@/store/galleryStore';
import { useUiStore } from '@/store/uiStore';
import { computedEager, onKeyStroke, useElementBounding, useFocus, useKeyModifier, useVModel } from '@vueuse/core';
import { computed, ref, StyleValue, watchEffect } from 'vue';
import { useI18n } from 'vue-i18n';

const props = defineProps({
  modelValue: { type: Array<TagSearch>, required: true },
});
const emit = defineEmits(['update:modelValue', 'search', 'opening', 'closing']);
const model = useVModel(props, 'modelValue', emit);

const { t } = useI18n();
const uiStore = useUiStore();
const galeryStore = useGalleryStore();
const indexFactory = useIndexFactory();

const search = ref('');
const showDropdown = ref(false);

watchEffect(() => (showDropdown.value = !!search.value));

// ---

const dropdown = ref();
const { top } = useElementBounding(dropdown);
const dropdownStyle = computedEager<StyleValue>(() => ({ height: `calc(100vh - 8px - ${top.value}px)` }));

const input = ref();
const { focused } = useFocus(input);

// ---

const controlState = useKeyModifier('Control');
onKeyStroke('k', e => {
  if (!controlState.value || focused.value) return;
  e.preventDefault();
  uiStore.toggleFullWidth(false);
  focused.value = true;
});

// ---

const filteredTags = computed(() => indexFactory.searchTags(galeryStore.tagsIndex, search.value, false)
  .filter(filterAlreadyPresent)
  .sort((a, b) => b.items.length - a.items.length));

function filterAlreadyPresent(newSearch: TagSearch) {
  return !model.value.find(
    currentSearch =>
      currentSearch.tag === newSearch.tag && (!currentSearch.parent || currentSearch.parent === newSearch.parent),
  );
}

function addTag(tag?: TagSearch) {
  const toPush = tag ?? filteredTags.value[0];
  if (!toPush) return;
  model.value.push(toPush);
  cleanSearch();
}
function inputEnter() {
  if (search.value) addTag();
  else emit('search');
}
function inputBackspace() {
  !showDropdown.value && model.value.pop();
}
function cleanSearch() {
  search.value = '';
  focused.value = true;
}
</script>

<style lang="scss" module>
@import "~@/assets/scss/theme";

.dropdown {
  > div {
    display: flex;
    justify-content: space-between;
    > div {
      padding: 0 4px;
    }
    > div:last-child {
      color: $text-light;
    }
  }
  .nomatch {
    color: $disabled-color;
    justify-content: center;
    cursor: default;
  }
}
</style>