You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The **Mail Merge** feature in Syncfusion Blazor Rich Text Editor enables you 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. Instead of manually editing each document, you can design a single template and populate it with user-specific data automatically.
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
13
14
-
To achieve this, the Rich Text Editor is customized with **custom toolbar items** and integrated with the **Mention control**. The toolbar provides two key options:
15
-
-**Insert Field**: A dropdown menu that allows users to insert predefined merge fields like `{{FirstName}}` or `{{Email}}` into the editor.
16
-
-**Merge Data**: A button that triggers the process of replacing all placeholders in the editor with actual values from a data source (e.g., a dictionary or database).
14
+
## Key Outcomes:
17
15
18
-
The Mention control enhances usability by enabling users to type a special character (like `{`) and select merge fields from a popup list. This combination of toolbar customization and mention functionality makes the editor highly interactive and suitable for mail merge scenarios.
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.
19
19
20
-
## How Custom Toolbar Items Work
20
+
## Rendering Custom Toolbar Items
21
21
22
-
Custom toolbar items are added using the `RichTextEditorCustomToolbarItems` tag inside the editor. Each item is defined with a **Name** and a **Template**. The template can include any Blazor control, such as a button or dropdown. Event handlers like `OnClickHandler` and `OnItemSelect` are used to perform actions when these items are clicked or selected.
22
+
Custom toolbar items are added using the [RichTextEditorCustomToolbarItems](https://help.syncfusion.com/cr/blazor/Syncfusion.Blazor.RichTextEditor.RichTextEditorCustomToolbarItems.html) tag. Each item is defined with:
23
23
24
-
-**Merge Data Button**: Executes the `OnClickHandler` method, which retrieves the editor content and replaces placeholders using a regex-based function.
25
-
-**Insert Field Dropdown**: Displays a list of merge fields. When a field is selected, the `OnItemSelect` method inserts the corresponding placeholder into the editor at the current cursor position.
24
+
-**Name**: Identifier for the toolbar item.
25
+
-**Template**: Razor markup for rendering UI elements such as buttons or dropdowns.
This ensures all placeholders are dynamically replaced without manual editing.
79
+
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:
83
+
84
+
- The `InsertField()` method retrieves the corresponding field value.
85
+
- It constructs an HTML snippet with a non-editable span containing the placeholder.
86
+
- 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_).
This feature is ideal for users who prefer keyboard-driven workflows.
125
+
126
+
## Maintaining Cursor Position During Dropdown Operations
127
+
128
+
When the `Insert Field` dropdown opens, the editor loses its current selection because focus shifts to the popup. To ensure the placeholder is inserted at the correct position:
129
+
130
+
-**SaveSelectionAsync()** is called when the dropdown opens. This stores the current cursor position in the editor before focus changes.
131
+
-**RestoreSelectionAsync()** is called when the dropdown closes. This restores the saved cursor position so that the next insertion happens exactly where the user intended.
132
+
133
+
**Why is this important?** Without saving and restoring the selection, placeholders might be inserted at the wrong location (e.g., at the end of the content), breaking the user experience.
## Handling Editor Mode Changes with OnActionComplete
154
+
155
+
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.
156
+
157
+
- When entering **Source Code mode**, custom toolbar buttons (Merge Data, Insert Field) should be disabled because HTML editing is manual in this mode.
158
+
- When returning to **Preview mode**, these buttons are re-enabled for normal usage.
**Why is this important?** This prevents users from triggering merge operations or inserting fields while editing raw HTML, which could cause unexpected behavior.
0 commit comments