@@ -7,95 +7,72 @@ import PromptsView from './PromptsView';
7
7
8
8
export abstract class BaseDom {
9
9
protected abstract name : string ;
10
- protected abstract textAreaSelector : string ;
11
10
12
- templatesView ! : HTMLDivElement ;
13
- isTemplatesViewOpen = false ;
11
+ promptsView ! : HTMLDivElement ;
12
+ isPromptsViewOpen = false ;
14
13
15
14
constructor ( ) {
16
15
this . init = this . init . bind ( this ) ;
17
- this . handleTemplateSelected = this . handleTemplateSelected . bind ( this ) ;
18
- this . hideTemplates = this . hideTemplates . bind ( this ) ;
16
+ this . handlePromptSelected = this . handlePromptSelected . bind ( this ) ;
17
+ this . hidePrompts = this . hidePrompts . bind ( this ) ;
19
18
}
20
19
21
- protected abstract setText ( text : string ) : void ;
20
+ protected abstract addCustomTrigger ( ) : void ;
21
+ protected abstract usePrompt ( prompt : IPrompt ) : void ;
22
22
23
23
init ( ) {
24
24
console . log ( `Initializing ${ this . name } DOM` ) ;
25
- this . templatesView = this . createTemplatesElement ( ) ;
26
- this . addEventListeners ( ) ;
25
+ this . promptsView = this . createPromptsElement ( ) ;
26
+ this . addTriggers ( ) ;
27
27
}
28
28
29
- protected getTextArea ( ) : HTMLTextAreaElement {
30
- const textArea = document . querySelector < HTMLTextAreaElement > (
31
- this . textAreaSelector
32
- ) ;
33
-
34
- if ( ! textArea ) throw new Error ( 'Could not find text area' ) ;
35
-
36
- return textArea ;
37
- }
38
-
39
- private handleTemplateSelected ( template : IPrompt ) : void {
40
- this . isTemplatesViewOpen = false ;
41
- this . setText ( template . content ) ;
42
- this . hideTemplates ( ) ;
29
+ private handlePromptSelected ( prompt : IPrompt ) : void {
30
+ this . isPromptsViewOpen = false ;
31
+ this . usePrompt ( prompt ) ;
32
+ this . hidePrompts ( ) ;
43
33
}
44
34
45
- private createTemplatesElement ( ) : HTMLDivElement {
46
- const templatesView = document . createElement ( 'div' ) ;
47
- document . body . appendChild ( templatesView ) ;
48
- return templatesView ;
35
+ private createPromptsElement ( ) : HTMLDivElement {
36
+ const promptsView = document . createElement ( 'div' ) ;
37
+ document . body . appendChild ( promptsView ) ;
38
+ return promptsView ;
49
39
}
50
40
51
41
private async render ( ) {
52
42
ReactDOM . render (
53
43
< PromptsView
54
44
groupedPrompts = { groupedPrompts }
55
- visible = { this . isTemplatesViewOpen }
56
- onItemSelected = { this . handleTemplateSelected }
57
- onCancel = { this . hideTemplates }
45
+ visible = { this . isPromptsViewOpen }
46
+ onItemSelected = { this . handlePromptSelected }
47
+ onCancel = { this . hidePrompts }
58
48
/> ,
59
- this . templatesView
49
+ this . promptsView
60
50
) ;
61
51
}
62
52
63
- private async showTemplates ( ) {
64
- this . isTemplatesViewOpen = true ;
53
+ protected async showPrompts ( ) {
54
+ this . isPromptsViewOpen = true ;
65
55
await this . render ( ) ;
66
- this . templatesView . focus ( ) ;
56
+ this . promptsView . focus ( ) ;
67
57
}
68
58
69
- private async hideTemplates ( ) {
70
- this . isTemplatesViewOpen = false ;
59
+ protected async hidePrompts ( ) {
60
+ this . isPromptsViewOpen = false ;
71
61
await this . render ( ) ;
72
62
}
73
63
74
- private addHotKeysEventListener ( ) {
64
+ private addHotKeysTrigger ( ) {
75
65
document . addEventListener ( 'keydown' , ( event ) => {
76
66
if ( event . key === 'Escape' || event . key === 'q' ) {
77
- this . hideTemplates ( ) ;
67
+ this . hidePrompts ( ) ;
78
68
} else if ( event . ctrlKey && event . key === 't' ) {
79
- this . showTemplates ( ) ;
80
- }
81
- } ) ;
82
- }
83
-
84
- private addTextAreaEventListener ( ) {
85
- this . getTextArea ( ) . addEventListener ( 'input' , ( event ) => {
86
- const input = event . target as HTMLTextAreaElement ;
87
- const text = input . value ;
88
-
89
- if ( text === '/templates' ) {
90
- this . showTemplates ( ) ;
91
- } else if ( this . isTemplatesViewOpen ) {
92
- this . hideTemplates ( ) ;
69
+ this . showPrompts ( ) ;
93
70
}
94
71
} ) ;
95
72
}
96
73
97
- private addEventListeners ( ) {
98
- this . addHotKeysEventListener ( ) ;
99
- this . addTextAreaEventListener ( ) ;
74
+ private addTriggers ( ) {
75
+ this . addHotKeysTrigger ( ) ;
76
+ this . addCustomTrigger ( ) ;
100
77
}
101
78
}
0 commit comments