331 lines
12 KiB
JavaScript
331 lines
12 KiB
JavaScript
// 脚本编辑器模块
|
|
|
|
let editor = null;
|
|
let currentFile = null;
|
|
|
|
// 初始化CodeMirror编辑器
|
|
function initEditor() {
|
|
if (!editor) {
|
|
editor = CodeMirror.fromTextArea(document.getElementById('editor'), {
|
|
mode: 'text/x-python',
|
|
theme: 'monokai',
|
|
lineNumbers: true,
|
|
indentUnit: 4,
|
|
lineWrapping: true,
|
|
matchBrackets: true,
|
|
autoCloseBrackets: true
|
|
});
|
|
|
|
editor.setValue("请选择一个文件");
|
|
}
|
|
return editor;
|
|
}
|
|
|
|
// 根据文件扩展名设置编辑器模式
|
|
function setEditorMode(ext) {
|
|
if (!editor) return;
|
|
|
|
const modeMap = {
|
|
'py': 'text/x-python',
|
|
'json': 'application/json',
|
|
'sh': 'text/x-sh',
|
|
'html': 'text/html',
|
|
'js': 'javascript',
|
|
'css': 'css'
|
|
};
|
|
|
|
editor.setOption('mode', modeMap[ext] || 'text/plain');
|
|
}
|
|
|
|
// 加载文件内容 - 分段读取
|
|
function loadFile(path) {
|
|
if (!editor) initEditor();
|
|
|
|
// 先清空编辑器
|
|
editor.setValue("");
|
|
$('#currentFile').text(path);
|
|
currentFile = 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();
|
|
setEditorMode(ext);
|
|
editor.setValue(fileContent);
|
|
$('#saveFile, #deleteFile').prop('disabled', false);
|
|
showToast('文件加载成功');
|
|
return;
|
|
}
|
|
|
|
$.get('/api/script/read?path=' + encodeURIComponent(path) + '&offset=' + offset + '&size=' + chunkSize, function(response) {
|
|
if (response.success) {
|
|
// 使用js-base64库解码
|
|
const chunkContent = Base64.decode(response.data);
|
|
fileContent += chunkContent;
|
|
loadedChunks++;
|
|
|
|
// 更新进度
|
|
const progress = Math.min(100, Math.round((loadedChunks / totalChunks) * 100));
|
|
showToast('正在加载文件... (' + progress + '%)');
|
|
|
|
// 继续读取下一个分块
|
|
setTimeout(function() {
|
|
readChunk(response.offset + response.size);
|
|
}, 10); // 短暂延迟,避免请求过于密集
|
|
} else {
|
|
showToast('加载文件失败:' + response.message);
|
|
}
|
|
}).fail(function(jqXHR, textStatus, errorThrown) {
|
|
showToast('加载文件失败:服务器错误');
|
|
});
|
|
}
|
|
|
|
// 开始读取第一个分块
|
|
readChunk(0);
|
|
} else {
|
|
// 如果获取文件信息失败,尝试使用旧的一次性读取方式作为降级方案
|
|
$.get('/api/script/read?path=' + encodeURIComponent(path), function(response) {
|
|
if (response.success) {
|
|
const content = Base64.decode(response.data);
|
|
const ext = path.split('.').pop().toLowerCase();
|
|
setEditorMode(ext);
|
|
editor.setValue(content);
|
|
$('#saveFile, #deleteFile').prop('disabled', false);
|
|
showToast('文件加载成功');
|
|
} else {
|
|
showToast('加载文件失败:' + response.message);
|
|
}
|
|
}).fail(function(jqXHR, textStatus, errorThrown) {
|
|
showToast('加载文件失败:服务器错误');
|
|
});
|
|
}
|
|
}).fail(function() {
|
|
// 如果获取文件信息接口失败,尝试使用旧的一次性读取方式
|
|
$.get('/api/script/read?path=' + encodeURIComponent(path), function(response) {
|
|
if (response.success) {
|
|
const content = Base64.decode(response.data);
|
|
const ext = path.split('.').pop().toLowerCase();
|
|
setEditorMode(ext);
|
|
editor.setValue(content);
|
|
$('#saveFile, #deleteFile').prop('disabled', false);
|
|
showToast('文件加载成功');
|
|
} else {
|
|
showToast('加载文件失败:' + response.message);
|
|
}
|
|
}).fail(function() {
|
|
showToast('加载文件失败:服务器错误');
|
|
});
|
|
});
|
|
}
|
|
|
|
// 保存文件 - 分段写入
|
|
function saveFile() {
|
|
if (!editor || !currentFile) {
|
|
showToast('请先选择一个文件');
|
|
return;
|
|
}
|
|
|
|
const path = currentFile;
|
|
const fullContent = editor.getValue();
|
|
const chunkSize = 4096; // 4K 分段(字节数)
|
|
let currentChunk = 0;
|
|
let currentByteOffset = 0;
|
|
|
|
showToast('正在保存文件...');
|
|
|
|
// 使用TextEncoder API进行更高效的UTF-8字节处理
|
|
const encoder = new TextEncoder();
|
|
const decoder = new TextDecoder('utf-8');
|
|
|
|
// 基于字节数的字符串截取函数 - 优化版本
|
|
function substrBytes(str, startByte, lengthBytes) {
|
|
// 将整个字符串编码为Uint8Array以获取字节数据
|
|
const bytes = encoder.encode(str);
|
|
|
|
// 确保截取范围在有效范围内
|
|
const endByte = Math.min(startByte + lengthBytes, bytes.length);
|
|
|
|
// 截取指定范围的字节
|
|
const chunkBytes = bytes.subarray(startByte, endByte);
|
|
|
|
// 将截取的字节解码回字符串
|
|
const result = decoder.decode(chunkBytes);
|
|
|
|
return result;
|
|
}
|
|
|
|
// 计算字符串的UTF-8字节长度 - 优化版本
|
|
function getByteLength(str) {
|
|
return encoder.encode(str).length;
|
|
}
|
|
|
|
// 分段写入函数
|
|
function writeChunk(byteOffset) {
|
|
const totalBytes = getByteLength(fullContent);
|
|
if (byteOffset >= totalBytes) {
|
|
// 所有分块写入完成,请求服务器进行最终校验和重命名
|
|
$.ajax({
|
|
url: '/api/script/write/finalize',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
path: path,
|
|
total_size: totalBytes
|
|
}),
|
|
success: function(response) {
|
|
if (response.success) {
|
|
showToast('文件保存成功');
|
|
} else {
|
|
showToast('文件保存成功,但最终校验失败:' + response.message);
|
|
console.warn('文件校验失败,但内容可能已保存:', response.message);
|
|
}
|
|
},
|
|
error: function() {
|
|
// 即使最终校验请求失败,我们仍然告知用户文件已保存
|
|
showToast('文件内容已保存,但最终处理请求失败');
|
|
console.warn('最终处理请求失败');
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
|
|
// 获取当前分块内容(按字节数截取)
|
|
const chunkContent = substrBytes(fullContent, byteOffset, chunkSize);
|
|
|
|
// 使用js-base64库编码
|
|
const encodedContent = Base64.encode(chunkContent);
|
|
|
|
const nextByteOffset = byteOffset + getByteLength(chunkContent);
|
|
const isFinal = nextByteOffset >= totalBytes;
|
|
|
|
// 更新进度
|
|
currentChunk++;
|
|
const totalByteChunks = Math.ceil(totalBytes / chunkSize);
|
|
const progress = Math.min(100, Math.round((currentChunk / totalByteChunks) * 100));
|
|
showToast('正在保存文件... (' + progress + '%)');
|
|
|
|
$.ajax({
|
|
url: '/api/script/write',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
path: path,
|
|
content: encodedContent,
|
|
offset: byteOffset,
|
|
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);
|
|
showToast('保存文件失败:网络错误');
|
|
// 如果分段写入失败,尝试使用旧的一次性写入方式作为降级方案
|
|
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(currentByteOffset);
|
|
}
|
|
|
|
// 删除当前文件
|
|
function deleteCurrentFile() {
|
|
if (!currentFile) {
|
|
showToast('请先选择一个文件');
|
|
return;
|
|
}
|
|
|
|
if (confirm('确定要删除这个文件吗?')) {
|
|
const path = currentFile;
|
|
$.ajax({
|
|
url: '/api/script/delete',
|
|
type: 'POST',
|
|
contentType: 'application/json',
|
|
data: JSON.stringify({
|
|
path: path
|
|
}),
|
|
success: function(response) {
|
|
if (response.success) {
|
|
showToast('文件删除成功');
|
|
$('#currentFile').text('未选择文件');
|
|
if (editor) editor.setValue('');
|
|
$('#saveFile, #deleteFile').prop('disabled', true);
|
|
currentFile = null;
|
|
// 通知文件树刷新
|
|
if (window.refreshFileTree) {
|
|
window.refreshFileTree();
|
|
}
|
|
} else {
|
|
showToast('删除文件失败:' + response.message);
|
|
}
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
showToast('删除文件失败:服务器错误');
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
// 获取当前编辑器实例
|
|
function getEditor() {
|
|
return editor;
|
|
}
|
|
|
|
// 获取当前打开的文件路径
|
|
function getCurrentFile() {
|
|
return currentFile;
|
|
}
|
|
|
|
// 导出模块
|
|
export {
|
|
initEditor,
|
|
setEditorMode,
|
|
loadFile,
|
|
saveFile,
|
|
deleteCurrentFile,
|
|
getEditor,
|
|
getCurrentFile
|
|
}; |