-
Notifications
You must be signed in to change notification settings - Fork 80
Added new kb article editor-add-class-to-selected-text #2810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
kendo-bot
wants to merge
2
commits into
master
from
new-kb-editor-add-class-to-selected-text-25a1c0ddd6364650b69778245f8bc0a7
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| --- | ||
| title: How to Add CSS Class to Selected Text with Editor Custom Tool | ||
| description: Learn how to add CSS classes to elements using custom toolbar tools in the Telerik Editor for Blazor, enhancing the styling capabilities of your application. | ||
| type: how-to | ||
| page_title: How to Add CSS Class to Selected Text with Editor Custom Tool | ||
| slug: editor-kb-add-class-to-selected-text | ||
| tags: editor, blazor, custom tool | ||
| res_type: kb | ||
| ticketid: 1679881 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Editor for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| In certain situations, you might want to style the content within the [Editor for Blazor](slug:editor-overview) without relying on inline styles. Specifically, you might need to add CSS class to specific element of the Editor cotent. This knowledge base article guides you on how to add a [Editor Custom Tool](slug:editor-custom-tools) that applies a `<p>` and a CSS class to selected text. | ||
|
|
||
| This knowledge base article also answers the following questions: | ||
|
|
||
| - How can I enhance text styling in the Editor for Blazor using CSS classes? | ||
| - Is it possible to add a toolbar button in the Editor for Blazor that applies styling classes? | ||
| - How do I use custom tools in the Editor for Blazor to manipulate text styling? | ||
|
|
||
| ## Solution | ||
|
|
||
| To add a custom CSS class to a `<p>` element or any other element using the Editor toolbar button, create a custom tool that applies the desired class. The following steps and example code illustrate how to achieve this functionality: | ||
|
|
||
| 1. Define a Custom Tool in the Editor toolbar. This tool will specifically target the selected text and apply a CSS class to it. | ||
|
|
||
| 2. Use JavaScript Interop to get the selected text. | ||
|
|
||
| ````RAZOR | ||
| @using Telerik.Blazor.Components.Editor | ||
| @inject IJSRuntime js | ||
|
|
||
| <TelerikEditor Tools="@Tools" @bind-Value="@TheEditorContent" EditMode="@EditorEditMode.Div" @ref="@EditorRef"> | ||
| <EditorCustomTools> | ||
| <EditorCustomTool Name="GetSelectedText"> | ||
| <TelerikButton OnClick="@ApplyImportantClass">Set span and class</TelerikButton> | ||
| </EditorCustomTool> | ||
| </EditorCustomTools> | ||
| </TelerikEditor> | ||
|
|
||
| <script> | ||
| window.getSelectedText = function () { | ||
| return window.getSelection().toString(); | ||
| } | ||
| </script> | ||
|
|
||
| <style> | ||
| .@CustomClass { | ||
| color: red; | ||
| } | ||
| </style> | ||
|
|
||
| @code { | ||
| private string TheEditorContent { get; set; } = "<h1>Sample Text</h1><p>More content here.</p>"; | ||
| private List<IEditorTool>? Tools { get; set; } | ||
| private TelerikEditor? EditorRef { get; set; } | ||
| private string CustomClass { get; set; } = "custom-content"; | ||
|
|
||
| private async Task ApplyImportantClass() | ||
| { | ||
| var selectedText = await js.InvokeAsync<string>("getSelectedText"); | ||
|
|
||
| if (!string.IsNullOrEmpty(selectedText)) | ||
| { | ||
| // Apply a <span> with the class without modifying the actual HTML content manually | ||
| await EditorRef!.ExecuteAsync(new HtmlCommandArgs("insertHtml", $"<p class='{CustomClass}'>{selectedText}</p>", false)); | ||
| } | ||
| } | ||
|
|
||
| protected override Task OnInitializedAsync() | ||
| { | ||
| Tools = new List<IEditorTool>(EditorToolSets.Default); | ||
| Tools.Add(new CustomTool("GetSelectedText")); | ||
|
|
||
| return base.OnInitializedAsync(); | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| - [Editor Overview](slug:editor-overview) | ||
| - [JavaScript Interop in Blazor](https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability) | ||
| - [Editor Custom Tools](slug:editor-custom-tools) | ||
| - [How to Get Selected Text in Editor](slug:editor-kb-get-selection) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.