Skip to content

Commit 2612e46

Browse files
authored
Merge pull request pnp#1238 from fabiofranzini/adaptive-card-designer-host
Added 'AdaptiveCardDesignerHost' Control
2 parents 3478d79 + d08781c commit 2612e46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+3701
-455
lines changed
Loading
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# Adaptive Card Designer Host
2+
3+
This control allows you to embed the official Adaptive Cards designer inside a React SPFx solution.
4+
5+
The control consists of 2 components:
6+
7+
* **AdaptiveCardDesigner**: implements all the logic to embed the designer control as a React component;
8+
* **AdaptiveCardDesignerHost**: main control to render the designer in a full page Fluent UI panel;
9+
10+
**Due to the nature in which the original Adaptive Card Designer control was implemented**, it is not possible at this time to adapt it to the current theme applied to the site and especially to localize it to give multilingual support. **The designer, therefore, is only available in the English language**.
11+
12+
This control shares a lot of code with another control in this library, the "AdaptiveCardHost" control. In this way you have a uniformity of display between the cards created with this designer and those rendered with "AdaptiveCardHost". The same thing goes for the various HostContainer objects, so that you can test the cards with the themes available for "AdaptiveCardHost".
13+
14+
The Adaptive Cards version supported is 1.5, by using the 'adaptivecards' npm package version 2.10.0.
15+
16+
All Inputs Elements and Actions of Adaptive Cards have been redefined using Fluent UI React, adding and improving features that are not managed in Microsoft's implementation of the "adaptivecards-fluentui" library (Theme support for example).
17+
18+
Thanks to the "context" property that allows you to pass the SPFx context, whether the "data" property is passed or not, a new field called @context will be injected into the data object.
19+
20+
This allows, using Adaptive Cards templating syntax and binding feature of the Designer, to access to the context informations.
21+
22+
For more info please to refear tot he documentation of [AdaptiveCardHost ](http://www.google.com)control
23+
24+
Here is an example of the control in action inside a Web Part:
25+
26+
![Adaptive Card Host control](../assets/AdaptiveCardDesignerHost.gif)
27+
28+
## How to use this control in your solutions
29+
30+
* Check that you installed the `@pnp/spfx-controls-react` dependency. Check out the [getting started](../../#getting-started) page for more information about installing the dependency.
31+
32+
* In your component file, import the `AdaptiveCardDesignerHost` control as follows:
33+
```Typescript
34+
import { AdaptiveCardDesignerHost, HostContainer, BindingPreviewMode, Versions } from "@pnp/spfx-controls-react/lib/AdaptiveCardDesignerHost";
35+
```
36+
37+
- Example on use the `AdaptiveCardDesignerHost` control with only required properties:
38+
```Typescript
39+
<AdaptiveCardDesignerHost
40+
headerText="Adaptive Card Designer"
41+
buttonText="Open the Designer"
42+
context={props.context}
43+
onSave={(payload: object) => setCard(payload)}
44+
/>
45+
```
46+
47+
- Example on use the `AdaptiveCardDesignerHost` control with all properties:
48+
```Typescript
49+
<AdaptiveCardDesignerHost
50+
headerText="Adaptive Card Designer"
51+
buttonText="Open the Designer"
52+
context={props.context}
53+
onSave={(payload: object) => setCard(payload)}
54+
addDefaultAdaptiveCardHostContainer={true}
55+
bindingPreviewMode={BindingPreviewMode.SampleData}
56+
theme={props.theme}
57+
card={card}
58+
data={data}
59+
enableDataBindingSupport={true}
60+
hostConfig={hostConfig}
61+
hostContainers={[]}
62+
injectAdaptiveCardHostContextProperty={true}
63+
newCardPayload={newCard}
64+
selectedHostContainerControlsTargetVersion={false}
65+
showCopyToJsonToolbarCommand={true}
66+
showDataStructureToolbox={true}
67+
showFluentBreakpointsPicker={true}
68+
showSampleDataEditorToolbox={true}
69+
showTargetVersionMismatchWarning={true}
70+
showVersionPicker={true}
71+
supportedTargetVersions={[Versions.v1_5]}
72+
snippets={snippets}
73+
/>
74+
```
75+
## Implementation
76+
77+
The `AdaptiveCardDesignerHost` control can be configured with the following properties:
78+
79+
| Property | Type | Required | Description | Default Value |
80+
| ---- | ---- | ---- | ---- | ---- |
81+
| context | BaseComponentContext | true | Set the context from SPFx component | - |
82+
| theme | IPartialTheme or ITheme | false | Set Fluent UI Theme | - |
83+
| onSave | (payload: object) => void | true | Callback for saving the card | - |
84+
| card | object | false | Set Adaptive Card payload | - |
85+
| data | { "$root": object } | false | Set Data Source for template rendering | - |
86+
| newCardPayload | object | false | Set Adaptive Card payload for the New Card | - |
87+
| hostContainers | HostContainer[] | false | Set custom HostContainers | [] |
88+
| supportedTargetVersions | Version[] | false | Set the suported Versions | [Versions.v1_5] |
89+
| snippets | IToolboxSnippet[] | false | Set the Toolbox Snippets | [] |
90+
| bindingPreviewMode | BindingPreviewMode | false | Set the Binding preview mode | BindingPreviewMode.GeneratedData |
91+
| enableDataBindingSupport | boolean | false | Enable the support for Data Binding | true |
92+
| selectedHostContainerControlsTargetVersion | boolean | false | Enable the support for Data Binding | false |
93+
| showTargetVersionMismatchWarning | boolean | false | Show the target version mismatch warning | true |
94+
| showVersionPicker | boolean | false | Show the Version Picker | false |
95+
| showSampleDataEditorToolbox | boolean | false | Show the Sample Data Editor Toolbox | false |
96+
| showDataStructureToolbox | boolean | false | Show the Data Structure Toolbox | true |
97+
| showFluentBreakpointsPicker | boolean | false | Show the Fluent UI Breakpoint Picker | true |
98+
| showCopyToJsonToolbarCommand | boolean | false | Show the copy to json button | false |
99+
| addDefaultAdaptiveCardHostContainer | boolean | false | Add the default Host Containers to the Picker | true |
100+
| injectAdaptiveCardHostContextProperty | boolean | false | Inject the SPFx Context Property inside the Adaptive Card data object | true |
101+
| headerText | boolean | false | Set the Header text for the Adaptive Card Designer | - |
102+
| buttonText | boolean | false | Set the Button text for open the Adaptive Card Designer | - |
103+
104+
![](https://telemetry.sharepointpnp.com/sp-dev-fx-controls-react/wiki/controls/AdaptiveCardDesignerHost)

docs/documentation/docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The following controls are currently available:
6464
- [AccessibleAccordion](./controls/AccessibleAccordion) (Control to render an accordion. React `AccessibleAccourdion`-based implementation)
6565
- [Accordion](./controls/Accordion) (Control to render an accordion)
6666
- [AdaptiveCardHost](./controls/AdaptiveCardHost.md) (Control to render Adaptive Cards)
67+
- [AdaptiveCardDesignerHost](./controls/AdaptiveCardDesignerHost.md) (Control to render Adaptive Cards Designer)
6768
- [AnimatedDialog](./controls/AnimatedDialog) (Animated dialog control)
6869
- [Carousel](./controls/Carousel) (Control displays children elements with 'previous/next element' options)
6970
- [Charts](./controls/ChartControl) (makes it easy to integrate [Chart.js](https://www.chartjs.org/) charts into web part)

docs/documentation/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ nav:
1212
- AccessibleAccordion: 'controls/AccessibleAccordion.md'
1313
- Accordion: 'controls/Accordion.md'
1414
- AdaptiveCardHost: 'controls/AdaptiveCardHost.md'
15+
- AdaptiveCardDesignerHost: 'controls/AdaptiveCardDesignerHost.md'
1516
- AnimatedDialog: 'controls/AnimatedDialog.md'
1617
- Carousel: 'controls/Carousel.md'
1718
- Charts:

package-lock.json

Lines changed: 67 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
"@microsoft/sp-office-ui-fabric-core": "1.14.0",
3535
"@microsoft/sp-property-pane": "1.14.0",
3636
"@microsoft/sp-webpart-base": "1.14.0",
37-
"@monaco-editor/loader": "^1.2.0",
37+
"@monaco-editor/loader": "^1.3.1",
3838
"@pnp/sp": "2.5.0",
3939
"@pnp/telemetry-js": "2.0.0",
4040
"@popperjs/core": "2.5.4",
4141
"@uifabric/icons": "7.3.0",
4242
"adaptive-expressions": "^4.15.0",
4343
"adaptivecards": "^2.10.0",
44+
"adaptivecards-designer": "^2.3.0",
4445
"adaptivecards-templating": "^2.2.0",
4546
"animate.css": "^4.1.1",
4647
"chart.js": "2.9.4",

src/AdaptiveCardDesignerHost.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './controls/adaptiveCardDesignerHost/index';
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* This file contains reusable functions useful for managing designer functionality */
2+
3+
import { Guid } from '@microsoft/sp-core-library';
4+
import { CardDesigner, IChoicePickerItem, SnippetPaletteItem, ToolbarButton, ToolbarChoicePicker } from "adaptivecards-designer";
5+
6+
// Given the instance of a "CardDesigner", it allows you to hide an element of the toolbar given its id.
7+
export const hideToolbarElement = (cardDesigner: CardDesigner, elementId: string) => {
8+
cardDesigner.toolbar.getElementById(elementId).isVisible = false;
9+
};
10+
11+
// Given the instance of a "CardDesigner", it allows you to add an element in the toolbar.
12+
export const addToolbarButton = (cardDesigner: CardDesigner, caption: string, iconClass: string, positionElementId: string, isAfter: true, hideElementId?: string, onClick?: (sender: ToolbarButton) => void): string => {
13+
let id = `__${Guid.newGuid().toString()}_ToolbarButton`;
14+
let newToolbarButton = new ToolbarButton(
15+
id,
16+
caption,
17+
iconClass,
18+
onClick);
19+
newToolbarButton.separator = true;
20+
21+
if (isAfter)
22+
cardDesigner.toolbar.insertElementAfter(newToolbarButton, positionElementId);
23+
else
24+
cardDesigner.toolbar.insertElementBefore(newToolbarButton, positionElementId);
25+
26+
if (hideElementId)
27+
hideToolbarElement(cardDesigner, hideElementId);
28+
29+
return id;
30+
};
31+
32+
// Given the instance of a "CarDesigner", it allows you to add a snippets to the toolbox.
33+
export const addToolboxSnippet = (cardDesigner: CardDesigner, category: string, name: string, payload: object) => {
34+
let newSnippet = new SnippetPaletteItem(category, name);
35+
newSnippet.snippet = payload;
36+
37+
if (!cardDesigner.customPaletteItems)
38+
cardDesigner.customPaletteItems = [];
39+
40+
cardDesigner.customPaletteItems.push(newSnippet);
41+
};
42+
43+
// Given the instance of a "CarDesigner", it allows to add a "Choice Picker" to the toolbar
44+
export const addToolbarChoicePicker = (cardDesigner: CardDesigner,
45+
afterElementId: string,
46+
separator: boolean,
47+
label: string,
48+
choices: IChoicePickerItem[],
49+
onChanged: (sender: ToolbarChoicePicker) => void) => {
50+
let id = `__${Guid.newGuid().toString()}_ChoicePicker`;
51+
let breakpointsChoicePicker = new ToolbarChoicePicker(id);
52+
breakpointsChoicePicker.label = label;
53+
breakpointsChoicePicker.choices = choices;
54+
breakpointsChoicePicker.onChanged = onChanged;
55+
breakpointsChoicePicker.separator = separator;
56+
cardDesigner.toolbar.insertElementAfter(breakpointsChoicePicker, afterElementId);
57+
return id;
58+
};
59+
60+
// Convert nulls to empty strings, used for binding with Adaptive Card Template
61+
export const convertNullToEmptyString = (object: any) => {
62+
for (var key in object) {
63+
if (null === object[key] || undefined === object[key]) object[key] = '';
64+
if (typeof object[key] === 'object') convertNullToEmptyString(object[key]);
65+
}
66+
};

0 commit comments

Comments
 (0)