Skip to content

Commit 55351b3

Browse files
committed
增加内置模块
1 parent a4419ae commit 55351b3

9 files changed

Lines changed: 17082 additions & 13617 deletions

File tree

build-ext-manifest.cjs

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/**
2+
* Build script to generate manifest of built-in projects from ext folder
3+
* Generates iframe/script/ext_manifest.js with all project files
4+
*/
5+
6+
const fs = require('fs');
7+
const path = require('path');
8+
9+
const EXT_DIR = path.join(__dirname, 'iframe', 'ext');
10+
const OUTPUT_FILE = path.join(__dirname, 'iframe', 'script', 'ext_manifest.js');
11+
12+
/**
13+
* Recursively read all files in a directory
14+
*/
15+
function readDirRecursive(dir, baseDir = dir) {
16+
const files = [];
17+
const entries = fs.readdirSync(dir, { withFileTypes: true });
18+
19+
for (const entry of entries) {
20+
const fullPath = path.join(dir, entry.name);
21+
if (entry.isDirectory()) {
22+
files.push(...readDirRecursive(fullPath, baseDir));
23+
} else if (entry.isFile()) {
24+
const relativePath = path.relative(baseDir, fullPath).replace(/\\/g, '/');
25+
files.push(relativePath);
26+
}
27+
}
28+
29+
return files;
30+
}
31+
32+
/**
33+
* Group files by project (top-level folder)
34+
*/
35+
function groupFilesByProject(files) {
36+
const projects = {};
37+
38+
for (const file of files) {
39+
const parts = file.split('/');
40+
if (parts.length < 2) continue;
41+
42+
const projectName = parts[0];
43+
const fileName = parts.slice(1).join('/');
44+
45+
if (!projects[projectName]) {
46+
projects[projectName] = [];
47+
}
48+
49+
projects[projectName].push(fileName);
50+
}
51+
52+
return projects;
53+
}
54+
55+
/**
56+
* Read file content with error handling
57+
*/
58+
function readFileContent(filePath) {
59+
try {
60+
return fs.readFileSync(filePath, 'utf-8');
61+
} catch (error) {
62+
console.warn(`Warning: Could not read ${filePath}: ${error.message}`);
63+
return '';
64+
}
65+
}
66+
67+
/**
68+
* Generate manifest
69+
*/
70+
function generateManifest() {
71+
console.log('Generating ext manifest...');
72+
73+
if (!fs.existsSync(EXT_DIR)) {
74+
console.log('No ext directory found, creating empty manifest');
75+
const emptyManifest = 'window.extManifest = [];\n';
76+
fs.writeFileSync(OUTPUT_FILE, emptyManifest, 'utf-8');
77+
return;
78+
}
79+
80+
// Read all files
81+
const allFiles = readDirRecursive(EXT_DIR);
82+
console.log(`Found ${allFiles.length} files in ext folder`);
83+
84+
// Group by project
85+
const projectGroups = groupFilesByProject(allFiles);
86+
const projectNames = Object.keys(projectGroups);
87+
console.log(`Found ${projectNames.length} projects: ${projectNames.join(', ')}`);
88+
89+
// Build manifest
90+
const manifest = [];
91+
92+
for (const projectName of projectNames) {
93+
const fileNames = projectGroups[projectName];
94+
const files = [];
95+
96+
for (const fileName of fileNames) {
97+
const fullPath = path.join(EXT_DIR, projectName, fileName);
98+
const content = readFileContent(fullPath);
99+
100+
files.push({
101+
fileName,
102+
content,
103+
createdAt: Date.now(),
104+
});
105+
}
106+
107+
manifest.push({
108+
projectName,
109+
files,
110+
isBuiltIn: true,
111+
createdAt: Date.now(),
112+
updatedAt: Date.now(),
113+
});
114+
115+
console.log(` - ${projectName}: ${files.length} files`);
116+
}
117+
118+
// Write manifest
119+
const output = `/**
120+
* Built-in projects manifest
121+
* Auto-generated by build-ext-manifest.cjs
122+
* DO NOT EDIT MANUALLY
123+
*/
124+
window.extManifest = ${JSON.stringify(manifest, null, 2)};
125+
`;
126+
127+
fs.writeFileSync(OUTPUT_FILE, output, 'utf-8');
128+
console.log(`✓ Manifest written to ${OUTPUT_FILE}`);
129+
console.log(`✓ Total size: ${(output.length / 1024).toFixed(2)} KB`);
130+
}
131+
132+
// Run
133+
try {
134+
generateManifest();
135+
} catch (error) {
136+
console.error('Error generating manifest:', error);
137+
process.exit(1);
138+
}

iframe/main/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<script src="/iframe/script/eext_tool/highlight.min.js"></script>
124124
<script src="/iframe/script/eext_tool/marked.min.js"></script>
125125
<!-- 应用逻辑 -->
126+
<script src="/iframe/script/ext_manifest.js"></script>
126127
<script src="/iframe/script/eda_coder/EDA_Codes.js"></script>
127128
<script src="/iframe/script/User_config/Project_Manager.js"></script>
128129
<script src="/iframe/script/User_config/FileTree_UI.js"></script>
@@ -212,7 +213,7 @@
212213
// 自动保存文件内容
213214
let saveTimeout;
214215
editor.session.on('change', function() {
215-
if (window.projectManager.currentFile) {
216+
if (window.projectManager.currentFile && !(window.projectManager.currentProject && window.projectManager.currentProject.isBuiltIn)) {
216217
clearTimeout(saveTimeout);
217218
saveTimeout = setTimeout(async () => {
218219
await window.projectManager.saveFileContent(

iframe/script/User_config/FileTree_UI.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,18 @@ class FileTreeUI {
302302

303303
// 加载文件到编辑器
304304
async loadFile(fileName) {
305-
// 保存当前文件
306-
if (this.projectManager.currentFile) {
305+
const isBuiltIn = this.projectManager.currentProject && this.projectManager.currentProject.isBuiltIn;
306+
307+
// 保存当前文件(仅非内置项目)
308+
if (this.projectManager.currentFile && !isBuiltIn) {
307309
await this.projectManager.saveFileContent(this.projectManager.currentFile, this.editor.getValue());
308310
}
309311

310312
// 加载新文件
311313
const content = this.projectManager.getFileContent(fileName);
312314
this.editor.setValue(content, -1);
313315
this.projectManager.currentFile = fileName;
316+
this.editor.setReadOnly(!!isBuiltIn);
314317

315318
// 根据文件类型设置编辑器模式
316319
this.setEditorMode(fileName);
@@ -368,6 +371,11 @@ class FileTreeUI {
368371

369372
let menuItems = [];
370373

374+
// 内置项目不显示编辑菜单
375+
if (this.projectManager.currentProject && this.projectManager.currentProject.isBuiltIn) {
376+
return;
377+
}
378+
371379
if (type === 'file') {
372380
// 文件右键菜单
373381
const selectedCount = this.selectedItems.size;

iframe/script/User_config/HTML_Renderer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class HTMLRenderer {
4646
selectedFile = htmlFiles.find((f) => f.fileName === result.value);
4747
}
4848

49-
// 保存当前文件
50-
if (projectManager.currentFile) {
49+
// 保存当前文件(仅非内置项目)
50+
if (projectManager.currentFile && !(projectManager.currentProject && projectManager.currentProject.isBuiltIn)) {
5151
await projectManager.saveFileContent(projectManager.currentFile, editor.getValue());
5252
}
5353

0 commit comments

Comments
 (0)