UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

104 lines (103 loc) 5.32 kB
/** * * Copyright (c) "Neo4j" * Neo4j Sweden AB [http://neo4j.com] * * This file is part of Neo4j. * * Neo4j is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { jsx as _jsx } from "react/jsx-runtime"; import { useEffect, useState } from 'react'; import { PrismLight as SyntaxHighlighter } from 'react-syntax-highlighter'; import { base16AteliersulphurpoolLight, coy, duotoneLight, ghcolors, prism, solarizedlight, vs, } from 'react-syntax-highlighter/dist/cjs/styles/prism'; import { CodeBlockWrapper } from '../_common/CodeBlockWrapper'; import { useNeedleTheme } from '../theme'; import { loadLanguage } from './code-languages'; import ndlCodeDark from './themes/ndl-code-dark'; import ndlCodeLight from './themes/ndl-code-light'; export const CodeBlock = (_a) => { var { as, maxHeight, code, language, shouldShowLineNumbers, theme, heading, actions, isDisabled, className, style, htmlAttributes, ref } = _a, restProps = __rest(_a, ["as", "maxHeight", "code", "language", "shouldShowLineNumbers", "theme", "heading", "actions", "isDisabled", "className", "style", "htmlAttributes", "ref"]); const [shouldShowExpandButton, setShouldShowExpandButton] = useState(maxHeight !== undefined); const { theme: ndlTheme } = useNeedleTheme(); const [loadedLanguage, setLoadedLanguage] = useState(null); useEffect(() => { if (language === undefined || language === 'text') { setLoadedLanguage('text'); return; } loadLanguage(language) .then((module) => { SyntaxHighlighter.registerLanguage(language, module.default); setLoadedLanguage(language); }) // TODO: add visual feedback for the user .catch((err) => console.error(err)); }, [language]); const getTheme = () => { switch (theme) { case 'ndl-code-dark': return ndlCodeDark; case 'ndl-code-light': return ndlCodeLight; case 'vs': return vs; case 'base16-ateliersulphurpool.light': return base16AteliersulphurpoolLight; case 'coy': return coy; case 'duotone-light': return duotoneLight; case 'ghcolors': return ghcolors; case 'prism': return prism; case 'solarizedlight': return solarizedlight; default: return ndlTheme === 'light' ? ndlCodeLight : ndlCodeDark; } }; const calculateRightPadding = (() => { var _a; if (Boolean(heading) === false) { const expandButtonCount = shouldShowExpandButton ? 1 : 0; const actionCount = (_a = actions === null || actions === void 0 ? void 0 : actions.length) !== null && _a !== void 0 ? _a : 0; return Math.max(actionCount * 36, expandButtonCount * 36) + 8; } })(); return (_jsx(CodeBlockWrapper, Object.assign({ as: as, className: className, style: style, ref: ref, maxHeight: maxHeight, code: code, heading: heading, isDisabled: isDisabled, actions: actions, shouldShowExpandButton: shouldShowExpandButton, setShouldShowExpandButton: setShouldShowExpandButton }, restProps, htmlAttributes, { children: _jsx(SyntaxHighlighter, { language: loadedLanguage || 'text', style: Object.assign(Object.assign({}, getTheme()), { 'pre[class*="language-"]': { border: 0, color: 'var(--theme-color-neutral-text-default)', lineHeight: '1', padding: `0 ${calculateRightPadding !== null && calculateRightPadding !== void 0 ? calculateRightPadding : 0}px 0.75em ${shouldShowLineNumbers === true ? 0 : 0.75}em`, width: 'fit-content', } }), // Turn on 'shouldShowLineNumbers' & 'wrapLongLines' at the same time, the display is wrong // https://github.com/react-syntax-highlighter/react-syntax-highlighter/issues/402 // wrapLongLines codeTagProps: { className: 'n-code' }, showLineNumbers: shouldShowLineNumbers, children: code }) }))); }; //# sourceMappingURL=CodeBlock.js.map