Skip to content

Commit f8193c7

Browse files
Update to 25.1.3+
1 parent ceeac84 commit f8193c7

19 files changed

+761
-26
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
@using ASP_NET_Core.Models
2+
3+
@(Html.DevExtreme().HtmlEditor()
4+
.ID("editor")
5+
.Height(400)
6+
.Value(new JS("value"))
7+
.Variables(variable => variable
8+
.DataSource(new[] { "FirstName", "LastName", })
9+
.EscapeChar(new[] { "{", "}" })
10+
)
11+
.Toolbar(toolbar => toolbar
12+
.Multiline(true)
13+
.Items(items =>
14+
{
15+
items.Add().Name(HtmlEditorToolbarItem.Undo);
16+
items.Add().Name(HtmlEditorToolbarItem.Redo);
17+
items.Add().Name(HtmlEditorToolbarItem.Separator);
18+
items.Add().Name(HtmlEditorToolbarItem.Variable);
19+
items.Add().Name(HtmlEditorToolbarItem.Separator);
20+
items.Add().Name(HtmlEditorToolbarItem.Bold);
21+
items.Add().Name(HtmlEditorToolbarItem.Italic);
22+
items.Add().Name(HtmlEditorToolbarItem.Strike);
23+
items.Add().Name(HtmlEditorToolbarItem.Underline);
24+
items.Add().Name(HtmlEditorToolbarItem.Separator);
25+
items.Add().Widget(w => w.Button().Text("Convert HtmlEditor variables").OnClick("convertVariableOnClick"));
26+
})
27+
)
28+
)
29+
30+
@(Html.DevExtreme().TextBox()
31+
.ID("first")
32+
.Label("FirstName - Default: 'John'")
33+
.LabelMode(EditorLabelMode.Floating)
34+
)
35+
36+
@(Html.DevExtreme().TextBox()
37+
.ID("last")
38+
.Label("LastName - Default: 'Smith'")
39+
.LabelMode(EditorLabelMode.Floating)
40+
)
41+
42+
<script>
43+
const value = `<p>This is a demo to illustrate how to parse and convert variables. To get started, input several variables into the editor, type the variable's values in the TextBoxes below, and click the "Convert HtmlEditor variables" button.</p>`;
44+
const DX_VARIABLE_CLASS = 'dx-variable';
45+
const DATA_VAR_VALUE_ATTR = 'data-var-value';
46+
47+
/*
48+
value: dxHtmlEditor.value
49+
variableMap: object = Must contain `key: value` pairs of variable content
50+
{
51+
'FirstName': 'John',
52+
'LastName': 'Smith'
53+
}
54+
*/
55+
56+
function replaceVariables (value, variablesMap) {
57+
const parser = new DOMParser();
58+
const doc = parser.parseFromString(value, 'text/html');
59+
const variables = doc.querySelectorAll(`.${DX_VARIABLE_CLASS}`);
60+
61+
variables.forEach(variable => {
62+
const variableValue = variablesMap[variable.getAttribute(DATA_VAR_VALUE_ATTR)];
63+
variable.outerHTML = variableValue;
64+
});
65+
66+
return doc.body.innerHTML.toString();
67+
}
68+
69+
function convertVariableOnClick(e) {
70+
const htmlEditor = $("#editor").dxHtmlEditor("instance");
71+
const firstName = $("#first").dxTextBox("instance").option("value") || "John";
72+
const lastName = $("#last").dxTextBox("instance").option("value") || "Smith";
73+
74+
const valueWithReplacedVariables = replaceVariables(
75+
htmlEditor.option("value"),
76+
{
77+
"FirstName": firstName,
78+
"LastName": lastName
79+
}
80+
);
81+
82+
htmlEditor.option("value", valueWithReplacedVariables);
83+
}
84+
</script>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div class="default-style">
2+
<dx-html-editor #editor [value]="initialValue" [height]="400">
3+
<dxo-toolbar [multiline]="true">
4+
<dxi-item name="undo"></dxi-item>
5+
<dxi-item name="redo"></dxi-item>
6+
<dxi-item name="separator"></dxi-item>
7+
<dxi-item name="variable"></dxi-item>
8+
<dxi-item name="separator"></dxi-item>
9+
<dxi-item name="bold"></dxi-item>
10+
<dxi-item name="italic"></dxi-item>
11+
<dxi-item name="strike"></dxi-item>
12+
<dxi-item name="underline"></dxi-item>
13+
<dxi-item name="separator"></dxi-item>
14+
<dxi-item widget="dxButton"
15+
[options]="{ text: 'Convert HtmlEditor variables', onClick: convertVariableOnClick }"></dxi-item>
16+
</dxo-toolbar>
17+
<dxo-variables [dataSource]="[ 'FirstName', 'LastName' ]" [escapeChar]="[ '{', '}' ]">
18+
</dxo-variables>
19+
</dx-html-editor>
20+
<dx-text-box [(value)]="firstName" label="FirstName - Default: 'John'" labelMode="floating"></dx-text-box>
21+
<dx-text-box [(value)]="lastName" label="LastName - Default: 'John'" labelMode="floating"></dx-text-box>
22+
</div>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.default-style {
2+
margin: 50px 50px;
3+
width: 90vh;
4+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Component, ViewChild } from '@angular/core';
2+
import { DxHtmlEditorComponent } from "devextreme-angular/ui/html-editor";
3+
import { replaceVariables } from "./replace-variables";
4+
5+
@Component({
6+
selector: 'app-root',
7+
templateUrl: './app.component.html',
8+
styleUrls: ['./app.component.scss']
9+
})
10+
export class AppComponent {
11+
@ViewChild("editor", { static: false }) htmlEditor!: DxHtmlEditorComponent;
12+
13+
title = 'Angular';
14+
initialValue: string = `<p>This is a demo to illustrate how to parse and convert variables. To get started, input several variables into the editor, type the variable's values in the TextBoxes below, and click the "Convert HtmlEditor variables" button.</p>`;
15+
firstName: string = "";
16+
lastName: string = "";
17+
18+
convertVariableOnClick() {
19+
const firstName = (this.firstName.length > 0) ? this.firstName : "John";
20+
const lastName = (this.lastName.length > 0) ? this.lastName : "Smith";
21+
let value: string = this.htmlEditor?.instance.option("value");
22+
23+
value = replaceVariables(value,
24+
{
25+
"FirstName": firstName,
26+
"LastName": lastName
27+
}
28+
);
29+
30+
this.htmlEditor?.instance.option("value", value);
31+
}
32+
33+
constructor() {
34+
this.convertVariableOnClick = this.convertVariableOnClick.bind(this);
35+
}
36+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const DX_VARIABLE_CLASS = 'dx-variable';
2+
const DATA_VAR_VALUE_ATTR = 'data-var-value';
3+
4+
/**
5+
* Contains `key: value` pairs of variables for HtmlEditor.
6+
* { 'FirstName': 'John', 'LastName': 'Smith' }
7+
*/
8+
type Variable = {
9+
[key: string]: string;
10+
}
11+
12+
/**
13+
* This method parses HTML string and swaps variables with real values
14+
* @param value Value must come from HtmlEditor's value property
15+
* @param variablesMap Must contain `key: value` pairs of variable content. e.g. `{ 'FirstName': 'John', 'LastName': 'Smith' }`
16+
* @returns Processed HTML string. Assign this value back to HtmlEditor's value property
17+
*/
18+
export function replaceVariables(value: string, variablesMap: Variable): string {
19+
const parser = new DOMParser();
20+
21+
let replaceVariables = (value: string, variablesMap: Variable) => {
22+
const doc = parser.parseFromString(value, 'text/html');
23+
const variables = doc.querySelectorAll(`.${DX_VARIABLE_CLASS}`);
24+
25+
variables.forEach(variable => {
26+
const variableValue = variablesMap[variable.getAttribute(DATA_VAR_VALUE_ATTR) || ''];
27+
variable.outerHTML = variableValue;
28+
});
29+
30+
return doc.body.innerHTML.toString();
31+
}
32+
33+
return replaceVariables(value, variablesMap);
34+
}

HtmlEditor - Mail merge.png

17.8 KB
Loading

README.md

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,72 @@
11
<!-- default badges list -->
2-
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/340354634/25.1.2%2B)
3-
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1129779)
2+
![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/633913710/22.2.6%2B)
3+
[![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1162966)
44
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
55
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
66
<!-- default badges end -->
7-
# DevExtreme Examples Template
7+
# HtmlEditor for DevExtreme - How to implement mail merge with variables
88

9-
This is the repository template for creating new examples.
9+
The HtmlEditor component supports [variables](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/variables/). This example demonstrates how to implement mail merge with variables.
1010

11-
![Example image](images/image-template.png)
11+
The application contains HtmlEditor and two TextBoxes. Type values in the TextBoxes to see them merged with HtmlEditor text.
1212

13-
Use **DevExtreme _Product_ - _Task_** template for a title.
13+
![HtmlEditor - Mail merge](/HtmlEditor%20-%20Mail%20merge.png)
1414

15-
Describe the solved task in this section.
15+
The core implementation for mail merge can be found within the `replaceVariables(value, variablesMap)` utility method. The method accepts two parameters: HtmlEditor's [value](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/#value) and an object map. These parameters are used to parse the value, then to find and replace the variables based on the map object.
1616

17-
Put a screenshot/gif that illustrates the result here.
17+
```
18+
const DX_VARIABLE_CLASS = 'dx-variable';
19+
const DATA_VAR_VALUE_ATTR = 'data-var-value';
1820
19-
Then, add implementation details (steps, code snippets, and other technical information in a free form), or add a link to an existing document with implementation details.
21+
/*
22+
value: dxHtmlEditor.value
23+
variableMap: object = Must contain `key: value` pairs of variable content.
24+
{
25+
'FirstName': 'John',
26+
'LastName': 'Smith'
27+
}
28+
*/
29+
const replaceVariables = (value, variablesMap) => {
30+
const parser = new DOMParser();
31+
32+
replaceVariables = (value, variablesMap) => {
33+
const doc = parser.parseFromString(value, 'text/html');
34+
const variables = doc.querySelectorAll(`.${DX_VARIABLE_CLASS}`);
35+
36+
variables.forEach(variable => {
37+
const variableValue = variablesMap[variable.getAttribute(DATA_VAR_VALUE_ATTR)];
38+
variable.outerHTML = variableValue;
39+
});
40+
41+
return doc.body.innerHTML.toString();
42+
}
43+
44+
return replaceVariables(value, variablesMap);
45+
};
46+
```
2047

2148
## Files to Review
2249

50+
- **jQuery**
51+
- [index.js](jQuery/src/index.js)
2352
- **Angular**
2453
- [app.component.html](Angular/src/app/app.component.html)
2554
- [app.component.ts](Angular/src/app/app.component.ts)
26-
- **React**
27-
- [App.tsx](React/src/App.tsx)
2855
- **Vue**
29-
- [App.vue](Vue/src/App.vue)
3056
- [Home.vue](Vue/src/components/HomeContent.vue)
31-
- **jQuery**
32-
- [index.html](jQuery/src/index.html)
33-
- [index.js](jQuery/src/index.js)
34-
- **ASP.NET Core**
57+
- **React**
58+
- [App.tsx](React/src/App.tsx)
59+
- **NetCore**
3560
- [Index.cshtml](ASP.NET%20Core/Views/Home/Index.cshtml)
3661

3762
## Documentation
3863

39-
- link
40-
- link
41-
- ...
42-
43-
## More Examples
44-
45-
- link
46-
- link
47-
- ...
64+
- [Getting Started with HtmlEditor](https://js.devexpress.com/Documentation/Guide/UI_Components/HtmlEditor/Getting_Started_with_HtmlEditor/)
65+
- [HtmlEditor - Variables](https://js.devexpress.com/Documentation/ApiReference/UI_Components/dxHtmlEditor/Configuration/variables/)
4866
<!-- feedback -->
4967
## Does this example address your development requirements/objectives?
5068

51-
[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-examples-template&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-examples-template&~~~was_helpful=no)
69+
[<img src="https://www.devexpress.com/support/examples/i/yes-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-htmleditor-mail-merge-with-variables&~~~was_helpful=yes) [<img src="https://www.devexpress.com/support/examples/i/no-button.svg"/>](https://www.devexpress.com/support/examples/survey.xml?utm_source=github&utm_campaign=devextreme-htmleditor-mail-merge-with-variables&~~~was_helpful=no)
5270

5371
(you will be redirected to DevExpress.com to submit your response)
5472
<!-- feedback end -->

0 commit comments

Comments
 (0)