making image preview webview tab + on mouse hover
First, we started researching the extension API of VS Code
From this documentation we have learned:
- How to build, run, debug, test
and publish an extension.
- How to take advantage of VS
Code's Extension API.
- Where to find guides and code samples to help get you started.
So, after that overview, we started our first experience by developing a basic “Hello world” extension.
We run VS Code extensions generator with “yo code” command, and then we filled out a few fields for creating a TypeScript project.
And here is our first extension!
Our next step was creating an extension that open in a new tab a marked URL string which represent an image.
For doing this, we have defined a vscode.WebViewPanel variable and created a function that receives URL as a string and returns it as an html page.
Then, our variable, by using its method, presents the image, just like below:
Now, for validating the chosen address extension and identifying the URL address by hovering and not by marking the entire line, we wrote a function that will identify the cursor location and extract the URL address as a string.
We have decided to search about vs code hovering feature,
and after a little while of googling,
found the basic code example:
(from: https://code.visualstudio.com/api/references/vscode-api
- in the "languages" section)
which does this:
The next thing was to try and display an image instead of
the 'I am a hover!' string.
We learned that in order to do that, we need to create an instance
of the vscode 'MarkdownString' class.
But we didn’t really knew what we need the send to the class
constructor.
So the first try was to give the ctor the HTML string we
made for the webview tab.
Then
we understood that we have to send the ctor a string with a markdown syntax.
We
searched for a way to convert the HTML string to markdown string but found out
there isn't any, so we started to learn some basic markdown.
we
learned that the syntax for displaying image in markdown is:

It
took us a while to figure out the exact syntax to make it display some image (a
"static" image url at
thos point), but eventually figured it out !
The
next thing was to make this work with the url the cursor is hover over.
We
added a function to extract the url (if there is any) from the current line
(where the cursor is, at this point), so now it looks like:
Conclusions:
So
far we have made a hover that displays image according to the url the cursor is
on.
In
addition, we made a webview that display the image as well.
The
next move for us is to make some sort of button in the hover display which
allows the user to open the image in a new tab in the vscode and another button
to open the image in the default browser.
Also,
right now the hover doesn’t "run over" the previous displayed image,
but creating a new one in addition, so we need to fix that.
Comments
Post a Comment