SilentVoid13 / Templater

A template plugin for obsidian
https://silentvoid13.github.io/Templater
GNU Affero General Public License v3.0
3.13k stars 187 forks source link

tp.file.cursor should change the mode to edit mode if in reading mode #1232

Open RonMcKay opened 10 months ago

RonMcKay commented 10 months ago

Plugin information (please complete the following information):

Daily note template:

---
created: <% tp.file.creation_date() %>
---

<% tp.file.cursor(1) %>

To reproduce:

These steps worked for me in a new vault with the minimal setup from above.

Describe the bug When I trigger the "Periodic Notes: Open daily note" command from the active tab which is in reading view, the diary page is getting created and commands are replaced, except for <% tp.file.cursor(1) %>. This command remains on the diary page without being replaced.

Expected behavior I understand that there can not be a cursor position when the diary page is in reading view, but I expect the templater code to be replaced with nothing.

Screenshots This is the result of the daily note creation. The creation date is replaced, but <% tp.file.cursor(1) %> remains as is. image

RonMcKay commented 10 months ago

Could be related to #1112

spenserpothier commented 10 months ago

I am experiencing this as well with same versions but macOS 13.4.1 (a). It also happens when I try to create a periodic note from editing view.

edit: Sorry I might have misread the original issue,

<% tp.file.cursor() %>

No longer works for me

Zachatoo commented 10 months ago

I'd argue that this is expected and desired behavior. It would be weird if a user setup multiple cursors and suddenly they're all gone because they were accidentally in reading mode instead of editing mode.

[!NOTE] Example of multiple cursors.

Name: <% tp.file.cursor(0) %>
Status: <% tp.file.cursor(1) %>

I recommend switching to edit mode and run the Templater: Jump to next cursor location command to jump to the cursor and remove it.

RonMcKay commented 10 months ago

True, for multiple cursors this would be weird. But even for a single cursor with <% tp.file.cursor(0) %> this happens for me. But maybe it is just me finding this weird. 😄

davidolrik commented 9 months ago

I also have this issue.

When creating a new node from the calendar widget, and either of these conditions are pressent:

Then <% tp.file.cursor() %> is not executed.

I propose that when creating a new note and Templater is engaged, then it should switch to edit mode automatically.

Zachatoo commented 9 months ago

I don't think it's a good idea to switch the mode on the user. Part of the benefit of Templater is you can bind templates to a hotkey to quickly run a script on the current note to manipulate it, without having to be in edit mode. I know of users who stay in reading mode and run hotkeys to mark a note as reviewed before moving on to the next note.

I would be open to a contribution for a new Templater function in the file module or something that can switch the mode explicitly.

davidolrik commented 9 months ago

Maybe <% tp.file.cursor() %> should also do that, as it only makes sense to use in edit mode.

Zachatoo commented 9 months ago

I like that, should be easy enough to implement.

Even just making the change to have tp.file.cursor() change the view to edit mode if you're in reading mode should resolve this issue, no need to add a new function to templater.

Fred-Vatin commented 4 months ago

In the meantime, one can add a user function following the doc. image

Script above exporting the function:

function switch_to_editmode () {
    /* Be sure to switch to edit mode first (live preview)
         It is required so it can insert the template in existing file. */
    const view = app.workspace.activeLeaf.getViewState()
    view.state.mode = 'source'
    view.state.source = false
    app.workspace.activeLeaf.setViewState(view)
}
module.exports = switch_to_editmode;

Then you call it in your template:

<%*
/* Be sure to switch to edit mode first (live preview)
   It is required so it can insert the template in existing file. */
tp.user.Switch_to_edit_mode();

// start of template construct
// insert your logic here

if (title.startsWith("Untitled")) { 
  title = await tp.system.prompt("What is the plugin name ?"); 
  await tp.file.rename(title);
} 

// and so on
%>

Further in the template, add this to jump to the cursor on any case. It is necessary if create a new note from the template where the focus is on the note title. You just press enter to validate it and the cursor jumps to the anchor.

# Description

<% tp.file.cursor(1) %><%* app.workspace.activeLeaf.view.editor?.focus(); %>

Source: https://forum.obsidian.md/t/easily-switch-between-source-mode-live-preview-preview/27151/25