Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I rewrite this any better - without setTimeout? #1331

Open
SunnyAureliusRichard opened this issue Mar 2, 2024 · 2 comments
Open

Can I rewrite this any better - without setTimeout? #1331

SunnyAureliusRichard opened this issue Mar 2, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@SunnyAureliusRichard
Copy link

SunnyAureliusRichard commented Mar 2, 2024

Sometimes if Obsidian is lagging, the previous file I was in gets obliterated (content becomes empty).

Can there be more hooks?

I've considered increasing the time from 500 to 1500. It is still not perfect.

I've posted the code I'm working with right now at the end.

---
time: "<% tp.date.now("YYYY-MM-DD HH:mm") %>"
---
<%*
// rename file in the format "2024-01-13 00-46 - JOURNAL - - - - - -" if called Untitled
if (tp.file.title.indexOf("ntitled") != -1) {
  let fileNameShouldBe = tp.date.now("YYYY-MM-DD HH-mm") + " - journal"

  for(var i = 0; i < 10; i++) {
    let fileNameShouldBeFull = tp.file.folder(true) + "/" + fileNameShouldBe + ".md"
    
    if (await tp.file.exists(fileNameShouldBeFull)) {
      fileNameShouldBe += " -"
      continue;
    }
    
    await tp.file.rename(fileNameShouldBe);
    break;
  }
} else {
  // remove its contents otherwise
  var file = app.workspace.getActiveFile()
  setTimeout(function() {
	app.vault.modify(file, "") 
  }, 500)
}
%>
<%*
let textToday = tp.date.now("YYYY-MM-DD")
tR += `[[${textToday}]]`
%>

<% tp.date.now("HH:mm") %>

Much appreciated!

@SunnyAureliusRichard SunnyAureliusRichard added the enhancement New feature or request label Mar 2, 2024
@Zachatoo
Copy link
Collaborator

Zachatoo commented Mar 3, 2024

Have you tried using the tp.hooks.on_all_templates_executed hook? Also I would consider a different way of getting the current file. Would look something like this.

tp.hooks.on_all_templates_executed(async () => {
  var file = tp.file.find_tfile(tp.file.path(true));
  // var file = app.workspace.getActiveFile();
  await app.vault.modify(file, "");
});

What kind of hook would you be looking for? I'm not sure what this template is trying to accomplish.

@SunnyAureliusRichard
Copy link
Author

I have a shortcut on my android that when I click it it opens Obsidian and creates a file called Untitled in a specific folder.
Templater adds a property and textToday to the file
Templater renames the file from Untitled to current date.
If Templater sees that the file with the current date exists (maybe there were two created in the same minute), then it adds a dash at the end. This is attempted 10 times.

Once that's complete, I can start writing in the file.

Sometimes I want to spin off different files from that one. For example I may write [[Koenar]], and press on it.

Templater does its usual activities of writing textToday.
However, it does not need to, and I also don't need to change the file name, it's already correct - Koenar.

From there I just move the file (I have a shortcut CTRL+M, or a button above the keyboard on Android).

Some callbacks for deciding what name the file should have, with conditions, would be nice.
Also some callback to decide if templater should run for the file, or it should just leave the file blank.

I will give that hook a try, I had some issues with it yesterday..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants