-
Notifications
You must be signed in to change notification settings - Fork 81
kb(Editor): Add KB for focus event handling #3179
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 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,174 @@ | ||
| --- | ||
| title: Editor Focus Event | ||
| description: Learn how to subscribe to the Editor focus event. | ||
| type: how-to | ||
| page_title: How to Handle the Editor Focus Event | ||
| slug: editor-kb-focus-event | ||
| tags: telerik, blazor, editor, events | ||
| ticketid: 1695979 | ||
| res_type: kb | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>Editor for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| This KB article answers the following questions: | ||
|
|
||
| * How to detect when the user focuses the Editor content? | ||
| * How to handle the Editor focus event? | ||
| * Is it possible to do a Blazor action on Editor OnFocus? | ||
|
|
||
| ## Solution | ||
|
|
||
| The required approach depends on the [Editor `EditMode`](slug:editor-edit-modes-overview), due to the different Editor HTML rendering: | ||
|
|
||
| * [Using `Div` edit mode](#div-editmode) | ||
| * [Using `Iframe` edit mode](#iframe-editmode) | ||
|
|
||
| ### `Div` EditMode | ||
|
|
||
| 1. Learn how to [call JavaScript code from C# code](https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet). | ||
| 1. Wrap the Editor in a container with an `@onfocusin` event handler. | ||
| 1. Use JavaScript to check if the Editor content `<div class="k-content ProseMirror">` is focused. | ||
|
|
||
| >caption Detect Editor focus when using Div EditMode | ||
|
|
||
| ````RAZOR | ||
| @using Telerik.Blazor.Components.Editor | ||
|
|
||
| @inject IJSRuntime js | ||
|
|
||
| <div @onfocusin="@OnEditorFocus1"> | ||
| <TelerikEditor @bind-Value="@EditorValue1" | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Tools="@EditorToolSets.All" | ||
| EditMode="@EditorEditMode.Div" | ||
| Height="300px"> | ||
| </TelerikEditor> | ||
| </div> | ||
|
|
||
| @* Move JavaScript code to a separate JS file *@ | ||
| <script suppress-error="BL9992"> | ||
| function isEditorDivFocused() { | ||
| return document.activeElement && | ||
| document.activeElement.classList.contains("k-content") && | ||
| document.activeElement.classList.contains("ProseMirror"); | ||
| } | ||
|
|
||
| </script> | ||
|
|
||
| @code { | ||
| #nullable enable | ||
|
|
||
| private string EditorValue1 { get; set; } = @"<p>foo 1</p><p>bar 1</p>"; | ||
|
|
||
| private async Task OnEditorFocus1() | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| bool isEditorContentFocused = await js.InvokeAsync<bool>("isEditorDivFocused"); | ||
|
|
||
| if (isEditorContentFocused) | ||
| { | ||
| EditorValue1 = $"<p>Editor content DIV was focused at {DateTime.Now.ToLongTimeString()}.</p>"; | ||
| } | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ### `Iframe` EditMode | ||
|
|
||
| 1. Learn how to [call C# code from JavaScript code](https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-dotnet-from-javascript) and [vice-versa](https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet). | ||
| 1. Use the first firing of `OnAfterRenderAsync` to: | ||
| 1. Obtain the `<body>` element inside the Editor content `iframe`. | ||
| 1. [Subscribe](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) to the `focusin` event to the `iframe` `body`. | ||
| 1. In the JavaScript `focusin` handler, call a C# method. | ||
|
|
||
| >caption Detect Editor focus when using Iframe EditMode | ||
|
|
||
| ````RAZOR | ||
| @using Telerik.Blazor.Components.Editor | ||
|
|
||
| @implements IDisposable | ||
|
|
||
| @inject IJSRuntime js | ||
|
|
||
| <TelerikEditor @bind-Value="@EditorValue2" | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Tools="@EditorToolSets.All" | ||
| Id="editor2" | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Height="300px"> | ||
| </TelerikEditor> | ||
|
|
||
| @* Move JavaScript code to a separate JS file *@ | ||
| <script suppress-error="BL9992"> | ||
| var dotNet; | ||
|
|
||
| function attachIframeFocusHandler(id, dotNetRef) { | ||
| var editor = document.getElementById(id); | ||
| if (editor) { | ||
| dotNet = dotNetRef; | ||
| editor.querySelector("iframe").contentWindow.document.body.addEventListener("focusin", OnEditorBodyFocus); | ||
| } | ||
| } | ||
|
|
||
| function OnEditorBodyFocus(args) { | ||
| dotNet.invokeMethodAsync("OnEditorFocus2"); | ||
dimodi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function detachIframeFocusHandler(id) { | ||
| var editor = document.getElementById(id); | ||
| if (editor) { | ||
| editor.querySelector("iframe").contentWindow.document.body.removeEventListener("focusin", OnEditorBodyFocus); | ||
| } | ||
| } | ||
| </script> | ||
|
|
||
| @code { | ||
| #nullable enable | ||
|
|
||
| private string EditorValue2 { get; set; } = @"<p>foo 2</p><p>bar 2</p>"; | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Replace __Main with your Razor component type | ||
| private DotNetObjectReference<__Main>? DotNetRef { get; set; } | ||
|
|
||
| [JSInvokable("OnEditorFocus2")] | ||
| public void OnEditorFocus2() | ||
dimodi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| EditorValue2 = $"<p>Editor content IFRAME was focused at {DateTime.Now.ToLongTimeString()}.</p>"; | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| StateHasChanged(); | ||
| } | ||
|
|
||
| protected override void OnInitialized() | ||
| { | ||
| DotNetRef = DotNetObjectReference.Create(this); | ||
| } | ||
|
|
||
| protected override async Task OnAfterRenderAsync(bool firstRender) | ||
| { | ||
| if (firstRender) | ||
| { | ||
| await Task.Delay(1); // wait for HTML to render | ||
|
|
||
| await js.InvokeVoidAsync("attachIframeFocusHandler", "editor2", DotNetRef); | ||
dimodi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| await base.OnAfterRenderAsync(firstRender); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| DotNetRef?.Dispose(); | ||
| } | ||
| } | ||
| ```` | ||
|
|
||
| ## See Also | ||
|
|
||
| * [Editor Events](slug:editor-events) | ||
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.