All files / src/components MultilingualTextarea.vue

96.29% Statements 52/54
90.38% Branches 47/52
95% Functions 19/20
96.07% Lines 49/51

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 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229  152x                 12x 58x                         52x           4x                                     1x                                                                 136x                                 136x         136x 136x   136x   136x   141x     4x 4x         136x 146x 87x   59x     136x   151x     7x 7x       136x   136x 118x 89x         2x 2x 2x 34x 10x 8x   32x   2x       7x 7x 7x 2x 2x 1x     7x     136x       110x       115x 115x       136x 136x   136x                                                                                    
<template>
  <div class="multilingual-textarea">
    <div
      v-if="label && showLanguageSelector"
      class="d-flex justify-content-between align-items-center mb-2"
    >
      <label :for="textareaId" class="form-label mb-0">
        {{ label }}
        <span v-if="required" class="text-danger">*</span>
      </label>
      <div class="language-flags">
        <button
          v-for="lang in enabledLanguages"
          :key="lang"
          type="button"
          :class="['btn btn-sm language-flag', { active: currentLang === lang }]"
          :title="getLanguageName(lang)"
          @click="currentLang = lang"
        >
          {{ getLanguageLabel(lang) }}
        </button>
      </div>
    </div>
    <div v-else-if="showLanguageSelector" class="language-flags mb-2">
      <button
        v-for="lang in enabledLanguages"
        :key="lang"
        type="button"
        :class="['btn btn-sm language-flag', { active: currentLang === lang }]"
        :title="getLanguageName(lang)"
        @click="currentLang = lang"
      >
        {{ getLanguageLabel(lang) }}
      </button>
    </div>
    <label v-else-if="label" :for="textareaId" class="form-label">
      {{ label }}
      <span v-if="required" class="text-danger">*</span>
    </label>
 
    <textarea
      :id="textareaId"
      :value="currentValue"
      :class="['form-control', textareaClass]"
      :placeholder="placeholder"
      :required="required && isDefaultLanguage"
      :rows="rows"
      :maxlength="maxlength"
      @input="updateValue"
      @blur="$emit('blur')"
    />
 
    <small v-if="showLanguageHint" class="text-muted">
      <span v-if="!currentValue && hasOtherTranslations" class="text-warning">
        (empty, will fallback to {{ getLanguageName(defaultLanguage) }})
      </span>
    </small>
  </div>
</template>
 
<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { getLanguageInfo } from '@/utils/languages'
import { useLanguageFlags } from '@/composables/useLanguageFlags'
 
interface Props {
  modelValue?: string | Record<string, string>
  label?: string
  placeholder?: string
  required?: boolean
  textareaClass?: string
  rows?: number
  defaultLanguage?: string
  enabledLanguages?: string[]
  showLanguageHint?: boolean
  currentLanguage?: string
  showLanguageSelector?: boolean
  inputId?: string
  maxNewlines?: number
  maxlength?: number
}
 
const props = withDefaults(defineProps<Props>(), {
  modelValue: () => ({}),
  label: '',
  placeholder: '',
  textareaClass: '',
  required: false,
  rows: 3,
  defaultLanguage: 'en',
  enabledLanguages: () => ['en', 'fr'],
  showLanguageHint: true,
  currentLanguage: undefined,
  showLanguageSelector: true,
  inputId: undefined,
  maxlength: undefined,
  maxNewlines: undefined
})
 
const emit = defineEmits<{
  'update:modelValue': [value: string | Record<string, string>]
  blur: []
}>()
 
const textareaId = computed(
  () => props.inputId ?? `multilingual-textarea-${Math.random().toString(36).substr(2, 9)}`
)
const internalCurrentLang = ref(props.defaultLanguage)
 
const currentLang = computed({
  get() {
    return props.currentLanguage !== undefined ? props.currentLanguage : internalCurrentLang.value
  },
  set(value: string) {
    Eif (props.currentLanguage === undefined) {
      internalCurrentLang.value = value
    }
  }
})
 
const normalizedValue = computed(() => {
  if (typeof props.modelValue === 'string') {
    return { [props.defaultLanguage]: props.modelValue }
  }
  return props.modelValue || {}
})
 
const currentValue = computed({
  get() {
    return normalizedValue.value[currentLang.value] || ''
  },
  set(value: string) {
    const updated = { ...normalizedValue.value, [currentLang.value]: value }
    emit('update:modelValue', updated)
  }
})
 
const isDefaultLanguage = computed(() => currentLang.value === props.defaultLanguage)
 
const hasOtherTranslations = computed(() => {
  return Object.keys(normalizedValue.value).some(
    (lang) => lang !== currentLang.value && normalizedValue.value[lang]
  )
})
 
function limitNewlines(value: string, max: number): string {
  let count = 0
  let result = ''
  for (const char of value) {
    if (char === '\n') {
      if (count >= max) continue
      count++
    }
    result += char
  }
  return result
}
 
function updateValue(event: Event) {
  const target = event.target as HTMLTextAreaElement
  let value = target.value
  if (props.maxNewlines !== undefined) {
    value = limitNewlines(value, props.maxNewlines)
    if (value !== target.value) {
      target.value = value
    }
  }
  currentValue.value = value
}
 
const { flagFor } = useLanguageFlags()
 
/** The flag when flags are shown, else the language name: never a code like "FR" */
function getLanguageLabel(lang: string): string {
  return flagFor(lang) || getLanguageName(lang)
}
 
function getLanguageName(lang: string): string {
  const info = getLanguageInfo(lang)
  return info?.nativeName || lang
}
 
// Auto-switch to default language if current language becomes disabled
watch(
  () => props.enabledLanguages,
  (newLangs) => {
    Iif (props.currentLanguage === undefined && !newLangs.includes(internalCurrentLang.value)) {
      internalCurrentLang.value = props.defaultLanguage
    }
  },
  { immediate: true }
)
</script>
 
<style scoped>
.multilingual-textarea {
  width: 100%;
}
 
.language-flags {
  display: flex;
  gap: 0.25rem;
}
 
.language-flag {
  padding: 0.125rem 0.375rem;
  border: 1px solid #ddd;
  background: white;
  border-radius: 3px;
  font-size: 0.875rem;
  line-height: 1;
  cursor: pointer;
  transition: all 0.2s;
  opacity: 0.6;
}
 
.language-flag:hover {
  opacity: 0.8;
  border-color: #0d6efd;
}
 
.language-flag.active {
  opacity: 1;
  border-color: #0d6efd;
  background: #e7f1ff;
  box-shadow: 0 0 0 0.2rem rgba(13, 110, 253, 0.15);
}
</style>