UNPKG

optimizely-oui

Version:

Optimizely's Component Library.

125 lines (110 loc) 4.87 kB
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import classNames from "classnames"; import { cloneDeep } from "lodash"; import PropTypes from "prop-types"; import React from "react"; import Icon from "react-oui-icons"; import ReactDiffViewer from "react-diff-viewer"; import { brandBlueDark } from "../../tokens/forimport/index.es"; /** * * @param {Object} props - Properties passed to component * @returns {ReactElement} */ export var CodeDiff = function CodeDiff(_ref) { var borderLocation = _ref.borderLocation, className = _ref.className, header = _ref.header, newValue = _ref.newValue, oldValue = _ref.oldValue, testSection = _ref.testSection, props = _objectWithoutProperties(_ref, ["borderLocation", "className", "header", "newValue", "oldValue", "testSection"]); var before = cloneDeep(oldValue); var after = cloneDeep(newValue); // Attempt to stringify, since non-strings will // throw an error in ReactDiffViewer anyways if ([before, after].some(function (val) { return typeof val !== "string"; })) { before = JSON.stringify(before, null, 2); after = JSON.stringify(after, null, 2); } return React.createElement("div", { className: classNames("oui-codediff", { "border--top": borderLocation.includes("top"), "border--right": borderLocation.includes("right"), "border--bottom": borderLocation.includes("bottom"), "border--left": borderLocation.includes("left") }, className), "data-test-section": testSection }, header && React.createElement("div", { className: "background--grey-25 border--bottom color--base soft--sides soft-half--ends" }, header), React.createElement(ReactDiffViewer, _extends({}, props, { codeFoldMessageRenderer: function codeFoldMessageRenderer() { return (// eslint-disable-line react/jsx-no-bind React.createElement("div", { className: "flex", style: { marginTop: 3 } }, React.createElement(Icon, { name: "ellipsis", fill: brandBlueDark })) ); }, oldValue: before, newValue: after /** * BEGIN: TODO(AXIOM-138) Ensure styling is correct and remove * remaining default declarations mentioned below */ // See https://github.com/praneshr/react-diff-viewer/tree/v3.0.0#overriding-styles // for more info on styling options , styles: { line: { wordBreak: "break-word" }, codeFoldGutter: { backgroundColor: "#f1f8ff" // Sames as variables.light.codeFoldBackground } } /** * END: TODO(AXIOM-138) Ensure styling is correct and * remove remaining default declarations above */ }))); }; CodeDiff.propTypes = { /** Where borders should be added. Defaults to borders on every side (OUI) */ borderLocation: PropTypes.arrayOf(PropTypes.oneOf(["top", "right", "bottom", "left"])), /** CSS class names. */ className: PropTypes.string, /** * Differential title that spans all columns. * If not provided, no header will be shown (OUI) */ header: PropTypes.node, /** * New value. If of type "object", will be * formatted with JSON.stringify (ReactDiffViewer) */ newValue: PropTypes.any.isRequired, /** Show and hide line numbers (ReactDiffViewer) */ oldValue: PropTypes.any.isRequired, /** Value used for testing via data-test-section attribute (OUI) */ testSection: PropTypes.string /** * Get info on the core component (ReactDiffViewer) * props at https://github.com/praneshr/react-diff-viewer/tree/v3.0.0#props */ }; CodeDiff.defaultProps = { borderLocation: ["top", "right", "bottom", "left"], compareMethod: "diffWords", header: "", testSection: "" }; export default CodeDiff;