upd
This commit is contained in:
@@ -68,7 +68,7 @@
|
||||
let tempFilePath = '';
|
||||
|
||||
// 初始化Monaco Editor
|
||||
require(['vs/editor/editor.main'], function () {
|
||||
require(['vs/editor/editor.main'], function() {
|
||||
// 从URL参数获取内容和语言
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const contentParam = urlParams.get('content');
|
||||
@@ -82,6 +82,7 @@
|
||||
initialContent = Base.decode(contentParam);
|
||||
} catch (e) {
|
||||
console.error('解码内容失败:', e);
|
||||
initialContent = contentParam; // 如果解码失败,使用原始内容
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,28 +90,33 @@
|
||||
editor = monaco.editor.create(document.getElementById('container'), {
|
||||
value: initialContent,
|
||||
language: languageParam,
|
||||
theme: 'vs-dark',
|
||||
automaticLayout: true,
|
||||
minimap: { enabled: true },
|
||||
scrollBeyondLastLine: false,
|
||||
fontSize: 14,
|
||||
wordWrap: 'on',
|
||||
fontFamily: 'Consolas, "Courier New", monospace',
|
||||
scrollBeyondLastLine: false,
|
||||
lineNumbers: 'on',
|
||||
readOnly: false,
|
||||
fontFamily: 'Consolas, "Microsoft YaHei", monospace'
|
||||
roundedSelection: true,
|
||||
scrollbar: {
|
||||
useShadows: false,
|
||||
verticalScrollbarSize: 10,
|
||||
horizontalScrollbarSize: 10
|
||||
}
|
||||
});
|
||||
|
||||
// 添加内容变化监听器
|
||||
editor.onDidChangeModelContent(function() {
|
||||
// 内容变化处理
|
||||
});
|
||||
|
||||
// 窗口大小改变时重新布局
|
||||
window.onresize = function () {
|
||||
// 监听窗口大小变化
|
||||
window.addEventListener('resize', function() {
|
||||
if (editor) {
|
||||
editor.layout();
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// 延迟执行,确保编辑器已初始化
|
||||
setTimeout(function() {
|
||||
if (editor) {
|
||||
editor.focus();
|
||||
}
|
||||
}, 100);
|
||||
});
|
||||
|
||||
// 暴露获取内容的方法给QWebEngineView调用
|
||||
@@ -151,10 +157,7 @@
|
||||
language: editor && editor.getModel() ? editor.getModel().getLanguageId() : 'text'
|
||||
};
|
||||
|
||||
// 方法1: 保存到localStorage
|
||||
localStorage.setItem('leonpan_editor_content', JSON.stringify(saveData));
|
||||
|
||||
// 方法2: 直接向本地服务器发送POST请求(主要方式)
|
||||
// 直接向本地服务器发送POST请求(主要方式)
|
||||
try {
|
||||
// 获取当前端口号
|
||||
const currentPort = window.location.port;
|
||||
@@ -167,53 +170,38 @@
|
||||
xhr.onload = function() {
|
||||
if (xhr.status === 200) {
|
||||
console.log('服务器接收保存成功');
|
||||
alert('内容已成功保存并返回应用程序');
|
||||
|
||||
// 尝试使用自定义协议返回应用程序
|
||||
try {
|
||||
window.location.href = 'leonpan:save-success';
|
||||
} catch (e) {
|
||||
console.error('无法通过协议返回应用程序:', e);
|
||||
}
|
||||
|
||||
// 尝试关闭窗口(某些浏览器可能阻止此操作)
|
||||
setTimeout(function() {
|
||||
window.close();
|
||||
}, 500);
|
||||
} else {
|
||||
console.error('服务器返回错误状态:', xhr.status);
|
||||
alert('保存失败,请重试');
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = function() {
|
||||
console.error('向服务器发送保存请求失败');
|
||||
alert('保存请求发送失败,请检查网络连接');
|
||||
};
|
||||
|
||||
xhr.send(JSON.stringify(saveData));
|
||||
} catch (e) {
|
||||
console.error('发送保存请求时出错:', e);
|
||||
alert('保存过程中发生错误: ' + e.message);
|
||||
}
|
||||
|
||||
// 方法3: 创建一个下载链接作为备用方式
|
||||
const blob = new Blob([JSON.stringify(saveData)], {type: 'application/json'});
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
|
||||
// 设置文件名和下载路径
|
||||
a.download = 'editor_content.json';
|
||||
|
||||
// 如果提供了临时文件路径,尝试使用该路径
|
||||
if (tempFilePath) {
|
||||
// 对于Windows路径,需要进行特殊处理
|
||||
if (navigator.platform.indexOf('Win') !== -1) {
|
||||
// 在Windows中,我们不能直接设置文件系统路径,但可以提示用户
|
||||
console.log('建议保存路径:', tempFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
a.href = url;
|
||||
|
||||
// 显示成功提示
|
||||
alert('内容已成功保存!应用程序将自动检测到您的更改。');
|
||||
|
||||
// 自动触发下载(作为备用机制)
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
document.body.removeChild(a);
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
// 尝试通过协议处理程序返回应用程序
|
||||
try {
|
||||
// 使用自定义协议打开应用程序
|
||||
window.location.href = 'leonpan:save-success';
|
||||
} catch (e) {
|
||||
console.error('无法返回应用程序:', e);
|
||||
}
|
||||
// 备用: 保存到localStorage,以便应用程序在POST失败时可以尝试读取
|
||||
localStorage.setItem('leonpan_editor_content', JSON.stringify(saveData));
|
||||
}
|
||||
|
||||
// 获取系统临时目录路径(用于显示给用户)
|
||||
@@ -228,56 +216,28 @@
|
||||
}
|
||||
}
|
||||
|
||||
// 从URL参数获取初始化数据
|
||||
// 初始化函数(根据URL参数)
|
||||
function initFromUrl() {
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const contentParam = urlParams.get('content');
|
||||
const languageParam = urlParams.get('language');
|
||||
|
||||
// 获取并设置内容
|
||||
const encodedContent = urlParams.get('content');
|
||||
if (encodedContent) {
|
||||
if (contentParam) {
|
||||
try {
|
||||
const decodedContent = Base.decode(encodedContent);
|
||||
const decodedContent = Base.decode(contentParam);
|
||||
setEditorContent(decodedContent);
|
||||
} catch (e) {
|
||||
console.error('解码内容失败:', e);
|
||||
console.error('从URL初始化内容失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取并设置语言
|
||||
const language = urlParams.get('language') || 'text';
|
||||
setEditorLanguage(language);
|
||||
|
||||
// 保存文件ID到本地存储
|
||||
const fileId = urlParams.get('fileId');
|
||||
if (fileId) {
|
||||
localStorage.setItem('currentFileId', fileId);
|
||||
if (languageParam) {
|
||||
setEditorLanguage(languageParam);
|
||||
}
|
||||
|
||||
// 获取临时文件路径
|
||||
tempFilePath = urlParams.get('temp_file') || '';
|
||||
}
|
||||
|
||||
// 初始化时从URL参数加载数据
|
||||
// 页面加载完成后执行初始化
|
||||
window.addEventListener('load', function() {
|
||||
// 从localStorage中加载可能保存的内容
|
||||
const savedContent = localStorage.getItem('leonpan_editor_content');
|
||||
if (savedContent) {
|
||||
try {
|
||||
const parsed = JSON.parse(savedContent);
|
||||
if (parsed.saved && parsed.content) {
|
||||
// 延迟执行,确保编辑器已初始化
|
||||
setTimeout(() => {
|
||||
if (editor) {
|
||||
const decodedContent = Base.decode(parsed.content);
|
||||
editor.setValue(decodedContent);
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('解析保存的内容失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟执行,确保编辑器已初始化
|
||||
setTimeout(initFromUrl, 1000);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user