|
| 1 | +import { Controller } from '@hotwired/stimulus'; |
| 2 | + |
| 3 | +import debug from '../../utils/debug'; |
| 4 | + |
| 5 | +const console = debug('app:javascript:controllers:snippets:preview'); |
| 6 | + |
| 7 | +export default class extends Controller { |
| 8 | + // Make timeout type play nice with TypeScript |
| 9 | + // based on https://donatstudios.com/TypeScriptTimeoutTrouble |
| 10 | + private idleTimeout?: ReturnType<typeof setTimeout>; |
| 11 | + |
| 12 | + static targets = ['source', 'previewButton']; |
| 13 | + |
| 14 | + declare readonly hasSourceTarget: boolean; |
| 15 | + declare readonly sourceTarget: HTMLInputElement; |
| 16 | + declare readonly sourceTargets: HTMLInputElement[]; |
| 17 | + |
| 18 | + declare readonly hasPreviewButtonTarget: boolean; |
| 19 | + declare readonly previewButtonTarget: HTMLInputElement; |
| 20 | + declare readonly previewButtonTargets: HTMLInputElement[]; |
| 21 | + |
| 22 | + connect(): void { |
| 23 | + console.log('Connect!'); |
| 24 | + } |
| 25 | + |
| 26 | + disconnect(): void { |
| 27 | + this.clearIdleTimeout(); |
| 28 | + } |
| 29 | + |
| 30 | + clearIdleTimeout(): void { |
| 31 | + if (this.idleTimeout) clearTimeout(this.idleTimeout); |
| 32 | + } |
| 33 | + |
| 34 | + preview = (): void => { |
| 35 | + console.log('Start preview timer!'); |
| 36 | + this.clearIdleTimeout(); |
| 37 | + this.idleTimeout = setTimeout(this.clickPreviewButton, 500); |
| 38 | + }; |
| 39 | + |
| 40 | + clickPreviewButton = (): void => { |
| 41 | + console.log('Click preview button!'); |
| 42 | + this.previewButtonTarget.click(); |
| 43 | + }; |
| 44 | +} |
0 commit comments