react-terminal-viewer
Version:
<h1 align="center"> react-terminal-viewer </h1>
88 lines (87 loc) • 5.24 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
/* eslint-disable no-plusplus */
import Mark from "./Mark";
var MAX_COUNT = 1000;
var Searcher = /*#__PURE__*/function () {
function Searcher() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
markClassName: 'terminal-viewer-hl-mark',
markedClassName: 'terminal-viewer-hl-mark terminal-viewer-hl-marked'
};
_classCallCheck(this, Searcher);
_defineProperty(this, "options", void 0);
this.options = options;
}
_createClass(Searcher, [{
key: "search",
value: function search(keyword, logs) {
var _options$decorations, _options$decorations2, _options$decorations3, _options$decorations4, _options$decorations5;
var hlIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined;
if (!Array.isArray(logs) || logs.length === 0 || !keyword) {
return {
count: 0,
map: {},
markedKey: 0
};
}
var _this$options = this.options,
markClassName = _this$options.markClassName,
markedClassName = _this$options.markedClassName;
var style = {
color: options === null || options === void 0 ? void 0 : (_options$decorations = options.decorations) === null || _options$decorations === void 0 ? void 0 : _options$decorations.matchForegroundColor,
background: options === null || options === void 0 ? void 0 : (_options$decorations2 = options.decorations) === null || _options$decorations2 === void 0 ? void 0 : _options$decorations2.matchBackground,
border: options === null || options === void 0 ? void 0 : (_options$decorations3 = options.decorations) === null || _options$decorations3 === void 0 ? void 0 : _options$decorations3.matchBorder
};
var markedStyle = {
background: options === null || options === void 0 ? void 0 : (_options$decorations4 = options.decorations) === null || _options$decorations4 === void 0 ? void 0 : _options$decorations4.activeMatchBackground,
border: options === null || options === void 0 ? void 0 : (_options$decorations5 = options.decorations) === null || _options$decorations5 === void 0 ? void 0 : _options$decorations5.activeMatchBorder
};
var count = 0;
var markedId = 0;
var map = {};
for (var i = 0, len = logs.length; i < len; i++) {
if (count >= MAX_COUNT) {
break;
}
var text = logs[i] || '';
if (!text.toLowerCase().includes(keyword.toLowerCase())) {
// eslint-disable-next-line no-continue
continue;
}
var mark = new Mark(text, {
className: markClassName,
markedClassName: markedClassName,
style: style,
markedStyle: markedStyle
});
var markedText = mark.mark(keyword, {
matchedIndex: count,
hlIndex: hlIndex
}).toString();
if (mark.isMarked) {
markedId = i;
}
if (mark.matchCount !== 0) {
count += mark.matchCount;
map[i] = markedText || text;
}
mark.distory();
mark = null;
}
return {
count: count,
map: map,
markedKey: markedId
};
}
}]);
return Searcher;
}();
export { Searcher as default };