@gitlab/ui 68.2.0 → 68.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,8 @@
1
1
  <script>
2
2
  import throttle from 'lodash/throttle';
3
3
  import emptySvg from '@gitlab/svgs/dist/illustrations/empty-state/empty-activity-md.svg';
4
+ import GlDropdownItem from '../../../base/dropdown/dropdown_item.vue';
5
+ import GlCard from '../../../base/card/card.vue';
4
6
  import GlEmptyState from '../../../regions/empty_state/empty_state.vue';
5
7
  import GlButton from '../../../base/button/button.vue';
6
8
  import GlAlert from '../../../base/alert/alert.vue';
@@ -34,6 +36,29 @@ export const i18n = {
34
36
  ],
35
37
  };
36
38
 
39
+ export const slashCommands = [
40
+ {
41
+ name: '/reset',
42
+ shouldSubmit: true,
43
+ description: 'Reset conversation, ignore the previous messages.',
44
+ },
45
+ {
46
+ name: '/test',
47
+ shouldSubmit: false,
48
+ description: 'Write tests for the code snippet.',
49
+ },
50
+ {
51
+ name: '/refactor',
52
+ shouldSubmit: false,
53
+ description: 'Refactor the code snippet.',
54
+ },
55
+ {
56
+ name: '/explain',
57
+ shouldSubmit: false,
58
+ description: 'Explain the code snippet.',
59
+ },
60
+ ];
61
+
37
62
  const isMessage = (item) => Boolean(item) && item?.role;
38
63
 
39
64
  const itemsValidator = (items) => items.every(isMessage);
@@ -52,6 +77,8 @@ export default {
52
77
  GlDuoChatLoader,
53
78
  GlDuoChatPredefinedPrompts,
54
79
  GlDuoChatConversation,
80
+ GlCard,
81
+ GlDropdownItem,
55
82
  },
56
83
  directives: {
57
84
  SafeHtml,
@@ -131,12 +158,21 @@ export default {
131
158
  required: false,
132
159
  default: i18n.CHAT_DEFAULT_TITLE,
133
160
  },
161
+ /**
162
+ * Whether the slash commands should be available to user when typing the prompt.
163
+ */
164
+ withSlashCommands: {
165
+ type: Boolean,
166
+ required: false,
167
+ default: false,
168
+ },
134
169
  },
135
170
  data() {
136
171
  return {
137
172
  isHidden: false,
138
173
  prompt: '',
139
174
  scrolledToBottom: true,
175
+ activeCommandIndex: 0,
140
176
  };
141
177
  },
142
178
  computed: {
@@ -166,6 +202,19 @@ export default {
166
202
  const lastMessage = this.messages[this.messages.length - 1];
167
203
  return lastMessage.content === CHAT_RESET_MESSAGE;
168
204
  },
205
+ filteredSlashCommands() {
206
+ const caseInsensitivePrompt = this.prompt.toLowerCase();
207
+ return slashCommands.filter((c) => c.name.toLowerCase().startsWith(caseInsensitivePrompt));
208
+ },
209
+ shouldShowSlashCommands() {
210
+ if (!this.withSlashCommands) return false;
211
+ const caseInsensitivePrompt = this.prompt.toLowerCase();
212
+ const startsWithSlash = caseInsensitivePrompt.startsWith('/');
213
+ const startsWithSlashCommand = slashCommands.some((c) =>
214
+ caseInsensitivePrompt.startsWith(c.name)
215
+ );
216
+ return startsWithSlash && this.filteredSlashCommands.length && !startsWithSlashCommand;
217
+ },
169
218
  },
170
219
  watch: {
171
220
  isLoading() {
@@ -225,6 +274,51 @@ export default {
225
274
  */
226
275
  this.$emit('track-feedback', event);
227
276
  },
277
+ onInputKeyup(e) {
278
+ const { metaKey, ctrlKey, altKey, shiftKey } = e;
279
+
280
+ if (this.shouldShowSlashCommands) {
281
+ e.preventDefault();
282
+
283
+ if (e.key === 'Enter') {
284
+ this.selectSlashCommand(this.activeCommandIndex);
285
+ } else if (e.key === 'ArrowUp') {
286
+ this.prevCommand();
287
+ } else if (e.key === 'ArrowDown') {
288
+ this.nextCommand();
289
+ } else {
290
+ this.activeCommandIndex = 0;
291
+ }
292
+ } else if (e.key === 'Enter' && !(metaKey || ctrlKey || altKey || shiftKey)) {
293
+ e.preventDefault();
294
+ this.sendChatPrompt();
295
+ }
296
+ },
297
+ prevCommand() {
298
+ this.activeCommandIndex -= 1;
299
+ this.wrapCommandIndex();
300
+ },
301
+ nextCommand() {
302
+ this.activeCommandIndex += 1;
303
+ this.wrapCommandIndex();
304
+ },
305
+ wrapCommandIndex() {
306
+ if (this.activeCommandIndex < 0) {
307
+ this.activeCommandIndex = this.filteredSlashCommands.length - 1;
308
+ } else if (this.activeCommandIndex >= this.filteredSlashCommands.length) {
309
+ this.activeCommandIndex = 0;
310
+ }
311
+ },
312
+ selectSlashCommand(index) {
313
+ const command = this.filteredSlashCommands[index];
314
+ if (command.shouldSubmit) {
315
+ this.prompt = command.name;
316
+ this.sendChatPrompt();
317
+ } else {
318
+ this.prompt = `${command.name} `;
319
+ this.$refs.prompt.$el.focus();
320
+ }
321
+ },
228
322
  },
229
323
  i18n,
230
324
  emptySvg,
@@ -349,7 +443,30 @@ export default {
349
443
  class="duo-chat-input gl-flex-grow-1 gl-vertical-align-top gl-max-w-full gl-min-h-8 gl-inset-border-1-gray-400 gl-rounded-base gl-bg-white"
350
444
  :data-value="prompt"
351
445
  >
446
+ <gl-card
447
+ v-if="shouldShowSlashCommands"
448
+ ref="commands"
449
+ class="slash-commands gl-absolute! gl-translate-y-n100 gl-list-style-none gl-pl-0 gl-w-full gl-shadow-md"
450
+ body-class="gl-p-2!"
451
+ >
452
+ <gl-dropdown-item
453
+ v-for="(command, index) in filteredSlashCommands"
454
+ :key="command.name"
455
+ :class="{ 'active-command': index === activeCommandIndex }"
456
+ @mouseenter.native="activeCommandIndex = index"
457
+ @click="selectSlashCommand(index)"
458
+ >
459
+ <span class="gl-display-flex gl-justify-content-space-between">
460
+ <span class="gl-display-block">{{ command.name }}</span>
461
+ <small class="gl-text-gray-500 gl-font-style-italic">{{
462
+ command.description
463
+ }}</small>
464
+ </span>
465
+ </gl-dropdown-item>
466
+ </gl-card>
467
+
352
468
  <gl-form-textarea
469
+ ref="prompt"
353
470
  v-model="prompt"
354
471
  data-testid="chat-prompt-input"
355
472
  class="gl-absolute gl-h-full! gl-py-4! gl-bg-transparent! gl-rounded-top-right-none gl-rounded-bottom-right-none gl-shadow-none!"
@@ -357,7 +474,8 @@ export default {
357
474
  :placeholder="$options.i18n.CHAT_PROMPT_PLACEHOLDER"
358
475
  :disabled="isLoading"
359
476
  autofocus
360
- @keydown.enter.exact.native.prevent="sendChatPrompt"
477
+ @keydown.enter.exact.native.prevent
478
+ @keyup.native="onInputKeyup"
361
479
  />
362
480
  </div>
363
481
  <template #append>
package/src/config.js CHANGED
@@ -14,6 +14,11 @@ const tooltipGlobalConfig = {
14
14
  delay: tooltipDelay,
15
15
  };
16
16
 
17
+ const popoverDelayConfig = {
18
+ show: 50, // BootstrapVue's default delay on show.
19
+ hide: 150, // Increased hide delay so that it doesn't disappear to quickly when user attempts to interact with the content.
20
+ };
21
+
17
22
  /**
18
23
  * Guard against nonexistent localStorage,
19
24
  * or corrupted localStorage
@@ -57,6 +62,9 @@ const setConfigs = ({ translations } = {}) => {
57
62
  Vue.use(BVConfigPlugin, {
58
63
  BFormText: bFormTextGlobalConfig,
59
64
  BTooltip: tooltipGlobalConfig,
65
+ BPopover: {
66
+ delay: popoverDelayConfig,
67
+ },
60
68
  });
61
69
 
62
70
  if (typeof translations === 'object') {