Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editor object in jsfile / jscommand fails to get selection #99

Closed
twio142 opened this issue May 17, 2022 · 8 comments
Closed

editor object in jsfile / jscommand fails to get selection #99

twio142 opened this issue May 17, 2022 · 8 comments

Comments

@twio142
Copy link
Contributor

twio142 commented May 17, 2022

The editor.getSelection() function seems to fail in jsfile or jscommand, no matter selected with insert or visual mode.

jscommand { console.log(editor.getSelection()) } gets nothing, no matter mapped in vimrc file or put in editor command line.

But editor.getCursor() works just fine.

@esm7
Copy link
Owner

esm7 commented May 17, 2022

There's an Obsidian/CodeMirror bug/behavior (maybe that's intentional, I don't know), that the selection is cleared when a Vim command is initiated. For this reason plugin functionalities that rely on the selection use ugly trickery that constantly saves the last selection.
Since you can't do that as part of a jscommand, I guess it makes sense to expose the last selection as a variable.

@twio142
Copy link
Contributor Author

twio142 commented May 17, 2022

expose the last selection as a variable

Could you please explain me how?

@esm7
Copy link
Owner

esm7 commented May 17, 2022

It's not something you can do right now unfortunately; I will add the functionality that like jscommand receives editor and view, it will also receive lastSelection.

@esm7
Copy link
Owner

esm7 commented Jun 1, 2022

Added a selection parameter in 0.6.2.

@esm7 esm7 closed this as completed Jun 1, 2022
@huyz
Copy link

huyz commented Nov 7, 2022

There's an Obsidian/CodeMirror bug/behavior (maybe that's intentional, I don't know), that the selection is cleared when a Vim command is initiated. For this reason plugin functionalities that rely on the selection use ugly trickery that constantly saves the last selection

@esm7 Can you tell me a bit more about this hack? Cause it doesn't seem to work 100% for me. I'm trying to do

exmap TransformTitlecase obcommand obsidian-editor-shortcuts:transformToTitlecase
vmap <A-ç>T :TransformTitlecase

But the selection is still lost (and thus only one word gets titlecased, rather than the whole selection)

@esm7
Copy link
Owner

esm7 commented Nov 7, 2022

Unfortunately that hack only applies to the internal workings of the Vimrc plugin, e.g. if you use jscommand you can get the "original" selection (before the Ex command clears it). AFAIK when it comes to Obsidian editor commands, they take the editor selection as-is, and it happens to be empty after executing an Ex command :(
I suggest you implement your TransformTitlecase command as a jscommand instead of calling the Obsidian functionality.

@huyz
Copy link

huyz commented Nov 7, 2022

Got it, thanks:

exmap TransformTitlecaseSelection jscommand { editor.setSelections([selection]); this.app.commands.commands['obsidian-editor-shortcuts:transformToTitlecase'].editorCallback(editor) }
vmap <A-ç>T :TransformTitlecaseSelection

@huyz
Copy link

huyz commented Nov 7, 2022

More examples at #146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants