这是indexloc提供的服务,不要输入任何密码
Skip to content
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
5 changes: 4 additions & 1 deletion MLS.Agent.Tests/GetChartHtmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public void Returns_the_html_with_script_containing_require_config()
var document = new HtmlDocument();
document.LoadHtml(html);

document.DocumentNode.SelectSingleNode("//script").InnerHtml.Should().Contain("var xplotRequire = require.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});");
document.DocumentNode.SelectSingleNode("//script")
.InnerHtml
.Should()
.Contain("var xplotRequire = requirejs.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});");
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion MLS.Agent.Tests/XplotKernelExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task When_a_chart_is_returned_the_value_produced_has_html_with_the_
ContainSingle(valueProduced =>
valueProduced.FormattedValues.Any(formattedValue =>
formattedValue.MimeType == "text/html"
&& formattedValue.Value.ToString().Contains("var xplotRequire = require.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});")
&& formattedValue.Value.ToString().Contains("var xplotRequire = requirejs.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});")
&& formattedValue.Value.ToString().Contains("xplotRequire([\'plotly\'], function(Plotly)")
));
}
Expand Down
34 changes: 24 additions & 10 deletions XPlot.DotNet.Interactive.KernelExtensions/XPlotKernelExtension.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using HtmlAgilityPack;
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using HtmlAgilityPack;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Rendering;
using System;
using System.Text;
using System.Threading.Tasks;
using XPlot.Plotly;
using static Microsoft.DotNet.Interactive.Rendering.PocketViewTags;

namespace XPlot.DotNet.Interactive.KernelExtensions
{
Expand All @@ -30,24 +31,37 @@ public string GetChartHtml(PlotlyChart chart)
var scriptNode = document.DocumentNode.SelectSingleNode("//script");

var newHtmlDocument = new HtmlDocument();

newHtmlDocument.DocumentNode.ChildNodes.Add(divNode);
newHtmlDocument.DocumentNode.ChildNodes.Add(GetScriptNodeWithRequire(scriptNode));

return newHtmlDocument.DocumentNode.WriteContentTo();
}


private static HtmlNode GetScriptNodeWithRequire(HtmlNode scriptNode)
{
var newScript = new StringBuilder();

newScript.AppendLine("<script>");
newScript.Append(@"
var xplotRequire = require.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});
xplotRequire(['plotly'], function(Plotly) {
");
newScript.AppendLine("<script type=\"text/javascript\">");
newScript.AppendLine(@"
var renderPlotly = function() {
var xplotRequire = requirejs.config({context:'xplot-2.0.0',paths:{plotly:'https://cdn.plot.ly/plotly-1.49.2.min'}});
xplotRequire(['plotly'], function(Plotly) {");

newScript.Append(scriptNode.InnerText);
newScript.AppendLine(@"});");
newScript.AppendLine(@"});
};
if ((typeof(requirejs) !== typeof(Function)) || (typeof(requirejs.config) !== typeof(Function))) {
var script = document.createElement(""script"");
script.setAttribute(""src"", ""https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"");
script.onload = function(){
renderPlotly();
};
document.getElementsByTagName(""head"")[0].appendChild(script);
}
else {
renderPlotly();
}");
newScript.AppendLine("</script>");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use PocketView rather than add a dependency on HtmlAgilityPack?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need parsing anyway to select the div and script element from the xplot html

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't they just be rendered as separate script elements?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The script tag injects the other script element conditionally if requrirejs is not defined, they cannot be 2 tags

return HtmlNode.CreateNode(newScript.ToString());
}
Expand Down