Skip to content

Commit 900be21

Browse files
committed
Convert snippet preview controller to TypeScript
1 parent 0d29802 commit 900be21

File tree

2 files changed

+44
-36
lines changed

2 files changed

+44
-36
lines changed

app/javascript/controllers/snippets/preview.js

Lines changed: 0 additions & 36 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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

Comments
 (0)