SilentVoid13 / Templater

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

tp.file.cursor() doesn't work inside tags properties except in source mode. #1205

Open YousufSSyed opened 1 year ago

YousufSSyed commented 1 year ago

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:

Tags: <% tp.file.cursor() %>

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 Screenshot 2023-09-25 112557

tprotopopescu commented 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

Zachatoo commented 1 year ago

tp.file.cursor() will need to be updated to work in the properties widget. That is the reason it doesn't work.

Zachatoo commented 11 months ago

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);
j3qb commented 3 months ago

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.

connerdassen commented 1 month ago

@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);