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
|
<script>
import { GlTooltipDirective as GlTooltip, GlCollapsibleListbox } from '@gitlab/ui';
import { __ } from '~/locale';
import { TEXT_STYLE_DROPDOWN_ITEMS } from '../constants';
import EditorStateObserver from './editor_state_observer.vue';
export default {
components: {
GlCollapsibleListbox,
EditorStateObserver,
},
directives: {
GlTooltip,
},
inject: ['tiptapEditor'],
data() {
return {
activeItem: null,
};
},
computed: {
activeItemLabel() {
const { activeItem } = this;
return activeItem ? activeItem.label : this.$options.i18n.placeholder;
},
listboxItems() {
return this.$options.items.map((item) => {
return {
value: item.label,
text: item.label,
};
});
},
},
methods: {
mapDropdownItemToCommand(dropdownItem) {
return this.$options.items.find((option) => option.label === dropdownItem);
},
updateActiveItem({ editor }) {
this.activeItem = TEXT_STYLE_DROPDOWN_ITEMS.find((item) =>
editor.isActive(item.contentType, item.commandParams),
);
},
execute(item) {
const { editorCommand, contentType, commandParams } = this.mapDropdownItemToCommand(item);
const value = commandParams?.level;
if (editorCommand) {
this.tiptapEditor
.chain()
[editorCommand](commandParams || {})
.focus()
.run();
}
this.$emit('execute', { contentType, value });
},
isActive(dropdownItem) {
return this.tiptapEditor.isActive(dropdownItem.contentType, dropdownItem.commandParams);
},
},
items: TEXT_STYLE_DROPDOWN_ITEMS,
i18n: {
placeholder: __('Text style'),
},
};
</script>
<template>
<editor-state-observer @transaction="updateActiveItem">
<gl-collapsible-listbox
v-gl-tooltip.hover="$options.i18n.placeholder"
:items="listboxItems"
:toggle-text="activeItemLabel"
:selected="activeItemLabel"
:disabled="!activeItem"
:data-qa-text-style="activeItemLabel"
data-testid="text-style-dropdown"
size="small"
toggle-class="btn-default-tertiary"
@select="execute"
/>
</editor-state-observer>
</template>
|