Skip to content

Commit 33fb35e

Browse files
991223: Blazor Documentation cleanup
1 parent 52e71a8 commit 33fb35e

File tree

1 file changed

+77
-40
lines changed

1 file changed

+77
-40
lines changed

blazor/rich-text-editor/mail-merge.md

Lines changed: 77 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@ control: RichTextEditor
77
documentation: ug
88
---
99

10-
# Mail Merge in Syncfusion Blazor RichTextEditor
10+
# Mail Merge in Blazor Rich Text Editor
1111

12-
The Mail Merge feature in Syncfusion Blazor RichTextEditor enables developers to create dynamic, personalized documents by inserting placeholders (merge fields) into the editor content. These placeholders are later replaced with actual data at runtime, making it ideal for generating letters, invoices, and bulk communication templates.
13-
14-
## Key Outcomes:
15-
16-
- Render custom toolbar items for mail merge actions.
17-
- Insert merge fields dynamically using dropdown or Mention control.
18-
- Replace placeholders with real data using a single click.
12+
The Mail Merge feature in Blazor Rich Text Editor enables developers to create dynamic, personalized documents by inserting placeholders (merge fields) into the editor content. These placeholders are later replaced with actual data at runtime, making it ideal for generating letters, invoices, and bulk communication templates.
1913

2014
## Rendering Custom Toolbar Items
2115

@@ -30,14 +24,20 @@ Custom toolbar items are added using the [RichTextEditorCustomToolbarItems](http
3024
<RichTextEditorCustomToolbarItems>
3125
<RichTextEditorCustomToolbarItem Name="MergeData">
3226
<Template>
33-
<SfButton OnClick="MergeData">Merge Data</SfButton>
27+
<SfButton CssClass="@_buttonClass" OnClick="OnClickHandler" id="merge_data" tabindex="-1" aria-label="Merge User-specific Data" Disabled="@_sourceCodeEnabled">
28+
<div class="e-tbar-btn-text">Merge Data</div>
29+
</SfButton>
3430
</Template>
3531
</RichTextEditorCustomToolbarItem>
3632
<RichTextEditorCustomToolbarItem Name="InsertField">
3733
<Template>
38-
<SfDropDownButton Items="@items">
39-
<ChildContent>Insert Field</ChildContent>
40-
<DropDownButtonEvents ItemSelected="InsertField" />
34+
<SfDropDownButton CssClass="@_dropDownButtonClass" id="insertField" Items="@_items" aria-label="Insert Merge Field" Disabled="@_sourceCodeEnabled">
35+
<ChildContent>
36+
<span style="display:inline-flex;">
37+
<span class="e-rte-dropdown-btn-text">Insert Field</span>
38+
</span>
39+
<DropDownButtonEvents ItemSelected="OnItemSelect" OnOpen="OnDropDownOpen" Closed="OnDropDownClose"></DropDownButtonEvents>
40+
</ChildContent>
4141
</SfDropDownButton>
4242
</Template>
4343
</RichTextEditorCustomToolbarItem>
@@ -46,42 +46,48 @@ Custom toolbar items are added using the [RichTextEditorCustomToolbarItems](http
4646
{% endhighlight %}
4747
{% endtabs %}
4848

49-
## Executing Merge Data Action
49+
## Populating and Using Insert Field Dropdown
5050

51-
When the `Merge Data` button is clicked:
51+
The `Insert Field` dropdown in the Rich Text Editor is designed to let users quickly insert predefined merge fields into the editor content. This dropdown is powered by the `SfDropDownButton` control, which uses its [Items](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SplitButtons.SfDropDownButton.html#Syncfusion_Blazor_SplitButtons_SfDropDownButton_Items) property to bind a collection of menu items.
5252

53-
1. The editor’s current content (rte.Value) is retrieved.
54-
2. A regex-based function scans for placeholders in the format {{FieldName}}.
55-
3. Each placeholder is replaced with its corresponding value from a dictionary.
53+
### How the Items Property Works
5654

57-
```csharp
55+
- The `Items` property accepts a list of [DropDownMenuItem](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SplitButtons.DropDownMenuItem.html) objects.
56+
- Each item in this list represents a merge field and contains a [Text](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.SplitButtons.DropDownMenuItem.html#Syncfusion_Blazor_SplitButtons_DropDownMenuItem_Text) property, which is displayed in the dropdown.
57+
- These text values correspond to the merge fields available for insertion.
5858

59-
public void OnClickHandler()
60-
{
61-
if (this._mailMergeEditor != null)
62-
{
63-
var editorContent = this._mailMergeEditor.Value;
64-
var mergedContent = ReplacePlaceholders(editorContent, this._placeholderData);
65-
_rteValue = mergedContent;
66-
}
67-
}
68-
public static string ReplacePlaceholders(string template, Dictionary<string, string> data)
59+
{% tabs %}
60+
{% highlight razor %}
61+
62+
<SfDropDownButton Items="@items">
63+
<ChildContent>Insert Field</ChildContent>
64+
<DropDownButtonEvents ItemSelected="OnItemSelect" />
65+
</SfDropDownButton>
66+
67+
{% endhighlight %}
68+
{% endtabs %}
69+
70+
Here, `@items` refers to a list of `DropDownMenuItem` objects defined in the `@code` block.
71+
72+
```csharp
73+
private List<DropDownMenuItem> items = new List<DropDownMenuItem>
6974
{
70-
return Regex.Replace(template, @"{{\s*(\w+)\s*}}", match =>
71-
{
72-
string key = match.Groups[1].Value.Trim();
73-
return data.TryGetValue(key, out var value) ? value : match.Value;
74-
});
75-
}
75+
new DropDownMenuItem { Text = "First Name" },
76+
new DropDownMenuItem { Text = "Last Name" },
77+
new DropDownMenuItem { Text = "Support Email" },
78+
new DropDownMenuItem { Text = "Company Name" },
79+
new DropDownMenuItem { Text = "Promo Code" },
80+
new DropDownMenuItem { Text = "Support Phone Number" },
81+
new DropDownMenuItem { Text = "Customer ID" },
82+
new DropDownMenuItem { Text = "Expiration Date" },
83+
new DropDownMenuItem { Text = "Subscription Plan" }
84+
};
7685

7786
```
78-
This ensures all placeholders are dynamically replaced without manual editing.
7987

80-
## Populating and Using Insert Field Dropdown
81-
82-
The `Insert Field` dropdown is populated from a predefined list of merge fields (items). When a user selects an item:
88+
When the user selects an item from the dropdown:
8389

84-
- The `InsertField()` method retrieves the corresponding field value.
90+
- The `OnItemSelect()` method retrieves the corresponding field value.
8591
- It constructs an HTML snippet with a non-editable span containing the placeholder.
8692
- The snippet is inserted at the current cursor position using [ExecuteCommandAsync](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.RichTextEditor.SfRichTextEditor.html#Syncfusion_Blazor_RichTextEditor_SfRichTextEditor_ExecuteCommandAsync_Syncfusion_Blazor_RichTextEditor_CommandName_System_String_Syncfusion_Blazor_RichTextEditor_ExecuteCommandOption_).
8793

@@ -98,6 +104,7 @@ The `Insert Field` dropdown is populated from a predefined list of merge fields
98104
}
99105
}
100106
```
107+
101108
## Role of Mention Control in Mail Merge
102109

103110
Mention control enhances usability by enabling inline field suggestions:
@@ -149,7 +156,6 @@ When the `Insert Field` dropdown opens, the editor loses its current selection b
149156
}
150157
```
151158

152-
153159
## Handling Editor Mode Changes with OnActionComplete
154160

155161
The [OnActionComplete](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.RichTextEditor.RichTextEditorEvents.html#Syncfusion_Blazor_RichTextEditor_RichTextEditorEvents_OnActionComplete) event fires after specific actions in the RichTextEditor, such as switching between Source Code and Preview modes.
@@ -177,6 +183,37 @@ The [OnActionComplete](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.R
177183

178184
**Why is this important?** This prevents users from triggering merge operations or inserting fields while editing raw HTML, which could cause unexpected behavior.
179185

186+
## Executing Merge Data Action
187+
188+
When the `Merge Data` button is clicked:
189+
190+
- The editor’s current content is retrieved by using [Value](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.RichTextEditor.SfRichTextEditor.html#Syncfusion_Blazor_RichTextEditor_SfRichTextEditor_Value) property.
191+
- A regex-based function scans for placeholders in the format {{FieldName}}.
192+
- Each placeholder is replaced with its corresponding value from a dictionary.
193+
194+
```csharp
195+
196+
public void OnClickHandler()
197+
{
198+
if (this._mailMergeEditor != null)
199+
{
200+
var editorContent = this._mailMergeEditor.Value;
201+
var mergedContent = ReplacePlaceholders(editorContent, this._placeholderData);
202+
_rteValue = mergedContent;
203+
}
204+
}
205+
public static string ReplacePlaceholders(string template, Dictionary<string, string> data)
206+
{
207+
return Regex.Replace(template, @"{{\s*(\w+)\s*}}", match =>
208+
{
209+
string key = match.Groups[1].Value.Trim();
210+
return data.TryGetValue(key, out var value) ? value : match.Value;
211+
});
212+
}
213+
214+
```
215+
This ensures all placeholders are dynamically replaced without manual editing.
216+
180217
{% highlight cshtml %}
181218

182219
{% include_relative code-snippet/mail-merge.razor %}

0 commit comments

Comments
 (0)