Skip to content

Commit 632ee12

Browse files
committed
Allow inclusion of JavaScript and CSS file paths in the HtmlContentView
This change adds new parameters to Set-VSCodeHtmlContentView which allow the user to specify JavaScriptPaths and StyleSheetPaths to include local JavaScript and CSS files into their HTML views. Part of the fix for PowerShell/vscode-powershell#910.
1 parent 090c97d commit 632ee12

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

module/PowerShellEditorServices.VSCode/Public/HtmlContentView/Set-VSCodeHtmlContentView.ps1

+23-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ function Set-VSCodeHtmlContentView {
1919
The HTML content that will be placed inside the <body> tag
2020
of the view.
2121
22+
.PARAMETER JavaScriptPaths
23+
An array of paths to JavaScript files that will be loaded
24+
into the view.
25+
26+
.PARAMETER StyleSheetPaths
27+
An array of paths to stylesheet (CSS) files that will be
28+
loaded into the view.
29+
2230
.EXAMPLE
2331
# Set the view content with an h1 header
2432
Set-VSCodeHtmlContentView -HtmlContentView $htmlContentView -HtmlBodyContent "<h1>Hello world!</h1>"
@@ -39,10 +47,23 @@ function Set-VSCodeHtmlContentView {
3947
[Alias("Content")]
4048
[AllowEmptyString()]
4149
[string]
42-
$HtmlBodyContent
50+
$HtmlBodyContent,
51+
52+
[Parameter(Mandatory = $false)]
53+
[string[]]
54+
$JavaScriptPaths,
55+
56+
[Parameter(Mandatory = $false)]
57+
[string[]]
58+
$StyleSheetPaths
4359
)
4460

4561
process {
46-
$HtmlContentView.SetContent($HtmlBodyContent).Wait();
62+
$htmlContent = New-Object Microsoft.PowerShell.EditorServices.VSCode.CustomViews.HtmlContent
63+
$htmlContent.BodyContent = $HtmlBodyContent
64+
$htmlContent.JavaScriptPaths = $JavaScriptPaths
65+
$htmlContent.StyleSheetPaths = $StyleSheetPaths
66+
67+
$HtmlContentView.SetContent($htmlContent).Wait();
4768
}
4869
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.PowerShell.EditorServices.VSCode.CustomViews
7+
{
8+
/// <summary>
9+
/// Contains details about the HTML content to be
10+
/// displayed in an IHtmlContentView.
11+
/// </summary>
12+
public class HtmlContent
13+
{
14+
/// <summary>
15+
/// Gets or sets the HTML body content.
16+
/// </summary>
17+
public string BodyContent { get; set; }
18+
19+
/// <summary>
20+
/// Gets or sets the array of JavaScript file paths
21+
/// to be used in the HTML content.
22+
/// </summary>
23+
public string[] JavaScriptPaths { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets the array of stylesheet (CSS) file
27+
/// paths to be used in the HTML content.
28+
/// </summary>
29+
public string[] StyleSheetPaths { get; set; }
30+
}
31+
}

src/PowerShellEditorServices.VSCode/CustomViews/HtmlContentView.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//
55

66
using System;
7+
using System.IO;
8+
using System.Linq;
79
using System.Threading.Tasks;
810
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
911
using Microsoft.PowerShell.EditorServices.Utility;
@@ -32,7 +34,27 @@ public Task SetContent(string htmlBodyContent)
3234
new SetHtmlContentViewRequest
3335
{
3436
Id = this.Id,
35-
HtmlBodyContent = htmlBodyContent
37+
HtmlContent = new HtmlContent { BodyContent = htmlBodyContent }
38+
}, true);
39+
}
40+
41+
public Task SetContent(HtmlContent htmlContent)
42+
{
43+
HtmlContent validatedContent =
44+
new HtmlContent()
45+
{
46+
BodyContent = htmlContent.BodyContent,
47+
JavaScriptPaths = this.GetUriPaths(htmlContent.JavaScriptPaths),
48+
StyleSheetPaths = this.GetUriPaths(htmlContent.StyleSheetPaths)
49+
};
50+
51+
return
52+
this.messageSender.SendRequest(
53+
SetHtmlContentViewRequest.Type,
54+
new SetHtmlContentViewRequest
55+
{
56+
Id = this.Id,
57+
HtmlContent = validatedContent
3658
}, true);
3759
}
3860

@@ -47,5 +69,18 @@ public Task AppendContent(string appendedHtmlBodyContent)
4769
AppendedHtmlBodyContent = appendedHtmlBodyContent
4870
}, true);
4971
}
72+
73+
private string[] GetUriPaths(string[] filePaths)
74+
{
75+
return
76+
filePaths?
77+
.Select(p => {
78+
return
79+
new Uri(
80+
Path.GetFullPath(p),
81+
UriKind.Absolute).ToString();
82+
})
83+
.ToArray();
84+
}
5085
}
5186
}

src/PowerShellEditorServices.VSCode/CustomViews/HtmlContentViewMessages.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static readonly
2828
/// <summary>
2929
/// Gets or sets the HTML body content to set in the view.
3030
/// </summary>
31-
public string HtmlBodyContent { get; set; }
31+
public HtmlContent HtmlContent { get; set; }
3232
}
3333

3434
/// <summary>

src/PowerShellEditorServices.VSCode/CustomViews/IHtmlContentView.cs

+9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ public interface IHtmlContentView : ICustomView
2323
/// <returns>A Task which can be awaited for completion.</returns>
2424
Task SetContent(string htmlBodyContent);
2525

26+
/// <summary>
27+
/// Sets the HTML content of the view.
28+
/// </summary>
29+
/// <param name="htmlContent">
30+
/// The HTML content that is placed inside of the page's body tag.
31+
/// </param>
32+
/// <returns>A Task which can be awaited for completion.</returns>
33+
Task SetContent(HtmlContent htmlContent);
34+
2635
/// <summary>
2736
/// Appends HTML body content to the view.
2837
/// </summary>

0 commit comments

Comments
 (0)