(activeProject: Project | undefined, refresh: () => Promise<void>)
| 311 | * 在当前激活的工程文件的同一目录下创建prg文件 |
| 312 | */ |
| 313 | export async function createFileAtCurrentProjectDir(activeProject: Project | undefined, refresh: () => Promise<void>) { |
| 314 | if (!activeProject || activeProject.isDraft) return; |
| 315 | |
| 316 | setTimeout(() => { |
| 317 | Dialog.input("请输入文件名(不需要输入后缀名)").then(async (userInput) => { |
| 318 | if (userInput === undefined || userInput.trim() === "") return; |
| 319 | |
| 320 | // 检查文件名是否合法 |
| 321 | const invalidChars = /[\\/:*?"<>|]/; |
| 322 | if (invalidChars.test(userInput)) { |
| 323 | toast.error('文件名不能包含以下字符:\\ / : * ? " < > |'); |
| 324 | return; |
| 325 | } |
| 326 | |
| 327 | // 移除可能存在的.prg后缀 |
| 328 | let fileName = userInput.trim(); |
| 329 | if (fileName.endsWith(".prg")) { |
| 330 | fileName = fileName.slice(0, -4); |
| 331 | } |
| 332 | |
| 333 | // 创建新文件路径 |
| 334 | const currentDir = PathString.dirPath(activeProject.uri.fsPath); |
| 335 | const newFilePath = currentDir + "/" + fileName + ".prg"; |
| 336 | |
| 337 | // 检查文件是否已存在 |
| 338 | const fileExists = await exists(newFilePath); |
| 339 | if (fileExists) { |
| 340 | toast.error(`文件 "${fileName}.prg" 已存在,请使用其他文件名`); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | const newUri = URI.file(newFilePath); |
| 345 | |
| 346 | // 创建新项目 |
| 347 | const newProject = Project.newDraft(); |
| 348 | newProject.uri = newUri; |
| 349 | |
| 350 | // 初始化项目 |
| 351 | loadAllServicesBeforeInit(newProject); |
| 352 | newProject |
| 353 | .init() |
| 354 | .then(() => { |
| 355 | loadAllServicesAfterInit(newProject); |
| 356 | // 在舞台上创建文本节点 |
| 357 | const newTextNode = new TextNode(newProject, { |
| 358 | text: fileName, |
| 359 | }); |
| 360 | newProject.stageManager.add(newTextNode); |
| 361 | newTextNode.isSelected = true; |
| 362 | |
| 363 | // 保存文件 |
| 364 | newProject |
| 365 | .save() |
| 366 | .then(async () => { |
| 367 | // 更新项目列表和活动项目 |
| 368 | store.set(tabsAtom, [...store.get(tabsAtom), newProject]); |
| 369 | store.set(activeTabAtom, newProject); |
| 370 | await RecentFileManager.addRecentFileByUri(newUri); |
no test coverage detected