UNPKG

react-terminal-viewer

Version:

<h1 align="center"> react-terminal-viewer </h1>

189 lines (188 loc) 8.57 kB
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-restricted-syntax */ /* eslint-disable prefer-destructuring */ /* eslint-disable no-cond-assign */ /* eslint-disable consistent-return */ /* eslint-disable no-plusplus */ /* eslint-disable max-len */ /* eslint-disable no-param-reassign */ /* eslint-disable class-methods-use-this */ /* eslint-disable @typescript-eslint/no-non-null-assertion */ /* eslint-disable no-underscore-dangle */ /** * 基于关键字的自定义高亮插件 */ var HighlightAddon = /*#__PURE__*/function () { function HighlightAddon() { _classCallCheck(this, HighlightAddon); _defineProperty(this, "_terminal", void 0); _defineProperty(this, "_cacheDecorations", void 0); _defineProperty(this, "_highlightTimeout", void 0); } _createClass(HighlightAddon, [{ key: "activate", value: function activate(terminal) { this._terminal = terminal; } }, { key: "_getLineProperty", value: function _getLineProperty(line) { var index = 0; var cell = line.getCell(index); var width = 0; var isAllBgDefault = true; var isAllFgDefault = true; while (cell) { if (!cell.isBgDefault()) { isAllBgDefault = false; } if (!cell.isFgDefault()) { isAllFgDefault = false; } width += cell.getWidth(); cell = line.getCell(index); index++; } return { width: width, isAllBgDefault: isAllBgDefault, isAllFgDefault: isAllFgDefault }; } }, { key: "_decoration", value: function _decoration(line, lineIndex, matchOption) { var _matchOption$decorati, _matchOption$decorati2; if (!this._terminal) { return; } if (!(matchOption !== null && matchOption !== void 0 && (_matchOption$decorati = matchOption.decorations) !== null && _matchOption$decorati !== void 0 && _matchOption$decorati.matchBackground) && !(matchOption !== null && matchOption !== void 0 && (_matchOption$decorati2 = matchOption.decorations) !== null && _matchOption$decorati2 !== void 0 && _matchOption$decorati2.matchForegroundColor)) { return; } var terminal = this._terminal; var _this$_getLinePropert = this._getLineProperty(line), width = _this$_getLinePropert.width, isAllBgDefault = _this$_getLinePropert.isAllBgDefault, isAllFgDefault = _this$_getLinePropert.isAllFgDefault; if (!isAllBgDefault || !isAllFgDefault) { return; } var marker = terminal.registerMarker(-terminal.buffer.active.baseY - terminal.buffer.active.cursorY + lineIndex); if (marker) { var _matchOption$decorati3, _matchOption$decorati4; var decorationOptions = { marker: marker, x: 0, width: width, backgroundColor: (_matchOption$decorati3 = matchOption.decorations) === null || _matchOption$decorati3 === void 0 ? void 0 : _matchOption$decorati3.matchBackground, foregroundColor: (_matchOption$decorati4 = matchOption.decorations) === null || _matchOption$decorati4 === void 0 ? void 0 : _matchOption$decorati4.matchForegroundColor, layer: 'bottom' }; var decoration = terminal.registerDecoration(decorationOptions); if (decoration) { var _this$_cacheDecoratio; decoration === null || decoration === void 0 ? void 0 : decoration.onDispose(function () { marker.dispose(); }); // 可以在 dom 模式下操作 dom // this._highlightDecoration?.onRender((e) => { // }); (_this$_cacheDecoratio = this._cacheDecorations) === null || _this$_cacheDecoratio === void 0 ? void 0 : _this$_cacheDecoratio.set(lineIndex, decoration); } } } }, { key: "_clearDecoration", value: function _clearDecoration() { var _this$_cacheDecoratio2, _this$_cacheDecoratio3; (_this$_cacheDecoratio2 = this._cacheDecorations) === null || _this$_cacheDecoratio2 === void 0 ? void 0 : _this$_cacheDecoratio2.forEach(function (decoration) { decoration.marker.dispose(); decoration.dispose(); }); (_this$_cacheDecoratio3 = this._cacheDecorations) === null || _this$_cacheDecoratio3 === void 0 ? void 0 : _this$_cacheDecoratio3.clear(); } }, { key: "_updateHighlight", value: function _updateHighlight(options) { var _this = this; if (!this._terminal) { return; } if (!Array.isArray(options)) { return; } this._clearDecoration(); var terminal = this._terminal; var lineIndex = 0; var line = terminal.buffer.active.getLine(lineIndex); var prevMatch; var _loop = function _loop() { var nextLine = terminal.buffer.active.getLine(lineIndex + 1); var lineWrapsToNext = nextLine ? nextLine.isWrapped : false; var string = line.translateToString(!lineWrapsToNext); if (string) { if (line.isWrapped && prevMatch) { _this._decoration(line, lineIndex, prevMatch); } else { var matchOption = options.find(function (option) { var keyword = option.keyword || ''; if (option.ignoreSensitive) { var lowerString = string.toLowerCase(); return lowerString.includes(keyword.toLowerCase()) || lowerString.includes(keyword); } return string.includes(keyword); }); prevMatch = matchOption; _this._decoration(line, lineIndex, matchOption); } } lineIndex++; line = nextLine; }; while (line) { _loop(); } } }, { key: "highlight", value: function highlight(options) { var _this2 = this; if (!this._terminal) { return; } if (!Array.isArray(options)) { return; } this._cacheDecorations = new Map(); var terminal = this._terminal; terminal.onWriteParsed(function () { clearTimeout(_this2._highlightTimeout); _this2._highlightTimeout = setTimeout(function () { _this2._updateHighlight(options); }, 0); // this._clearDecoration(); }); terminal.onResize(function () { clearTimeout(_this2._highlightTimeout); _this2._highlightTimeout = setTimeout(function () { _this2._updateHighlight(options); }, 0); }); } }, { key: "dispose", value: function dispose() { clearTimeout(this._highlightTimeout); this._cacheDecorations = undefined; } }]); return HighlightAddon; }(); export { HighlightAddon }; export default HighlightAddon;