On selection Change Event


After developing the entire hover panel end connecting it with the webview (via button), the last thing was to make sure that when the cursor changes its position, the hover panel will change accordingly (if necessary).


So, the first thing was to google how "onClickChange"/"onSelectionChange" events work in typescript and the vscode developing environment.


We found this GitHub page:

https://github.com/microsoft/vscode-extension-samples/blob/master/statusbar-sample/src/extension.ts


And under the "statusbar-sample" we found an easy example of how to use vscode "onDidChangeTextEditorSelection" function:



Using that code, we realized we needed to rearrange our code a little bit.


firstly, we moved the new command we made ("newTabCommand") to a new function which is called

at the beginning of the "activate" function:



The next thing was to move the main condition from the "activate" function to the "getImageUrl"

function:



Then we took the local variables from the "activate" function and made them global, so we can access them from outside the "activate" function, when their value will need to be changed.




Also, we moved the Markdown string from inside the "registerHoverProvider" call into the new "hoverStringValue" global variable. before:

after:

Next thing was to change our "activate" function to look a bit more like the one in the example: example code:

our code:


** NOTE: the hoverPanel variable is giving us the ability to change the content in the hover panel without making a new hover. The condition is to make sure not to make a new hover panel for the same URL.
Then we made the "updateHoverPanel" function:



explanation: 1. get the current selection. 2. if its an image URL and it's not the same as the last URL selected: 2.1. update the global URL variable to the current URL 2.2. update the markdown string according to the current URL 2.3. update the hoverPanel variable according to the current markdown string. Finally, to not make unnecessary hover panels, we made sure there is only one initialization to the "hoverPanel" variable:



And this is the final look:


Comments

Popular posts from this blog

Turorial- How to add a new feature

Little Bug Fix

Publishing First Beta & Refactoring The Code