util.js 3.3 KB

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