UNPKG

@neo4j-ndl/react

Version:

React implementation of Neo4j Design System

163 lines 8.7 kB
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; /** * * 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/>. */ import React, { 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 { classNames } from '../_common/defaultImports'; import { ConditionalWrap } from '../conditional-wrap'; import { useNeedleTheme } from '../theme'; import { Tooltip } from '../tooltip'; import ndlCodeDark from './themes/ndl-code-dark'; import ndlCodeLight from './themes/ndl-code-light'; const loadLanguage = (language) => { /* need to import the languages dynamically to reduce the bundle size and need to use a string literal for proper tree shaking */ switch (language) { case 'asciidoc': return import('react-syntax-highlighter/dist/esm/languages/prism/asciidoc'); case 'bash': return import('react-syntax-highlighter/dist/esm/languages/prism/bash'); case 'c': return import('react-syntax-highlighter/dist/esm/languages/prism/c'); case 'csharp': return import('react-syntax-highlighter/dist/esm/languages/prism/csharp'); case 'css': return import('react-syntax-highlighter/dist/esm/languages/prism/css'); case 'css-extras': return import('react-syntax-highlighter/dist/esm/languages/prism/css-extras'); case 'cypher': return import('react-syntax-highlighter/dist/esm/languages/prism/cypher'); case 'docker': return import('react-syntax-highlighter/dist/esm/languages/prism/docker'); case 'json': return import('react-syntax-highlighter/dist/esm/languages/prism/json'); case 'go': return import('react-syntax-highlighter/dist/esm/languages/prism/go'); case 'graphql': return import('react-syntax-highlighter/dist/esm/languages/prism/graphql'); case 'java': return import('react-syntax-highlighter/dist/esm/languages/prism/java'); case 'javadoc': return import('react-syntax-highlighter/dist/esm/languages/prism/javadoc'); case 'javascript': return import('react-syntax-highlighter/dist/esm/languages/prism/javascript'); case 'jsx': return import('react-syntax-highlighter/dist/esm/languages/prism/jsx'); case 'kotlin': return import('react-syntax-highlighter/dist/esm/languages/prism/kotlin'); case 'php': return import('react-syntax-highlighter/dist/esm/languages/prism/php'); case 'python': return import('react-syntax-highlighter/dist/esm/languages/prism/python'); case 'regex': return import('react-syntax-highlighter/dist/esm/languages/prism/regex'); case 'rust': return import('react-syntax-highlighter/dist/esm/languages/prism/rust'); case 'sql': return import('react-syntax-highlighter/dist/esm/languages/prism/sql'); case 'typescript': return import('react-syntax-highlighter/dist/esm/languages/prism/typescript'); case 'xml': return import('react-syntax-highlighter/dist/esm/languages/prism/xml-doc'); case 'yaml': return import('react-syntax-highlighter/dist/esm/languages/prism/yaml'); default: throw new Error(`Unsupported language: ${language}`); } }; export const Code = React.forwardRef(function Code({ maxHeight, code, language, showLineNumbers, theme, headerTitle, actions, isDisabled, type = 'block', isRunnable = false, onClick, tooltipText, as, 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 Component = as !== null && as !== void 0 ? as : (onClick ? 'button' : 'span'); 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 (headerTitle === undefined || headerTitle === '') { 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; } })(); const handleClick = (e) => { if (isDisabled === true) { e.preventDefault(); return; } onClick === null || onClick === void 0 ? void 0 : onClick(e); }; const inlineClasses = classNames('ndl-inline-code', { 'ndl-disabled': isDisabled, 'ndl-runnable': isRunnable, }, className); if (type === 'inline') { return (_jsx(ConditionalWrap, { shouldWrap: tooltipText !== undefined, wrap: (children) => (_jsxs(Tooltip, { type: "simple", children: [_jsx(Tooltip.Trigger, { hasButtonWrapper: Component === 'button', children: children }), _jsx(Tooltip.Content, { children: tooltipText })] })), children: _jsx(Component, Object.assign({ className: inlineClasses, style: style, onClick: handleClick, disabled: isDisabled, ref: ref }, htmlAttributes, { children: _jsx("code", { children: code }) })) })); } return (_jsx(CodeBlockWrapper, Object.assign({ className: className, style: style, ref: ref, maxHeight: maxHeight, code: code, headerTitle: headerTitle, isDisabled: isDisabled, actions: actions, shouldShowExpandButton: shouldShowExpandButton, setShouldShowExpandButton: setShouldShowExpandButton }, htmlAttributes, { children: _jsx(SyntaxHighlighter, { language: loadedLanguage || 'text', style: Object.assign(Object.assign({}, getTheme()), { 'pre[class*="language-"]': { border: 0, color: 'rgb(var(--theme-palette-neutral-text-default))', lineHeight: '1', padding: `0 ${calculateRightPadding !== null && calculateRightPadding !== void 0 ? calculateRightPadding : 0}px 0.75em ${showLineNumbers === true ? 0 : 0.75}em`, width: 'fit-content', } }), // Turn on 'showLineNumbers' & '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: showLineNumbers, children: code }) }))); }); //# sourceMappingURL=Code.js.map