UNPKG

flipper-plugin

Version:

Flipper Desktop plugin SDK and components

119 lines 5.2 kB
"use strict"; /** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.useHighlighter = exports.HighlightProvider = exports.HighlightContext = void 0; const react_1 = __importStar(require("react")); const lodash_1 = require("lodash"); const theme_1 = require("./theme"); function createHighlightManager(initialText = '', initialHighlightColor = theme_1.theme.searchHighlightBackground.yellow) { const callbacks = new Set(); let matches = 0; let currentFilter = initialText; let currHighlightColor = initialHighlightColor; const Highlight = (0, react_1.memo)(({ text }) => { const [, setUpdate] = (0, react_1.useState)(0); const elem = (0, react_1.useRef)(null); (0, react_1.useEffect)(() => { function onChange(prevHighlight, newHighlight) { const prevIndex = text.toLowerCase().indexOf(prevHighlight); const newIndex = text.toLowerCase().indexOf(newHighlight); if (prevIndex !== newIndex || newIndex !== -1) { // either we had a result, and we have no longer, // or we still have a result, but the highlightable text changed if (newIndex !== -1) { if (++matches === 1) { elem.current?.parentElement?.parentElement?.scrollIntoView?.(); } } setUpdate((s) => s + 1); } } callbacks.add(onChange); return () => { callbacks.delete(onChange); }; }, [text]); const index = text.toLowerCase().indexOf(currentFilter); return (react_1.default.createElement("span", { ref: elem }, index === -1 ? (text) : (react_1.default.createElement(react_1.default.Fragment, null, text.substr(0, index), react_1.default.createElement("span", { style: { backgroundColor: currHighlightColor } }, text.substr(index, currentFilter.length)), text.substr(index + currentFilter.length))))); }); return { setFilter: (0, lodash_1.debounce)((text = '') => { if (currentFilter !== text) { matches = 0; const base = currentFilter; currentFilter = text.toLowerCase(); callbacks.forEach((cb) => cb(base, currentFilter)); } }, 100), render(text) { return react_1.default.createElement(Highlight, { text: text }); }, setHighlightColor(color) { if (color !== currHighlightColor) { currHighlightColor = color; callbacks.forEach((cb) => cb(currentFilter, currentFilter)); } }, }; } exports.HighlightContext = (0, react_1.createContext)({ setFilter(_text) { throw new Error('Cannot set the filter of a stub highlight manager'); }, render(text) { // stub implementation in case we render a component without a Highlight context return text; }, setHighlightColor(_color) { throw new Error('Cannot set the color of a stub highlight manager'); }, }); function HighlightProvider({ text, highlightColor, children, }) { const [highlightManager] = (0, react_1.useState)(() => createHighlightManager(text, highlightColor)); (0, react_1.useEffect)(() => { highlightManager.setFilter(text); }, [text, highlightManager]); (0, react_1.useEffect)(() => { highlightManager.setHighlightColor(highlightColor); }, [highlightColor, highlightManager]); return (react_1.default.createElement(exports.HighlightContext.Provider, { value: highlightManager }, children)); } exports.HighlightProvider = HighlightProvider; function useHighlighter() { return (0, react_1.useContext)(exports.HighlightContext); } exports.useHighlighter = useHighlighter; //# sourceMappingURL=Highlight.js.map