11document . getElementById ( "toggleNav" ) . addEventListener ( "click" , toggleNav ) ;
22document . getElementById ( "addStep" ) . addEventListener ( "click" , addStep ) ;
3+ document . getElementById ( "deleteStep" ) . addEventListener ( "click" , deleteStep ) ;
34document . getElementById ( "submitButton" ) . addEventListener ( "click" , submitData ) ;
45document . getElementById ( "resetButton" ) . addEventListener ( "click" , resetForm ) ;
5-
6- const vscode = acquireVsCodeApi ( ) ; // webview.ts 와 정보 주고받기
7-
8- function toggleNav ( ) {
9- const inputSection = document . getElementById ( "inputSection" ) ;
10- const outputSection = document . getElementById ( "outputSection" ) ;
11- const toggleNavButton = document . getElementById ( "toggleNav" ) ;
12-
13- toggleNavButton . textContent = "" ;
14-
15- if ( inputSection . style . maxHeight === "0px" ) {
16- inputSection . style . maxHeight = "100vh" ;
17- inputSection . style . opacity = "1" ;
18- outputSection . style . height = "0" ;
19- outputSection . style . opacity = "0" ;
20- } else {
21- inputSection . style . maxHeight = "0" ;
22- inputSection . style . opacity = "0" ;
23- outputSection . style . height = "100%" ;
24- outputSection . style . opacity = "1" ;
25- }
26- }
27-
28- function addStep ( ) {
29- const stepContainer = document . getElementById ( "steps" ) ;
30- const stepCount = stepContainer . querySelectorAll ( ".step" ) . length + 1 ;
31- const newStep = document . createElement ( "div" ) ;
32- newStep . classList . add ( "step" , "mb-2" ) ;
33- newStep . innerHTML = `<input type="text" class="form-control stepInput" placeholder="Step ${ stepCount } ">` ;
34- stepContainer . appendChild ( newStep ) ;
6+ document . getElementById ( "button1" ) . addEventListener ( "click" , ( ) => buttonClick ( 1 ) ) ;
7+ document . getElementById ( "button2" ) . addEventListener ( "click" , ( ) => buttonClick ( 2 ) ) ;
8+ document . getElementById ( "button3" ) . addEventListener ( "click" , ( ) => buttonClick ( 3 ) ) ;
9+ const loadingContent = document . getElementById ( "loadingContent" ) ;
10+ const gptOutputContent = document . getElementById ( "gptOutputContent" ) ;
11+ const additionalBtn = document . getElementById ( "additionalBtn" ) ;
12+ const firstResponseContent = document . getElementById ( "firstResponseContent" ) ;
13+ const advancedResponseContent = document . getElementById ( "advancedResponseContent" ) ;
14+ const vscode = acquireVsCodeApi ( ) ;
15+ let initialResponse = "" ;
16+ let submitCount = 0 ;
17+ let gptListenerAdded = false ;
18+
19+ function debug ( message ) {
20+ vscode . postMessage ( {
21+ command : "debug" ,
22+ data : message ,
23+ } ) ;
3524}
3625
3726function submitData ( ) {
@@ -58,54 +47,78 @@ function submitData() {
5847 let dataToSend = "problem definition: " + problemInput + ", steps: " ; // save data to send to Chat-GPT
5948
6049 let userInputStepIndex = 1 ;
50+ userOutputStepBtn . innerHTML = "" ;
6151 steps . forEach ( ( userInputStep ) => {
6252 userOutputStepBtn . innerHTML += `<li>${ userInputStep } </li>` ;
6353 dataToSend += userInputStepIndex + ". " + userInputStep + " " ;
6454 userInputStepIndex ++ ;
6555 } ) ;
6656 delete userInputStepIndex ; //reset userInputStepIndex number
6757
68- sendData ( dataToSend ) ; // send data into webview.ts
58+ submitCount ++ ;
6959
70- showGptResult ( ) ; // get gpt's response and show chat-GPT's result to html.
60+ if ( submitCount > 1 ) {
61+ clearContent ( ) ;
62+ }
63+
64+ sendData ( dataToSend ) ;
65+ showGptResult ( ) ;
7166
7267 toggleNav ( ) ;
73- }
7468
75- // send data into webview.ts
76- function sendData ( data ) {
77- vscode . postMessage ( {
78- command : "process" ,
79- value : data ,
80- } ) ;
69+ additionalBtn . style . display = "block" ;
8170}
8271
8372// get gpt's response and show chat-GPT's result to html.
8473function showGptResult ( ) {
85- window . addEventListener ( "message" , ( event ) => {
86- const message = event . data ; // get gpt's response
87-
88- if ( message . command === "setData" ) {
89- const gptOutputContent = document . getElementById ( "gptOutputContent" ) ;
90-
91- if ( gptOutputContent ) {
92- gptOutputContent . innerHTML = `<h3>GPT Response:</h3><p>${ marked . parse ( message . data ) } </p>` ; // show chat-GPT's result to html.
74+ if ( ! gptListenerAdded ) {
75+ window . addEventListener ( "message" , ( event ) => {
76+ const message = event . data ; // get gpt's response
77+ if ( message . command === "setData" ) {
78+ const gptOutputContent = document . getElementById ( "gptOutputContent" ) ;
79+
80+ if ( gptOutputContent ) {
81+ loadingContent . classList . add ( "invisible" ) ;
82+ gptResponse = message . data ;
83+
84+ // If it's the first response, store it and show it along with the user's prompt
85+ if ( initialResponse === "" ) {
86+ initialResponse = gptResponse ;
87+
88+ gptOutputContent . classList . remove ( "invisible" ) ;
89+ firstResponseContent . classList . remove ( "invisible" ) ;
90+ firstResponseContent . innerHTML += `<h3>GPT's Response</h3><p>${ marked . parse ( initialResponse ) } </p>` ;
91+ } else {
92+ // If it's not the first response, show it above the previous response
93+
94+ advancedResponseContent . innerHTML = "" ;
95+ advancedResponseContent . classList . remove ( "invisible" ) ;
96+ advancedResponseContent . innerHTML += `<h3 class="advancedResponse">Advanced questions</h3><p>${ marked . parse ( gptResponse ) } </p>` ;
97+ }
98+
99+ // Display the buttons again below the responses
100+ const additionalBtn = document . getElementById ( "additionalBtn" ) ;
93101
94- const additionalBtn = document . getElementById ( "additionalBtn" ) ;
95- if ( additionalBtn ) {
96102 additionalBtn . classList . remove ( "invisible" ) ;
103+ additionalBtn . style . display = "block" ;
97104 }
98105 } else {
99- alert ( "No response from Chat-GPT .") ;
106+ console . log ( "message command is not a setData .") ;
100107 }
101- }
102- } ) ;
108+ } ) ;
109+ gptListenerAdded = true ;
110+ } else {
111+ console . log ( "gptListenerAdded is already true" ) ;
112+ }
103113}
104114
105115function resetForm ( ) {
106116 const inputSection = document . getElementById ( "inputSection" ) ;
117+ const additionalBtn = document . getElementById ( "additionalBtn" ) ;
118+
119+ // Ensure input section is visible
107120 if ( inputSection . style . maxHeight === "0px" ) {
108- toggleNav ( ) ; // Ensure input section is visible
121+ toggleNav ( ) ;
109122 }
110123
111124 // Clear problem input
@@ -126,14 +139,81 @@ function resetForm() {
126139 document . getElementById ( "userOutputStepBtn" ) . innerHTML = "" ;
127140
128141 // Reset GPT output content
129- const gptOutputContent = document . getElementById ( "gptOutputContent" ) ;
130- if ( gptOutputContent ) {
131- gptOutputContent . innerHTML = "" ;
132- }
142+ document . getElementById ( "firstResponseContent" ) . innerHTML = "" ;
143+ document . getElementById ( "advancedResponseContent" ) . innerHTML = "" ;
133144
134145 // Hide additional buttons
135- const additionalBtn = document . getElementById ( "additionalBtn" ) ;
136146 if ( additionalBtn ) {
147+ additionalBtn . classList . add ( "invisible" ) ;
137148 additionalBtn . style . display = "none" ;
138149 }
150+
151+ // Reset stored responses
152+ initialResponse = "" ;
153+ gptResponse = "" ;
154+ }
155+
156+ function sendData ( data ) {
157+ loadingContent . classList . remove ( "invisible" ) ;
158+ additionalBtn . classList . add ( "invisible" ) ;
159+ gptOutputContent . classList . add ( "invisible" ) ;
160+
161+ vscode . postMessage ( {
162+ command : "process" ,
163+ value : data ,
164+ } ) ;
165+ }
166+
167+ function addStep ( ) {
168+ const stepContainer = document . getElementById ( "steps" ) ;
169+ const stepCount = stepContainer . querySelectorAll ( ".step" ) . length + 1 ;
170+ const newStep = document . createElement ( "div" ) ;
171+ newStep . classList . add ( "step" , "mb-2" ) ;
172+ newStep . innerHTML = `<input type="text" class="form-control stepInput" placeholder="Step ${ stepCount } ">` ;
173+ stepContainer . appendChild ( newStep ) ;
174+ }
175+
176+ function deleteStep ( ) {
177+ const stepContainer = document . getElementById ( "steps" ) ;
178+ const stepCount = stepContainer . querySelectorAll ( ".step" ) . length ;
179+
180+ if ( stepCount > 1 ) {
181+ stepContainer . removeChild ( stepContainer . lastChild ) ;
182+ }
183+ }
184+
185+ function toggleNav ( ) {
186+ const inputSection = document . getElementById ( "inputSection" ) ;
187+ const outputSection = document . getElementById ( "outputSection" ) ;
188+ const toggleNavButton = document . getElementById ( "toggleNav" ) ;
189+
190+ toggleNavButton . textContent = "" ;
191+
192+ if ( inputSection . style . maxHeight === "0px" ) {
193+ inputSection . style . maxHeight = "100vh" ;
194+ inputSection . style . opacity = "1" ;
195+ outputSection . style . height = "0" ;
196+ outputSection . style . opacity = "0" ;
197+ } else {
198+ inputSection . style . maxHeight = "0" ;
199+ inputSection . style . opacity = "0" ;
200+ outputSection . style . height = "100%" ;
201+ outputSection . style . opacity = "1" ;
202+ }
203+ }
204+
205+ function clearContent ( ) {
206+ firstResponseContent . innerHTML = "" ;
207+ advancedResponseContent . innerHTML = "" ;
208+ initialResponse = "" ;
209+ gptResponse = "" ;
210+ }
211+
212+ function buttonClick ( buttonNumber ) {
213+ loadingContent . classList . remove ( "invisible" ) ;
214+
215+ vscode . postMessage ( {
216+ command : `button${ buttonNumber } ` ,
217+ data : initialResponse ,
218+ } ) ;
139219}
0 commit comments