UNPKG

@lobehub/ui

Version:

Lobe UI is an open-source UI component library for building AIGC web apps

186 lines (184 loc) 8.71 kB
'use client'; function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } import { cva } from 'class-variance-authority'; import { memo, useEffect, useMemo, useState } from 'react'; import { useHighlight } from "../../hooks/useHighlight"; import Line from "./Line"; import { useStyles } from "./style"; import { jsx as _jsx } from "react/jsx-runtime"; var SyntaxHighlighter = /*#__PURE__*/memo(function (_ref) { var ref = _ref.ref, children = _ref.children, language = _ref.language, className = _ref.className, style = _ref.style, enableTransformer = _ref.enableTransformer, variant = _ref.variant, theme = _ref.theme, animated = _ref.animated; var _useStyles = useStyles(), styles = _useStyles.styles, cx = _useStyles.cx; var isDefaultTheme = theme === 'lobe-theme' || !theme; var showBackground = !isDefaultTheme && variant === 'filled'; var _useHighlight = useHighlight(children, { enableTransformer: enableTransformer, language: language, theme: isDefaultTheme ? undefined : theme }), data = _useHighlight.data; var _useState = useState([]), _useState2 = _slicedToArray(_useState, 2), contentLines = _useState2[0], setContentLines = _useState2[1]; var _useState3 = useState({}), _useState4 = _slicedToArray(_useState3, 2), preStyle = _useState4[0], setPreStyle = _useState4[1]; useEffect(function () { if (data && typeof data === 'string') { // Extract all lines from the HTML content // We need to handle the structure from shiki which gives us HTML with a <pre><code> structure var parser = new DOMParser(); var doc = parser.parseFromString(data, 'text/html'); var preElement = doc.querySelector('pre'); var _preStyle = preElement === null || preElement === void 0 ? void 0 : preElement.style; if (_preStyle) { setPreStyle({ backgroundColor: _preStyle === null || _preStyle === void 0 ? void 0 : _preStyle.backgroundColor, color: _preStyle === null || _preStyle === void 0 ? void 0 : _preStyle.color }); } var codeElement = doc.querySelector('pre code'); if (codeElement) { var spanLines = codeElement.querySelectorAll('.line'); var newLines = _toConsumableArray(spanLines).map(function (line) { return line.outerHTML; }); // Only update if the lines have changed setContentLines(function (prevLines) { if (prevLines.length !== newLines.length) return newLines; var hasChanged = false; var _iterator = _createForOfIteratorHelper(newLines.entries()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var _step$value = _slicedToArray(_step.value, 2), i = _step$value[0], newLine = _step$value[1]; if (prevLines[i] !== newLine) { hasChanged = true; break; } } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return hasChanged ? newLines : prevLines; }); } else { // Fallback if the structure is different var htmlLines = data.split('\n').map(function (line) { return "<span class=\"line\">".concat(line, "</span>"); }); setContentLines(htmlLines); } } }, [data]); var variants = useMemo(function () { return cva(styles.root, { defaultVariants: { shiki: false, showBackground: false, variant: 'borderless' }, /* eslint-disable sort-keys-fix/sort-keys-fix */ variants: { shiki: { false: styles.unshiki, true: styles.shiki }, variant: { filled: styles.padding, outlined: styles.padding, borderless: styles.noPadding }, showBackground: { false: styles.noBackground, true: null } } /* eslint-enable sort-keys-fix/sort-keys-fix */ }); }, [styles]); if (contentLines.length === 0) return /*#__PURE__*/_jsx("div", { className: cx(variants({ shiki: false, showBackground: showBackground, variant: variant }), className), dir: "ltr", ref: ref, style: style, children: /*#__PURE__*/_jsx("pre", { children: /*#__PURE__*/_jsx("code", { children: children }) }) }); if (!animated) { return /*#__PURE__*/_jsx("div", { className: cx(variants({ shiki: true, showBackground: showBackground, variant: variant }), className), dangerouslySetInnerHTML: { __html: data || '' }, dir: "ltr", ref: ref, style: style }); } return /*#__PURE__*/_jsx("div", { className: cx(variants({ shiki: true, showBackground: showBackground, variant: variant }), className), dir: "ltr", ref: ref, style: style, children: /*#__PURE__*/_jsx("pre", { className: cx('shiki', isDefaultTheme ? undefined : theme), style: preStyle, tabIndex: 0, children: /*#__PURE__*/_jsx("code", { style: { display: 'flex', flexDirection: 'column' }, children: contentLines.map(function (line, index) { return /*#__PURE__*/_jsx(Line, { children: line }, index); }) }) }) }); }); SyntaxHighlighter.displayName = 'SyntaxHighlighter'; export default SyntaxHighlighter;