Skip to content

Commit 81dd341

Browse files
KB article on how to get the selected content from the editor (#172)
* feat(kb): how to get the selected content from the editor * chore(kb): remove redundant wording * chore(kb): improve wording * kb(editor): improve get selection article Co-authored-by: Marin Bratanov <[email protected]>
1 parent f147f90 commit 81dd341

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
---
2+
title: Get the content of the Editor selected by the user
3+
description: How to get the content of the Editor selected by the user
4+
type: how-to
5+
page_title: Get the user selection in the Editor
6+
slug: editor-kb-get-selection
7+
position:
8+
tags:
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>Editor for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
23+
## Description
24+
25+
I would like to get the content of the Editor selected (highlighted) by the user to perform some custom formatting.
26+
27+
28+
## Solution
29+
30+
In order to receive or modify the selected (highlighted) content of the Editor, you need to use JavaScript. The `selection` object is inherently JavaScript-based because HTML editing is based on the rich text editing engine of the browser, and so it has no .NET counterpart.
31+
32+
Since the Editor has two different edit modes - [Div]({%slug editor-edit-modes-div%}) and [Iframe]({%slug editor-edit-modes-iframe%}), the examples below will showcase how to get the selected text for both of them.
33+
34+
## Examples
35+
36+
* [Div Mode](#div-mode)
37+
* [Iframe Mode](#iframe-mode)
38+
39+
### Div Mode
40+
41+
To get the selected text from an Editor in `Div mode` you should use the `getSelection()` method of the `window`.
42+
43+
At this point, you can apply changes to it with JavaScript.
44+
45+
If you want to use it on the .NET (Blazor) side, you need to:
46+
47+
1. Serialize the selection to a string so .NET can understand it, by using the `toString()` method.
48+
1. Call a JavaScript function from a [Custom Tool]({%slug editor-custom-tool%}) in the Editor that will return that selection.
49+
50+
````Component
51+
@using Telerik.Blazor.Components.Editor
52+
53+
@inject IJSRuntime js
54+
55+
<TelerikEditor Tools="@Tools" @bind-Value="@TheEditorContent" EditMode="@EditorEditMode.Iframe">
56+
<EditorCustomTools>
57+
<EditorCustomTool Name="GetSelectedText">
58+
<TelerikButton OnClick="@GetSelectedText">Get selected text</TelerikButton>
59+
</EditorCustomTool>
60+
</EditorCustomTools>
61+
</TelerikEditor>
62+
63+
64+
Selected text: @SelectedText
65+
66+
@code {
67+
string TheEditorContent { get; set; } = "<h1>Lorem ipsum</h1><p>Dolor sit amet.</p>";
68+
List<IEditorTool> Tools { get; set; }
69+
public string SelectedText { get; set; }
70+
71+
async Task GetSelectedText()
72+
{
73+
SelectedText = await js.InvokeAsync<string>("getSelectedText");
74+
}
75+
76+
protected override Task OnInitializedAsync()
77+
{
78+
Tools = new List<IEditorTool>(EditorToolSets.Default);
79+
80+
// register the custom tool for the toolbar - it uses the Name parameter from the markup
81+
Tools.Add(new CustomTool("GetSelectedText"));
82+
83+
return base.OnInitializedAsync();
84+
}
85+
}
86+
````
87+
````JavaScript
88+
function getSelectedText() {
89+
return window.getSelection().toString();
90+
}
91+
````
92+
93+
### Iframe Mode
94+
95+
To get the selected text from an Editor in `Iframe mode` you need to:
96+
97+
1. Select the DOM element that holds the editor `<iframe>` element by using a `querySelector()` call with a suitable CSS selector.
98+
1. Use the `getSelection()` method available for the `contentDocument` of the `iframe`.
99+
100+
101+
At this point, you can apply changes to it with JavaScript.
102+
103+
If you want to use it on the .NET (Blazor) side, you need to:
104+
105+
1. Serialize the selection to a string so .NET can understand it, by using the `toString()` method.
106+
1. Call a JavaScript function from a [Custom Tool]({%slug editor-custom-tool%}) in the Editor that will return that selection.
107+
108+
````Component
109+
@using Telerik.Blazor.Components.Editor
110+
111+
@inject IJSRuntime js
112+
113+
<TelerikEditor Tools="@Tools" @bind-Value="@TheEditorContent" EditMode="@EditorEditMode.Iframe">
114+
<EditorCustomTools>
115+
<EditorCustomTool Name="GetSelectedText">
116+
<TelerikButton OnClick="@GetSelectedText">Get selected text</TelerikButton>
117+
</EditorCustomTool>
118+
</EditorCustomTools>
119+
</TelerikEditor>
120+
121+
122+
Selected text: @SelectedText
123+
124+
@code {
125+
string TheEditorContent { get; set; } = "<h1>Lorem ipsum</h1><p>Dolor sit amet.</p>";
126+
List<IEditorTool> Tools { get; set; }
127+
public string SelectedText { get; set; }
128+
129+
async Task GetSelectedText()
130+
{
131+
SelectedText = await js.InvokeAsync<string>("getSelectedText");
132+
}
133+
134+
protected override Task OnInitializedAsync()
135+
{
136+
Tools = new List<IEditorTool>(EditorToolSets.Default);
137+
138+
// register the custom tool for the toolbar - it uses the Name parameter from the markup
139+
Tools.Add(new CustomTool("GetSelectedText"));
140+
141+
return base.OnInitializedAsync();
142+
}
143+
}
144+
````
145+
````JavaScript
146+
function getSelectedText() {
147+
var editorIframe = document.querySelector(".k-editor iframe");
148+
return editorIframe.contentDocument.getSelection().toString();
149+
}
150+
````
151+
152+
## Notes
153+
154+
The browser selection exists only in the browser. Sending it to the Blazor app will remove all its functionality and its context. For example, you no longer have access to its start and end, you cannot alter them, and it will be up to your C# code to determine where exactly that selection is in the content and how to modify the entire content to both produce the desired results, and remain valid HTML. With this in mind, consider applying changes directly with JavaScript to the selection object.
155+
156+
157+

0 commit comments

Comments
 (0)