kelciour / edit-field-during-review

3 stars 1 forks source link

Have a value assigned to a field automatically without Cntrl+click #14

Open sweetins opened 1 year ago

sweetins commented 1 year ago

Am not sure if this is the right place for this though.

Edit Field During Review already does exactly what it's suppose to. However, I was hoping I could tweak it for the purpose of having a particular field in my note updated with the current scroll position: perhaps at the press of a hotkey. Am hoping to do this whilst at the back of a given card. Please can this be achieved? Am newbie in python and any assistance will be well appreciated. Thank you

kelciour commented 1 year ago

Back Template:

...

<div id="elem" hidden>{{edit:Back}}</div>

<script>
document.onscroll = (event) => { 
  elem = document.querySelector('#elem [contenteditable]');
  fld = "Back";
  nid = elem.dataset.nid;
  value = window.scrollY;
  pycmd(`ankisave#${fld}#${nid}#${value}`);
}; 
</script>

AnkiWebView Inspector might be helpful.

If you're using Edit Field During Review (Cloze), the pycmd command will be different.

sweetins commented 1 year ago

Thank you so much for the response Sir. Am grateful.

From the preamble I get it that am to populate my back template with the provided code. What am not so sure of is how to initiate the task (preferably with a hotkey) so that the operation happens seamlessly without a prompt. I had appreciate it if you can throw some light on that. Thanks again.

kelciour commented 1 year ago

It happens automatically as soon as the 'scroll' event is fired.

To trigger it with the hotkey (Ctrl+D) instead:

...

<div id="elem" hidden>{{edit:Back}}</div>

<script>
function saveScrollPosition() {
  elem = document.querySelector('#elem [contenteditable]');
  fld = "Back";
  nid = elem.dataset.nid;
  value = window.scrollY;
  pycmd(`ankisave#${fld}#${nid}#${value}`);
}

document.onkeyup = function(event) {
  if (event.code == 'KeyD' && (event.ctrlKey || event.metaKey)) {
    saveScrollPosition();
  }
};
</script>

https://javascript.info/keyboard-events

sweetins commented 1 year ago

You are a life-saver!! Thanks a lot.

I suppose this cannot be achieved (in that case I would have used a button instead of a hotkey) on Ankidroid yet because of its inability to support addon.