summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/work_items/components/item_title.vue
blob: 79840cc4f0ffaaa9210637a5087ad4f519284534 (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
<script>
import { escape } from 'lodash';
import { __ } from '~/locale';

import { WI_TITLE_TRACK_LABEL } from '../constants';

export default {
  WI_TITLE_TRACK_LABEL,
  props: {
    initialTitle: {
      type: String,
      required: false,
      default: '',
    },
    placeholder: {
      type: String,
      required: false,
      default: __('Add a title...'),
    },
    disabled: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      title: this.initialTitle,
    };
  },
  methods: {
    getSanitizedTitle(inputEl) {
      const { innerText } = inputEl;
      return escape(innerText);
    },
    handleBlur({ target }) {
      this.$emit('title-changed', this.getSanitizedTitle(target));
    },
    handleInput({ target }) {
      this.$emit('title-input', this.getSanitizedTitle(target));
    },
    handleSubmit() {
      this.$refs.titleEl.blur();
    },
  },
};
</script>

<template>
  <h2
    class="gl-font-weight-normal gl-sm-font-weight-bold gl-my-5 gl-display-inline-block"
    :class="{ 'gl-cursor-not-allowed': disabled }"
    data-testid="title"
    aria-labelledby="item-title"
  >
    <span
      id="item-title"
      ref="titleEl"
      role="textbox"
      :aria-label="__('Title')"
      :data-placeholder="placeholder"
      :data-track-label="$options.WI_TITLE_TRACK_LABEL"
      :contenteditable="!disabled"
      class="gl-pseudo-placeholder"
      @blur="handleBlur"
      @keyup="handleInput"
      @keydown.enter.exact="handleSubmit"
      @keydown.ctrl.u.prevent
      @keydown.meta.u.prevent
      @keydown.ctrl.b.prevent
      @keydown.meta.b.prevent
      >{{ title }}</span
    >
  </h2>
</template>