|
| 1 | +<template> |
| 2 | + <div :id="id" class="d-flex flex-row"> |
| 3 | + <div ref="lineNumbers" class="container__lines border-e bg-muted" /> |
| 4 | + <v-textarea |
| 5 | + ref="textarea" |
| 6 | + wrap="off" |
| 7 | + :label="label" |
| 8 | + :hint="hint" |
| 9 | + persistent-hint |
| 10 | + persistent-placeholder |
| 11 | + class="pa-0 container__textarea" |
| 12 | + :placeholder="placeholder" |
| 13 | + v-model="model" |
| 14 | + :rows="15" |
| 15 | + @keydown.tab.prevent="tabber($event)" |
| 16 | + /> |
| 17 | + </div> |
| 18 | +</template> |
| 19 | + |
| 20 | +<script setup lang="ts"> |
| 21 | + import { getId } from '@/plugins/id' |
| 22 | +
|
| 23 | + const model = defineModel<string>() |
| 24 | +
|
| 25 | + const compProps = defineProps<{ |
| 26 | + placeholder: string |
| 27 | + label: string |
| 28 | + hint: string |
| 29 | + }>() |
| 30 | +
|
| 31 | + const id = ref(`line-number-input-${getId()}`) |
| 32 | +
|
| 33 | + const styleProps = [ |
| 34 | + 'fontFamily', |
| 35 | + 'fontSize', |
| 36 | + 'fontWeight', |
| 37 | + 'letterSpacing', |
| 38 | + 'lineHeight', |
| 39 | + 'padding', |
| 40 | + ] |
| 41 | +
|
| 42 | + function tabber (event: Event) { |
| 43 | + const text = model.value || '' |
| 44 | + // @ts-ignore |
| 45 | + const originalSelectionStart = event.target.selectionStart |
| 46 | + const textStart = text.slice(0, originalSelectionStart) |
| 47 | + const textEnd = text.slice(originalSelectionStart) |
| 48 | +
|
| 49 | + model.value = `${textStart}\t${textEnd}` |
| 50 | + // @ts-ignore |
| 51 | + event.target.value = model.value // required to make the cursor stay in place. |
| 52 | +
|
| 53 | + nextTick(() => { |
| 54 | + // @ts-ignore |
| 55 | + event.target.selectionEnd = event.target.selectionStart = originalSelectionStart + 1 |
| 56 | + }) |
| 57 | + } |
| 58 | +
|
| 59 | + onMounted(() => { |
| 60 | + const ta = document.querySelector(`#${id.value} .container__textarea textarea`) as HTMLTextAreaElement |
| 61 | + const lineNumbersEle = document.querySelector(`#${id.value} .container__lines`) as HTMLDivElement |
| 62 | +
|
| 63 | + if (!ta || !lineNumbersEle) { |
| 64 | + return |
| 65 | + } |
| 66 | +
|
| 67 | + const textareaStyles = window.getComputedStyle(ta) |
| 68 | + styleProps.forEach((property: string) => { |
| 69 | + lineNumbersEle.style.setProperty(property, textareaStyles.getPropertyValue(property)) |
| 70 | + }) |
| 71 | +
|
| 72 | + const font = `${textareaStyles.fontSize} ${textareaStyles.fontFamily}` |
| 73 | +
|
| 74 | + const canvas = document.createElement('canvas') |
| 75 | + const context = canvas.getContext('2d') |
| 76 | + // @ts-ignore |
| 77 | + context.font = font |
| 78 | +
|
| 79 | + const calculateLineNumbers = () => { |
| 80 | + const lines = (ta.value || '').split(/\r?\n/) |
| 81 | + const numLines = lines.concat() |
| 82 | +
|
| 83 | + const lineNumbers = [] |
| 84 | + let i = 1 |
| 85 | + while (numLines.length > 0) { |
| 86 | + const numLinesOfSentence = +(numLines.shift() || '0') |
| 87 | + lineNumbers.push(i) |
| 88 | + if (numLinesOfSentence > 1) { |
| 89 | + new Array(numLinesOfSentence - 1).fill('').forEach(() => lineNumbers.push('')) |
| 90 | + } |
| 91 | + i++ |
| 92 | + } |
| 93 | +
|
| 94 | + return lineNumbers |
| 95 | + } |
| 96 | +
|
| 97 | + const displayLineNumbers = () => { |
| 98 | + const lineNumbers = calculateLineNumbers() |
| 99 | + lineNumbersEle.innerHTML = Array.from({ length: lineNumbers.length }, (_, i) => `<div>${lineNumbers[i] || ' '}</div>`).join('') |
| 100 | + } |
| 101 | +
|
| 102 | + ta.addEventListener('input', () => displayLineNumbers()) |
| 103 | +
|
| 104 | + displayLineNumbers() |
| 105 | +
|
| 106 | + const ro = new ResizeObserver(() => { |
| 107 | + const rect = ta.getBoundingClientRect() |
| 108 | + lineNumbersEle.style.height = `${rect.height}px` |
| 109 | + displayLineNumbers() |
| 110 | + }) |
| 111 | + ro.observe(ta) |
| 112 | +
|
| 113 | + ta.addEventListener('scroll', () => { |
| 114 | + lineNumbersEle.scrollTop = ta.scrollTop |
| 115 | + }) |
| 116 | + }) |
| 117 | +</script> |
| 118 | + |
| 119 | +<style scoped> |
| 120 | +.container__textarea { |
| 121 | + border: none; |
| 122 | + outline: none; |
| 123 | + padding: 0.5rem; |
| 124 | + width: 100%; |
| 125 | +} |
| 126 | +.container__lines { |
| 127 | + padding: 0.5rem; |
| 128 | + text-align: right; |
| 129 | + overflow: hidden; |
| 130 | + white-space: nowrap; |
| 131 | + -webkit-user-select: none; /* Safari */ |
| 132 | + -ms-user-select: none; /* IE 10 and IE 11 */ |
| 133 | + user-select: none; /* Standard syntax */ |
| 134 | +} |
| 135 | +</style> |
0 commit comments