Skip to content

Commit e03aeec

Browse files
authored
fix: create form failed (#66)
1 parent a1c742e commit e03aeec

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

plugin/src/service/command/ApplicationCommandService.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { localInstance } from "src/i18n/locals";
22
import FormPlugin from "src/main";
33
import { FormService } from "../FormService";
44
import CFormSuggestModal from "src/component/modal/CFormSuggestModal";
5-
import { normalizePath } from "obsidian";
5+
import { normalizePath, Notice } from "obsidian";
66
import { FormConfig } from "src/model/FormConfig";
77
import { DEFAULT_SETTINGS } from "src/settings/PluginSettings";
88
import { openFilePathDirectly } from "src/utils/openFilePathDirectly";
@@ -53,10 +53,15 @@ export class ApplicationCommandService {
5353
defaultTargetFolder: targetFolder,
5454
fileType: "cform",
5555
onSubmit: async (fileName, targetFolder) => {
56-
const formConfig = new FormConfig(v4())
57-
const json = JSON.stringify(formConfig, null, 2);
58-
const file = await Files.createFile(app, fileName, targetFolder, json);
59-
await openFilePathDirectly(app, file.path, "modal");
56+
try {
57+
const formConfig = new FormConfig(v4())
58+
const json = JSON.stringify(formConfig, null, 2);
59+
const file = await Files.createFile(app, fileName, targetFolder, json);
60+
await openFilePathDirectly(app, file.path, "modal");
61+
} catch (error) {
62+
console.error("Error creating form file:", error);
63+
new Notice("create file " + fileName + " failed", 5000);
64+
}
6065
modal.close();
6166
}
6267
})

plugin/src/utils/Files.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ export class Files {
2222
if (!folderExists) {
2323
await app.vault.createFolder(normlizedFolder);
2424
}
25-
26-
27-
const res = app.vault.getAbstractFileByPath(filePath)
28-
if (res instanceof TFile) {
29-
await app.vault.modify(res, data);
30-
return res;
31-
} else {
32-
throw new Error(`Failed to create file: ${filePath}`);
33-
}
25+
return await app.vault.create(filePath, data);
3426
}
3527
}

0 commit comments

Comments
 (0)