Skip to content

Commit

Permalink
Update refactored UI hint handling (#97)
Browse files Browse the repository at this point in the history
* Remove Elsa.Studio.UIHints.Models namespace and refactor dependent classes

The Elsa.Studio.UIHints.Models namespace and related classes (SelectList.cs, SelectListItem.cs, and related JSON converters) are removed. Code that relied on these classes is updated to use the Elsa.Api.Client.Shared.UIHints.DropDown namespace instead. The project reference in Elsa.Studio.Core.csproj is updated to reference Elsa.Api.Client instead of a specific version of the package. Other minor code adjustments and cleanup are performed throughout the codebase to ensure consistency and correct functioning after the deletion and refactoring.

* Update ExpressionType assignment in Cases component

The code now assigns "ExpressionType" in the Cases component based on the case condition's type. If the case condition's type is null or whitespace, "defaultExpressionType" is used instead. This adjustment enhances code reliability by avoiding possible issues related to undefined or empty types.

* Update Elsa.Api.Client reference in Elsa.Studio.Core

The project reference to Elsa.Api.Client in the Elsa.Studio.Core project file has been replaced by a package reference. This change transitions from direct project dependency to NuGet package dependency, likely to improve version management and distribution.

* Remove CheckListItem model, update UIHints

The CheckListItem model from Elsa.Studio.UIHints.Models was removed, which affected various components and the project structure. Components were adjusted accordingly, and references to Elsa.Studio.UIHints.Models.CheckListItem were eliminated. UIHint handler names were updated to remove hyphenation (e.g., "check-list" became "checklist").

* Remove unused Models folder from Elsa.Studio.UIHints project

The commit removes the reference to the Models folder in the Elsa.Studio.UIHints.csproj file. This folder was not in use, resulting in cleaner and easier to maintain code.
  • Loading branch information
sfmskywalker authored Dec 26, 2023
1 parent f709571 commit 7386df0
Show file tree
Hide file tree
Showing 26 changed files with 64 additions and 205 deletions.
2 changes: 1 addition & 1 deletion src/framework/Elsa.Studio.Core/Elsa.Studio.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<!-- </ItemGroup>-->

<ItemGroup Label="Elsa">
<PackageReference Include="Elsa.Api.Client" Version="3.0.0-preview.904"/>
<PackageReference Include="Elsa.Api.Client" Version="3.0.0-preview.912"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override IEnumerable<Port> GetPorts(PortProviderContext context)

private static DynamicOutcomesOptions? GetDynamicOutcomeOptions(InputDescriptor dynamicOutcomesInputDescriptor)
{
if (dynamicOutcomesInputDescriptor.Options?.TryGetValue(nameof(DynamicOutcomesOptions), out var dynamicOutcomesOptions) != true)
if (dynamicOutcomesInputDescriptor.UISpecifications?.TryGetValue(nameof(DynamicOutcomesOptions), out var dynamicOutcomesOptions) != true)
return default;

var serializerOptions = CreateSerializerOptions();
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Elsa.Studio.UIHints/Components/Cases.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private SwitchCaseRecord Map(SwitchCase @case)
{
Label = @case.Label,
Condition = @case.Condition.ToString(),
ExpressionType = defaultExpressionType,
ExpressionType = string.IsNullOrWhiteSpace(@case.Condition.Type) ? defaultExpressionType : @case.Condition.Type,
Activity = @case.Activity
};
}
Expand Down
23 changes: 12 additions & 11 deletions src/modules/Elsa.Studio.UIHints/Components/CheckList.razor.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Text.Json;
using Elsa.Api.Client.Resources.Scripting.Models;
using Elsa.Api.Client.Shared.UIHints.CheckList;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Extensions;
using Elsa.Studio.UIHints.Helpers;
using Elsa.Studio.UIHints.Models;
using Microsoft.AspNetCore.Components;

namespace Elsa.Studio.UIHints.Components;
Expand All @@ -18,16 +18,17 @@ public partial class CheckList
/// <summary>
/// The editor context.
/// </summary>
[Parameter] public DisplayInputEditorContext EditorContext { get; set; } = default!;
[Parameter]
public DisplayInputEditorContext EditorContext { get; set; } = default!;

/// <inheritdoc />
protected override void OnInitialized()
{
var selectList = EditorContext.InputDescriptor.GetSelectList();
var selectedValues = GetSelectedValues(selectList.IsFlagsEnum);
var checkList = EditorContext.InputDescriptor.GetCheckList();
var selectedValues = GetSelectedValues(checkList.IsFlagsEnum);

_checkListItems = selectList.Items
.Select(i => new CheckListItem(i.Value, i.Text, selectedValues.Contains(i.Value)))
_checkListItems = checkList.Items
.Select(i => new CheckListItem { Value = i.Value, Text = i.Text, IsChecked = selectedValues.Contains(i.Value) })
.ToList();
}

Expand All @@ -41,8 +42,8 @@ private ICollection<string> GetSelectedValues(bool isFlagsEnum)
if (isFlagsEnum)
{
return !string.IsNullOrWhiteSpace(json)
? int.TryParse(json, out var n) ? new[] { n.ToString() } : Array.Empty<string>()
: new[] { defaultValue?.ToString() ?? string.Empty };
? int.TryParse(json, out var n) ? [n.ToString()] : Array.Empty<string>()
: [defaultValue?.ToString() ?? string.Empty];
}

var selectListItems = ParseJson(json);
Expand All @@ -62,13 +63,13 @@ private async Task OnCheckedChanged(CheckListItem item, bool? state)
{
// Toggle state.
item.IsChecked = state == true;

// Get selected values.
var selectedValues = _checkListItems.Where(x => x.IsChecked).Select(x => x.Value).ToList();

// Serialize to JSON.
var json = JsonSerializer.Serialize(selectedValues);

// Update expression.
var expression = Expression.CreateObject(json);
await EditorContext.UpdateExpressionAsync(expression);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Elsa.Studio.UIHints/Components/Dropdown.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Elsa.Studio.UIHints.Models
@using Elsa.Api.Client.Shared.UIHints.DropDown
@inherits StudioComponentBase
@{
var inputDescriptor = EditorContext.InputDescriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Elsa.Api.Client.Resources.Scripting.Models;
using Elsa.Api.Client.Shared.UIHints.DropDown;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Extensions;
using Elsa.Studio.UIHints.Models;
using Microsoft.AspNetCore.Components;

namespace Elsa.Studio.UIHints.Components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Elsa.Studio.UIHints.Models
@using Elsa.Api.Client.Shared.UIHints.DropDown
@{
var inputDescriptor = EditorContext.InputDescriptor;
var displayName = inputDescriptor.DisplayName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Elsa.Api.Client.Resources.Scripting.Models;
using Elsa.Api.Client.Shared.UIHints.DropDown;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Models;
using Microsoft.AspNetCore.Components;

namespace Elsa.Studio.UIHints.Components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Elsa.Studio.UIHints.Models
@using Elsa.Api.Client.Shared.UIHints.DropDown
@{
var inputDescriptor = EditorContext.InputDescriptor;
var displayName = inputDescriptor.DisplayName;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Elsa.Api.Client.Resources.Scripting.Models;
using Elsa.Api.Client.Resources.WorkflowDefinitions.Models;
using Elsa.Api.Client.Shared.UIHints.DropDown;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Models;
using Microsoft.AspNetCore.Components;

namespace Elsa.Studio.UIHints.Components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Elsa.Studio.UIHints.Models
@using Elsa.Api.Client.Shared.UIHints.DropDown
@inherits StudioComponentBase
@{
var inputDescriptor = EditorContext.InputDescriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Elsa.Api.Client.Resources.WorkflowDefinitions.Models;
using Elsa.Api.Client.Shared.UIHints.DropDown;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Models;
using Microsoft.AspNetCore.Components;

namespace Elsa.Studio.UIHints.Components;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@using Elsa.Studio.UIHints.Models
@using Elsa.Api.Client.Shared.UIHints.DropDown
@inherits StudioComponentBase
@{
var inputDescriptor = EditorContext.InputDescriptor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Elsa.Api.Client.Resources.Scripting.Models;
using Elsa.Api.Client.Resources.WorkflowDefinitions.Responses;
using Elsa.Api.Client.Shared.Models;
using Elsa.Api.Client.Shared.UIHints.DropDown;
using Elsa.Studio.Models;
using Elsa.Studio.UIHints.Models;
using Elsa.Studio.Workflows.Domain.Contracts;
using Microsoft.AspNetCore.Components;

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Text.Json;
using Elsa.Api.Client.Extensions;
using Elsa.Api.Client.Resources.ActivityDescriptors.Models;
using Elsa.Studio.UIHints.Converters;
using Elsa.Studio.UIHints.Models;
using Elsa.Api.Client.Shared.UIHints.CheckList;
using Elsa.Api.Client.Shared.UIHints.DropDown;

namespace Elsa.Studio.UIHints.Extensions;

Expand All @@ -16,45 +15,38 @@ public static class InputDescriptorExtensions
/// </summary>
public static SelectList GetSelectList(this InputDescriptor descriptor)
{
var options = descriptor.Options;
var specifications = descriptor.UISpecifications;
var props = specifications != null ? specifications.TryGetValue("dropdown", out var propsValue) ? propsValue is JsonElement value ? value : default : default : default;

var selectListOptions = options?.TryGetValue("items", out var selectList) == true
? selectList is JsonElement list
? list
: default
: default;

if (options == null || selectListOptions.ValueKind == JsonValueKind.Null)
return new SelectList(new List<SelectListItem>(), false);
if (props.ValueKind == JsonValueKind.Undefined)
return new SelectList(new List<SelectListItem>());

var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

serializerOptions.Converters.Add(new SelectListJsonConverter());
var dropDownProps = props.Deserialize<DropDownProps>(serializerOptions);
return dropDownProps?.SelectList ?? new SelectList(new List<SelectListItem>(), false);
}

if (selectListOptions.ValueKind == JsonValueKind.Object)
{
if (selectListOptions.TryGetPropertySafe("items", out var items))
{
return new SelectList(items.Deserialize<List<SelectListItem>>(serializerOptions)!, false);
}

if (selectListOptions.TryGetPropertySafe("provider", out var provider))
{
// TODO: Invoke remote provider
return new SelectList(new List<SelectListItem>(), false);
}
}

if (selectListOptions.ValueKind == JsonValueKind.Array)
/// <summary>
/// Gets a list of <see cref="CheckListItem"/>s for the specified <see cref="InputDescriptor"/>.
/// </summary>
public static CheckList GetCheckList(this InputDescriptor descriptor)
{
var specifications = descriptor.UISpecifications;
var props = specifications != null ? specifications.TryGetValue("checklist", out var propsValue) ? propsValue is JsonElement value ? value : default : default : default;

if (props.ValueKind == JsonValueKind.Undefined)
return new CheckList(Array.Empty<CheckListItem>());

var serializerOptions = new JsonSerializerOptions
{
serializerOptions.Converters.Add(new SelectListItemJsonConverter());
var items = selectListOptions.Deserialize<List<SelectListItem>>(serializerOptions)!;
return new SelectList(items, false);
}
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};

return new SelectList(new List<SelectListItem>(), false);
var checkListProps = props.Deserialize<CheckListProps>(serializerOptions);
return checkListProps?.CheckList ?? new CheckList(Array.Empty<CheckListItem>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static IServiceCollection AddDefaultUIHintHandlers(this IServiceCollectio
.AddUIHintHandler<CheckListHandler>()
.AddUIHintHandler<MultiTextHandler>()
.AddUIHintHandler<MultiLineHandler>()
.AddUIHintHandler<DropdownHandler>()
.AddUIHintHandler<DropDownHandler>()
.AddUIHintHandler<CodeEditorHandler>()
.AddUIHintHandler<SwitchEditorHandler>()
.AddUIHintHandler<HttpStatusCodesHandler>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Elsa.Studio.UIHints.Handlers;

public class CheckListHandler : IUIHintHandler
{
public bool GetSupportsUIHint(string uiHint) => uiHint == "check-list";
public bool GetSupportsUIHint(string uiHint) => uiHint == "checklist";
public string UISyntax => WellKnownSyntaxNames.Object;

public RenderFragment DisplayInputEditor(DisplayInputEditorContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@

namespace Elsa.Studio.UIHints.Handlers;

public class DropdownHandler : IUIHintHandler
/// <summary>
/// Represents a class that handles UI hints for dropdown inputs.
/// </summary>
public class DropDownHandler : IUIHintHandler
{
/// <inheritdoc />
public bool GetSupportsUIHint(string uiHint) => uiHint == "dropdown";

/// <inheritdoc />
public string UISyntax => WellKnownSyntaxNames.Literal;

/// <inheritdoc />
public RenderFragment DisplayInputEditor(DisplayInputEditorContext context)
{
return builder =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Elsa.Studio.UIHints.Handlers;

public class MultiLineHandler : IUIHintHandler
{
public bool GetSupportsUIHint(string uiHint) => uiHint == "multi-line";
public bool GetSupportsUIHint(string uiHint) => uiHint == "multiline";
public string UISyntax => WellKnownSyntaxNames.Literal;

public RenderFragment DisplayInputEditor(DisplayInputEditorContext context)
Expand Down
Loading

0 comments on commit 7386df0

Please sign in to comment.