@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
101 lines (98 loc) • 3.33 kB
JavaScript
'use client';
import FlexBasic_default from "../Flex/FlexBasic.mjs";
import MaterialFileTypeIcon_default from "../MaterialFileTypeIcon/MaterialFileTypeIcon.mjs";
import { headerVariants, prefix, styles, variants } from "./style.mjs";
import { memo, useMemo } from "react";
import { jsx, jsxs } from "react/jsx-runtime";
import { cx } from "antd-style";
import { PatchDiff } from "@pierre/diffs/react";
//#region src/CodeDiff/PatchDiff.tsx
const countPatchChanges = (patch) => {
const lines = patch.split("\n");
let additions = 0;
let deletions = 0;
for (const line of lines) if (line.startsWith("+") && !line.startsWith("+++")) additions++;
else if (line.startsWith("-") && !line.startsWith("---")) deletions++;
return {
additions,
deletions
};
};
const PatchDiff$1 = memo(({ patch, language, fileName, viewMode = "split", showHeader = true, variant = "filled", className, classNames, styles: customStyles, actionsRender, diffOptions, ...rest }) => {
const displayName = useMemo(() => {
if (fileName) return fileName;
const match = patch.match(/^(?:-{3}|\+{3})\s+(?:a\/|b\/)?(.+?)(?:\t|$)/m);
if (match?.[1]) return match[1];
if (language) return language;
return "patch";
}, [
fileName,
patch,
language
]);
const { additions, deletions } = useMemo(() => countPatchChanges(patch), [patch]);
const actions = useMemo(() => {
if (!actionsRender) return null;
return actionsRender({
originalNode: null,
patch
});
}, [actionsRender, patch]);
const options = useMemo(() => ({
diffStyle: viewMode,
disableFileHeader: true,
...diffOptions
}), [viewMode, diffOptions]);
return /* @__PURE__ */ jsxs(FlexBasic_default, {
className: cx(variants({ variant }), className),
"data-code-type": "patch-diff",
...rest,
children: [showHeader && /* @__PURE__ */ jsxs("div", {
className: cx(headerVariants({ variant }), classNames?.header),
style: customStyles?.header,
children: [/* @__PURE__ */ jsxs(FlexBasic_default, {
align: "center",
gap: 8,
horizontal: true,
children: [/* @__PURE__ */ jsx(MaterialFileTypeIcon_default, {
filename: fileName || displayName,
size: 18,
type: "file"
}), /* @__PURE__ */ jsx("span", { children: displayName })]
}), /* @__PURE__ */ jsxs(FlexBasic_default, {
align: "center",
gap: 8,
horizontal: true,
children: [(deletions > 0 || additions > 0) && /* @__PURE__ */ jsxs(FlexBasic_default, {
className: styles.stats,
gap: 8,
horizontal: true,
children: [deletions > 0 && /* @__PURE__ */ jsxs("span", {
className: styles.deletions,
children: ["-", deletions]
}), additions > 0 && /* @__PURE__ */ jsxs("span", {
className: styles.additions,
children: ["+", additions]
})]
}), actions && /* @__PURE__ */ jsx(FlexBasic_default, {
align: "center",
className: cx(`${prefix}-actions`, styles.actions),
gap: 4,
children: actions
})]
})]
}), /* @__PURE__ */ jsx("div", {
className: cx(styles.body, classNames?.body),
style: customStyles?.body,
children: /* @__PURE__ */ jsx(PatchDiff, {
options,
patch
})
})]
});
});
PatchDiff$1.displayName = "PatchDiff";
var PatchDiff_default = PatchDiff$1;
//#endregion
export { PatchDiff_default as default };
//# sourceMappingURL=PatchDiff.mjs.map