Skip to content

Commit

Permalink
Refactor event handling to utilize CommonEvents.getElementWindow for …
Browse files Browse the repository at this point in the history
…element type checks
  • Loading branch information
dharmesh-hemaram committed Nov 12, 2024
1 parent 0e83b19 commit eabb78b
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 26 deletions.
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/append.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import CommonEvents, { UNKNOWN_ELEMENT_TYPE_ERROR } from './common.events';
const CHANGE_EVENT = ['input', 'change'];
export const AppendEvents = (() => {
const checkNode = (element: HTMLElement, value: string) => {
if (element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value += value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down
6 changes: 4 additions & 2 deletions libs/acf/events/src/lib/clipboard.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const ClipboardEvents = (() => {
};

const getValue = (element: HTMLElement) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
return element.value;
}
if (element.isContentEditable) {
Expand All @@ -31,7 +32,8 @@ export const ClipboardEvents = (() => {
};

const checkNode = (element: HTMLElement, value: string) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down
6 changes: 5 additions & 1 deletion libs/acf/events/src/lib/common.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ const CommonEvents = (() => {
which = 0,
}): KeyboardEventInit => ({ key, code, location, ctrlKey, shiftKey, altKey, metaKey, bubbles, repeat, isComposing, charCode, keyCode, which });

return { getFillEvent, getMouseEvent, getMouseEventProperties, getKeyboardEventProperties, loopElements, getVerifiedEvents, getTouchEvent, getTouchEventProperties, getTouch };
const getElementWindow = (element: HTMLElement): Window & typeof globalThis => {
return element.ownerDocument.defaultView || window;
};

return { getFillEvent, getElementWindow, getMouseEvent, getMouseEventProperties, getKeyboardEventProperties, loopElements, getVerifiedEvents, getTouchEvent, getTouchEventProperties, getTouch };
})();

export const EVENTS = {
Expand Down
4 changes: 3 additions & 1 deletion libs/acf/events/src/lib/copy.events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { RADIO_CHECKBOX_NODE_NAME } from '@dhruv-techapps/acf-common';
import { GoogleAnalyticsService } from '@dhruv-techapps/google-analytics';
import { ACTION_I18N_TITLE } from '.';
import CommonEvents from './common.events';

const LOCAL_STORAGE_COPY = 'auto-clicker-copy';

Expand All @@ -16,7 +17,8 @@ export const CopyEvents = (() => {
};

const getValue = (element: HTMLElement) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
return element.value;
}
if (element.isContentEditable) {
Expand Down
25 changes: 13 additions & 12 deletions libs/acf/events/src/lib/form.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const FORM_EVENTS = ['blur', 'click', 'click-once', 'focus', 'select', 'submit',

export const FormEvents = (() => {
const dispatchEvent = (element: HTMLElement, events: Array<string | Event>) => {
if (!(element instanceof HTMLElement)) {
const eW = CommonEvents.getElementWindow(element);
if (!(element instanceof eW.HTMLElement)) {
throw new ConfigError(`elementFinder: ${element}`, 'Not HTMLElement');
}
events.forEach((event) => {
Expand All @@ -26,17 +27,17 @@ export const FormEvents = (() => {
element.focus();
break;
case 'submit':
if (element instanceof HTMLFormElement) {
if (element instanceof eW.HTMLFormElement) {
element.submit();
} else if (
element instanceof HTMLSelectElement ||
element instanceof HTMLTextAreaElement ||
element instanceof HTMLInputElement ||
element instanceof HTMLButtonElement ||
element instanceof HTMLLabelElement ||
element instanceof HTMLOptionElement ||
element instanceof HTMLFieldSetElement ||
element instanceof HTMLOutputElement
element instanceof eW.HTMLSelectElement ||
element instanceof eW.HTMLTextAreaElement ||
element instanceof eW.HTMLInputElement ||
element instanceof eW.HTMLButtonElement ||
element instanceof eW.HTMLLabelElement ||
element instanceof eW.HTMLOptionElement ||
element instanceof eW.HTMLFieldSetElement ||
element instanceof eW.HTMLOutputElement
) {
element.form?.submit();
} else {
Expand All @@ -45,15 +46,15 @@ export const FormEvents = (() => {
}
break;
case 'select':
if (element instanceof HTMLTextAreaElement || element instanceof HTMLInputElement) {
if (element instanceof eW.HTMLTextAreaElement || element instanceof eW.HTMLInputElement) {
element.select();
}
break;
case 'remove':
element.remove();
break;
case 'clear':
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || element instanceof HTMLInputElement) {
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || element instanceof eW.HTMLInputElement) {
element.value = '';
} else {
console.error(element);
Expand Down
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/func.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const CHANGE_EVENT = ['input', 'change'];

export const FuncEvents = (() => {
const checkNode = (element: HTMLElement, value: string) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down
2 changes: 1 addition & 1 deletion libs/acf/events/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const Events = (() => {
await ClipboardEvents.start(elements, value);
break;
case EVENTS.ELEMENT:
await ElementEvents.start(elements, value);
ElementEvents.start(elements, value);
break;
default:
PlainEvents.start(elements, value);
Expand Down
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/key.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export const KeyEvents = (() => {
};

const dispatchEvent = async (element: HTMLElement, events: Array<KeyEvent>) => {
if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) {
const eW = CommonEvents.getElementWindow(element); // Get eW
if (element instanceof eW.HTMLInputElement || element instanceof eW.HTMLTextAreaElement) {
// eslint-disable-next-line no-restricted-syntax
for (const event of events) {
element.dispatchEvent(new KeyboardEvent(KEYBOARD_EVENT_KEYDOWN, event));
Expand Down
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/paste.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const CHANGE_EVENT = ['input', 'change'];

export const PasteEvents = (() => {
const checkNode = (element: HTMLElement, value: string) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down
7 changes: 4 additions & 3 deletions libs/acf/events/src/lib/plain.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export const PlainEvents = (() => {
const checkEmptyValue = (value: string) => (value === '::empty' ? '' : value);

const dispatchEvent = (element: HTMLElement, value: string) => {
if (element instanceof HTMLSelectElement) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement) {
const nodes = document.evaluate(
`.//option[text()="${value}" or contains(text(),"${value}") or @value="${value}" or @id="${value}"]`,
element,
Expand All @@ -22,10 +23,10 @@ export const PlainEvents = (() => {
if (nodes.snapshotLength !== 0) {
(nodes.snapshotItem(0) as HTMLOptionElement).selected = true;
}
} else if (element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
} else if (element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element instanceof HTMLOptionElement) {
} else if (element instanceof eW.HTMLOptionElement) {
element.selected = true;
} else if (element.isContentEditable) {
GoogleAnalyticsService.fireEvent('isContentEditable', { event: 'PlainEvents' });
Expand Down
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/prepend.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const CHANGE_EVENT = ['input', 'change'];

export const PrependEvents = (() => {
const checkNode = (element: HTMLElement, value: string) => {
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = value + element.value;
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down
3 changes: 2 additions & 1 deletion libs/acf/events/src/lib/replace.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const CHANGE_EVENT = ['input', 'change'];
export const ReplaceEvents = (() => {
const checkNode = (element: HTMLElement, value: string) => {
const [target, string] = value.split('::');
if (element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || (element instanceof HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
const eW = CommonEvents.getElementWindow(element);
if (element instanceof eW.HTMLSelectElement || element instanceof eW.HTMLTextAreaElement || (element instanceof eW.HTMLInputElement && !RADIO_CHECKBOX_NODE_NAME.test(element.type))) {
element.value = element.value.replace(new RegExp(target, 'g'), string);
element.dispatchEvent(CommonEvents.getFillEvent());
} else if (element.isContentEditable) {
Expand Down

0 comments on commit eabb78b

Please sign in to comment.