2020-01-22 12:08:28 +01:00
|
|
|
import React, { FC } from "react";
|
|
|
|
|
import styled from "styled-components";
|
|
|
|
|
// @ts-ignore we have no typings for react-diff-view
|
|
|
|
|
import { Diff, useTokenizeWorker } from "react-diff-view";
|
|
|
|
|
import { File } from "./DiffTypes";
|
|
|
|
|
|
|
|
|
|
// styling for the diff tokens
|
|
|
|
|
// this must be aligned with th style, which is used in the SyntaxHighlighter component
|
|
|
|
|
import "highlight.js/styles/arduino-light.css";
|
|
|
|
|
|
|
|
|
|
const DiffView = styled(Diff)`
|
|
|
|
|
/* align line numbers */
|
|
|
|
|
& .diff-gutter {
|
|
|
|
|
text-align: right;
|
|
|
|
|
}
|
|
|
|
|
/* column sizing */
|
|
|
|
|
> colgroup .diff-gutter-col {
|
|
|
|
|
width: 3.25rem;
|
|
|
|
|
}
|
|
|
|
|
/* prevent following content from moving down */
|
|
|
|
|
> .diff-gutter:empty:hover::after {
|
|
|
|
|
font-size: 0.7rem;
|
|
|
|
|
}
|
|
|
|
|
/* smaller font size for code */
|
|
|
|
|
& .diff-line {
|
|
|
|
|
font-size: 0.75rem;
|
|
|
|
|
}
|
|
|
|
|
/* comment padding for sidebyside view */
|
|
|
|
|
&.split .diff-widget-content .is-indented-line {
|
|
|
|
|
padding-left: 3.25rem;
|
|
|
|
|
}
|
|
|
|
|
/* comment padding for combined view */
|
|
|
|
|
&.unified .diff-widget-content .is-indented-line {
|
|
|
|
|
padding-left: 6.5rem;
|
|
|
|
|
}
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
// WebWorker which creates tokens for syntax highlighting
|
2020-01-27 13:30:46 +01:00
|
|
|
const tokenize = new Worker("./Tokenize.worker.ts", { name: "tokenizer", type: "module" });
|
2020-01-22 12:08:28 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
file: File;
|
|
|
|
|
viewType: "split" | "unified";
|
|
|
|
|
className?: string;
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-22 15:49:50 +01:00
|
|
|
const determineLanguage = (file: File) => {
|
|
|
|
|
if (file.language) {
|
|
|
|
|
return file.language.toLowerCase();
|
|
|
|
|
}
|
|
|
|
|
return "text";
|
|
|
|
|
};
|
|
|
|
|
|
2020-01-22 12:08:28 +01:00
|
|
|
const TokenizedDiffView: FC<Props> = ({ file, viewType, className, children }) => {
|
|
|
|
|
const { tokens } = useTokenizeWorker(tokenize, {
|
|
|
|
|
hunks: file.hunks,
|
2020-01-22 15:49:50 +01:00
|
|
|
language: determineLanguage(file)
|
2020-01-22 12:08:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<DiffView className={className} viewType={viewType} tokens={tokens} hunks={file.hunks} diffType={file.type}>
|
|
|
|
|
{children}
|
|
|
|
|
</DiffView>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default TokenizedDiffView;
|