File size: 1,288 Bytes
3092643
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const SELECTOR = '.admin-page textarea.ct-input'

export function growTextarea(el) {
  if (!el || el.tagName !== 'TEXTAREA') return
  const min = Number.parseFloat(getComputedStyle(el).minHeight) || 52
  el.style.height = 'auto'
  el.style.height = `${Math.max(el.scrollHeight, min)}px`
}

export function bindTextarea(el) {
  if (!el?.matches?.(SELECTOR) || el.dataset.autoGrowBound) return
  el.dataset.autoGrowBound = '1'
  el.style.resize = 'none'
  el.style.overflow = 'hidden'
  growTextarea(el)
}

export function scanAdminTextareas() {
  document.querySelectorAll(SELECTOR).forEach((el) => {
    bindTextarea(el)
    growTextarea(el)
  })
}

let scanTimer = null

function scheduleScan() {
  clearTimeout(scanTimer)
  scanTimer = setTimeout(scanAdminTextareas, 40)
}

export function setupAdminTextareaAutoGrow(router) {
  document.addEventListener('input', (event) => {
    if (event.target?.matches?.(SELECTOR)) growTextarea(event.target)
  })

  document.addEventListener(
    'click',
    (event) => {
      if (event.target?.closest?.('.admin-page')) scheduleScan()
    },
    true,
  )

  router.afterEach((to) => {
    if (!to.path.startsWith('/admin')) return
    requestAnimationFrame(scanAdminTextareas)
    scheduleScan()
    setTimeout(scanAdminTextareas, 250)
  })
}