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

Update dependencies #27

Merged
merged 2 commits into from
Feb 19, 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
12 changes: 6 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export function insertTextIntoField(
field: HTMLTextAreaElement | HTMLInputElement,
text: string,
): void {
const document = field.ownerDocument!;
const document = field.ownerDocument;
const initialFocus = document.activeElement;
if (initialFocus !== field) {
field.focus();
Expand Down Expand Up @@ -54,7 +54,7 @@ export function wrapFieldSelection(
field.selectionEnd = selectionEnd! + wrap.length;
}

type ReplacerCallback = (substring: string, ...args: any[]) => string;
type ReplacerCallback = (substring: string, ...arguments_: any[]) => string;

/** Finds and replaces strings and regex in the field’s value, like `field.value = field.value.replace()` but better */
export function replaceFieldText(
Expand All @@ -66,14 +66,14 @@ export function replaceFieldText(
/** Keeps track of how much each match offset should be adjusted */
let drift = 0;

field.value.replace(searchValue, (...args): string => {
field.value.replace(searchValue, (...arguments_): string => {
// Select current match to replace it later
const matchStart = drift + (args.at(-2) as number);
const matchLength = args[0].length;
const matchStart = drift + (arguments_.at(-2) as number);
const matchLength = arguments_[0].length;
field.selectionStart = matchStart;
field.selectionEnd = matchStart + matchLength;

const replacement = typeof replacer === 'string' ? replacer : replacer(...args);
const replacement = typeof replacer === 'string' ? replacer : replacer(...arguments_);
insertTextIntoField(field, replacement);

if (cursor === 'select') {
Expand Down
Loading