-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
263 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
Contentful.Core.Tests/JsonFiles/EditorInterfaceWithMetadata.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
{ | ||
"controls": [ | ||
{ | ||
"fieldId": "field1", | ||
"widgetId": "singleLine" | ||
}, | ||
{ | ||
"fieldId": "field2", | ||
"widgetId": "locationEditor" | ||
}, | ||
{ | ||
"fieldId": "field3", | ||
"widgetId": "markdown" | ||
}, | ||
{ | ||
"fieldId": "field4", | ||
"widgetId": "assetLinkEditor" | ||
}, | ||
{ | ||
"fieldId": "field5", | ||
"widgetId": "boolean", | ||
"settings": { | ||
"helpText": "Should the post be featured on the homepage?", | ||
"trueLabel": "absolutely", | ||
"falseLabel": "rather not" | ||
} | ||
}, | ||
{ | ||
"fieldId": "field6", | ||
"widgetId": "rating", | ||
"settings": { | ||
"helpText": "How many do you likez?", | ||
"stars": "7" | ||
} | ||
}, | ||
{ | ||
"fieldId": "field7", | ||
"widgetId": "datePicker", | ||
"settings": { | ||
"helpText": "When do we should getz?", | ||
"format": "time", | ||
"ampm": "12" | ||
} | ||
} | ||
], | ||
"editorLayout": [ | ||
{ | ||
"groupId": "content", | ||
"name": "Content", | ||
"items": [ | ||
{ "fieldId": "title" }, | ||
{ "fieldId": "body" } | ||
] | ||
}, | ||
{ | ||
"groupId": "settings", | ||
"name": "Settings", | ||
"items": [ | ||
{ | ||
"groupId": "seo", | ||
"name": "Seo", | ||
"items": [ { "fieldId": "slug" } ] | ||
} | ||
] | ||
} | ||
], | ||
"groupControls": [ | ||
{ | ||
"groupId": "content", | ||
"widgetId": "topLevelTab", | ||
"widgetNamespace": "builtin" | ||
}, | ||
{ | ||
"groupId": "settings", | ||
"widgetId": "topLevelTab", | ||
"widgetNamespace": "builtin" | ||
} | ||
], | ||
"sys": { | ||
"id": "default", | ||
"type": "EditorInterface", | ||
"version": 1, | ||
"createdAt": "2016-11-23T07:37:45.356Z", | ||
"createdBy": { | ||
"sys": { | ||
"type": "Link", | ||
"linkType": "User", | ||
"id": "0w7AzFDgzUA8MWo79R1Qf7" | ||
} | ||
}, | ||
"space": { | ||
"sys": { | ||
"type": "Link", | ||
"linkType": "Space", | ||
"id": "gw484zixu8ng" | ||
} | ||
}, | ||
"contentType": { | ||
"sys": { | ||
"type": "Link", | ||
"linkType": "ContentType", | ||
"id": "testagain" | ||
} | ||
}, | ||
"updatedAt": "2016-11-23T07:37:45.357Z", | ||
"updatedBy": { | ||
"sys": { | ||
"type": "Link", | ||
"linkType": "User", | ||
"id": "0w7AzFDgzUA8MWo79R1Qf7" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Contentful.Core/Configuration/EditorLayoutGroupJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using Contentful.Core.Models; | ||
using Contentful.Core.Models.Management; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Contentful.Core.Configuration | ||
{ | ||
/// <summary> | ||
/// JsonConverter for converting <see cref="Contentful.Core.Models.Management.EditorInterface"/>. | ||
/// </summary> | ||
public class EditorInterfaceControlJsonConverter : JsonConverter | ||
{ | ||
/// <summary> | ||
/// Determines whether this instance can convert the specified object type. | ||
/// </summary> | ||
/// <param name="objectType">The type to convert to.</param> | ||
public override bool CanConvert(Type objectType) => objectType == typeof(EditorInterfaceControl); | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether this JsonConverter can write JSON. | ||
/// </summary> | ||
public override bool CanWrite => false; | ||
|
||
/// <summary> | ||
/// Reads the JSON representation of the object. | ||
/// </summary> | ||
/// <param name="reader">The reader to use.</param> | ||
/// <param name="objectType">The object type to serialize into.</param> | ||
/// <param name="existingValue">The current value of the property.</param> | ||
/// <param name="serializer">The serializer to use.</param> | ||
/// <returns>The deserialized <see cref="Asset"/>.</returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
if (reader.TokenType == JsonToken.Null) | ||
return null; | ||
|
||
var jsonObject = JObject.Load(reader); | ||
var settings = new EditorInterfaceControlSettings(); | ||
|
||
var editorInterfaceControl = new EditorInterfaceControl() | ||
{ | ||
FieldId = jsonObject["fieldId"]?.ToString(), | ||
WidgetId = jsonObject["widgetId"]?.ToString() | ||
}; | ||
|
||
if (jsonObject["settings"] == null) | ||
{ | ||
return editorInterfaceControl; | ||
} | ||
|
||
if (jsonObject["widgetId"]?.ToString() == "boolean") | ||
{ | ||
var boolSettings = new BooleanEditorInterfaceControlSettings() | ||
{ | ||
FalseLabel = jsonObject["settings"]?["falseLabel"]?.ToString(), | ||
TrueLabel = jsonObject["settings"]?["trueLabel"]?.ToString() | ||
}; | ||
settings = boolSettings; | ||
} | ||
|
||
if (jsonObject["widgetId"]?.ToString() == "rating") | ||
{ | ||
var ratingSettings = new RatingEditorInterfaceControlSettings(); | ||
|
||
var stars = 0; | ||
|
||
if (!int.TryParse(jsonObject["settings"]?["stars"]?.ToString(), out stars)) | ||
{ | ||
stars = 5; | ||
} | ||
|
||
ratingSettings.NumberOfStars = stars; | ||
|
||
settings = ratingSettings; | ||
} | ||
|
||
if (jsonObject["widgetId"]?.ToString() == "datePicker") | ||
{ | ||
var dateSettings = new DatePickerEditorInterfaceControlSettings() | ||
{ | ||
ClockFormat = jsonObject["settings"]?["ampm"]?.ToString(), | ||
DateFormat = (EditorInterfaceDateFormat)Enum.Parse(typeof(EditorInterfaceDateFormat), jsonObject["settings"]?["format"]?.ToString(), true) | ||
}; | ||
settings = dateSettings; | ||
} | ||
|
||
settings.HelpText = jsonObject["settings"]?["helpText"]?.ToString(); | ||
|
||
editorInterfaceControl.Settings = settings; | ||
|
||
return editorInterfaceControl; | ||
} | ||
|
||
/// <summary> | ||
/// Writes the JSON representation of the object. | ||
/// **NOTE: This method is not implemented and will throw an exception.** | ||
/// </summary> | ||
/// <param name="writer"></param> | ||
/// <param name="value"></param> | ||
/// <param name="serializer"></param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.