Templater icon indicating copy to clipboard operation
Templater copied to clipboard

Can't get `tp.config.active_file` to report correctly

Open holroy opened this issue 1 year ago • 5 comments

Plugin information:

  • OS: macOS Mojave 10.14.6
  • Templater version: 1.16.0
  • Obsidian version: 1.1.9
  • Templater settings: n/a

Describe the bug When I in "Note A" add a link to an uncreated file, like "Note B", and click on that note the new file template (in folder) triggers correctly, but tp.config.active_file reports "Note B", and not "Note A". In other words, it's the same as tp.file.target_file.

Expected behavior I expect it to hold the file I came from, and not the file I'm creating.

holroy avatar Jan 27 '23 21:01 holroy

This is not a bug. The sequence of events go like this:

  1. At Note A, Control + Click hyperlink to generate new note Note B
  2. When Note B is generated, Templater detects that this is a new file, and activates Folder template
  3. Folder Templates contains tp.config.active_file and since Note B is the currently active file, it has the TFile for Note B.

This is perfectly normal behaviour and no bugs are here. Please mark this Bug as solved.


But wait there's more! I have a solution for what you want!

In your folder template you can get the tFile of the previous note by using the following code:

let files = this.app.workspace.getLastOpenFiles();
let lastFile = this.app.vault.getAbstractFileByPath(files[0]); 

Here's a quick demo:

Demo demo

I actually use this to make custom breadcrumbs for my notes lol

~welp

welpdx avatar Jan 28 '23 04:01 welpdx

From the documentation: image

If your explanation was the intended version, ~welp, wouldn't that always mean that this file is existing?

Isn't Note A the file I was launching Templater from? That was the file where I clicked Note B to trigger the creation of it.

holroy avatar Jan 28 '23 17:01 holroy

There might be a misunderstanding here.

The keyword here from the doc is when "launching Templater".

This could mean, when you do any of the following: img For example, when doing Open Insert Template modal for example, you are "launching" Templater on that note.

Another way you can "launch Templater" is if you enabled Trigger Templater on new file creation and whenever a new file is created, it automatically triggers Templater and Templater will follow the settings you have selected in the Folder Templates.

So in the current use case, here are the steps again:

steps the action what is happening currently viewed file
1 At Note A, Control + Click hyperlink to generate new note Note B. Obsidian creates new file Note A
2 When Note B is generated, Templater detects that this is a new file, and activates Folder template Templater is "launched" on the new file that is created, which is Note B Note B
3 Folder Templates contains tp.config.active_file and since Note B is the currently active file, it has the TFile for Note B. Templater runs the tp.config.active_file code) Note B

Somethings you can look at:

https://github.com/SilentVoid13/Templater/blob/7f85beceeaded7527cfafec10b1dc3bab480d93b/src/settings/Settings.ts#L186

https://github.com/SilentVoid13/Templater/blob/7f85beceeaded7527cfafec10b1dc3bab480d93b/src/handlers/EventHandler.ts#L58-L72


tl;dr

I understand where you are coming from but I think if you look at the code and steps, you'll understand it a bit more.

Please note that when you click on that hyperlink, Obsidian creates the new file. Afterwards, Templater is activated. Once templater is activated, then it will run tp.config.active_file, which will be the file that you are current on, which is the new file, Note B.

welpdx avatar Jan 28 '23 18:01 welpdx

I still believe the wording is indicating something else, but I do focus on the "when launching Templater" part, not what it's being "launched on" as you state.

So according to my understanding, albeit as a non-native english speaking, I would expect the following:

  • Open Insert Template modalactive_file set to either none if called from a macro or a non-active file (like file explorer), or potentially from the currently active file if you were editing something when calling the command
  • Jump to file cursoractive_file is currently active file
  • Create new note from template – Would depend on where the create new note came from. If from file explorer, then it would be set to none, or the active file if you click on a link (to a previously non-existing file)
  • Replace templates in the active file – This would also be the currently active file

The special case of using template folder, is just another version of Create new note from template, as I see it.

tl;dr So I still believe either the wording needs to be seriously changed, or this is still a bug which would need some attention. And if the developers agree with you (are you one of the developers?), I would want this to be a feature request to actually have access to what was the originating file for the template execution (as indicated in the list above).

And I do see your point of view, but that is not what I read into "The active file (if existing) when launching Templater". From your point of view I do not however understand the "if existing" at all. That wouldn't be a possibility if your understanding of the wording was correct.

holroy avatar Jan 29 '23 01:01 holroy

@holroy

I completely understand that it must be infuriating to read the doc thinking one thing but it turns out different when used. I get that.

Details

So according to my understanding, albeit as a non-native english speaking, I would expect the following:

Excellent summary here of the 4 functions.

The special case of using template folder, is just another version of Create new note from template, as I see it.

See this is where I think you unfortunately got it wrong. Please read the code I have linked above and judge for yourself.

The real question is: When is the Templater Folder triggered? The answer: When it detects a new event and when that event is a new folder creation.

The code indicates this:

 this.trigger_on_file_creation_event = app.vault.on( 
             "create", 
...

In Obsidian, when ever a file is edited, renamed, created, etc, an event trigger is created. Other plugins can capitalize on this triggering mechanism. For example, one of my fav plugins (that is now incorporated into Obsidian directly) is obsidian-embedded-note-titles which basically syncs the first h1 to the name of the file. So after a user changes the file name it triggers an "rename" event, the plugin will take the new title and replace it with h1.

That code looks like this:

...
this.registerEvent(this.app.vault.on("rename", notifyFileChange));
...

For more information, check out all events listed in the official Obsidian API.

This event trigger is what Templater capitalizes on for Folder Templates. On a new event that is "create", apply the template to this new note. Since the new note is Note B, tp.config.active_file refers to Note B

tldr

And I do see your point of view, but that is not what I read into "The active file (if existing) when launching Templater".

I think the most objective way to go about this is to understand the code. For a moment, step away from the wording in the doc, my interpretation, and your preexisting interpretation of the code. Try to understand the code first and then come back to: is this a bug or a bad documentation?

So I still believe either the wording needs to be seriously changed, or this is still a bug which would need some attention.

I agree with this question here. Is it a bug? or is it a bad documentation?

My opinion is that this is not a bug. It's the latter. I strongly encourage you to make a pull request to make a change to the description of this function in the documentation. I would love to see what you would like to change it the description of tp.config.active_file to.

Just note that the author of this repo hasn't committed since Nov last year, so please don't expect an immediate update to the docs.

~welp

welpdx avatar Jan 29 '23 04:01 welpdx