| 619 | } |
| 620 | |
| 621 | createCodeFile() { |
| 622 | // Determine base path based on settings |
| 623 | let basePath = ""; |
| 624 | if (this.settings.newFileLocationMode === 'current') { |
| 625 | // Use the folder of the currently active file |
| 626 | const activeFile = this.app.workspace.getActiveFile(); |
| 627 | if (activeFile) { |
| 628 | const parent = activeFile.parent; |
| 629 | if (parent) { |
| 630 | basePath = parent.path; |
| 631 | } |
| 632 | } |
| 633 | } else { |
| 634 | // Use custom folder path from settings |
| 635 | basePath = this.settings.newFileFolderPath || ""; |
| 636 | } |
| 637 | |
| 638 | // 打开创建文件模态框 |
| 639 | new CreateCodeFileModal(this.app, basePath, (fullPath: string) => { |
| 640 | void (async () => { |
| 641 | try { |
| 642 | // 1. 处理扩展名 |
| 643 | // 检查是否有扩展名,没有则默认 .md |
| 644 | if (!fullPath.includes(".")) { |
| 645 | fullPath = fullPath + ".md"; |
| 646 | } |
| 647 | |
| 648 | // fullPath is already normalized and combined by the Modal |
| 649 | |
| 650 | // 2. 确保文件夹存在 |
| 651 | const folderPath = fullPath.substring(0, fullPath.lastIndexOf("/")); |
| 652 | if (folderPath) { |
| 653 | const folder = this.app.vault.getAbstractFileByPath(folderPath); |
| 654 | if (!folder) { |
| 655 | await this.app.vault.createFolder(folderPath); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | // 3. 创建文件 |
| 660 | const newFile = await this.app.vault.create(fullPath, ""); |
| 661 | new Notice(`${t('NOTICE_CREATE_SUCCESS')} ${fullPath}`); |
| 662 | |
| 663 | // 根据文件类型决定是否在 Code Space 中打开 |
| 664 | const ext = newFile.extension.toLowerCase(); |
| 665 | const isCodeFile = this.settings.extensions |
| 666 | .split(',') |
| 667 | .map(s => s.trim().toLowerCase()) |
| 668 | .includes(ext); |
| 669 | |
| 670 | if (isCodeFile) { |
| 671 | // 在 Code Space 中打开 |
| 672 | const leaf = this.app.workspace.getLeaf(true); |
| 673 | await leaf.setViewState({ |
| 674 | type: VIEW_TYPE_CODE_SPACE, |
| 675 | active: true, |
| 676 | state: { file: newFile.path } |
| 677 | }); |
| 678 | void this.app.workspace.revealLeaf(leaf); |