This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
text-field-edit.js
72 lines (67 loc) · 2.5 KB
/
text-field-edit.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* https://github.com/fregante/text-field-edit @ v3.1.0 */
var textFieldEdit = (function (exports) {
'use strict';
function insertTextFirefox(field, text) {
field.setRangeText(text, field.selectionStart || 0, field.selectionEnd || 0, 'end'
);
field.dispatchEvent(new InputEvent('input', {
data: text,
inputType: 'insertText',
isComposing: false
}));
}
function insert(field, text) {
var document = field.ownerDocument;
var initialFocus = document.activeElement;
if (initialFocus !== field) {
field.focus();
}
if (!document.execCommand('insertText', false, text)) {
insertTextFirefox(field, text);
}
if (initialFocus === document.body) {
field.blur();
}
else if (initialFocus instanceof HTMLElement && initialFocus !== field) {
initialFocus.focus();
}
}
function set(field, text) {
field.select();
insert(field, text);
}
function getSelection(field) {
return field.value.slice(field.selectionStart, field.selectionEnd);
}
function wrapSelection(field, wrap, wrapEnd) {
var selectionStart = field.selectionStart, selectionEnd = field.selectionEnd;
var selection = getSelection(field);
insert(field, wrap + selection + (wrapEnd !== null && wrapEnd !== void 0 ? wrapEnd : wrap));
field.selectionStart = selectionStart + wrap.length;
field.selectionEnd = selectionEnd + wrap.length;
}
function replace(field, searchValue, replacer) {
var drift = 0;
field.value.replace(searchValue, function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
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);
field.selectionStart = matchStart;
drift += replacement.length - matchLength;
return replacement;
});
}
exports.getSelection = getSelection;
exports.insert = insert;
exports.replace = replace;
exports.set = set;
exports.wrapSelection = wrapSelection;
return exports;
}({}));