@neo4j-ndl/react
Version:
React implementation of Neo4j Design System
111 lines (110 loc) • 5.83 kB
JavaScript
/**
*
* 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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBlock = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const react_1 = require("react");
const react_syntax_highlighter_1 = require("react-syntax-highlighter");
const prism_1 = require("react-syntax-highlighter/dist/cjs/styles/prism");
const CodeBlockWrapper_1 = require("../_common/CodeBlockWrapper");
const theme_1 = require("../theme");
const code_languages_1 = require("./code-languages");
const ndl_code_dark_1 = __importDefault(require("./themes/ndl-code-dark"));
const ndl_code_light_1 = __importDefault(require("./themes/ndl-code-light"));
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] = (0, react_1.useState)(maxHeight !== undefined);
const { theme: ndlTheme } = (0, theme_1.useNeedleTheme)();
const [loadedLanguage, setLoadedLanguage] = (0, react_1.useState)(null);
(0, react_1.useEffect)(() => {
if (language === undefined || language === 'text') {
setLoadedLanguage('text');
return;
}
(0, code_languages_1.loadLanguage)(language)
.then((module) => {
react_syntax_highlighter_1.PrismLight.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 ndl_code_dark_1.default;
case 'ndl-code-light':
return ndl_code_light_1.default;
case 'vs':
return prism_1.vs;
case 'base16-ateliersulphurpool.light':
return prism_1.base16AteliersulphurpoolLight;
case 'coy':
return prism_1.coy;
case 'duotone-light':
return prism_1.duotoneLight;
case 'ghcolors':
return prism_1.ghcolors;
case 'prism':
return prism_1.prism;
case 'solarizedlight':
return prism_1.solarizedlight;
default:
return ndlTheme === 'light' ? ndl_code_light_1.default : ndl_code_dark_1.default;
}
};
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 ((0, jsx_runtime_1.jsx)(CodeBlockWrapper_1.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: (0, jsx_runtime_1.jsx)(react_syntax_highlighter_1.PrismLight, { 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 }) })));
};
exports.CodeBlock = CodeBlock;
//# sourceMappingURL=CodeBlock.js.map