Open CSS File
To complete the launch of the project as an open source, we
have left to include some more documentation (Contributing guidelines and Code
of conduct).
We have decided to leave this at the moment and come back
to it later (this is about documentation so the writing can be split between the
two of us) So, we preferred to continue developing the next feature.
As we mentioned, we want to support preview display of CSS file
by opening it in a new tab (when clicking on "open new tab" button).
We saw that CSS file can be referenced with a full (web page) URL or
with a path relative to the current web page (learned this from HERE).
Examples:
- A full URL link: <link rel="stylesheet" href="https://www.w3schools.com/html/styles.css">
- A CSS file located in the HTML folder on the current web site: <link rel="stylesheet" href="/html/styles.css">
- A CSS file located in the same folder as the current page: <link rel="stylesheet" href="styles.css">
So, for start, we created a function for extracting the
selected path. We start with searching for a “.css” extension closest to the current position (once again, only if we yet to find the correct URL).
Than we made sure there were no white spaces in the extracted
path. If there are any white spaces, then the cursor is not standing on a css
path (explained that issue before).
**NOTE TO SELF: For now, we asume that there a not white spaces in the folders/files name in the path. We will need to address that in the future.
Once the path we extracted is valid, we use the split
function to separate the path (by " or ' characters) into an array so we can easly extract the path.
We go over each element in the array to extract
the CSS file path (by searching for the cell with the ‘css’ extension) and update
the value of foundUrl.url to that value.
Also, we added a function for creating a proper markdown
string that allow the option for open in a new tab. We also update the value of
previewObject.makeMarkdownString to that function (so this feature will also imlement the interface).
In addition, we started working on the function of opening
the CSS file for display.
Problems: we don’t know where the file is located relative
to the current folder location. It is located in the current folder the user is working on? Is in the folder one level above/under?
Also, we realized we don’t need the "newTabCommand" in that case (we just
need to open the CSS file and not a webview panel), so we knew we will need to deal with that later, and probably register a new command.
Therefore, in order to handle locating the CSS file relative
to the current folder location, we used the split function again to split both
paths (one for the current working path and the another for the extracted URL path).
We saw
that there are 4 ways of writing the path location (took from here)
We started dealing with the case that the CSS file is
in the same path of the current working folder (the first one in the image above).
We
had the current working file path and the CSS url file path after split each to an array.
Also we know that in that case the length of the CSS url file
path array is one (just the name of the file).
The current path includes the current file (so it will be place in the last cell in the split array), so all we needed is to override that cell (from the current working file path) with the CSS file name.
(the current
working path has the full path, so instead open current file we create new path
ending with the CSS file name).
After we updated the array, we used the "join" function to create a new updated path string.
Than, we needed to open that file. To do so, we converted the extracted file path string to vscode.uri
(learned from this stackoverflow question).
After that, we opened the file in a new tab (learned from this this stackoverflow question) as so:

* We defined that the file we open in a new tab beside the current file (like the webview does).To do so, we sent vscode.ViewColumn.Beside as a parameter to the "showTextDocument".
So what we did so far was:
1. extract the url of the CSS file ("extractCssUrl" function)
2. make a markdown function that implement the interface we created previously
3. if a CSS url is found, place that function as the current "getMarkdownString" function (interface function). You can see that placement in the first image, one line after the red box.
4. imlement a function that opens the CSS file. For now we only implemented the first relative path example.
The next thing was to try and debug what we wrote. To do that, we defined that the webview command will be called (from the markdown function).
As we explained before, the webview command invokes the interface "getHtmlContent" function.
So, like (3), we placed our "openCssFile" as the "getHtmlContent" (both functions placement is happening in "extractCssUrl").
When debuging this, the CSS file did open, but also an empty webview has opened.
This has happened because of the webview command definition:
You can see that we create a new webview panel every time we invoke that command.
We don't need that, so we decided to create and register a new command.
This command will call the "openCssFile" we made.
Now, we can associate that command with our markdown function like so:
Then, we continued to implement the "image/image.png" and "../image.png" examples:
EXPLANATION:
In those two examples, the split will creat a 2 cells array: ["image","image.png"] or ["..","image.png"].
WHAT IS LEFT TO DO IN THIS FEATURE?

* We defined that the file we open in a new tab beside the current file (like the webview does).To do so, we sent vscode.ViewColumn.Beside as a parameter to the "showTextDocument".
So what we did so far was:
1. extract the url of the CSS file ("extractCssUrl" function)
2. make a markdown function that implement the interface we created previously
3. if a CSS url is found, place that function as the current "getMarkdownString" function (interface function). You can see that placement in the first image, one line after the red box.
4. imlement a function that opens the CSS file. For now we only implemented the first relative path example.
The next thing was to try and debug what we wrote. To do that, we defined that the webview command will be called (from the markdown function).
As we explained before, the webview command invokes the interface "getHtmlContent" function.
So, like (3), we placed our "openCssFile" as the "getHtmlContent" (both functions placement is happening in "extractCssUrl").
When debuging this, the CSS file did open, but also an empty webview has opened.
This has happened because of the webview command definition:
You can see that we create a new webview panel every time we invoke that command.
We don't need that, so we decided to create and register a new command.
This command will call the "openCssFile" we made.
Now, we can associate that command with our markdown function like so:
Then, we continued to implement the "image/image.png" and "../image.png" examples:
EXPLANATION:
In those two examples, the split will creat a 2 cells array: ["image","image.png"] or ["..","image.png"].
- If it's the first option (the "else" in the green box): we need to override the last cell of the current path (which holds the current file's name) with the folder's name (in the examle: "image" folder), and add another cell with the CSS file name (in the examle: "image.png" file).
- If it's the second option (the blue box): we need to override the second to last cell of the current path (which holds the current file's mose internal folder) with the CSS file name (in the examle: "image.png"). We dont need the las cell so we overrode it with an empty string.
WHAT IS LEFT TO DO IN THIS FEATURE?
- Support a white space in the folders and files name.
- Support more difficalt path versions than those examples. (optional)
- Support the "/html/styles.css" format.
- Support the "https://www.w3schools.com/html/styles.css" format. (optional)
Comments
Post a Comment