From 1298f7f0ab881d9cc701d53872867d74577d6b9b Mon Sep 17 00:00:00 2001 From: Johannes Buchholz Date: Wed, 27 Sep 2023 17:15:27 +0200 Subject: [PATCH] finer excludes with css selector --- HotKeys2.Test/HotKeyEntryTest.cs | 12 ++-- HotKeys2/HotKeyEntry.cs | 14 +++- HotKeys2/HotKeyEntryByCode.cs | 10 +-- HotKeys2/HotKeyEntryByKey.cs | 10 +-- HotKeys2/HotKeysContext.cs | 112 +++++++++++++++++++------------ HotKeys2/script.js | 9 ++- HotKeys2/script.ts | 9 ++- HotKeys2/wwwroot/script.min.js | 2 +- 8 files changed, 110 insertions(+), 68 deletions(-) diff --git a/HotKeys2.Test/HotKeyEntryTest.cs b/HotKeys2.Test/HotKeyEntryTest.cs index a95330e..dfe5f83 100644 --- a/HotKeys2.Test/HotKeyEntryTest.cs +++ b/HotKeys2.Test/HotKeyEntryTest.cs @@ -5,34 +5,34 @@ public class HotKeyEntryTest [Test] public void Mode_for_ByKey_Test() { - var hotKeyEntry = new HotKeyEntryByKey(ModKey.None, Key.Question, Exclude.None, "Show help.", _ => ValueTask.CompletedTask); + var hotKeyEntry = new HotKeyEntryByKey(ModKey.None, Key.Question, Exclude.None, "", "Show help.", _ => ValueTask.CompletedTask); hotKeyEntry.Mode.Is(HotKeyMode.ByKey); } [Test] public void Mode_for_ByCode_Test() { - var hotKeyEntry = new HotKeyEntryByCode(ModCode.Ctrl | ModCode.Alt, Code.F12, Exclude.None, "Set the volume level to 10.", _ => ValueTask.CompletedTask); + var hotKeyEntry = new HotKeyEntryByCode(ModCode.Ctrl | ModCode.Alt, Code.F12, Exclude.None, "", "Set the volume level to 10.", _ => ValueTask.CompletedTask); hotKeyEntry.Mode.Is(HotKeyMode.ByCode); } [Test] public void ToString_for_ByKey_Test() { - var hotKeyEntry = new HotKeyEntryByKey(ModKey.None, Key.Question, Exclude.None, "Show help.", _ => ValueTask.CompletedTask); + var hotKeyEntry = new HotKeyEntryByKey(ModKey.None, Key.Question, Exclude.None, "", "Show help.", _ => ValueTask.CompletedTask); hotKeyEntry.ToString().Is("?: Show help."); } [Test] public void ToString_for_ByCode_Test() { - var ctrl_alt_f12 = new HotKeyEntryByCode(ModCode.Ctrl | ModCode.Alt, Code.F12, Exclude.None, "Set the volume level to 10.", _ => ValueTask.CompletedTask); + var ctrl_alt_f12 = new HotKeyEntryByCode(ModCode.Ctrl | ModCode.Alt, Code.F12, Exclude.None, "", "Set the volume level to 10.", _ => ValueTask.CompletedTask); ctrl_alt_f12.ToString().Is("Ctrl + Alt + F12: Set the volume level to 10."); - var meta_one = new HotKeyEntryByCode(ModCode.Meta, Code.Num1, Exclude.None, "Launch the notepad.", _ => ValueTask.CompletedTask); + var meta_one = new HotKeyEntryByCode(ModCode.Meta, Code.Num1, Exclude.None, "", "Launch the notepad.", _ => ValueTask.CompletedTask); meta_one.ToString().Is("Meta + 1: Launch the notepad."); - var u = new HotKeyEntryByCode(ModCode.None, Code.U, Exclude.None, "Increment counter.", _ => ValueTask.CompletedTask); + var u = new HotKeyEntryByCode(ModCode.None, Code.U, Exclude.None, "", "Increment counter.", _ => ValueTask.CompletedTask); u.ToString().Is("U: Increment counter."); } } diff --git a/HotKeys2/HotKeyEntry.cs b/HotKeys2/HotKeyEntry.cs index 73783cb..a57d22b 100644 --- a/HotKeys2/HotKeyEntry.cs +++ b/HotKeys2/HotKeyEntry.cs @@ -22,6 +22,11 @@ public abstract class HotKeyEntry : IDisposable /// public Exclude Exclude { get; } + /// + /// Get Additional CSS selector for HTML elements that will not allow hotkey to work. + /// + public string ExcludeSelector { get; } + /// /// Get the description of the meaning of this hot key entry. /// @@ -58,10 +63,11 @@ public abstract class HotKeyEntry : IDisposable /// The combination of modifier flags /// The key or code of the hot key /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. [DynamicDependency(nameof(InvokeAction), typeof(HotKeyEntry)), Obsolete] - internal HotKeyEntry(HotKeyMode mode, Type typeOfModifiers, int modifiers, string keyEntry, Exclude exclude, string? description) - : this(null, mode, typeOfModifiers, modifiers, keyEntry, exclude, description, null) { } + internal HotKeyEntry(HotKeyMode mode, Type typeOfModifiers, int modifiers, string keyEntry, Exclude exclude, string excludeSelector, string? description) + : this(null, mode, typeOfModifiers, modifiers, keyEntry, exclude, excludeSelector, description, null) { } /// /// Initialize a new instance of the HotKeyEntry class. @@ -72,10 +78,11 @@ internal HotKeyEntry(HotKeyMode mode, Type typeOfModifiers, int modifiers, strin /// The combination of modifier flags /// The key or code of the hot key /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// The instance of a Razor component that is an owner of the callback action method. [DynamicDependency(nameof(InvokeAction), typeof(HotKeyEntry))] - internal HotKeyEntry(ILogger? logger, HotKeyMode mode, Type typeOfModifiers, int modifiers, string keyEntry, Exclude exclude, string? description, IHandleEvent? ownerOfAction) + internal HotKeyEntry(ILogger? logger, HotKeyMode mode, Type typeOfModifiers, int modifiers, string keyEntry, Exclude exclude, string excludeSelector, string? description, IHandleEvent? ownerOfAction) { this._Logger = logger; this.Mode = mode; @@ -83,6 +90,7 @@ internal HotKeyEntry(ILogger? logger, HotKeyMode mode, Type typeOfModifiers, int this._TypeOfModifiers = typeOfModifiers; this._KeyEntry = keyEntry; this.Exclude = exclude; + this.ExcludeSelector = excludeSelector; this.Description = description; this._OwnerComponent = ownerOfAction; this._ObjectRef = DotNetObjectReference.Create(this); diff --git a/HotKeys2/HotKeyEntryByCode.cs b/HotKeys2/HotKeyEntryByCode.cs index 22798d2..568c34f 100644 --- a/HotKeys2/HotKeyEntryByCode.cs +++ b/HotKeys2/HotKeyEntryByCode.cs @@ -24,9 +24,10 @@ public class HotKeyEntryByCode : HotKeyEntry /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The callback action that will be invoked when user enter modKeys + key combination on the browser. - public HotKeyEntryByCode(ModCode modCodes, Code code, Exclude exclude, string? description, Func action) - : base(null, HotKeyMode.ByCode, typeof(ModCode), (int)modCodes, code.ToString(), exclude, description, action.Target as IHandleEvent) + public HotKeyEntryByCode(ModCode modCodes, Code code, Exclude exclude, string excludeSelector, string? description, Func action) + : base(null, HotKeyMode.ByCode, typeof(ModCode), (int)modCodes, code.ToString(), exclude, excludeSelector, description, action.Target as IHandleEvent) { this.Modifiers = modCodes; this.Code = code; @@ -41,10 +42,11 @@ public HotKeyEntryByCode(ModCode modCodes, Code code, Exclude exclude, string? d /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The callback action that will be invoked when user enter modKeys + key combination on the browser. /// The instance of a Razor component that is an owner of the callback action method. - internal HotKeyEntryByCode(ILogger logger, ModCode modCodes, Code code, Exclude exclude, string? description, Func action, IHandleEvent? ownerOfAction) - : base(logger, HotKeyMode.ByCode, typeof(ModCode), (int)modCodes, code.ToString(), exclude, description, ownerOfAction) + internal HotKeyEntryByCode(ILogger logger, ModCode modCodes, Code code, Exclude exclude, string excludeSelector, string? description, Func action, IHandleEvent? ownerOfAction) + : base(logger, HotKeyMode.ByCode, typeof(ModCode), (int)modCodes, code.ToString(), exclude, excludeSelector, description, ownerOfAction) { this.Modifiers = modCodes; this.Code = code; diff --git a/HotKeys2/HotKeyEntryByKey.cs b/HotKeys2/HotKeyEntryByKey.cs index b17af7d..fe066de 100644 --- a/HotKeys2/HotKeyEntryByKey.cs +++ b/HotKeys2/HotKeyEntryByKey.cs @@ -27,8 +27,9 @@ public class HotKeyEntryByKey : HotKeyEntry /// The callback action that will be invoked when user enter modKeys + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. - public HotKeyEntryByKey(ModKey modKeys, Key key, Exclude exclude, string? description, Func action) - : base(null, HotKeyMode.ByKey, typeof(ModKey), (int)modKeys, key.ToString(), exclude, description, action.Target as IHandleEvent) + /// Additional CSS selector for HTML elements that will not allow hotkey to work. + public HotKeyEntryByKey(ModKey modKeys, Key key, Exclude exclude, string excludeSelector, string? description, Func action) + : base(null, HotKeyMode.ByKey, typeof(ModKey), (int)modKeys, key.ToString(), exclude, excludeSelector, description, action.Target as IHandleEvent) { this.Modifiers = modKeys; this.Key = key; @@ -44,9 +45,10 @@ public HotKeyEntryByKey(ModKey modKeys, Key key, Exclude exclude, string? descri /// The callback action that will be invoked when user enter modKeys + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The instance of a Razor component that is an owner of the callback action method. - internal HotKeyEntryByKey(ILogger logger, ModKey modKeys, Key key, Exclude exclude, string? description, Func action, IHandleEvent? ownerOfAction) - : base(logger, HotKeyMode.ByKey, typeof(ModKey), (int)modKeys, key.ToString(), exclude, description, ownerOfAction) + internal HotKeyEntryByKey(ILogger logger, ModKey modKeys, Key key, Exclude exclude, string excludeSelector, string? description, Func action, IHandleEvent? ownerOfAction) + : base(logger, HotKeyMode.ByKey, typeof(ModKey), (int)modKeys, key.ToString(), exclude, excludeSelector, description, ownerOfAction) { this.Modifiers = modKeys; this.Key = key; diff --git a/HotKeys2/HotKeysContext.cs b/HotKeys2/HotKeysContext.cs index 295e991..e68dde6 100644 --- a/HotKeys2/HotKeysContext.cs +++ b/HotKeys2/HotKeysContext.cs @@ -36,9 +36,10 @@ internal HotKeysContext(Task attachTask, ILogger logger) /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(Key key, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModKey.None, key, _ => { action(); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Key key, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModKey.None, key, _ => { action(); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -47,9 +48,10 @@ public HotKeysContext Add(Key key, Action action, string description = "", Exclu /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(Key key, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModKey.None, key, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Key key, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModKey.None, key, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -57,10 +59,11 @@ public HotKeysContext Add(Key key, Action action, string descr /// The identifier of hotkey by key. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Key key, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModKey.None, key, _ => action(), description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Key key, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModKey.None, key, _ => action(), description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -68,10 +71,11 @@ public HotKeysContext Add(Key key, Func action, string description = /// The identifier of hotkey by key. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Key key, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModKey.None, key, arg => action(arg), description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Key key, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModKey.None, key, arg => action(arg), description, exclude, excludeSelector, action.Target as IHandleEvent); // ----------------------------------------------------------------------------------------------- @@ -83,9 +87,10 @@ public HotKeysContext Add(Key key, Func action, str /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(ModKey modifiers, Key key, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, key, _ => { action(); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModKey modifiers, Key key, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, key, _ => { action(); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -95,9 +100,10 @@ public HotKeysContext Add(ModKey modifiers, Key key, Action action, string descr /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(ModKey modifiers, Key key, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, key, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModKey modifiers, Key key, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, key, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -106,10 +112,11 @@ public HotKeysContext Add(ModKey modifiers, Key key, Action ac /// The identifier of hotkey by key. /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(ModKey modifiers, Key key, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, key, _ => action(), description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModKey modifiers, Key key, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, key, _ => action(), description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -119,9 +126,10 @@ public HotKeysContext Add(ModKey modifiers, Key key, Func action, str /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(ModKey modifiers, Key key, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, key, action, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModKey modifiers, Key key, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, key, action, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -131,11 +139,12 @@ public HotKeysContext Add(ModKey modifiers, Key key, FuncThe callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The owner of the action. If the owner is disposed, the hotkey will be removed automatically. /// This context. - private HotKeysContext AddInternal(ModKey modifiers, Key key, Func action, string description, Exclude exclude, IHandleEvent? ownerOfAction) + private HotKeysContext AddInternal(ModKey modifiers, Key key, Func action, string description, Exclude exclude, string excludeSelector, IHandleEvent? ownerOfAction) { - lock (this.Keys) this.Keys.Add(this.Register(new HotKeyEntryByKey(this._Logger, modifiers, key, exclude, description, action, ownerOfAction))); + lock (this.Keys) this.Keys.Add(this.Register(new HotKeyEntryByKey(this._Logger, modifiers, key, exclude, excludeSelector, description, action, ownerOfAction))); return this; } @@ -147,10 +156,11 @@ private HotKeysContext AddInternal(ModKey modifiers, Key key, FuncThe identifier of hotkey by code. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Code code, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModCode.None, code, arg => { action(); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Code code, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModCode.None, code, arg => { action(); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -158,10 +168,11 @@ public HotKeysContext Add(Code code, Action action, string description = "", Exc /// The identifier of hotkey by code. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Code code, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModCode.None, code, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Code code, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModCode.None, code, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -169,10 +180,11 @@ public HotKeysContext Add(Code code, Action action, string de /// The identifier of hotkey by code. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Code code, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModCode.None, code, _ => action(), description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Code code, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModCode.None, code, _ => action(), description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -180,10 +192,11 @@ public HotKeysContext Add(Code code, Func action, string description /// The identifier of hotkey by code. /// The callback action that will be invoked when user enter the key without any modifiers on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(Code code, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(ModCode.None, code, action, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(Code code, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(ModCode.None, code, action, description, exclude, excludeSelector, action.Target as IHandleEvent); // ----------------------------------------------------------------------------------------------- @@ -194,10 +207,11 @@ public HotKeysContext Add(Code code, Func action, /// The identifier of hotkey by code. /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(ModCode modifiers, Code code, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, code, arg => { action(); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModCode modifiers, Code code, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, code, arg => { action(); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -206,10 +220,11 @@ public HotKeysContext Add(ModCode modifiers, Code code, Action action, string de /// The identifier of hotkey by code. /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(ModCode modifiers, Code code, Action action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, code, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModCode modifiers, Code code, Action action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, code, arg => { action(arg); return ValueTask.CompletedTask; }, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -218,10 +233,11 @@ public HotKeysContext Add(ModCode modifiers, Code code, ActionThe identifier of hotkey by code. /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The description of the meaning of this hot key entry. /// This context. - public HotKeysContext Add(ModCode modifiers, Code code, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, code, _ => action(), description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModCode modifiers, Code code, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, code, _ => action(), description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -231,9 +247,10 @@ public HotKeysContext Add(ModCode modifiers, Code code, Func action, /// The callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Add(ModCode modifiers, Code code, Func action, string description = "", Exclude exclude = Exclude.Default) - => this.AddInternal(modifiers, code, action, description, exclude, action.Target as IHandleEvent); + public HotKeysContext Add(ModCode modifiers, Code code, Func action, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.AddInternal(modifiers, code, action, description, exclude, excludeSelector, action.Target as IHandleEvent); /// /// Add a new hotkey entry to this context. @@ -243,11 +260,12 @@ public HotKeysContext Add(ModCode modifiers, Code code, FuncThe callback action that will be invoked when user enter modifiers + key combination on the browser. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// The instance of a Razor component that is an owner of the callback action method. /// This context. - private HotKeysContext AddInternal(ModCode modifiers, Code code, Func action, string description, Exclude exclude, IHandleEvent? ownerOfAction) + private HotKeysContext AddInternal(ModCode modifiers, Code code, Func action, string description, Exclude exclude, string excludeSelector, IHandleEvent? ownerOfAction) { - lock (this.Keys) this.Keys.Add(this.Register(new HotKeyEntryByCode(this._Logger, modifiers, code, exclude, description, action, ownerOfAction))); + lock (this.Keys) this.Keys.Add(this.Register(new HotKeyEntryByCode(this._Logger, modifiers, code, exclude, excludeSelector, description, action, ownerOfAction))); return this; } @@ -262,7 +280,7 @@ private HotKeyEntry Register(HotKeyEntry hotKeyEntry) { return t.Result.InvokeAsync( "Toolbelt.Blazor.HotKeys2.register", - hotKeyEntry._ObjectRef, hotKeyEntry.Mode, hotKeyEntry._Modifiers, hotKeyEntry._KeyEntry, hotKeyEntry.Exclude).AsTask(); + hotKeyEntry._ObjectRef, hotKeyEntry.Mode, hotKeyEntry._Modifiers, hotKeyEntry._KeyEntry, hotKeyEntry.Exclude, hotKeyEntry.ExcludeSelector).AsTask(); } else { @@ -308,9 +326,10 @@ private void Unregister(HotKeyEntry hotKeyEntry) /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Remove(Key key, string description = "", Exclude exclude = Exclude.Default) => - this.Remove(ModKey.None, key, description, exclude); + public HotKeysContext Remove(Key key, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") => + this.Remove(ModKey.None, key, description, exclude, excludeSelector); /// /// Remove one or more hotkey entries from this context. @@ -319,8 +338,9 @@ public HotKeysContext Remove(Key key, string description = "", Exclude exclude = /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Remove(ModKey modifiers, Key key, string description = "", Exclude exclude = Exclude.Default) + public HotKeysContext Remove(ModKey modifiers, Key key, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") { var keyEntry = key.ToString(); return this.Remove(keys => keys @@ -329,7 +349,8 @@ public HotKeysContext Remove(ModKey modifiers, Key key, string description = "", k => k.Modifiers == modifiers && k.Key.ToString() == keyEntry && k.Description == description && - k.Exclude == exclude)); + k.Exclude == exclude && + k.ExcludeSelector == excludeSelector)); } /// @@ -338,9 +359,10 @@ public HotKeysContext Remove(ModKey modifiers, Key key, string description = "", /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Remove(Code code, string description = "", Exclude exclude = Exclude.Default) - => this.Remove(ModCode.None, code, description, exclude); + public HotKeysContext Remove(Code code, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") + => this.Remove(ModCode.None, code, description, exclude, excludeSelector); /// /// Remove one or more hotkey entries from this context. @@ -349,8 +371,9 @@ public HotKeysContext Remove(Code code, string description = "", Exclude exclude /// The identifier of hotkey. /// The description of the meaning of this hot key entry. /// The combination of HTML element flags that will be not allowed hotkey works. + /// Additional CSS selector for HTML elements that will not allow hotkey to work. /// This context. - public HotKeysContext Remove(ModCode modifiers, Code code, string description = "", Exclude exclude = Exclude.Default) + public HotKeysContext Remove(ModCode modifiers, Code code, string description = "", Exclude exclude = Exclude.Default, string excludeSelector = "") { var keyEntry = code.ToString(); return this.Remove(keys => keys @@ -359,6 +382,7 @@ public HotKeysContext Remove(ModCode modifiers, Code code, string description = k => k.Modifiers == modifiers && k.Code.ToString() == keyEntry && k.Description == description && + k.ExcludeSelector == excludeSelector && k.Exclude == exclude)); } diff --git a/HotKeys2/script.js b/HotKeys2/script.js index e7de6bd..7070db7 100644 --- a/HotKeys2/script.js +++ b/HotKeys2/script.js @@ -5,12 +5,13 @@ export var Toolbelt; var HotKeys2; (function (HotKeys2) { class HotkeyEntry { - constructor(dotNetObj, mode, modifiers, keyEntry, exclude) { + constructor(dotNetObj, mode, modifiers, keyEntry, exclude, excludeSelector) { this.dotNetObj = dotNetObj; this.mode = mode; this.modifiers = modifiers; this.keyEntry = keyEntry; this.exclude = exclude; + this.excludeSelector = excludeSelector; } action() { this.dotNetObj.invokeMethodAsync('InvokeAction'); @@ -18,9 +19,9 @@ export var Toolbelt; } let idSeq = 0; const hotKeyEntries = new Map(); - HotKeys2.register = (dotNetObj, mode, modifiers, keyEntry, exclude) => { + HotKeys2.register = (dotNetObj, mode, modifiers, keyEntry, exclude, excludeSelector) => { const id = idSeq++; - const hotKeyEntry = new HotkeyEntry(dotNetObj, mode, modifiers, keyEntry, exclude); + const hotKeyEntry = new HotkeyEntry(dotNetObj, mode, modifiers, keyEntry, exclude, excludeSelector); hotKeyEntries.set(id, hotKeyEntry); return id; }; @@ -78,6 +79,8 @@ export var Toolbelt; return; if (!isAllowedIn(entry, srcElement, tagName, type)) return; + if (entry.excludeSelector !== '' && srcElement.matches(entry.excludeSelector)) + return; preventDefault = true; entry.action(); }); diff --git a/HotKeys2/script.ts b/HotKeys2/script.ts index d2e8551..54316f5 100644 --- a/HotKeys2/script.ts +++ b/HotKeys2/script.ts @@ -28,7 +28,8 @@ public mode: HotKeyMode, public modifiers: ModCodes, public keyEntry: string, - public exclude: Exclude + public exclude: Exclude, + public excludeSelector: string ) { } public action(): void { @@ -39,9 +40,9 @@ let idSeq: number = 0; const hotKeyEntries = new Map(); - export const register = (dotNetObj: any, mode: HotKeyMode, modifiers: ModCodes, keyEntry: string, exclude: Exclude): number => { + export const register = (dotNetObj: any, mode: HotKeyMode, modifiers: ModCodes, keyEntry: string, exclude: Exclude, excludeSelector: string): number => { const id = idSeq++; - const hotKeyEntry = new HotkeyEntry(dotNetObj, mode, modifiers, keyEntry, exclude); + const hotKeyEntry = new HotkeyEntry(dotNetObj, mode, modifiers, keyEntry, exclude, excludeSelector); hotKeyEntries.set(id, hotKeyEntry); return id; } @@ -104,6 +105,8 @@ if (!isAllowedIn(entry, srcElement, tagName, type)) return; + if (entry.excludeSelector !== '' && srcElement.matches(entry.excludeSelector)) return; + preventDefault = true; entry.action(); }); diff --git a/HotKeys2/wwwroot/script.min.js b/HotKeys2/wwwroot/script.min.js index 88a070c..09e8722 100644 --- a/HotKeys2/wwwroot/script.min.js +++ b/HotKeys2/wwwroot/script.min.js @@ -1 +1 @@ -export var Toolbelt;(function(n){var t;(function(n){var t;(function(n){class f{constructor(n,t,i,r,u){this.dotNetObj=n;this.mode=t;this.modifiers=i;this.keyEntry=r;this.exclude=u}action(){this.dotNetObj.invokeMethodAsync("InvokeAction")}}let e=0;const t=new Map;n.register=(n,i,r,u,o)=>{const s=e++,h=new f(n,i,r,u,o);return t.set(s,h),s};n.unregister=n=>{t.delete(n)};const o={OS:"Meta",Decimal:"Period"},s=n=>o[n.key]||n.key,i="OnKeyDown";n.attach=(n,t)=>{document.addEventListener("keydown",r=>{if(typeof r.altKey!="undefined"){const u=(r.shiftKey?1:0)+(r.ctrlKey?2:0)+(r.altKey?4:0)+(r.metaKey?8:0),f=s(r),e=r.code,o=r.srcElement,c=o.tagName,l=o.getAttribute("type"),a=h(u,f,e,o,c,l),v=t===!0?n.invokeMethod(i,u,c,l,f,e):!1;(a||v)&&r.preventDefault();t===!1&&n.invokeMethodAsync(i,u,c,l,f,e)}})};const h=(n,i,r,u,f,e)=>{let o=!1;return t.forEach(t=>{const l=t.mode===1,a=l?r:i,s=t.keyEntry;if(s===a){const v=l?n:n&65534;let h=l?t.modifiers:t.modifiers&65534;(s.startsWith("Shift")&&l&&(h|=1),s.startsWith("Control")&&(h|=2),s.startsWith("Alt")&&(h|=4),s.startsWith("Meta")&&(h|=8),v===h)&&c(t,u,f,e)&&(o=!0,t.action())}}),o},r=["button","checkbox","color","file","image","radio","range","reset","submit",],u="INPUT",c=(n,t,i,f)=>(n.exclude&1)!=0&&i===u&&r.indexOf(f||"")===-1?!1:(n.exclude&2)!=0&&i===u&&r.indexOf(f||"")!==-1?!1:(n.exclude&4)!=0&&i==="TEXTAREA"?!1:(n.exclude&8)!=0&&t.contentEditable==="true"?!1:!0})(t=n.HotKeys2||(n.HotKeys2={}))})(t=n.Blazor||(n.Blazor={}))})(Toolbelt||(Toolbelt={})); \ No newline at end of file +export var Toolbelt;(function(n){var t;(function(n){var t;(function(n){class f{constructor(n,t,i,r,u,f){this.dotNetObj=n;this.mode=t;this.modifiers=i;this.keyEntry=r;this.exclude=u;this.excludeSelector=f}action(){this.dotNetObj.invokeMethodAsync("InvokeAction")}}let e=0;const t=new Map;n.register=(n,i,r,u,o,s)=>{const h=e++,c=new f(n,i,r,u,o,s);return t.set(h,c),h};n.unregister=n=>{t.delete(n)};const o={OS:"Meta",Decimal:"Period"},s=n=>o[n.key]||n.key,i="OnKeyDown";n.attach=(n,t)=>{document.addEventListener("keydown",r=>{if(typeof r.altKey!="undefined"){const u=(r.shiftKey?1:0)+(r.ctrlKey?2:0)+(r.altKey?4:0)+(r.metaKey?8:0),f=s(r),e=r.code,o=r.srcElement,c=o.tagName,l=o.getAttribute("type"),a=h(u,f,e,o,c,l),v=t===!0?n.invokeMethod(i,u,c,l,f,e):!1;(a||v)&&r.preventDefault();t===!1&&n.invokeMethodAsync(i,u,c,l,f,e)}})};const h=(n,i,r,u,f,e)=>{let o=!1;return t.forEach(t=>{const l=t.mode===1,a=l?r:i,s=t.keyEntry;if(s===a){const v=l?n:n&65534;let h=l?t.modifiers:t.modifiers&65534;(s.startsWith("Shift")&&l&&(h|=1),s.startsWith("Control")&&(h|=2),s.startsWith("Alt")&&(h|=4),s.startsWith("Meta")&&(h|=8),v===h)&&c(t,u,f,e)&&(t.excludeSelector!==""&&u.matches(t.excludeSelector)||(o=!0,t.action()))}}),o},r=["button","checkbox","color","file","image","radio","range","reset","submit",],u="INPUT",c=(n,t,i,f)=>(n.exclude&1)!=0&&i===u&&r.indexOf(f||"")===-1?!1:(n.exclude&2)!=0&&i===u&&r.indexOf(f||"")!==-1?!1:(n.exclude&4)!=0&&i==="TEXTAREA"?!1:(n.exclude&8)!=0&&t.contentEditable==="true"?!1:!0})(t=n.HotKeys2||(n.HotKeys2={}))})(t=n.Blazor||(n.Blazor={}))})(Toolbelt||(Toolbelt={})); \ No newline at end of file