Skip to content

Commit

Permalink
fix: revert pr #816 (#835)
Browse files Browse the repository at this point in the history
Reverts #816 for now as it was causing issues in a non-deterministic way
  • Loading branch information
shabegom authored Sep 19, 2022
1 parent 64d8f63 commit 5ee485a
Showing 1 changed file with 28 additions and 47 deletions.
75 changes: 28 additions & 47 deletions src/core/Templater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,53 +393,34 @@ export class Templater {
return;
}

await new Promise<void>((resolve) => {
// eslint-disable-next-line prefer-const
let timeout: number;
const file_open_ref = app.workspace.on("file-open", async (opened_file: TFile) => {
if (opened_file !== file) {
return;
}
window.clearTimeout(timeout);
app.workspace.offref(file_open_ref);

// TODO: find a better way to do this
// Currently, I have to wait for the daily note plugin to add the file content before replacing
// Not a problem with Calendar however since it creates the file with the existing content
await delay(300);

if (
file.stat.size == 0 &&
templater.plugin.settings.enable_folder_templates
) {
const folder_template_match =
templater.get_new_file_template_for_folder(file.parent);
if (!folder_template_match) {
resolve();
return;
}
const template_file: TFile = await errorWrapper(
async (): Promise<TFile> => {
return resolve_tfile(folder_template_match);
},
`Couldn't find template ${folder_template_match}`
);
// errorWrapper failed
if (template_file == null) {
resolve();
return;
}
await templater.write_template_to_file(template_file, file);
} else {
await templater.overwrite_file_commands(file);
}
resolve();
});
timeout = window.setTimeout(() => {
app.workspace.offref(file_open_ref);
resolve();
}, 300);
});
// TODO: find a better way to do this
// Currently, I have to wait for the daily note plugin to add the file content before replacing
// Not a problem with Calendar however since it creates the file with the existing content
await delay(300);

if (
file.stat.size == 0 &&
templater.plugin.settings.enable_folder_templates
) {
const folder_template_match =
templater.get_new_file_template_for_folder(file.parent);
if (!folder_template_match) {
return;
}
const template_file: TFile = await errorWrapper(
async (): Promise<TFile> => {
return resolve_tfile(folder_template_match);
},
`Couldn't find template ${folder_template_match}`
);
// errorWrapper failed
if (template_file == null) {
return;
}
await templater.write_template_to_file(template_file, file);
} else {
await templater.overwrite_file_commands(file);
}
}

async execute_startup_scripts(): Promise<void> {
Expand Down

0 comments on commit 5ee485a

Please sign in to comment.