添加现有代码V1.6

This commit is contained in:
2026-06-20 15:44:09 +08:00
commit 3c92f6c285
107 changed files with 15211 additions and 0 deletions
+342
View File
@@ -0,0 +1,342 @@
// script-editor.js - 脚本编辑器模块
const ScriptEditor = (function() {
let scriptTabInited = false;
let editor = null;
// 初始化脚本编辑器功能
function init() {
// 监听标签页切换事件
$('button[data-bs-toggle="tab"]').on('shown.bs.tab', handleTabShown);
}
// 处理标签页显示事件
function handleTabShown(e) {
console.log('tab shown', e);
const target = $(e.target).attr('data-bs-target');
// 初始化脚本编辑器
if (target === '#script' && !scriptTabInited) {
initCodeMirror();
initFileTree();
// 保存文件 - 分段写入
$('#saveFile').click(function() {
const path = $('#currentFile').text();
const fullContent = editor.getValue();
const chunkSize = 4096; // 4K 分段
const totalSize = fullContent.length;
const totalChunks = Math.ceil(totalSize / chunkSize);
let currentChunk = 0;
let currentOffset = 0;
showToast('正在保存文件...');
// 分段写入函数
function writeChunk(offset) {
if (offset >= totalSize) {
// 所有分块写入完成,请求服务器进行最终校验和重命名
$.ajax({
url: '/api/script/write/finalize',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
path: path,
total_size: totalSize
}),
success: function(response) {
if (response.success) {
showToast('文件保存成功');
} else {
showToast('文件保存成功,但最终校验失败:' + response.message);
console.warn('文件校验失败,但内容可能已保存:', response.message);
}
},
error: function() {
// 即使最终校验请求失败,我们仍然告知用户文件已保存
// 因为分块已经写入,只是可能没有进行最后的重命名和校验
showToast('文件内容已保存,但最终处理请求失败');
console.warn('最终处理请求失败');
}
});
return;
}
// 获取当前分块内容
const chunkContent = fullContent.substring(offset, offset + chunkSize);
// 使用js-base64库编码
const encodedContent = Base64.encode(chunkContent);
const isFinal = offset + chunkSize >= totalSize;
// 更新进度
currentChunk++;
const progress = Math.min(100, Math.round((currentChunk / totalChunks) * 100));
showToast('正在保存文件... (' + progress + '%)');
$.ajax({
url: '/api/script/write',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
path: path,
content: encodedContent,
offset: offset,
final: isFinal
}),
success: function(response) {
if (response.success) {
// 继续写入下一个分块
setTimeout(function() {
writeChunk(response.offset);
}, 10); // 短暂延迟,避免请求过于密集
} else {
showToast('保存文件失败:' + response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('分段写入失败:', textStatus, errorThrown);
// 如果分段写入失败,尝试使用旧的一次性写入方式作为降级方案
console.log('分段写入失败,尝试一次性写入');
const fullEncodedContent = Base64.encode(fullContent);
$.ajax({
url: '/api/script/write',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
path: path,
content: fullEncodedContent
}),
success: function(backupResponse) {
if (backupResponse.success) {
showToast('文件保存成功');
} else {
showToast('保存文件失败:' + backupResponse.message);
}
},
error: function() {
showToast('保存文件失败:服务器错误');
}
});
}
});
}
// 开始写入第一个分块
writeChunk(0);
});
// 删除文件
$('#deleteFile').click(function() {
if (confirm('确定要删除这个文件吗?')) {
const path = $('#currentFile').text();
$.ajax({
url: '/api/script/delete',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify({
path: path
}),
success: function(response) {
if (response.success) {
showToast('文件删除成功');
$('#currentFile').text('未选择文件');
editor.setValue('');
$('#saveFile, #deleteFile').prop('disabled', true);
$('#fileTree').jstree('refresh');
} else {
showToast('删除文件失败:' + response.message);
}
},
error: function(jqXHR, textStatus, errorThrown) {
showToast('删除文件失败:服务器错误');
}
});
}
});
// 刷新文件树
$('#refreshTree').click(function() {
console.log('刷新文件树');
$('#fileTree').jstree('refresh');
});
scriptTabInited = true;
}
}
// 初始化CodeMirror编辑器
function initCodeMirror() {
// 初始化CodeMirror
editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
mode: 'text/x-python',
theme: 'monokai',
lineNumbers: true,
indentUnit: 4,
lineWrapping: true,
matchBrackets: true,
autoCloseBrackets: true
});
editor.setValue("请选择一个文件");
}
// 初始化文件树
function initFileTree() {
// 初始化文件树
$('#fileTree').jstree({
'core': {
'data': {
'url': function(node) {
const url = node.id === '#' ? '/api/script/list' : '/api/script/list?path=' + node.id;
return url;
},
'data': function(node) {
return { 'id': node.id };
},
'dataFilter': function(data) {
var json = {};
try {
json = JSON.parse(data);
} catch (e) {
return "[]";
}
if (json.success && Array.isArray(json.data)) {
return JSON.stringify(json.data);
}
return "[]";
},
'error': function(jqXHR, textStatus, errorThrown) {
showToast('加载文件列表失败:' + textStatus);
}
},
'themes': {
'name': 'default',
'responsive': true
},
'check_callback': true,
'multiple': false
},
'types': {
'default': {
'icon': 'bi bi-file-text'
},
'folder': {
'icon': 'bi bi-folder'
},
'python': {
'icon': 'bi bi-filetype-py'
},
'json': {
'icon': 'bi bi-filetype-json'
},
'shell': {
'icon': 'bi bi-filetype-sh'
}
},
'plugins': ['types']
}).on('ready.jstree', function() {
// 文件树初始化完成
});
// 文件树节点点击事件
$('#fileTree').on('select_node.jstree', function(e, data) {
const node = data.node;
if (node.original.type != 'folder') {
loadFile(node.id);
}
});
}
// 加载文件内容 - 分段读取
function loadFile(path) {
// 先清空编辑器
editor.setValue("");
$('#currentFile').text(path);
showToast('正在加载文件...');
// 首先获取文件信息
$.get('/api/script/info?path=' + encodeURIComponent(path), function(infoResponse) {
if (infoResponse.success) {
const fileSize = infoResponse.size;
const chunkSize = 4096; // 4K 分段
let totalChunks = Math.ceil(fileSize / chunkSize);
let loadedChunks = 0;
let fileContent = '';
// 显示加载进度
showToast('正在加载文件... (0%)');
// 分段读取函数
function readChunk(offset) {
if (offset >= fileSize) {
// 所有分块读取完成
const ext = path.split('.').pop().toLowerCase();
switch(ext) {
case 'py':
editor.setOption('mode', 'text/x-python');
break;
case 'json':
editor.setOption('mode', 'application/json');
break;
case 'sh':
editor.setOption('mode', 'text/x-sh');
break;
case 'html':
editor.setOption('mode', 'text/html');
break;
case 'js':
editor.setOption('mode', 'javascript');
break;
case 'css':
editor.setOption('mode', 'css');
break;
default:
editor.setOption('mode', 'text/plain');
}
// 设置编辑器内容
editor.setValue(fileContent);
$('#saveFile, #deleteFile').prop('disabled', false);
showToast('文件加载完成');
return;
}
// 读取当前分块
$.get('/api/script/read?path=' + encodeURIComponent(path) + '&offset=' + offset + '&size=' + chunkSize, function(chunkResponse) {
if (chunkResponse.success) {
// 使用js-base64库解码
const chunkContent = Base64.decode(chunkResponse.data);
fileContent += chunkContent;
loadedChunks++;
// 更新进度
const progress = Math.round((loadedChunks / totalChunks) * 100);
if (progress % 20 === 0 || progress === 100) {
showToast('正在加载文件... (' + progress + '%)');
}
// 读取下一个分块
readChunk(offset + chunkSize);
} else {
showToast('加载文件失败:' + chunkResponse.message);
}
}).fail(function() {
showToast('加载文件失败:网络错误');
});
}
// 开始读取第一个分块
readChunk(0);
} else {
showToast('获取文件信息失败:' + infoResponse.message);
}
}).fail(function() {
showToast('获取文件信息失败:网络错误');
});
}
// 暴露公共方法
return {
init: init
};
})();