Skip to content

Allow inclusion of JavaScript and CSS file paths in the HtmlContentView #538

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

Merged
merged 1 commit into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ function Set-VSCodeHtmlContentView {
The HTML content that will be placed inside the <body> tag
of the view.

.PARAMETER JavaScriptPaths
An array of paths to JavaScript files that will be loaded
into the view.

.PARAMETER StyleSheetPaths
An array of paths to stylesheet (CSS) files that will be
loaded into the view.

.EXAMPLE
# Set the view content with an h1 header
Set-VSCodeHtmlContentView -HtmlContentView $htmlContentView -HtmlBodyContent "<h1>Hello world!</h1>"
Expand All @@ -39,10 +47,23 @@ function Set-VSCodeHtmlContentView {
[Alias("Content")]
[AllowEmptyString()]
[string]
$HtmlBodyContent
$HtmlBodyContent,

[Parameter(Mandatory = $false)]
[string[]]
$JavaScriptPaths,

[Parameter(Mandatory = $false)]
[string[]]
$StyleSheetPaths
)

process {
$HtmlContentView.SetContent($HtmlBodyContent).Wait();
$htmlContent = New-Object Microsoft.PowerShell.EditorServices.VSCode.CustomViews.HtmlContent
$htmlContent.BodyContent = $HtmlBodyContent
$htmlContent.JavaScriptPaths = $JavaScriptPaths
$htmlContent.StyleSheetPaths = $StyleSheetPaths

$HtmlContentView.SetContent($htmlContent).Wait();
}
}
31 changes: 31 additions & 0 deletions src/PowerShellEditorServices.VSCode/CustomViews/HtmlContent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.PowerShell.EditorServices.VSCode.CustomViews
{
/// <summary>
/// Contains details about the HTML content to be
/// displayed in an IHtmlContentView.
/// </summary>
public class HtmlContent
{
/// <summary>
/// Gets or sets the HTML body content.
/// </summary>
public string BodyContent { get; set; }

/// <summary>
/// Gets or sets the array of JavaScript file paths
/// to be used in the HTML content.
/// </summary>
public string[] JavaScriptPaths { get; set; }

/// <summary>
/// Gets or sets the array of stylesheet (CSS) file
/// paths to be used in the HTML content.
/// </summary>
public string[] StyleSheetPaths { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Utility;
Expand Down Expand Up @@ -32,7 +34,27 @@ public Task SetContent(string htmlBodyContent)
new SetHtmlContentViewRequest
{
Id = this.Id,
HtmlBodyContent = htmlBodyContent
HtmlContent = new HtmlContent { BodyContent = htmlBodyContent }
}, true);
}

public Task SetContent(HtmlContent htmlContent)
{
HtmlContent validatedContent =
new HtmlContent()
{
BodyContent = htmlContent.BodyContent,
JavaScriptPaths = this.GetUriPaths(htmlContent.JavaScriptPaths),
StyleSheetPaths = this.GetUriPaths(htmlContent.StyleSheetPaths)
};

return
this.messageSender.SendRequest(
SetHtmlContentViewRequest.Type,
new SetHtmlContentViewRequest
{
Id = this.Id,
HtmlContent = validatedContent
}, true);
}

Expand All @@ -47,5 +69,18 @@ public Task AppendContent(string appendedHtmlBodyContent)
AppendedHtmlBodyContent = appendedHtmlBodyContent
}, true);
}

private string[] GetUriPaths(string[] filePaths)
{
return
filePaths?
.Select(p => {
return
new Uri(
Path.GetFullPath(p),
UriKind.Absolute).ToString();
})
.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static readonly
/// <summary>
/// Gets or sets the HTML body content to set in the view.
/// </summary>
public string HtmlBodyContent { get; set; }
public HtmlContent HtmlContent { get; set; }
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public interface IHtmlContentView : ICustomView
/// <returns>A Task which can be awaited for completion.</returns>
Task SetContent(string htmlBodyContent);

/// <summary>
/// Sets the HTML content of the view.
/// </summary>
/// <param name="htmlContent">
/// The HTML content that is placed inside of the page's body tag.
/// </param>
/// <returns>A Task which can be awaited for completion.</returns>
Task SetContent(HtmlContent htmlContent);

/// <summary>
/// Appends HTML body content to the view.
/// </summary>
Expand Down