-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from zadjii-msft/dev/migrie/f/TRA-forms-pr
Get the other two types of pages back in. Form pages and Markdown pages. ![image](https://github.com/user-attachments/assets/27d10b91-6920-421e-912b-a64ca26baf93) When forms fail to parse, we'll display the exception by replacing the card with one of our one authoring ![image](https://github.com/user-attachments/assets/e3e74d7c-d6da-4d8a-b91b-d66aff8b3d9c) Markdown pages support multiple bodies, and possibly details: ![image](https://github.com/user-attachments/assets/8abb36e9-d3aa-4d1a-b045-d34d84fe16e6) ![image](https://github.com/user-attachments/assets/047d4bdd-e356-4a70-b62d-4ccd678b7285) Ref #73
- Loading branch information
Showing
29 changed files
with
1,348 additions
and
47 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
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
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
145 changes: 145 additions & 0 deletions
145
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormViewModel.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,145 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Text.Json; | ||
using AdaptiveCards.ObjectModel.WinUI3; | ||
using AdaptiveCards.Templating; | ||
using CommunityToolkit.Mvvm.Messaging; | ||
using Microsoft.CmdPal.Extensions; | ||
using Microsoft.CmdPal.UI.ViewModels.Messages; | ||
using Microsoft.CmdPal.UI.ViewModels.Models; | ||
using Windows.Data.Json; | ||
|
||
namespace Microsoft.CmdPal.UI.ViewModels; | ||
|
||
public partial class FormViewModel(IForm _form, IPageContext context) : ExtensionObjectViewModel(context) | ||
{ | ||
private readonly ExtensionObject<IForm> _formModel = new(_form); | ||
|
||
// Remember - "observable" properties from the model (via PropChanged) | ||
// cannot be marked [ObservableProperty] | ||
public string TemplateJson { get; protected set; } = "{}"; | ||
|
||
public string StateJson { get; protected set; } = "{}"; | ||
|
||
public string DataJson { get; protected set; } = "{}"; | ||
|
||
public AdaptiveCardParseResult? Card { get; private set; } | ||
|
||
public override void InitializeProperties() | ||
{ | ||
var model = _formModel.Unsafe; | ||
if (model == null) | ||
{ | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
TemplateJson = model.TemplateJson(); | ||
StateJson = model.StateJson(); | ||
DataJson = model.DataJson(); | ||
|
||
AdaptiveCardTemplate template = new(TemplateJson); | ||
var cardJson = template.Expand(DataJson); | ||
Card = AdaptiveCard.FromJsonString(cardJson); | ||
} | ||
catch (Exception e) | ||
{ | ||
// If we fail to parse the card JSON, then display _our own card_ | ||
// with the exception | ||
AdaptiveCardTemplate template = new(ErrorCardJson); | ||
|
||
// todo: we could probably stick Card.Errrors in there too | ||
var dataJson = $$""" | ||
{ | ||
"error_message": {{JsonSerializer.Serialize(e.Message)}}, | ||
"error_stack": {{JsonSerializer.Serialize(e.StackTrace)}}, | ||
"inner_exception": {{JsonSerializer.Serialize(e.InnerException?.Message)}}, | ||
"template_json": {{JsonSerializer.Serialize(TemplateJson)}}, | ||
"data_json": {{JsonSerializer.Serialize(DataJson)}} | ||
} | ||
"""; | ||
var cardJson = template.Expand(dataJson); | ||
Card = AdaptiveCard.FromJsonString(cardJson); | ||
} | ||
|
||
UpdateProperty(nameof(Card)); | ||
} | ||
|
||
public void HandleSubmit(IAdaptiveActionElement action, JsonObject inputs) | ||
{ | ||
if (action is AdaptiveOpenUrlAction openUrlAction) | ||
{ | ||
WeakReferenceMessenger.Default.Send<LaunchUriMessage>(new(openUrlAction.Url)); | ||
return; | ||
} | ||
|
||
if (action is AdaptiveSubmitAction or AdaptiveExecuteAction) | ||
{ | ||
// Get the data and inputs | ||
// var data = submitAction.DataJson.Stringify(); | ||
var inputString = inputs.Stringify(); | ||
|
||
// _ = data; | ||
_ = inputString; | ||
|
||
try | ||
{ | ||
var model = _formModel.Unsafe!; | ||
if (model != null) | ||
{ | ||
var result = model.SubmitForm(inputString); | ||
|
||
// TODO Handle results | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
PageContext.ShowException(ex); | ||
} | ||
} | ||
} | ||
|
||
private static readonly string ErrorCardJson = """ | ||
{ | ||
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json", | ||
"type": "AdaptiveCard", | ||
"version": "1.5", | ||
"body": [ | ||
{ | ||
"type": "TextBlock", | ||
"text": "Error parsing form from extension", | ||
"wrap": true, | ||
"style": "heading", | ||
"size": "ExtraLarge", | ||
"weight": "Bolder", | ||
"color": "Attention" | ||
}, | ||
{ | ||
"type": "TextBlock", | ||
"wrap": true, | ||
"text": "${error_message}", | ||
"color": "Attention" | ||
}, | ||
{ | ||
"type": "TextBlock", | ||
"text": "${error_stack}", | ||
"fontType": "Monospace" | ||
}, | ||
{ | ||
"type": "TextBlock", | ||
"wrap": true, | ||
"text": "Inner exception:" | ||
}, | ||
{ | ||
"type": "TextBlock", | ||
"wrap": true, | ||
"text": "${inner_exception}", | ||
"color": "Attention" | ||
} | ||
] | ||
} | ||
"""; | ||
} |
88 changes: 88 additions & 0 deletions
88
src/modules/cmdpal/Microsoft.CmdPal.UI.ViewModels/FormsPageViewModel.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,88 @@ | ||
// Copyright (c) Microsoft Corporation | ||
// The Microsoft Corporation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.ObjectModel; | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using Microsoft.CmdPal.Extensions; | ||
using Microsoft.CmdPal.UI.ViewModels.Models; | ||
|
||
namespace Microsoft.CmdPal.UI.ViewModels; | ||
|
||
public partial class FormsPageViewModel : PageViewModel | ||
{ | ||
private readonly ExtensionObject<IFormPage> _model; | ||
|
||
[ObservableProperty] | ||
public partial ObservableCollection<FormViewModel> Forms { get; set; } = []; | ||
|
||
// Remember - "observable" properties from the model (via PropChanged) | ||
// cannot be marked [ObservableProperty] | ||
public FormsPageViewModel(IFormPage model, TaskScheduler scheduler) | ||
: base(model, scheduler) | ||
{ | ||
_model = new(model); | ||
} | ||
|
||
//// Run on background thread, from InitializeAsync or Model_ItemsChanged | ||
private void FetchForms() | ||
{ | ||
try | ||
{ | ||
var newItems = _model.Unsafe!.Forms(); | ||
|
||
Forms.Clear(); | ||
|
||
foreach (var item in newItems) | ||
{ | ||
FormViewModel viewModel = new(item, this); | ||
viewModel.InitializeProperties(); | ||
Forms.Add(viewModel); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
ShowException(ex); | ||
throw; | ||
} | ||
} | ||
|
||
public override void InitializeProperties() | ||
{ | ||
base.InitializeProperties(); | ||
|
||
var listPage = _model.Unsafe; | ||
if (listPage == null) | ||
{ | ||
return; // throw? | ||
} | ||
|
||
FetchForms(); | ||
} | ||
|
||
protected override void FetchProperty(string propertyName) | ||
{ | ||
base.FetchProperty(propertyName); | ||
|
||
var model = this._model.Unsafe; | ||
if (model == null) | ||
{ | ||
return; // throw? | ||
} | ||
|
||
// Do we really not have any here? | ||
|
||
// Should `Forms` be observable? That was what ended up footgunning widgets in DevHome, so :shurg: | ||
|
||
// switch (propertyName) | ||
// { | ||
// case nameof(ShowDetails): | ||
// this.ShowDetails = model.ShowDetails; | ||
// break; | ||
// case nameof(PlaceholderText): | ||
// this.PlaceholderText = model.PlaceholderText; | ||
// break; | ||
// } | ||
UpdateProperty(propertyName); | ||
} | ||
} |
Oops, something went wrong.