Skip to content
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

[AGD-1882] Support storing user defined graph documentation #11444

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
42 changes: 42 additions & 0 deletions src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class HomeWorkspaceModel : WorkspaceModel
{
#region Class Data Members and Properties

private string thumbnail;
private Uri graphDocumentationURL;
private readonly DynamoScheduler scheduler;
private PulseMaker pulseMaker;
private readonly bool verboseLogging;
Expand Down Expand Up @@ -89,6 +91,46 @@ public class HomeWorkspaceModel : WorkspaceModel
[JsonIgnore]
public long EvaluationCount { get; private set; }

/// <summary>
/// Link to documentation page for this workspace
/// </summary>
public Uri GraphDocumentationURL
{
get { return graphDocumentationURL; }
set
{
if (graphDocumentationURL == value)
return;

graphDocumentationURL = value;
RaisePropertyChanged(nameof(GraphDocumentationURL));
}
}


/// <summary>
/// Workspace thumbnail as Base64 string.
/// Returns null if provide value is not Base64 encoded.
/// </summary>
public string Thumbnail
{
get { return thumbnail; }
set
{
try
{
// if value is not a valid Base64 string this will throw, and we return null.
byte[] data = Convert.FromBase64String(value);
thumbnail = value;
RaisePropertyChanged(nameof(Thumbnail));
}
catch
{
return;
}
}
}

/// <summary>
/// In near future, the file loading mechanism will be completely moved
/// into WorkspaceModel, that's the time we removed this property setter below.
Expand Down
33 changes: 32 additions & 1 deletion src/DynamoCore/Graph/Workspaces/SerializationConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,30 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
}
else
{
ws = new HomeWorkspaceModel(guid, engine, scheduler, factory,
var homeWorkspace = new HomeWorkspaceModel(guid, engine, scheduler, factory,
loadedTraceData, nodes, notes, annotations,
Enumerable.Empty<PresetModel>(), elementResolver,
info, verboseLogging, isTestMode);

if (obj.TryGetValue(nameof(HomeWorkspaceModel.Thumbnail), StringComparison.OrdinalIgnoreCase, out JToken thumbnail))
homeWorkspace.Thumbnail = thumbnail.ToString();

if (obj.TryGetValue(nameof(HomeWorkspaceModel.GraphDocumentationURL), StringComparison.OrdinalIgnoreCase, out JToken helpLink))
{
if (Uri.TryCreate(helpLink.ToString(), UriKind.Absolute, out Uri uri))
homeWorkspace.GraphDocumentationURL = uri;
}

ws = homeWorkspace;
}

ws.NodeLibraryDependencies = nodeLibraryDependencies.ToList();
ws.ExtensionData = GetExtensionData(serializer, obj);

if (obj.TryGetValue(nameof(WorkspaceModel.Author), StringComparison.OrdinalIgnoreCase, out JToken author))
ws.Author = author.ToString();


return ws;
}

Expand Down Expand Up @@ -771,6 +786,22 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
writer.WritePropertyName(WorkspaceReadConverter.EXTENSION_WORKSPACE_DATA);
serializer.Serialize(writer, ws.ExtensionData);


if (!isCustomNode && ws is HomeWorkspaceModel hws)
{
// Thumbnail
writer.WritePropertyName(nameof(HomeWorkspaceModel.Thumbnail));
writer.WriteValue(hws.Thumbnail);

// GraphDocumentaionLink
writer.WritePropertyName(nameof(HomeWorkspaceModel.GraphDocumentationURL));
writer.WriteValue(hws.GraphDocumentationURL);
}

// Graph Author
writer.WritePropertyName(nameof(WorkspaceModel.Author));
writer.WriteValue(ws.Author);

if (engine != null)
{
// Bindings
Expand Down
4 changes: 2 additions & 2 deletions src/DynamoCore/Graph/Workspaces/WorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ internal List<INodeLibraryDependencyInfo> NodeLibraryDependencies
}

private Dictionary<Guid, PackageInfo> nodePackageDictionary = new Dictionary<Guid, PackageInfo>();


/// <summary>
/// An author of the workspace
Expand All @@ -707,7 +707,7 @@ public string Author
set
{
author = value;
RaisePropertyChanged("Author");
RaisePropertyChanged(nameof(Author));
}
}

Expand Down
35 changes: 35 additions & 0 deletions test/DynamoCoreTests/Graph/Workspaces/WorkspaceModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,40 @@ public void UpdateModelValueInvalidParameters()
Assert.AreEqual(Resources.ModelNotFoundError, ex3.Message);

}

[Test]
public void CanStoreBase64EncodedImageInThumbnailProperty()
{
// Arrange
var imagePath = Path.Combine(TestDirectory, @"DynamoCoreTests\Graph\Workspaces\thumbnailTestImage.png");
Assert.That(File.Exists(imagePath));

// Act
byte[] imageArray = System.IO.File.ReadAllBytes(imagePath);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
if (!(this.CurrentDynamoModel.CurrentWorkspace is HomeWorkspaceModel hws))
throw new Exception("current workspace is not a HomeWorkspaceModel");

hws.Thumbnail = base64ImageRepresentation;

// Assert
Assert.NotNull(base64ImageRepresentation);
Assert.AreEqual(hws.Thumbnail, base64ImageRepresentation);
}

[Test]
public void WillNotStoreInvalidBase64StringInThumbnailProperty()
{
// Arrange
var invalidImagePath = "GenericString";

// Act
if (!(this.CurrentDynamoModel.CurrentWorkspace is HomeWorkspaceModel hws))
throw new Exception("current workspace is not a HomeWorkspaceModel");
hws.Thumbnail = invalidImagePath;

// Assert
Assert.IsNull(hws.Thumbnail);
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions test/core/dummy_node/2080_JSONTESTCRASH undo_redo.dyn
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,16 @@
"Connectors": [],
"Dependencies": [],
"NodeLibraryDependencies": [],
"Thumbnail": "",
"GraphDocumentationURL": null,
"Author": "None provided",
"Bindings": [],
"View": {
"Dynamo": {
"ScaleFactor": 1.0,
"HasRunWithoutCrash": true,
"IsVisibleInDynamoLibrary": true,
"Version": "2.3.0.5184",
"Version": "2.11.0.4415",
"RunType": "Automatic",
"RunPeriod": "1000"
},
Expand All @@ -77,21 +80,21 @@
},
"NodeViews": [
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "158f8f25ddb746c88323ae262e87611e",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Name": "Code Block",
"ShowGeometry": true,
"Excluded": false,
"X": 179.8,
"Y": 160.39999999999992
},
{
"ShowGeometry": true,
"Name": "Code Block",
"Id": "6cbd0760736f4235a03b38fb359e9957",
"IsSetAsInput": false,
"IsSetAsOutput": false,
"Name": "Code Block",
"ShowGeometry": true,
"Excluded": false,
"X": 76.8,
"Y": 162.4
Expand Down