|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | + |
| 3 | +import debug from '../../utils/debug'; |
| 4 | + |
| 5 | +const console = debug('app:javascript:controllers:snippets:editor'); |
| 6 | + |
| 7 | +export default class extends Controller { |
| 8 | + static targets = ['previewButton', 'source', 'textarea']; |
| 9 | + |
| 10 | + declare previewButtonTarget: HTMLButtonElement; |
| 11 | + declare sourceTarget: HTMLElement; |
| 12 | + declare textareaTarget: HTMLTextAreaElement; |
| 13 | + |
| 14 | + connect() { |
| 15 | + console.log('Stimulus controller connected'); |
| 16 | + this.showCode(); |
| 17 | + this.syncTextarea(); |
| 18 | + // this.textareaTarget.addEventListener('input', this.autoGrow.bind(this)); |
| 19 | + // this.textareaTarget.addEventListener( |
| 20 | + // 'blur', |
| 21 | + // this.handleTextareaBlur.bind(this), |
| 22 | + // ); |
| 23 | + } |
| 24 | + |
| 25 | + disconnect() { |
| 26 | + // this.textareaTarget.removeEventListener('input', this.autoGrow.bind(this)); |
| 27 | + // this.textareaTarget.removeEventListener( |
| 28 | + // 'blur', |
| 29 | + // this.handleTextareaBlur.bind(this), |
| 30 | + // ); |
| 31 | + } |
| 32 | + |
| 33 | + preview() { |
| 34 | + this.previewButtonTarget.click(); |
| 35 | + } |
| 36 | + |
| 37 | + showTextarea() { |
| 38 | + this.sourceTarget.style.display = 'none'; |
| 39 | + this.textareaTarget.style.display = 'block'; |
| 40 | + this.textareaTarget.focus(); |
| 41 | + this.autoGrow(); |
| 42 | + } |
| 43 | + |
| 44 | + syncTextarea() { |
| 45 | + this.textareaTarget.value = this.sourceTarget.textContent || ''; |
| 46 | + this.autoGrow(); |
| 47 | + } |
| 48 | + |
| 49 | + showCode() { |
| 50 | + this.sourceTarget.style.display = 'block'; |
| 51 | + this.textareaTarget.style.display = 'none'; |
| 52 | + } |
| 53 | + |
| 54 | + autoGrow() { |
| 55 | + this.textareaTarget.style.height = 'auto'; |
| 56 | + this.textareaTarget.style.height = this.textareaTarget.scrollHeight + 'px'; |
| 57 | + } |
| 58 | +} |
0 commit comments