Skip to content

Commit 7c0e2bf

Browse files
RTE Team conflicts resolved.
RTE Team conflicts resolved.
1 parent 9f099ad commit 7c0e2bf

File tree

1 file changed

+90
-58
lines changed

1 file changed

+90
-58
lines changed

ej2-javascript/rich-text-editor/smart-editing/mail-merge.md

Lines changed: 90 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,32 @@ layout: post
33
title: Mail Merge in ##Platform_Name## Rich Text Editor Control | Syncfusion
44
description: Learn all about Mail Merge in the Syncfusion ##Platform_Name## Rich Text Editor control, part of Essential JS 2.
55
platform: ej2-javascript
6-
control: Mail Merge
6+
control: Mail Merge
77
publishingplatform: ##Platform_Name##
88
documentation: ug
99
domainurl: ##DomainURL##
1010
---
11-
11+
1212
# Mail merge in ##Platform_Name## Rich Text Editor Control
13-
13+
1414
The Rich Text Editor can be customized to implement **Mail Merge** functionality by inserting placeholders into the editor using custom toolbar items. These placeholders are later replaced with actual data to generate personalized content such as letters, invoices, and reports.
15-
15+
1616
This feature simplifies the creation of dynamic documents by allowing users to insert merge fields that are automatically populated with real data during content generation.
17-
17+
1818
## Adding custom toolbar items for inserting merge fields
19-
19+
2020
To enable mail merge functionality, the Rich Text Editor toolbar is extended with two custom buttons: `Insert Field` and `Merge Data`. These buttons are added using the `template` property in [toolbarSettings.items](https://ej2.syncfusion.com/documentation/api/rich-text-editor/toolbarsettings#items), which points to custom HTML elements (#insertField and #merge_data).
21-
21+
2222
- **Insert Field:** Opens a dropdown list of merge fields for inserting placeholders like `{{FirstName}}` into the editor.
2323
- **Merge Data:** Replaces all placeholders in the editor with actual values from a predefined data source.
24-
24+
2525
{% if page.publishingplatform == "typescript" %}
26-
26+
2727
{% tabs %}
2828
{% highlight ts tabtitle="app.ts" %}
29-
29+
30+
{% raw %}
31+
3032
const mailMergeEditor: RichTextEditor = new RichTextEditor({
3133
toolbarSettings: {
3234
items: [
@@ -39,15 +41,19 @@ const mailMergeEditor: RichTextEditor = new RichTextEditor({
3941
},
4042
});
4143
mailMergeEditor.appendTo('#mailMergeEditor');
42-
44+
45+
{% endraw %}
46+
4347
{% endhighlight %}
4448
{% endtabs %}
45-
49+
4650
{% elsif page.publishingplatform == "javascript" %}
47-
51+
4852
{% tabs %}
4953
{% highlight js tabtitle="index.js" %}
50-
54+
55+
{% raw %}
56+
5157
var mailMergeEditor = new ej.richtexteditor.RichTextEditor({
5258
toolbarSettings: {
5359
items: [
@@ -60,20 +66,24 @@ var mailMergeEditor = new ej.richtexteditor.RichTextEditor({
6066
},
6167
});
6268
mailMergeEditor.appendTo('#mailMergeEditor');
63-
69+
70+
{% endraw %}
71+
6472
{% endhighlight %}
6573
{% endtabs %}
6674
{% endif %}
67-
75+
6876
## Using DropDownButton for selecting placeholders
69-
77+
7078
The **DropDownButton** component displays a list of merge fields such as First Name, Last Name, and Company Name. When a user selects an item, the corresponding placeholder (e.g., {{FirstName}}) is inserted at the current cursor position using the `insertHTML` command.
71-
79+
7280
{% if page.publishingplatform == "typescript" %}
73-
81+
7482
{% tabs %}
7583
{% highlight ts tabtitle="app.ts" %}
76-
84+
85+
{% raw %}
86+
7787
let insertField: DropDownButton = new DropDownButton({
7888
items: [
7989
{ text: 'First Name' },
@@ -90,7 +100,7 @@ let insertField: DropDownButton = new DropDownButton({
90100
select: onItemSelect,
91101
});
92102
insertField.appendTo('#insertField');
93-
103+
94104
function onItemSelect(args: MenuEventArgs): void {
95105
const value = textToValueMap[args.item.text];
96106
mailMergeEditor.executeCommand(
@@ -99,15 +109,19 @@ function onItemSelect(args: MenuEventArgs): void {
99109
{ undo: true }
100110
);
101111
}
102-
112+
113+
{% endraw %}
114+
103115
{% endhighlight %}
104116
{% endtabs %}
105-
117+
106118
{% elsif page.publishingplatform == "javascript" %}
107-
119+
108120
{% tabs %}
109121
{% highlight js tabtitle="index.js" %}
110-
122+
123+
{% raw %}
124+
111125
var insertField = new ej.splitbuttons.DropDownButton({
112126
items: [
113127
{ text: 'First Name' },
@@ -123,7 +137,7 @@ var insertField = new ej.splitbuttons.DropDownButton({
123137
select: onItemSelect,
124138
});
125139
insertField.appendTo('#insertField');
126-
140+
127141
function onItemSelect(args) {
128142
if (args.item.text != null) {
129143
var value = textToValueMap[args.item.text];
@@ -138,85 +152,101 @@ function onItemSelect(args) {
138152
);
139153
}
140154
}
141-
155+
156+
{% endraw %}
157+
142158
{% endhighlight %}
143159
{% endtabs %}
144160
{% endif %}
145-
161+
146162
## Populating merge fields using Mention
147-
163+
148164
The **Mention** control provides an alternative way to insert placeholders by typing the <code>&#123;&#123;</code> character inside the editor. A popup list of merge fields appears, allowing quick selection without using the toolbar.
149-
165+
150166
{% if page.publishingplatform == "typescript" %}
151-
167+
152168
{% tabs %}
153169
{% highlight ts tabtitle="app.ts" %}
154-
170+
171+
{% raw %}
172+
155173
const mentionObj: Mention = new Mention({
156174
dataSource: mergeData,
157175
target: '#mailMergeEditor',
158-
mentionChar: <code>&#123;&#123;</code>,
176+
mentionChar: `{{`,
159177
fields: { text: 'text' },
160178
allowSpaces: true,
161179
popupWidth: '250px',
162180
popupHeight: '200px',
163181
displayTemplate: '<span> {{${value}}} </span>',
164182
});
165183
mentionObj.appendTo('#mentionField');
166-
184+
185+
{% endraw %}
186+
167187
{% endhighlight %}
168188
{% endtabs %}
169-
189+
170190
{% elsif page.publishingplatform == "javascript" %}
171-
191+
172192
{% tabs %}
173193
{% highlight js tabtitle="index.js" %}
174-
194+
195+
{% raw %}
196+
175197
var mergeObj = new ej.dropdowns.Mention({
176198
dataSource: mergeData,
177199
fields: { text: 'Name', value: 'Field' },
178200
displayTemplate: '<span>{{${Field}}}</span>',
179201
popupWidth: '250px',
180202
popupHeight: '200px',
181203
target: mailMergeEditor.inputElement,
182-
mentionChar: <code>&#123;&#123;</code> ,
204+
mentionChar: `{{` ,
183205
allowSpaces: true,
184206
});
185207
mergeObj.appendTo('#mentionField');
186-
208+
209+
{% endraw %}
210+
187211
{% endhighlight %}
188212
{% endtabs %}
189213
{% endif %}
190-
214+
191215
## Replacing placeholders with actual data dynamically
192-
216+
193217
When the **Merge Data** button is clicked, the editor content is processed to replace all placeholders with actual values from the `placeholderData` object. This is done using a regular expression in the `replacePlaceholders()` function.
194-
218+
195219
{% if page.publishingplatform == "typescript" %}
196-
220+
197221
{% tabs %}
198-
{% highlight %}
199-
222+
{% highlight ts tabtitle="app.ts" %}
223+
224+
{% raw %}
225+
200226
document.getElementById('merge_data')?.addEventListener('click', onClickHandler);
201-
227+
202228
function onClickHandler(): void {
203229
let editorContent = mailMergeEditor.value;
204230
let mergedContent = replacePlaceholders(editorContent, placeholderData);
205231
mailMergeEditor.value = mergedContent;
206232
}
207-
233+
208234
function replacePlaceholders(template: string, data: { [key: string]: string }): string {
209235
return template.replace(/{{\s*(\w+)\s*}}/g, (match, key) => data[key.trim()] || match);
210236
}
211-
237+
238+
{% endraw %}
239+
212240
{% endhighlight %}
213241
{% endtabs %}
214-
242+
215243
{% elsif page.publishingplatform == "javascript" %}
216-
244+
217245
{% tabs %}
218246
{% highlight js tabtitle="index.js" %}
219-
247+
248+
{% raw %}
249+
220250
var mergeField = document.getElementById('merge_data');
221251
if (mergeField) {
222252
mergeField.addEventListener('click', onClickHandler);
@@ -234,20 +264,22 @@ function onClickHandler() {
234264
console.log('MailMergeEditor is not initialized.');
235265
}
236266
}
237-
267+
238268
function replacePlaceholders(template, data) {
239269
return template.replace(/{{\s*(\w+)\s*}}/g, function (match, key) {
240270
var value = data[key.trim()];
241271
return value !== undefined ? value : match;
242272
});
243273
}
244-
274+
275+
{% endraw %}
276+
245277
{% endhighlight %}
246278
{% endtabs %}
247279
{% endif %}
248-
280+
249281
{% if page.publishingplatform == "typescript" %}
250-
282+
251283
{% tabs %}
252284
{% highlight ts tabtitle="index.ts" %}
253285
{% include code-snippet/rich-text-editor/mail-merge/index.ts %}
@@ -258,9 +290,9 @@ function replacePlaceholders(template, data) {
258290
{% endtabs %}
259291

260292
{% previewsample "page.domainurl/code-snippet/rich-text-editor/mail-merge-cs1" %}
261-
293+
262294
{% elsif page.publishingplatform == "javascript" %}
263-
295+
264296
{% tabs %}
265297
{% highlight js tabtitle="index.js" %}
266298
{% include code-snippet/rich-text-editor/mail-merge/index.js %}
@@ -269,6 +301,6 @@ function replacePlaceholders(template, data) {
269301
{% include code-snippet/rich-text-editor/mail-merge/index.html %}
270302
{% endhighlight %}
271303
{% endtabs %}
272-
304+
273305
{% previewsample "page.domainurl/code-snippet/rich-text-editor/mail-merge-cs1" %}
274306
{% endif %}

0 commit comments

Comments
 (0)