util.js
3.56 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
export var omit = function omit(obj) {
var clone = _objectSpread({}, obj);
for (var _len = arguments.length, keys = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
keys[_key - 1] = arguments[_key];
}
keys.forEach(function (key) {
delete clone[key];
});
return clone;
};
/**
* Cut input selection into 2 part and return text before selection start
*/
export function getBeforeSelectionText(input) {
var selectionStart = input.selectionStart;
return input.value.slice(0, selectionStart);
}
/**
* Find the last match prefix index
*/
export function getLastMeasureIndex(text) {
var prefix = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
var prefixList = Array.isArray(prefix) ? prefix : [prefix];
return prefixList.reduce(function (lastMatch, prefixStr) {
var lastIndex = text.lastIndexOf(prefixStr);
if (lastIndex > lastMatch.location) {
return {
location: lastIndex,
prefix: prefixStr
};
}
return lastMatch;
}, {
location: -1,
prefix: ''
});
}
function lower(char) {
return (char || '').toLowerCase();
}
function reduceText(text, targetText, split) {
var firstChar = text[0];
if (!firstChar || firstChar === split) {
return text;
} // Reuse rest text as it can
var restText = text;
var targetTextLen = targetText.length;
for (var i = 0; i < targetTextLen; i += 1) {
if (lower(restText[i]) !== lower(targetText[i])) {
restText = restText.slice(i);
break;
} else if (i === targetTextLen - 1) {
restText = restText.slice(targetTextLen);
}
}
return restText;
}
/**
* Paint targetText into current text:
* text: little@litest
* targetText: light
* => little @light test
*/
export function replaceWithMeasure(text, measureConfig) {
var measureLocation = measureConfig.measureLocation,
prefix = measureConfig.prefix,
targetText = measureConfig.targetText,
selectionStart = measureConfig.selectionStart,
split = measureConfig.split; // Before text will append one space if have other text
var beforeMeasureText = text.slice(0, measureLocation);
if (beforeMeasureText[beforeMeasureText.length - split.length] === split) {
beforeMeasureText = beforeMeasureText.slice(0, beforeMeasureText.length - split.length);
}
if (beforeMeasureText) {
beforeMeasureText = "".concat(beforeMeasureText).concat(split);
} // Cut duplicate string with current targetText
var restText = reduceText(text.slice(selectionStart), targetText.slice(selectionStart - measureLocation - prefix.length), split);
if (restText.slice(0, split.length) === split) {
restText = restText.slice(split.length);
}
var connectedStartText = "".concat(beforeMeasureText).concat(prefix).concat(targetText).concat(split);
return {
text: "".concat(connectedStartText).concat(restText),
selectionLocation: connectedStartText.length
};
}
export function setInputSelection(input, location) {
input.setSelectionRange(location, location);
/**
* Reset caret into view.
* Since this function always called by user control, it's safe to focus element.
*/
input.blur();
input.focus();
}
export function validateSearch(text, props) {
var split = props.split;
return !split || text.indexOf(split) === -1;
}
export function filterOption(input, _ref) {
var _ref$value = _ref.value,
value = _ref$value === void 0 ? '' : _ref$value;
var lowerCase = input.toLowerCase();
return value.toLowerCase().indexOf(lowerCase) !== -1;
}