All files / src/components MarkdownEditor.vue

100% Statements 188/188
100% Branches 0/0
100% Functions 0/0
100% Lines 188/188

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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270                        1x 1x 1x       1x   1x                         1x       1x   1x 1x   1x 1x 1x 1x         1x 1x 1x 1x 1x 1x       1x 1x               1x 1x 1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x         1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x       1x 1x 1x   1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x    
<script setup lang="ts">
/**
 * MarkdownEditor — textarea + toolbar + preview tab.
 *
 * Lightweight by design: no WYSIWYG. The toolbar wraps the current
 * selection in markdown syntax (e.g. select "foo" → click B → ``**foo**``).
 * Cursor lands inside the wrap when the selection was empty, so the user
 * can keep typing without manually nudging.
 *
 * Auto-resize behaviour is inlined from the previous AddAppsView
 * implementation so we don't regress that view's look.
 */
import { ref, computed, nextTick, watch, onMounted } from 'vue'
import { useI18n } from 'vue-i18n'
import {
  Bold, Italic, Heading1, Heading2, List, Link as LinkIcon,
  Code as CodeIcon, Code2, Eye, Pencil,
} from 'lucide-vue-next'
import MarkdownRenderer from './MarkdownRenderer.vue'
 
const props = withDefaults(defineProps<{
  modelValue: string
  placeholder?: string
  /** Minimum textarea height in px. Auto-resize grows past this. */
  minHeightPx?: number
  /** Hard cap on auto-resize. Scroll kicks in past this. */
  maxHeightPx?: number
}>(), {
  placeholder: '',
  minHeightPx: 96,
  maxHeightPx: 320,
})
 
const emit = defineEmits<{
  (e: 'update:modelValue', value: string): void
}>()
 
const { t } = useI18n()
 
const mode = ref<'edit' | 'preview'>('edit')
const textareaRef = ref<HTMLTextAreaElement | null>(null)
 
const local = computed({
  get: () => props.modelValue ?? '',
  set: (v: string) => emit('update:modelValue', v),
})
 
// ----------------------------------------------------------------
// Auto-resize
// ----------------------------------------------------------------
const autoResize = () => {
  const el = textareaRef.value
  if (!el) return
  el.style.height = 'auto'
  el.style.height = `${Math.min(Math.max(el.scrollHeight, props.minHeightPx), props.maxHeightPx)}px`
}
 
// Re-size after the parent updates the model (e.g. clearing the form
// from the outside) and on mount once the textarea is in the DOM.
watch(() => props.modelValue, () => nextTick(autoResize))
onMounted(autoResize)
 
// ----------------------------------------------------------------
// Toolbar actions
// ----------------------------------------------------------------
type WrapAction = { prefix: string; suffix: string; placeholder: string }
type LinePrefixAction = { linePrefix: string; placeholder: string }
 
const setSelection = (start: number, end: number) => {
  const el = textareaRef.value
  if (!el) return
  // Defer to after the v-model round-trip so the new value is in the
  // textarea before we touch selectionStart/End.
  nextTick(() => {
    el.focus()
    el.setSelectionRange(start, end)
    autoResize()
  })
}
 
const applyWrap = ({ prefix, suffix, placeholder }: WrapAction) => {
  const el = textareaRef.value
  if (!el) return
  const start = el.selectionStart
  const end = el.selectionEnd
  const value = local.value
  const selected = value.slice(start, end) || placeholder
  const next = value.slice(0, start) + prefix + selected + suffix + value.slice(end)
  local.value = next
  const cursorStart = start + prefix.length
  const cursorEnd = cursorStart + selected.length
  setSelection(cursorStart, cursorEnd)
}
 
const applyLinePrefix = ({ linePrefix, placeholder }: LinePrefixAction) => {
  const el = textareaRef.value
  if (!el) return
  const start = el.selectionStart
  const end = el.selectionEnd
  const value = local.value
  // Expand selection to full lines so the prefix applies cleanly to a
  // multi-line block (e.g. turning three pasted lines into a list).
  const lineStart = value.lastIndexOf('\n', start - 1) + 1
  const lineEnd = end === start ? end : (value.indexOf('\n', end - 1) === -1 ? value.length : value.indexOf('\n', end - 1))
  const block = value.slice(lineStart, lineEnd === -1 ? value.length : lineEnd) || placeholder
  const prefixed = block.split('\n').map(line => `${linePrefix}${line}`).join('\n')
  const next = value.slice(0, lineStart) + prefixed + value.slice(lineEnd === -1 ? value.length : lineEnd)
  local.value = next
  setSelection(lineStart, lineStart + prefixed.length)
}
 
const applyLink = () => {
  const el = textareaRef.value
  if (!el) return
  const start = el.selectionStart
  const end = el.selectionEnd
  const value = local.value
  const selected = value.slice(start, end) || t('markdownEditor.linkText')
  // ``url`` placeholder gets highlighted so the user can immediately
  // overwrite it; that's what most editors do.
  const insertion = `[${selected}](url)`
  const next = value.slice(0, start) + insertion + value.slice(end)
  local.value = next
  const urlStart = start + selected.length + 3 // after "[selected]("
  const urlEnd = urlStart + 3 // "url"
  setSelection(urlStart, urlEnd)
}
 
const applyCodeBlock = () => {
  const el = textareaRef.value
  if (!el) return
  const start = el.selectionStart
  const end = el.selectionEnd
  const value = local.value
  const selected = value.slice(start, end) || t('markdownEditor.codePlaceholder')
  // Always wrap with newlines so we don't accidentally inline-merge with
  // the surrounding paragraph.
  const insertion = `\n\`\`\`\n${selected}\n\`\`\`\n`
  const next = value.slice(0, start) + insertion + value.slice(end)
  local.value = next
  const codeStart = start + 5 // skip ``\n```\n``
  const codeEnd = codeStart + selected.length
  setSelection(codeStart, codeEnd)
}
 
// ----------------------------------------------------------------
// Keyboard shortcuts (Ctrl/Cmd-B, Ctrl/Cmd-I)
// ----------------------------------------------------------------
const onKeydown = (e: KeyboardEvent) => {
  const meta = e.ctrlKey || e.metaKey
  if (!meta) return
  if (e.key === 'b' || e.key === 'B') {
    e.preventDefault()
    applyWrap({ prefix: '**', suffix: '**', placeholder: t('markdownEditor.boldPlaceholder') })
  } else if (e.key === 'i' || e.key === 'I') {
    e.preventDefault()
    applyWrap({ prefix: '_', suffix: '_', placeholder: t('markdownEditor.italicPlaceholder') })
  }
}
 
// ----------------------------------------------------------------
// Toolbar button declarations
// ----------------------------------------------------------------
const onBold = () =>
  applyWrap({ prefix: '**', suffix: '**', placeholder: t('markdownEditor.boldPlaceholder') })
const onItalic = () =>
  applyWrap({ prefix: '_', suffix: '_', placeholder: t('markdownEditor.italicPlaceholder') })
const onInlineCode = () =>
  applyWrap({ prefix: '`', suffix: '`', placeholder: t('markdownEditor.codePlaceholder') })
const onH1 = () =>
  applyLinePrefix({ linePrefix: '# ', placeholder: t('markdownEditor.headingPlaceholder') })
const onH2 = () =>
  applyLinePrefix({ linePrefix: '## ', placeholder: t('markdownEditor.headingPlaceholder') })
const onList = () =>
  applyLinePrefix({ linePrefix: '- ', placeholder: t('markdownEditor.listPlaceholder') })
</script>
 
<template>
  <div class="w-full rounded-lg border border-gray-200 bg-white shadow-sm overflow-hidden">
    <!-- Tab bar -->
    <div class="flex items-center gap-0.5 border-b border-gray-200 bg-gray-50 px-2">
      <button
        type="button"
        @click="mode = 'edit'"
        class="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium border-b-2 transition-colors -mb-px"
        :class="mode === 'edit'
          ? 'border-primary text-primary'
          : 'border-transparent text-gray-500 hover:text-gray-800'"
      >
        <Pencil :size="12" />
        {{ t('markdownEditor.tabEdit') }}
      </button>
      <button
        type="button"
        @click="mode = 'preview'"
        class="flex items-center gap-1.5 px-3 py-1.5 text-xs font-medium border-b-2 transition-colors -mb-px"
        :class="mode === 'preview'
          ? 'border-primary text-primary'
          : 'border-transparent text-gray-500 hover:text-gray-800'"
      >
        <Eye :size="12" />
        {{ t('markdownEditor.tabPreview') }}
      </button>
 
      <!-- Toolbar (edit mode only) -->
      <div
        v-if="mode === 'edit'"
        class="ml-auto flex items-center gap-0.5 py-1"
      >
        <button type="button" @click="onBold" :title="t('markdownEditor.toolbar.bold')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <Bold :size="14" />
        </button>
        <button type="button" @click="onItalic" :title="t('markdownEditor.toolbar.italic')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <Italic :size="14" />
        </button>
        <span class="mx-1 h-4 w-px bg-gray-300"></span>
        <button type="button" @click="onH1" :title="t('markdownEditor.toolbar.h1')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <Heading1 :size="14" />
        </button>
        <button type="button" @click="onH2" :title="t('markdownEditor.toolbar.h2')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <Heading2 :size="14" />
        </button>
        <span class="mx-1 h-4 w-px bg-gray-300"></span>
        <button type="button" @click="onList" :title="t('markdownEditor.toolbar.list')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <List :size="14" />
        </button>
        <button type="button" @click="applyLink" :title="t('markdownEditor.toolbar.link')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <LinkIcon :size="14" />
        </button>
        <button type="button" @click="onInlineCode" :title="t('markdownEditor.toolbar.code')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <CodeIcon :size="14" />
        </button>
        <button type="button" @click="applyCodeBlock" :title="t('markdownEditor.toolbar.codeblock')"
                class="p-1 rounded hover:bg-gray-200 text-gray-600">
          <Code2 :size="14" />
        </button>
      </div>
    </div>
 
    <!-- Edit / Preview body -->
    <div class="p-2">
      <textarea
        v-show="mode === 'edit'"
        ref="textareaRef"
        v-model="local"
        :placeholder="placeholder"
        @input="autoResize"
        @keydown="onKeydown"
        :style="{ minHeight: `${minHeightPx}px`, maxHeight: `${maxHeightPx}px` }"
        class="block w-full resize-none rounded-md border border-transparent bg-white px-2 py-1.5 text-sm text-gray-700 focus:ring-2 focus:ring-primary focus:border-primary outline-none font-mono overflow-y-auto"
      />
      <div
        v-show="mode === 'preview'"
        class="px-2 py-1.5 text-sm"
        :style="{ minHeight: `${minHeightPx}px` }"
      >
        <MarkdownRenderer v-if="local.trim()" :source="local" variant="full" />
        <p v-else class="text-gray-400 italic">{{ t('markdownEditor.emptyPreview') }}</p>
      </div>
    </div>
  </div>
</template>