1- import * as vscode from 'vscode' ;
1+ import * as vscode from 'vscode' ;
22
33interface InputMultiLineBoxOptions {
44 prompt : string ;
55 placeHolder ?: string ;
66 title ?: string ;
77}
88
9- let currentPanel : vscode . WebviewPanel | undefined = undefined ; // 当前打开的 Webview 面板
9+ let currentPanel : vscode . WebviewPanel | undefined = undefined ; // 当前打开的 Webview 面板
1010
1111export async function showInputMultiLineBox ( options : InputMultiLineBoxOptions ) : Promise < string | undefined > {
1212 return new Promise ( ( resolve ) => {
13- // 如果已有面板,先关闭
13+ // 如果已有面板,先关闭
1414 if ( currentPanel ) {
1515 currentPanel . dispose ( ) ;
1616 }
1717
18- // 创建新的 Webview 面板
18+ // 创建新的 Webview 面板
1919 currentPanel = vscode . window . createWebviewPanel (
2020 'multiLineInput' ,
2121 options . title || 'Multi-line Input' ,
2222 vscode . ViewColumn . One ,
23- { enableScripts : true , localResourceRoots : [ ] } // 允许 JavaScript
23+ { enableScripts : true , localResourceRoots : [ ] } // 允许 JavaScript
2424 ) ;
2525
26- // 设置 Webview 内容
26+ // 设置 Webview 内容
2727 currentPanel . webview . html = getWebviewContent ( options . prompt , options . placeHolder || "" ) ;
2828
29- // 监听 Webview 消息
29+ // 监听 Webview 消息
3030 currentPanel . webview . onDidReceiveMessage (
3131 ( message ) => {
3232 if ( message . command === 'submit' ) {
33- resolve ( message . text ) ; // 返回用户输入的文本
33+ resolve ( message . text ) ; // 返回用户输入的文本
3434 if ( currentPanel ) {
35- currentPanel . dispose ( ) ; // 关闭 Webview
36- currentPanel = undefined ; // 清空当前面板引用
35+ currentPanel . dispose ( ) ; // 关闭 Webview
36+ currentPanel = undefined ; // 清空当前面板引用
3737 }
3838 }
3939 } ,
4040 undefined
4141 ) ;
4242
43- // 监听 Webview 关闭,避免无限等待
43+ // 监听 Webview 关闭,避免无限等待
4444 currentPanel . onDidDispose ( ( ) => {
45- currentPanel = undefined ; // 清空面板引用
45+ currentPanel = undefined ; // 清空面板引用
4646 resolve ( undefined ) ;
4747 } ) ;
4848 } ) ;
4949}
5050
51- // Webview HTML 内容,使用 Monaco Editor
51+ // Webview HTML 内容,使用 Monaco Editor
5252function getWebviewContent ( prompt : string , placeHolder : string ) : string {
5353 return `
5454 <!DOCTYPE html>
@@ -66,7 +66,7 @@ function getWebviewContent(prompt: string, placeHolder: string): string {
6666 }
6767 textarea {
6868 width: 100%;
69- height: 50vh; /* 使输入框高度为视口的一半 */
69+ height: 50vh; /* 使输入框高度为视口的一半 */
7070 background-color: #252526;
7171 color: white;
7272 border: 1px solid #444;
@@ -95,12 +95,24 @@ function getWebviewContent(prompt: string, placeHolder: string): string {
9595 <button id="submitButton">Submit</button>
9696 <script>
9797 const vscode = acquireVsCodeApi();
98- document.getElementById('submitButton').addEventListener('click', () => {
99- const inputText = document.getElementById('inputField').value;
98+ const inputField = document.getElementById('inputField');
99+ const submitButton = document.getElementById('submitButton');
100+
101+ function submitInput() {
102+ const inputText = inputField.value;
100103 vscode.postMessage({
101104 command: 'submit',
102105 text: inputText
103106 });
107+ }
108+
109+ submitButton.addEventListener('click', submitInput);
110+
111+ inputField.addEventListener('keydown', (event) => {
112+ if (event.ctrlKey && event.key === 'Enter') {
113+ event.preventDefault();
114+ submitInput();
115+ }
104116 });
105117 </script>
106118 </body>
0 commit comments