-
Notifications
You must be signed in to change notification settings - Fork 794
/
Copy pathadding-configuration-to-a-property-editor.md
387 lines (327 loc) · 12.7 KB
/
adding-configuration-to-a-property-editor.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
---
description: Adding configuration options to the editor.
---
# Adding configuration to a Property Editor
## Overview
This is step two in our guide to building a Property Editor. This step continues work on the Suggestion Data Type we built in part one but goes further to show how to add configuration options to our editor.
The steps we will go through in the second part are:
* [Adding settings object to umbraco-package.json](adding-configuration-to-a-property-editor.md#adding-settings-object-to-umbraco-package.json)
* [Using the configuration](adding-configuration-to-a-property-editor.md#using-the-configuration)
### Configuration
An important part of building good Property Editors is to build something flexible, so we can reuse it many times, for different things. Like the Rich Text Editor in Umbraco, which allows us to choose which buttons and stylesheets we want to use on each instance of the editor.
An editor can be used again and again, with different configurations, and that is what we will be working on now.
## Adding settings object to umbraco-package.json
To add a Data Type configuration field when using our Suggestion Property Editor, open the `umbraco-package.json` file. Inside the `meta` object, we can add the `settings` object, which has the optional objects `properties` and `defaultData`.
1. Add some `properties`:
{% code title="umbraco-package.json" %}
```json
...
"meta": {
...
"settings": {
"properties": [
{
"alias": "disabled",
"label": "Disabled",
"description": "Disables the suggestion button",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.Toggle"
},
{
"alias": "placeholder",
"label": "Placeholder text",
"description": "A nice placeholder description to help out our editor!",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.TextBox"
},
{
"alias": "maxChars",
"label": "Max characters allowed",
"description": "The maximum number of allowed characters in a suggestion",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.Integer"
}
]
}
...
}
```
{% endcode %}
In the section above, we added three configuration fields. Each entry in the `properties` collection represents a Configuration field. It contains the necessary information for that field.
* The field labeled "`Disabled`" uses the Toggle Property Editor UI. This enables to switch the suggestion button on or off and provides the user with a toggle button.
* The field labeled "`Placeholder text`" uses the TextBox Property Editor UI, allowing the user to write a text.
* The field labeled "`Max characters allowed`" uses the Integer Property Editor UI, enabling the user to enter a numeric value.
{% hint style="info" %}
The Property Editor UI needs to be declared as it declares what User Interface should be used for this field.
You can use any Property Editor UI to define Configuration fields. The alias of a given Property Editor UI can be found in Data Type configurations using that Property Editor.
{% endhint %}
2. We can now also set some default data on our new configurations:
{% code title="umbraco-package.json" %}
```json
...
"meta": {
...
"settings": {
"properties": [...],
"defaultData": [
{
"alias": "disabled",
"value": true
},
{
"alias": "placeholder",
"value": "Write a suggestion"
},
{
"alias": "maxChars",
"value": 50
}
]
}
...
```
{% endcode %}
<details>
<summary>See the entire file: umbraco-package.json</summary>
{% code title=" umbraco-package.json" %}
```json
{
"$schema": "../../umbraco-package-schema.json",
"id": "My.AwesomePackage",
"name": "My Awesome Package",
"version": "0.1.0",
"extensions": [
{
"type": "propertyEditorUi",
"alias": "My.PropertyEditorUi.Suggestions",
"name": "My Suggestions Property Editor UI",
"element": "/App_Plugins/Suggestions/dist/suggestions.js",
"elementName": "my-suggestions-property-editor-ui",
"meta": {
"label": "Suggestions",
"icon": "icon-list",
"group": "common",
"propertyEditorSchemaAlias": "Umbraco.Plain.String",
"settings": {
"properties": [
{
"alias": "disabled",
"label": "Disabled",
"description": "Disables the suggestion button",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.Toggle"
},
{
"alias": "placeholder",
"label": "Placeholder text",
"description": "A nice placeholder description to help out our editor!",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.TextBox"
},
{
"alias": "maxChars",
"label": "Max characters allowed",
"description": "The maximum number of allowed characters in a suggestion",
"propertyEditorUiAlias": "Umb.PropertyEditorUi.Integer"
}
],
"defaultData": [
{
"alias": "disabled",
"value": true
},
{
"alias": "placeholder",
"value": "Write a suggestion"
},
{
"alias": "maxChars",
"value": 50
}
]
}
}
}
]
}
```
{% endcode %}
</details>
3. Save the files and reload the backoffice. We can now configure our Data Type:
<figure><img src="images/suggestion-editor-config_3.png" alt=""><figcaption><p>Data Type configuration.</p></figcaption></figure>
## Using the configuration
The next step is to gain access to our new configuration options. For this, open the `suggestions-property-editor-ui.element.ts` file.
1. Create some state variables that can store our configurations:
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
@state()
private _disabled?: boolean;
@state()
private _placeholder?: string;
@state()
private _maxChars?: number;
```
{% endcode %}
2. Let's create a config property. Add a new import and add the following property:
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
import { type UmbPropertyEditorConfigCollection } from "@umbraco-cms/backoffice/property-editor";
```
{% endcode %}
3. Look up the alias of the config and then grab the value by said alias:
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
@property({ attribute: false })
public set config(config: UmbPropertyEditorConfigCollection) {
this._disabled = config.getValueByAlias("disabled");
this._placeholder = config.getValueByAlias("placeholder");
this._maxChars = config.getValueByAlias("maxChars");
}
```
{% endcode %}
We can now use the configurations. Let's use the `placeholder` and `maxChars` for the input field and the `disabled` option for the suggestion button.
4. Add a new import `ifDefined` :
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
import { ifDefined } from "@umbraco-cms/backoffice/external/lit";
```
{% endcode %}
5. Update the render method:
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
render() {
return html`
<uui-input
id="suggestion-input"
class="element"
label="text input"
placeholder=${ifDefined(this._placeholder)}
maxlength=${ifDefined(this._maxChars)}
.value=${this.value || ""}
@input=${this.#onInput}
>
</uui-input>
<div id="wrapper">
<uui-button
id="suggestion-button"
class="element"
look="primary"
label="give me suggestions"
?disabled=${this._disabled}
@click=${this.#onSuggestion}
>
Give me suggestions!
</uui-button>
<uui-button
id="suggestion-trimmer"
class="element"
look="outline"
label="Trim text"
>
Trim text
</uui-button>
</div>
`;
}
```
{% endcode %}
<details>
<summary>See the entire file: suggestions-property-editor-ui.element.ts</summary>
{% code title="suggestions-property-editor-ui.element.ts" %}
```typescript
import { LitElement, css, html, customElement, property, state, ifDefined } from "@umbraco-cms/backoffice/external/lit";
import { type UmbPropertyEditorUiElement } from "@umbraco-cms/backoffice/extension-registry";
import { type UmbPropertyEditorConfigCollection } from "@umbraco-cms/backoffice/property-editor";
import { UmbPropertyValueChangeEvent } from '@umbraco-cms/backoffice/property-editor';
@customElement('my-suggestions-property-editor-ui')
export default class MySuggestionsPropertyEditorUIElement extends LitElement implements UmbPropertyEditorUiElement {
@property({ type: String })
public value = "";
@state()
private _disabled?: boolean;
@state()
private _placeholder?: string;
@state()
private _maxChars?: number;
@state()
private _suggestions = [
"You should take a break",
"I suggest that you visit the Eiffel Tower",
"How about starting a book club today or this week?",
"Are you hungry?",
];
@property({ attribute: false })
public set config(config: UmbPropertyEditorConfigCollection) {
this._disabled = config.getValueByAlias("disabled");
this._placeholder = config.getValueByAlias("placeholder");
this._maxChars = config.getValueByAlias("maxChars");
}
#onInput(e: InputEvent) {
this.value = (e.target as HTMLInputElement).value;
this.#dispatchChangeEvent();
}
#onSuggestion() {
const randomIndex = (this._suggestions.length * Math.random()) | 0;
this.value = this._suggestions[randomIndex];
this.#dispatchChangeEvent();
}
#dispatchChangeEvent() {
this.dispatchEvent(new UmbPropertyValueChangeEvent());
}
render() {
return html`
<uui-input
id="suggestion-input"
class="element"
label="text input"
placeholder=${ifDefined(this._placeholder)}
maxlength=${ifDefined(this._maxChars)}
.value=${this.value || ""}
@input=${this.#onInput}
>
</uui-input>
<div id="wrapper">
<uui-button
id="suggestion-button"
class="element"
look="primary"
label="give me suggestions"
?disabled=${this._disabled}
@click=${this.#onSuggestion}
>
Give me suggestions!
</uui-button>
<uui-button
id="suggestion-trimmer"
class="element"
look="outline"
label="Trim text"
>
Trim text
</uui-button>
</div>
`;
}
static styles = [
css`
#wrapper {
margin-top: 10px;
display: flex;
gap: 10px;
}
.element {
width: 100%;
}
`,
];
}
declare global {
interface HTMLElementTagNameMap {
'my-suggestions-property-editor-ui': MySuggestionsPropertyEditorUIElement;
}
}
```
{% endcode %}
</details>
6. Run the command `npm run build` in the `suggestions` folder. 
7. Run the project. 
8. Go to the **Content** section of the Backoffice to see the new changes in the property editor:
<figure><img src="images/suggestion-editor-backoffice_2.png" alt=""><figcaption><p>Suggestions Property Editor with disabled suggestions option</p></figcaption></figure>
## Going further
We have now added some configurations to our data type and used them in our Property Editor.
In the next step, we are going to integrate context with our Property Editor.