text-field-edit
Advanced tools
Comparing version 3.0.1 to 3.1.0
@@ -9,1 +9,5 @@ /** Inserts `text` at the cursor’s position, replacing any selection, with **undo** support and by firing the `input` event. */ | ||
export declare function wrapSelection(field: HTMLTextAreaElement | HTMLInputElement, wrap: string, wrapEnd?: string): void; | ||
declare type ReplacerCallback = (substring: string, ...args: any[]) => string; | ||
/** Finds and replaces strings and regex in the field’s value, like `field.value = field.value.replace()` but better */ | ||
export declare function replace(field: HTMLTextAreaElement | HTMLInputElement, searchValue: string | RegExp, replacer: string | ReplacerCallback): void; | ||
export {}; |
22
index.js
@@ -46,1 +46,23 @@ function insertTextFirefox(field, text) { | ||
} | ||
/** Finds and replaces strings and regex in the field’s value, like `field.value = field.value.replace()` but better */ | ||
export function replace(field, searchValue, replacer) { | ||
/** Remembers how much each match offset should be adjusted */ | ||
var drift = 0; | ||
field.value.replace(searchValue, function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
// Select current match to replace it later | ||
var matchStart = drift + args[args.length - 2]; | ||
var matchLength = args[0].length; | ||
field.selectionStart = matchStart; | ||
field.selectionEnd = matchStart + matchLength; | ||
var replacement = typeof replacer === 'string' ? replacer : replacer.apply(void 0, args); | ||
insert(field, replacement); | ||
// Select replacement. Without this, the cursor would be after the replacement | ||
field.selectionStart = matchStart; | ||
drift += replacement.length - matchLength; | ||
return replacement; | ||
}); | ||
} |
{ | ||
"name": "text-field-edit", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Insert text in a <textarea> and <input> (supports Firefox and Undo, where possible)", | ||
@@ -42,3 +42,5 @@ "keywords": [ | ||
"rules": { | ||
"@typescript-eslint/prefer-nullish-coalescing": "off" | ||
"@typescript-eslint/no-unnecessary-type-assertion": "off", | ||
"@typescript-eslint/prefer-nullish-coalescing": "off", | ||
"@typescript-eslint/prefer-readonly-parameter-types": "off" | ||
} | ||
@@ -54,4 +56,4 @@ }, | ||
"typescript": "^3.8.3", | ||
"xo": "*" | ||
"xo": "^0.30.0" | ||
} | ||
} |
# text-field-edit [![][badge-gzip]](#link-npm) | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/text-field-edit.svg?label=gzipped | ||
[link-npm]: https://www.npmjs.com/package/text-field-edit | ||
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/text-field-edit.svg?label=gzipped | ||
[link-npm]: https://www.npmjs.com/package/text-field-edit | ||
@@ -99,2 +99,33 @@ <img align="right" width="360" src="https://user-images.githubusercontent.com/1402241/55075820-e3645800-50ce-11e9-8591-9195c3cdfc8a.gif"> | ||
### textFieldEdit.replace(field, searchValue, replacement) | ||
Finds and replaces strings and regular expressions in the field’s value, like `field.value = field.value.replace()` but leaves the last replacement selected like a text editor would. | ||
Similar to [String#replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) | ||
```js | ||
const textarea = document.querySelector('textarea'); | ||
textarea.value = 'Hello world'; | ||
textFieldEdit.replace(textarea, 'Hello', 'Ciao'); | ||
// Changes field's value from 'Hello world' to '|Ciao| world' (where | marks the selected text) | ||
``` | ||
#### field | ||
Type: `HTMLTextAreaElement | HTMLInputElement` | ||
#### searchValue | ||
Type: `string | RegExp` | ||
The text to replace in the field’s value. | ||
#### replacement | ||
Type: `string | function` | ||
The text that will replace `searchValue` or a callback function that matches [the signature in `String#replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter). | ||
**Note**: replacement patterns (`replace(field, /hello (world)/, 'ciao $1')`) aren't supported. | ||
### textFieldEdit.wrapSelection(field, wrappingText, endWrappingText?) | ||
@@ -125,3 +156,2 @@ | ||
// If the field's value is '|almost| cool' (where | marks the selected text) | ||
``` | ||
@@ -128,0 +158,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12353
79
166