Welcome to mirror list, hosted at ThFree Co, Russian Federation.

picker.vue « components « emoji « javascripts « assets « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 238f0d81b2212e5b03ef4c53e1ca205f0b1675b1 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<!-- eslint-disable vue/multi-word-component-names -->
<script>
import { GlIcon, GlDropdown, GlDropdownItem, GlSearchBoxByType } from '@gitlab/ui';
import { findLastIndex } from 'lodash';
import VirtualList from 'vue-virtual-scroll-list';
import { CATEGORY_NAMES, getEmojiCategoryMap, state } from '~/emoji';
import { CATEGORY_ICON_MAP, FREQUENTLY_USED_KEY } from '../constants';
import Category from './category.vue';
import EmojiList from './emoji_list.vue';
import { addToFrequentlyUsed, getEmojiCategories, hasFrequentlyUsedEmojis } from './utils';

export default {
  components: {
    GlIcon,
    GlDropdown,
    GlDropdownItem,
    GlSearchBoxByType,
    VirtualList,
    Category,
    EmojiList,
  },
  inject: {
    newCustomEmojiPath: {
      default: '',
    },
  },
  props: {
    toggleClass: {
      type: [Array, String, Object],
      required: false,
      default: () => [],
    },
    dropdownClass: {
      type: [Array, String, Object],
      required: false,
      default: () => [],
    },
    right: {
      type: Boolean,
      required: false,
      default: true,
    },
    boundary: {
      type: String,
      required: false,
      default: '',
    },
  },
  data() {
    return {
      currentCategory: 0,
      searchValue: '',
    };
  },
  computed: {
    categoryNames() {
      return CATEGORY_NAMES.filter((c) => {
        if (c === FREQUENTLY_USED_KEY) return hasFrequentlyUsedEmojis();
        if (c === 'custom') return !state.loading && getEmojiCategoryMap().custom.length > 0;
        return true;
      }).map((category) => ({
        name: category,
        icon: CATEGORY_ICON_MAP[category],
      }));
    },
  },
  methods: {
    categoryAppeared(category) {
      this.currentCategory = category;
    },
    async scrollToCategory(categoryName) {
      const categories = await getEmojiCategories();
      const { top } = categories[categoryName];

      this.$refs.virtualScoller.setScrollTop(top);
    },
    selectEmoji({ category, emoji }) {
      this.$emit('click', emoji);
      this.$refs.dropdown.hide();

      if (category !== 'custom') {
        addToFrequentlyUsed(emoji);
      }
    },
    getBoundaryElement() {
      return this.boundary || document.querySelector('.content-wrapper') || 'scrollParent';
    },
    onSearchInput() {
      this.$refs.virtualScoller.setScrollTop(0);
      this.$refs.virtualScoller.forceRender();
    },
    async onScroll(event, { offset }) {
      const categories = await getEmojiCategories();

      this.currentCategory = findLastIndex(Object.values(categories), ({ top }) => offset >= top);
    },
  },
};
</script>

<template>
  <div class="emoji-picker">
    <gl-dropdown
      ref="dropdown"
      :toggle-class="toggleClass"
      :boundary="getBoundaryElement()"
      :class="dropdownClass"
      menu-class="dropdown-extended-height"
      category="secondary"
      no-flip
      :right="right"
      lazy
      @shown="$emit('shown')"
      @hidden="$emit('hidden')"
    >
      <template #button-content>
        <slot name="button-content">
          <gl-icon class="award-control-icon-neutral gl-button-icon gl-icon" name="slight-smile" />
          <gl-icon
            class="award-control-icon-positive gl-button-icon gl-icon gl-left-3!"
            name="smiley"
          />
          <gl-icon
            class="award-control-icon-super-positive gl-button-icon gl-icon gl-left-3!"
            name="smile"
          />
        </slot>
        <span class="gl-sr-only">{{ __('Add reaction') }}</span>
      </template>
      <gl-search-box-by-type
        v-model="searchValue"
        class="gl-mx-5! gl-mb-2!"
        autofocus
        debounce="500"
        :aria-label="__('Search for an emoji')"
        @input="onSearchInput"
      />
      <div
        v-show="!searchValue"
        class="gl-display-flex gl-mx-5 gl-border-b-solid gl-border-gray-100 gl-border-b-1"
      >
        <button
          v-for="(category, index) in categoryNames"
          :key="category.name"
          :class="{
            'gl-text-body! emoji-picker-category-active': index === currentCategory,
          }"
          type="button"
          class="gl-border-0 gl-border-b-2 gl-border-b-solid gl-flex-grow-1 gl-text-gray-300 gl-pt-3 gl-pb-3 gl-bg-transparent emoji-picker-category-tab"
          :aria-label="category.name"
          @click="scrollToCategory(category.name)"
        >
          <gl-icon :name="category.icon" />
        </button>
      </div>
      <emoji-list :search-value="searchValue">
        <template #default="{ filteredCategories }">
          <virtual-list
            ref="virtualScoller"
            :size="258"
            :remain="1"
            :bench="2"
            variable
            :onscroll="onScroll"
          >
            <div
              v-for="(category, categoryKey) in filteredCategories"
              :key="categoryKey"
              :style="{ height: category.height + 'px' }"
            >
              <category :category="categoryKey" :emojis="category.emojis" @click="selectEmoji" />
            </div>
          </virtual-list>
        </template>
      </emoji-list>
      <template v-if="newCustomEmojiPath" #footer>
        <gl-dropdown-item :href="newCustomEmojiPath">
          {{ __('Create new emoji') }}
        </gl-dropdown-item>
      </template>
    </gl-dropdown>
  </div>
</template>