util.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Cut input selection into 2 part and return text before selection start
  3. */
  4. export function getBeforeSelectionText(input) {
  5. var selectionStart = input.selectionStart;
  6. return input.value.slice(0, selectionStart);
  7. }
  8. /**
  9. * Find the last match prefix index
  10. */
  11. export function getLastMeasureIndex(text, prefix) {
  12. return prefix.reduce(function (lastMatch, prefixStr) {
  13. var lastIndex = text.lastIndexOf(prefixStr);
  14. if (lastIndex > lastMatch.location) {
  15. return {
  16. location: lastIndex,
  17. prefix: prefixStr
  18. };
  19. }
  20. return lastMatch;
  21. }, {
  22. location: -1,
  23. prefix: ''
  24. });
  25. }
  26. function lower(char) {
  27. return (char || '').toLowerCase();
  28. }
  29. function reduceText(text, targetText, split) {
  30. var firstChar = text[0];
  31. if (!firstChar || firstChar === split) {
  32. return text;
  33. }
  34. // Reuse rest text as it can
  35. var restText = text;
  36. var targetTextLen = targetText.length;
  37. for (var i = 0; i < targetTextLen; i += 1) {
  38. if (lower(restText[i]) !== lower(targetText[i])) {
  39. restText = restText.slice(i);
  40. break;
  41. } else if (i === targetTextLen - 1) {
  42. restText = restText.slice(targetTextLen);
  43. }
  44. }
  45. return restText;
  46. }
  47. /**
  48. * Paint targetText into current text:
  49. * text: little@litest
  50. * targetText: light
  51. * => little @light test
  52. */
  53. export function replaceWithMeasure(text, measureConfig) {
  54. var measureLocation = measureConfig.measureLocation,
  55. prefix = measureConfig.prefix,
  56. targetText = measureConfig.targetText,
  57. selectionStart = measureConfig.selectionStart,
  58. split = measureConfig.split;
  59. // Before text will append one space if have other text
  60. var beforeMeasureText = text.slice(0, measureLocation);
  61. if (beforeMeasureText[beforeMeasureText.length - split.length] === split) {
  62. beforeMeasureText = beforeMeasureText.slice(0, beforeMeasureText.length - split.length);
  63. }
  64. if (beforeMeasureText) {
  65. beforeMeasureText = "".concat(beforeMeasureText).concat(split);
  66. }
  67. // Cut duplicate string with current targetText
  68. var restText = reduceText(text.slice(selectionStart), targetText.slice(selectionStart - measureLocation - prefix.length), split);
  69. if (restText.slice(0, split.length) === split) {
  70. restText = restText.slice(split.length);
  71. }
  72. var connectedStartText = "".concat(beforeMeasureText).concat(prefix).concat(targetText).concat(split);
  73. return {
  74. text: "".concat(connectedStartText).concat(restText),
  75. selectionLocation: connectedStartText.length
  76. };
  77. }
  78. export function setInputSelection(input, location) {
  79. input.setSelectionRange(location, location);
  80. /**
  81. * Reset caret into view.
  82. * Since this function always called by user control, it's safe to focus element.
  83. */
  84. input.blur();
  85. input.focus();
  86. }
  87. export function validateSearch(text, split) {
  88. return !split || text.indexOf(split) === -1;
  89. }
  90. export function filterOption(input, _ref) {
  91. var _ref$value = _ref.value,
  92. value = _ref$value === void 0 ? '' : _ref$value;
  93. var lowerCase = input.toLowerCase();
  94. return value.toLowerCase().indexOf(lowerCase) !== -1;
  95. }