From 7077740e9100ca3fb0a7e3ae58090c42686f5c8e Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 15 Sep 2022 14:34:54 +0100 Subject: [PATCH] initial implementation of ime interop for web --- .../Interop/InputHelperInterop.cs | 21 +++++++++++++++ .../webapp/modules/Avalonia/InputHelper.ts | 27 ++++++++++++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs b/src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs index ebc80618b1..ca09a4ba78 100644 --- a/src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs +++ b/src/Web/Avalonia.Web.Blazor/Interop/InputHelperInterop.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; namespace Avalonia.Web.Blazor.Interop { @@ -9,14 +10,24 @@ namespace Avalonia.Web.Blazor.Interop private const string SetCursorSymbol = "InputHelper.setCursor"; private const string HideSymbol = "InputHelper.hide"; private const string ShowSymbol = "InputHelper.show"; + private const string StartSymbol = "InputHelper.start"; private readonly AvaloniaModule _module; private readonly ElementReference _inputElement; + private readonly ActionHelper _actionHelper; + private DotNetObjectReference? callbackReference; public InputHelperInterop(AvaloniaModule module, ElementReference inputElement) { _module = module; _inputElement = inputElement; + + _actionHelper = new ActionHelper(() => + { + + }); + + Start(); } public void Clear() => _module.Invoke(ClearSymbol, _inputElement); @@ -28,5 +39,15 @@ namespace Avalonia.Web.Blazor.Interop public void Hide() => _module.Invoke(HideSymbol, _inputElement); public void Show() => _module.Invoke(ShowSymbol, _inputElement); + + private void Start() + { + if(callbackReference != null) + return; + + callbackReference = DotNetObjectReference.Create(_actionHelper); + + _module.Invoke(StartSymbol, _inputElement, callbackReference); + } } } diff --git a/src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts b/src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts index 8619fc8cee..21779449e6 100644 --- a/src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts +++ b/src/Web/Avalonia.Web.Blazor/webapp/modules/Avalonia/InputHelper.ts @@ -1,8 +1,18 @@ export class InputHelper { + static callback?: DotNet.DotNetObject; + + public static start(inputElement: HTMLInputElement, callback: DotNet.DotNetObject) + { + InputHelper.callback = callback; + + inputElement.addEventListener('compositionstart', InputHelper.onCompositionEvent); + inputElement.addEventListener('compositionupdate', InputHelper.onCompositionEvent); + inputElement.addEventListener('compositionend', InputHelper.onCompositionEvent); + } + public static clear(inputElement: HTMLInputElement) { inputElement.value = ""; } - public static isInputElement( element : HTMLInputElement | HTMLElement ) : element is HTMLInputElement { return ( element as HTMLInputElement).setSelectionRange !== undefined; @@ -28,4 +38,19 @@ public static show(inputElement: HTMLInputElement) { inputElement.style.display = 'block'; } + + private static onCompositionEvent(ev: CompositionEvent) + { + if(!InputHelper.callback) + return; + + switch (ev.type) + { + case "compositionstart": + case "compositionupdate": + case "compositionend": + InputHelper.callback.invokeMethod('Invoke'); + break; + } + } }