Skip to content

Commit

Permalink
DYN-5497-Markdown-NotLoading CodeReview2
Browse files Browse the repository at this point in the history
The Name property was named to packageName
A new test was added that is validating that the virtual folder will be created in the packages/package/doc folder so when the documentation for a node is loaded the images are loaded successfully.
Also the code for setting the VirtualFolderPath property was moved before WebView2.EnsureCoreWebView2Async() call because otherwise we were not reaching the code.
  • Loading branch information
RobertGlobant20 committed Jan 26, 2023
1 parent 0153528 commit 0e3c928
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public partial class DocumentationBrowserView : UserControl, IDisposable
internal string WebBrowserUserDataFolder { get; set; }
internal string FallbackDirectoryName { get; set; }

//Path in which the virtual folder for loading images will be created
internal string VirtualFolderPath { get; set; }

/// <summary>
/// Construct a new DocumentationBrowserView given an appropriate viewmodel.
/// </summary>
Expand Down Expand Up @@ -130,7 +133,21 @@ protected virtual void Dispose(bool disposing)

async void InitializeAsync()
{
string virtualFolder = string.Empty;
VirtualFolderPath = string.Empty;
try
{
if (viewModel.Link != null && !string.IsNullOrEmpty(viewModel.PackageName))
{
VirtualFolderPath = HttpUtility.UrlDecode(viewModel.Link.AbsolutePath).Replace(viewModel.PackageName + ".md", "");
}
else
VirtualFolderPath = FallbackDirectoryName;
}
catch (Exception ex)
{
VirtualFolderPath = string.Empty;
}

// Only initialize once
if (!hasBeenInitialized)
{
Expand All @@ -144,8 +161,8 @@ async void InitializeAsync()
}
//Initialize the CoreWebView2 component otherwise we can't navigate to a web page
await documentationBrowser.EnsureCoreWebView2Async();


this.documentationBrowser.CoreWebView2.WebMessageReceived += CoreWebView2OnWebMessageReceived;
comScriptingObject = new ScriptingObject(this.viewModel);
//register the interop object into the browser.
Expand All @@ -157,23 +174,9 @@ async void InitializeAsync()
hasBeenInitialized = true;
}

try
{
if (viewModel.Link != null && !string.IsNullOrEmpty(viewModel.Name))
{
virtualFolder = HttpUtility.UrlDecode(viewModel.Link.AbsolutePath).Replace(viewModel.Name + ".md", "");
}
else
virtualFolder = FallbackDirectoryName;
}
catch (Exception ex)
{
virtualFolder = string.Empty;
}

if(Directory.Exists(virtualFolder))
if(Directory.Exists(VirtualFolderPath))
//Due that the Web Browser(WebView2 - Chromium) security CORS is blocking the load of resources like images then we need to create a virtual folder in which the image are located.
this.documentationBrowser.CoreWebView2.SetVirtualHostNameToFolderMapping(VIRTUAL_FOLDER_MAPPING, virtualFolder, CoreWebView2HostResourceAccessKind.DenyCors);
this.documentationBrowser.CoreWebView2.SetVirtualHostNameToFolderMapping(VIRTUAL_FOLDER_MAPPING, VirtualFolderPath, CoreWebView2HostResourceAccessKind.DenyCors);

string htmlContent = this.viewModel.GetContent();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,21 +88,21 @@ private set
private Uri link;
private string graphPath;
private string content;
private string name;
private string packageName;


/// <summary>
/// Package Name
/// </summary>
internal string Name
internal string PackageName
{
get
{
return name;
return packageName;
}
set
{
name = value;
packageName = value;
}
}

Expand Down Expand Up @@ -244,7 +244,7 @@ private void HandleLocalResource(OpenDocumentationLinkEventArgs e)
{
this.content = targetContent;
this.graphPath = graph;
this.name = graphName;
this.packageName = graphName;
this.Link = link;
}
}
Expand Down Expand Up @@ -431,7 +431,7 @@ internal void InsertGraph()
{
if (graphPath != null)
{
var graphName = this.name ?? Path.GetFileNameWithoutExtension(graphPath);
var graphName = this.packageName ?? Path.GetFileNameWithoutExtension(graphPath);
raiseInsertGraph(this, new InsertDocumentationLinkEventArgs(graphPath, graphName));
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,48 @@ public void ShowingStartPageHidesBrowser()
Assert.AreEqual(Visibility.Hidden, visibilityAfterShowStartPageEvent);
}

/// <summary>
/// This test validates that the Virtual Directory that will be created with WebView2 exists so the images will be loaded for a package node documentation
/// </summary>
[Test]
public void CanCreatePackageNodeDocumentationAndLoadImages()
{
// Arrange
RaiseLoadedEvent(this.View);

var testDirectory = GetTestDirectory(this.ExecutingDirectory);
var localImagePath = Path.Combine(testDirectory, @"core\docbrowser\pkgs\PackageWithNodeDocumentation\doc\icon.png");
var packageDocPath = Path.GetDirectoryName(localImagePath);

var docBrowserviewExtension = this.View.viewExtensionManager.ViewExtensions.OfType<DocumentationBrowserViewExtension>().FirstOrDefault();
var browserView = docBrowserviewExtension.BrowserView;

var nodeName = "Package.Hello";
var nodeRename = "New node name";
var expectedImageContent = String.Format(@"<img id='drag--img' class='resizable--img' src=""http://appassets/{0}"" alt=""Dynamo Icon image"" />", Path.GetFileName(localImagePath));

// Act
this.ViewModel.ExecuteCommand(
new DynamoModel.CreateNodeCommand(
Guid.NewGuid().ToString(), nodeName, 0, 0, false, false)
);

var node = this.ViewModel.Model.CurrentWorkspace.Nodes.FirstOrDefault();
node.Name = nodeRename; // Forces original name header to appear
var nodeAnnotationEventArgs = new OpenNodeAnnotationEventArgs(node, this.ViewModel);

docBrowserviewExtension.HandleRequestOpenDocumentationLink(nodeAnnotationEventArgs);
var htmlContent = GetSidebarDocsBrowserContents();
htmlContent = htmlContent.Replace(@"%5C", "/");

// Assert
Assert.IsTrue(!string.IsNullOrEmpty(browserView.VirtualFolderPath));
Assert.IsTrue(Directory.Exists(browserView.VirtualFolderPath));
//Check that the virtual folder will be created in the Package/doc folder so images will be loaded correctly
Assert.IsTrue(browserView.VirtualFolderPath.Contains(packageDocPath.Replace("\\", "/")));
Assert.IsTrue(htmlContent.Contains(expectedImageContent));
}

[Test]
public void ViewExtensionIgnoresExternalEvents()
{
Expand Down

0 comments on commit 0e3c928

Please sign in to comment.