quickadd icon indicating copy to clipboard operation
quickadd copied to clipboard

[BUG] <% tp.file.cursor() %> doesn't work.

Open qawatake opened this issue 4 years ago • 7 comments

Describe the bug <% tp.file.cursor() %> doesn't work when templating.

To Reproduce Steps to reproduce the behavior:

  1. create template macro zettelkasten with ↓
    • template path = _template/zettelkasten
    • focus new pane = on
  2. create a file _template/zettelkasten with
# <% tp.file.cursor() %>
  1. execute QuickAdd: Run QuickAdd -> zettelkasten from the command palette.

Expected behavior A file with content # is generated and the cursor position is on the right side of # .

Screenshots If applicable, add screenshots to help explain your problem.

https://user-images.githubusercontent.com/38106890/144574614-9bd873b0-e245-43dc-95da-0b3d7ad78780.mov

Desktop (please complete the following information):

  • OS: macOS, Mac mini (M1, 2020)
  • Version: Big Sur (v11.6.1)

qawatake avatar Dec 03 '21 09:12 qawatake

Fun discovery: if you keep the note title the same, it works the second time you call the quickadd (and add it to your current/existing doc):

# Heading
2022-01-17
- ***cursor is actually here***

# Heading
2022-01-17
- <% tp.file.cursor() %>

chelming avatar Jan 18 '22 00:01 chelming

it also works if you run it twice and choose "overwrite file"

chelming avatar Jan 18 '22 00:01 chelming

There seems to be a related bug report over at the Templater repo. It's not clear to me after reading that thread if this is a QuickAdd defect or a Templater design/bug conflict. Maybe a feature request for QuickAdd to place the cursor at the bottom of a new file when it is created? Not sure that's the right design either.

macdrifter avatar Nov 28 '22 12:11 macdrifter

As a workaround for templates added by "capture", I've used a timeout with the command "Templater: Jump to next cursor location". It seems that the QuickAdd insertion logic is still running when the templater tries to set the cursor.

<%*
// Just some modal form
result = await tp.user.modal_form('add_single_example', {title: "Example"})  
const title = result.getValue('title');  
// I want to get a caption with example 
tR += `> [!example] ${title}`  
-%>

> <% tp.file.cursor() %>
// Add small timeout, e.g. 100, to finish QuickAdd logics and run command
<%* setTimeout(() => {
	app.commands.executeCommandById("templater-obsidian:jump-to-next-cursor-location");  
}, 100) -%>

apoyezzhayev avatar Apr 07 '24 17:04 apoyezzhayev