Skip to content

Commit 2a2ae0b

Browse files
committed
增加JS文件快捷导入
1 parent 7c30212 commit 2a2ae0b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

iframe/main/index.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<div id="sidebar">
1212
<ul>
1313
<li><button id="run-btn">运行</button></li>
14+
<li><button id="import-btn">导入</button></li>
1415
<li><button id="load-btn">加载</button></li>
1516
<li><button id="save-btn">保存</button></li>
1617
<li><button id="delete-btn">删除</button></li>
@@ -101,9 +102,13 @@
101102
eda.sys_Message.showToastMessage('注意,这玩意不会问你是否删除,点一下立马删,三思而后行', 'warn', 3);
102103
Code_OpenDeleteWindow(editor);
103104
});
105+
//导入JS文件
106+
document.getElementById('import-btn').addEventListener('click', async () => {
107+
ImportFile(editor);
108+
})
104109
//保存到插件
105110
document.getElementById('Ext-btn').addEventListener('click', async () => {
106-
showPluginManagerModal(editor,dark_theme);
111+
showPluginManagerModal(editor);
107112
})
108113
//关于编辑器 - 暂时先跳转GitHub 上架后修改跳转EDA扩展广场
109114
document.getElementById('about-btn').addEventListener('click', async () => {

iframe/script/User_config/ACE_Config.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,4 +756,42 @@ async function ExtStore_LoadAndRunAllPlugins(globalContext = {}, onLog = (msg, t
756756
onLog(`数据库初始化失败: ${err.message}`, 'error');
757757
throw err;
758758
}
759+
}
760+
761+
/**
762+
* 打开文件选择窗口,读取用户选择的 .js 文件内容,并加载到指定的 Ace Editor 中
763+
* @param {Object} editor - Ace Editor 实例
764+
*/
765+
function ImportFile(editor) {
766+
// 创建一个临时的 input 元素用于文件选择
767+
const input = document.createElement('input');
768+
input.type = 'file';
769+
input.accept = '.js'; // 仅接受 .js 文件
770+
771+
input.onchange = (event) => {
772+
const file = event.target.files[0];
773+
if (!file) return;
774+
775+
// 检查文件扩展名(虽然 accept 已限制,但再校验一次更安全)
776+
if (!file.name.endsWith('.js')) {
777+
alert('请选择一个有效的 JavaScript (.js) 文件。');
778+
return;
779+
}
780+
781+
const reader = new FileReader();
782+
reader.onload = (e) => {
783+
const content = e.target.result;
784+
editor.session.setValue(content); // 将内容设置到 Ace Editor
785+
editor.session.setMode('ace/mode/javascript'); // 设置语法高亮模式为 JavaScript
786+
};
787+
reader.onerror = () => {
788+
console.error('读取文件时出错');
789+
alert('读取文件失败,请重试。');
790+
};
791+
792+
reader.readAsText(file);
793+
};
794+
795+
// 触发文件选择窗口
796+
input.click();
759797
}

0 commit comments

Comments
 (0)