Open YousufSSyed opened 1 year ago
It is not just in tags; I have the same problem in the body of a note. <% tp.file.cursor(1) %> works in preview mode if the template is at the beginning of a line. It does not work if the template is inserted in the middle of a line, the cursor is placed at the beginning of the text to be inserted instead. It works as expected in source mode.
Probably related to https://github.com/SilentVoid13/Templater/issues/1089
tp.file.cursor()
will need to be updated to work in the properties widget. That is the reason it doesn't work.
I did some research into this, and I don't think this is feasible to implement at the moment. The current implementation uses a Codemirror transaction to set the selection. This does not work with the new properties UI because it's not part of the editor for the purpose of transactions, the properties input boxes are divs that are contenteditable
. We could maybe conditionally use browser APIs instead of Codemirror APIs, but that feels brittle.
If any Obsidian API experts have input on a way to accomplish this that isn't brittle, I would love to know about it.
Here's an example of what a browser API solution would look like, for reference.
function setCursorPosition(el, pos) {
var range = document.createRange();
var sel = window.getSelection();
range.setStart(el, pos);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);
}
// Assuming we have figured out which property value and what position within that contenteditable div for that property value
const propertyIndex = 0;
const characterIndex = 19;
setCursorPosition(this.app.workspace.activeEditor.metadataEditor.rendered.at(propertyIndex).valueEl.childNodes.at(0).childNodes.at(0), characterIndex);
Adding to this because it's probably related: If I'm in editing a property and jump to the next tp.file.cursor() position, the templater tag ("<% tp.file.cursor() %>") ist removed but the cursor isn't moved but stays in properties forcing me to use the mouse to move out of Properties.
@Zachatoo How about this:
function setCursorInProperty(propertyIndex, offset) {
var property = app.workspace.activeLeaf.view.metadataEditor.rendered[propertyIndex];
property.setSelected();
property.focusValue();
var sel = window.getSelection();
var text = sel.anchorNode.childNodes[0];
if (text)
sel.collapse(text, offset);
}
setCursorInProperty(0, 5);
Plugin information (please complete the following information):
Describe the bug If I'm in live preview mode and I try to add
tp.file.cursor()
like so:With tags set to the tag property, either inserting it in manually after creating a new note with it, it won't work unless I'm in source mode. This is likely due to the new properties behavior of not accepting invalid input, however it works fine in date properties for me.
Expected behavior The templater function just works.
Screenshots