Skip to content

Commit

Permalink
Improve helper JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Sep 30, 2023
1 parent 873037b commit fcd5328
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
34 changes: 17 additions & 17 deletions HotKeys2/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ export var Toolbelt;
(ev.metaKey ? 8 : 0);
const key = convertToKeyName(ev);
const code = ev.code;
const srcElement = ev.srcElement;
const tagName = srcElement.tagName;
const type = srcElement.getAttribute('type');
const preventDefault1 = onKeyDown(modifiers, key, code, srcElement, tagName, type);
const targetElement = ev.target;
const tagName = targetElement.tagName;
const type = targetElement.getAttribute('type');
const preventDefault1 = onKeyDown(modifiers, key, code, targetElement, tagName, type);
const preventDefault2 = isWasm === true ? hotKeysWrpper.invokeMethod(OnKeyDownMethodName, modifiers, tagName, type, key, code) : false;
if (preventDefault1 || preventDefault2)
ev.preventDefault();
if (isWasm === false)
hotKeysWrpper.invokeMethodAsync(OnKeyDownMethodName, modifiers, tagName, type, key, code);
});
};
const onKeyDown = (modifiers, key, code, srcElement, tagName, type) => {
const onKeyDown = (modifiers, key, code, targetElement, tagName, type) => {
let preventDefault = false;
hotKeyEntries.forEach(entry => {
const byCode = entry.mode === 1;
Expand All @@ -77,9 +77,7 @@ export var Toolbelt;
entryModKeys |= 8;
if (eventModkeys !== entryModKeys)
return;
if (!isAllowedIn(entry, srcElement, tagName, type))
return;
if (entry.excludeSelector !== '' && srcElement.matches(entry.excludeSelector))
if (isExcludeTarget(entry, targetElement, tagName, type))
return;
preventDefault = true;
entry.action();
Expand All @@ -88,24 +86,26 @@ export var Toolbelt;
};
const NonTextInputTypes = ["button", "checkbox", "color", "file", "image", "radio", "range", "reset", "submit",];
const InputTageName = "INPUT";
const isAllowedIn = (entry, srcElement, tagName, type) => {
const isExcludeTarget = (entry, targetElement, tagName, type) => {
if ((entry.exclude & 1) !== 0) {
if (tagName === InputTageName && NonTextInputTypes.indexOf(type || '') === -1)
return false;
if (tagName === InputTageName && NonTextInputTypes.every(t => t !== type))
return true;
}
if ((entry.exclude & 2) !== 0) {
if (tagName === InputTageName && NonTextInputTypes.indexOf(type || '') !== -1)
return false;
if (tagName === InputTageName && NonTextInputTypes.some(t => t === type))
return true;
}
if ((entry.exclude & 4) !== 0) {
if (tagName === "TEXTAREA")
return false;
return true;
}
if ((entry.exclude & 8) !== 0) {
if (srcElement.contentEditable === "true")
return false;
if (targetElement.isContentEditable)
return true;
}
return true;
if (entry.excludeSelector !== '' && targetElement.matches(entry.excludeSelector))
return true;
return false;
};
})(HotKeys2 = Blazor.HotKeys2 || (Blazor.HotKeys2 = {}));
})(Blazor = Toolbelt.Blazor || (Toolbelt.Blazor = {}));
Expand Down
28 changes: 14 additions & 14 deletions HotKeys2/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,18 @@
const key = convertToKeyName(ev);
const code = ev.code;

const srcElement = ev.srcElement as HTMLElement;
const tagName = srcElement.tagName;
const type = srcElement.getAttribute('type');
const targetElement = ev.target as HTMLElement;
const tagName = targetElement.tagName;
const type = targetElement.getAttribute('type');

const preventDefault1 = onKeyDown(modifiers, key, code, srcElement, tagName, type);
const preventDefault1 = onKeyDown(modifiers, key, code, targetElement, tagName, type);
const preventDefault2 = isWasm === true ? hotKeysWrpper.invokeMethod(OnKeyDownMethodName, modifiers, tagName, type, key, code) : false;
if (preventDefault1 || preventDefault2) ev.preventDefault();
if (isWasm === false) hotKeysWrpper.invokeMethodAsync(OnKeyDownMethodName, modifiers, tagName, type, key, code);
});
}

const onKeyDown = (modifiers: ModCodes, key: string, code: string, srcElement: HTMLElement, tagName: string, type: string | null ): boolean => {
const onKeyDown = (modifiers: ModCodes, key: string, code: string, targetElement: HTMLElement, tagName: string, type: string | null): boolean => {
let preventDefault = false;

hotKeyEntries.forEach(entry => {
Expand All @@ -103,9 +103,7 @@
if (keyEntry.startsWith("Meta")) entryModKeys |= ModCodes.Meta;
if (eventModkeys !== entryModKeys) return;

if (!isAllowedIn(entry, srcElement, tagName, type)) return;

if (entry.excludeSelector !== '' && srcElement.matches(entry.excludeSelector)) return;
if (isExcludeTarget(entry, targetElement, tagName, type)) return;

preventDefault = true;
entry.action();
Expand All @@ -118,22 +116,24 @@

const InputTageName = "INPUT";

const isAllowedIn = (entry: HotkeyEntry, srcElement: HTMLElement, tagName: string, type: string | null): boolean => {
const isExcludeTarget = (entry: HotkeyEntry, targetElement: HTMLElement, tagName: string, type: string | null): boolean => {

if ((entry.exclude & Exclude.InputText) !== 0) {
if (tagName === InputTageName && NonTextInputTypes.indexOf(type || '') === -1) return false;
if (tagName === InputTageName && NonTextInputTypes.every(t => t !== type)) return true;
}
if ((entry.exclude & Exclude.InputNonText) !== 0) {
if (tagName === InputTageName && NonTextInputTypes.indexOf(type || '') !== -1) return false;
if (tagName === InputTageName && NonTextInputTypes.some(t => t === type)) return true;
}
if ((entry.exclude & Exclude.TextArea) !== 0) {
if (tagName === "TEXTAREA") return false;
if (tagName === "TEXTAREA") return true;
}
if ((entry.exclude & Exclude.ContentEditable) !== 0) {
if (srcElement.contentEditable === "true") return false;
if (targetElement.isContentEditable) return true;
}

return true;
if (entry.excludeSelector !== '' && targetElement.matches(entry.excludeSelector)) return true;

return false;
}

}
2 changes: 1 addition & 1 deletion HotKeys2/wwwroot/script.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fcd5328

Please sign in to comment.