105 lines
3.0 KiB
JavaScript
105 lines
3.0 KiB
JavaScript
let vs_socket = null;
|
||
let debounceTimeout;
|
||
// 定义代码类型到文件后缀的映射
|
||
const typeToExtension = {
|
||
'python': 'py',
|
||
'cpp': 'cpp',
|
||
'javascript': 'js',
|
||
'java': 'java',
|
||
'html': 'html',
|
||
'css': 'css',
|
||
'php': 'php',
|
||
'ruby': 'rb',
|
||
'go': 'go',
|
||
'c': 'c',
|
||
'cs': 'cs'
|
||
// 可以根据需要添加更多映射
|
||
};
|
||
|
||
// 缓冲器函数(防抖)
|
||
function debounceSendToServer(data, delay) {
|
||
if (debounceTimeout) {
|
||
clearTimeout(debounceTimeout);
|
||
}
|
||
debounceTimeout = setTimeout(() => {
|
||
sendToServer(data);
|
||
}, delay);
|
||
}
|
||
|
||
|
||
|
||
function sendToServer(data){
|
||
try {
|
||
if (!vs_socket) {
|
||
return;
|
||
}
|
||
console.log('Sending data to Flask server:', data);
|
||
vs_socket.emit('message', data)
|
||
} catch (error) {
|
||
console.error('Error sending data to Flask server:', error);
|
||
}
|
||
}
|
||
function ChangeActiveTextEditor(path){ //打开文件
|
||
console.log(`Current open file: ${path}`);
|
||
sendToServer({
|
||
type: 'activeFile',
|
||
filePath: path,
|
||
config: code_like_config,
|
||
});
|
||
}
|
||
function ChangeWorkspacefileTree(fileTree) {
|
||
if (fileTree){
|
||
sendToServer({
|
||
type: 'workspaceFolders',
|
||
fileTree: fileTree,
|
||
config: code_like_config,
|
||
});
|
||
}
|
||
}
|
||
|
||
|
||
// vscode.window.showInformationMessage('Connecting to server...');
|
||
|
||
vs_socket = io('wss://hsamooc.cn/vscode');
|
||
vs_socket.on('connect', () => {
|
||
// vscode.window.showInformationMessage('Connected to server');
|
||
console.log('Connected to server');
|
||
vs_socket.emit('login', { config: code_like_config });
|
||
vs_socket.emit('load_chapter',{config: code_like_config})
|
||
});
|
||
vs_socket.on('disconnect', () => {
|
||
// vscode.window.showInformationMessage('Disconnected from server');
|
||
console.log('Disconnected from server');
|
||
});
|
||
vs_socket.on('terminal',(data)=>{
|
||
// vscode.window.showInformationMessage('Terminal command received'+data);
|
||
socket.emit("pty_input", {"input": data.data+'\r\n'}, function(response) {
|
||
});
|
||
})
|
||
vs_socket.on('code-file',(data)=>{
|
||
if (data.type=='create'){
|
||
let title = data.chapter_title
|
||
let content = data.data
|
||
const codeBlockRegex = /^```(\w+)\s([\s\S]*?)\s```$/;
|
||
const match = content.match(codeBlockRegex);
|
||
|
||
if (match && match.length >= 3) {
|
||
const codeType = match[1].toLowerCase();
|
||
const codeContent = match[2].trim();
|
||
// 根据代码类型获取后缀,如果没有匹配的则使用txt
|
||
const fileExtension = typeToExtension[codeType] || 'txt';
|
||
|
||
console.log('代码类型:', codeType);
|
||
console.log('文件后缀:', fileExtension);
|
||
console.log('代码内容:', codeContent);
|
||
|
||
createAndWriteNewFile(title+'.'+fileExtension, codeContent)
|
||
|
||
// 这里可以使用解析出的结果进行后续操作
|
||
} else {
|
||
console.error('无效的代码格式');
|
||
// 处理格式错误的情况
|
||
}
|
||
}
|
||
})
|