Skip to content
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

fix(core): don't execute setSelectionRange if element is not focused #937

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions projects/core/src/lib/mask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,13 @@ export class Maskito extends MaskHistory {
}

private updateSelectionRange([from, to]: SelectionRange): void {
if (this.element.selectionStart !== from || this.element.selectionEnd !== to) {
this.element.setSelectionRange?.(from, to);
const {element} = this;

if (
element.matches(':focus') &&
(element.selectionStart !== from || element.selectionEnd !== to)
) {
element.setSelectionRange?.(from, to);
}
}

Expand Down
5 changes: 4 additions & 1 deletion projects/core/src/lib/utils/dom/update-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export function maskitoUpdateElement(
const [from, to] = valueOrElementState.selection;

element.value = valueOrElementState.value;
element.setSelectionRange?.(from, to);

if (element.matches(':focus')) {
element.setSelectionRange?.(from, to);
}
}

element.dispatchEvent(
Expand Down
4 changes: 2 additions & 2 deletions projects/kit/src/lib/plugins/caret-guard.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {MaskitoPlugin} from '@maskito/core';

import {clamp, getFocused} from '../utils';
import {clamp} from '../utils';

export function maskitoCaretGuard(
guard: (
Expand All @@ -17,7 +17,7 @@ export function maskitoCaretGuard(
};

const listener = (): void => {
if (getFocused(document) !== element) {
if (!element.matches(':focus')) {
return;
}

Expand Down
18 changes: 0 additions & 18 deletions projects/kit/src/lib/utils/get-focused.ts

This file was deleted.

1 change: 0 additions & 1 deletion projects/kit/src/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export * from './date/validate-date-string';
export * from './escape-reg-exp';
export * from './extract-affixes';
export * from './find-common-beginning-substr';
export * from './get-focused';
export * from './identity';
export * from './is-empty';
export * from './pad-with-zeroes-until-valid';