All files / src/components CommentComposer.vue

100% Statements 38/38
100% Branches 20/20
100% Functions 9/9
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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  123x     26x                 7x               7x                                                           70x                               70x   70x     70x       36x     70x 70x 70x         103x 123x   70x 35x 35x 2x 2x   33x     70x 70x 9x 9x         15x 14x 14x 14x   14x           3x 3x 3x                  
<template>
  <div class="comment-composer">
    <textarea
      ref="textareaRef"
      v-model="text"
      class="form-control form-control-sm"
      rows="2"
      :aria-label="label"
      :placeholder="placeholder"
      :disabled="submitting"
      name="comment"
      @keydown.enter.exact.prevent="submit"
    ></textarea>
    <div class="d-flex justify-content-between align-items-center mt-1">
      <small class="comment-counter" :class="{ 'text-danger': length >= MAX_COMMENT_LENGTH }">
        {{
          t('comment.characterCount')
            .replace('{count}', String(length))
            .replace('{max}', String(MAX_COMMENT_LENGTH))
        }}
      </small>
      <div class="d-flex gap-2">
        <button
          v-if="cancelable"
          type="button"
          class="btn btn-sm btn-outline-secondary"
          :disabled="submitting"
          @click="cancel"
        >
          {{ t('common.cancel') }}
        </button>
        <button type="button" class="btn btn-sm btn-primary" :disabled="!canSubmit" @click="submit">
          <span
            v-if="submitting"
            class="spinner-border spinner-border-sm me-1"
            aria-hidden="true"
          />
          {{ submitLabel }}
        </button>
      </div>
    </div>
  </div>
</template>
 
<script setup lang="ts">
// @implements UC-COMMENT-008.1, UC-MOD-005.1
import { computed, nextTick, onMounted, ref, watch } from 'vue'
import { useUILanguage } from '@/composables/useUILanguage'
import { MAX_COMMENT_LENGTH } from '@/utils/constants'
import { readCommentDraft, writeCommentDraft } from '@/utils/commentDraft'
 
const props = withDefaults(
  defineProps<{
    label: string
    placeholder: string
    submitLabel: string
    // Resolves true when the comment was posted, so the field can be cleared.
    onSubmit: (body: string) => Promise<boolean>
    cancelable?: boolean
    autofocus?: boolean
    // Keeps the text being written as a draft under this key (see
    // utils/commentDraft.ts). No key, no draft.
    draftKey?: string
  }>(),
  { cancelable: false, autofocus: false, draftKey: undefined }
)
 
const emit = defineEmits<{ cancel: [] }>()
 
const { t } = useUILanguage()
 
function readDraft(): string {
  return props.draftKey ? readCommentDraft(props.draftKey) : ''
}
 
function writeDraft(value: string) {
  if (props.draftKey) writeCommentDraft(props.draftKey, value)
}
 
const text = ref(readDraft())
const submitting = ref(false)
const textareaRef = ref<HTMLTextAreaElement | null>(null)
 
// Counted in characters (code points), like the backend, not UTF-16 units: an
// emoji is one character but two units, so the textarea's native `maxlength`
// would cut the input short of what the API accepts.
const length = computed(() => [...text.value].length)
const canSubmit = computed(() => !submitting.value && text.value.trim().length > 0)
 
watch(text, (value) => {
  const characters = [...value]
  if (characters.length > MAX_COMMENT_LENGTH) {
    text.value = characters.slice(0, MAX_COMMENT_LENGTH).join('')
    return
  }
  writeDraft(value)
})
 
onMounted(async () => {
  if (props.autofocus) {
    await nextTick()
    textareaRef.value?.focus()
  }
})
 
async function submit() {
  if (!canSubmit.value) return
  submitting.value = true
  try {
    if (await props.onSubmit(text.value.trim())) text.value = ''
  } finally {
    submitting.value = false
  }
}
 
// Cancelling is deliberate: the draft goes with it
function cancel() {
  text.value = ''
  writeDraft('')
  emit('cancel')
}
</script>
 
<style scoped>
.comment-counter {
  color: var(--bw-text-muted, #6c757d);
}
</style>