Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions moon/apps/web/components/CodeView/BlobView/CodeContent.module.css
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@

.codeLineNumber {
margin-left: 15px;
margin-left: 4px;
margin-right: 25px;
text-align: center;
user-select: none;
}

.viewChangeTab {
background-color: rgba(53, 53, 53, 0.103);
.toolbar {
background-color: #f6f8fa;
display: flex;
position: absolute;
align-items: center;
text-align: center;
height: 50px;
border-top-left-radius: 1rem;
border-top-right-radius: 1rem;
border-bottom: 1px solid #d1d9e0;
}

.viewChangeTabButton {
padding-left: 20px;
padding-right: 20px;
.toolbarLeftButton {
padding:0 20px;
height: 100%;
border-radius: 1rem;
border: none;
background-color: transparent;
border-radius: 0.5rem;
}

.viewChangeTabButton:hover,
.viewChangeTabButton:checked {
background-color: rgba(0, 0, 0, 0.121);
.toolbarLeftButton.active {
background-color: #ffffff;
border: solid 1px #818b98;
}

.toolbarRightButton {
padding:0 20px;
height: 100%;
}

.toolbarRightButton:hover {
background-color: #e0e5ea;
transition: all 0.5s;
}
162 changes: 152 additions & 10 deletions moon/apps/web/components/CodeView/BlobView/CodeContent.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
'use client'
import 'github-markdown-css/github-markdown-light.css'

import { useEffect, useRef, useState } from 'react'
import { ListIcon } from '@gitmono/ui'
import { Button, Dropdown, MenuProps, message} from 'antd'
import { Highlight, themes } from 'prism-react-renderer'

import styles from './CodeContent.module.css'

// @ts-ignore
const CodeContent = ({ fileContent }) => {
const CodeContent = ({ fileContent, path }: { fileContent: string, path?: string[] }) => {
const [lfs, setLfs] = useState(false)
const [selectedLine, setSelectedLine] = useState<number | null>(null)

const menuItems: MenuProps = {
items: [
{
label: 'Copy line',
key: '1'
}
]
}

useEffect(() => {
if (isLfsContent(fileContent)) {
Expand All @@ -16,6 +30,86 @@ const CodeContent = ({ fileContent }) => {

const lineRef = useRef<HTMLDivElement[]>([])

const handleLineClick = (lineNumber: number) => {
setSelectedLine(lineNumber === selectedLine ? null : lineNumber)
}

const handleCopyLine = (line: string) => {
if (navigator.clipboard) {
navigator.clipboard.writeText(line)
.then(() => message.success('Copied to clipboard'))
.catch(() => message.error('Copied failed'))
} else {
const textarea = document.createElement('textarea');

textarea.value = line;
document.body.appendChild(textarea);
textarea.select();
try {
document.execCommand('copy');
message.success('Copied to clipboard');
} catch {
message.error('Copied failed');
}
}
}

const handleCopy = () => {
handleCopyLine(fileContent)
}

let filename;

if (!path || path.length === 0) {
message.error('Path information is missing');
filename = "";
}
else {
filename = path[path.length - 1]
}
Comment thread
benjamin-747 marked this conversation as resolved.
const handleRawView = () => {
// Create a new window/tab with the raw content
const newWindow = window.open();

if (newWindow) {
newWindow.document.write(`
<!DOCTYPE html>
<html lang="en">
<head>
<title>Raw Content</title>
<style>
body {
font-family: monospace;
white-space: pre;
padding: 20px;
}
</style>
</head>
<body>${fileContent.replace(/</g, '&lt;').replace(/>/g, '&gt;')}</body>
</html>
`);
newWindow.document.close();
} else {
message.error('Unable to open new window. Please check your browser settings.');
}
}
const handleDownload = () => {
const blob = new Blob([fileContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');

a.href = url;
a.download = filename;

// Append to the document, click it, and remove it
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

// Release the URL object
URL.revokeObjectURL(url);
}

function isLfsContent(content: string): boolean {
const lines = content.split('\n')
let foundVersion = false
Expand All @@ -39,15 +133,36 @@ const CodeContent = ({ fileContent }) => {

return (
<div>
<div className={styles.toolbar}>
<div className='m-2 h-8 rounded-lg bg-gray-200'>
<button className={`${styles.toolbarLeftButton} ${styles.active}`} defaultChecked={true}>
Code
</button>
<button className={styles.toolbarLeftButton}>Blame</button>
</div>
<span className='m-2 text-gray-500'>{`${fileContent.split('\n').length} lines . 2.79 KB`}</span>
Comment thread
benjamin-747 marked this conversation as resolved.
<div className='flex-1' />
<div className='m-2 h-8 rounded-lg border border-gray-200 p-1'>
<button className={styles.toolbarRightButton} onClick={handleRawView}>Raw</button>
<button className={styles.toolbarRightButton} onClick={handleCopy}>Copy</button>
<button className={styles.toolbarRightButton} onClick={handleDownload}>Download</button>
</div>
<div className='m-2 h-8 rounded-lg border border-gray-200 p-1'>
<button className={styles.toolbarRightButton}>Edit</button>
</div>
</div>
{/*todo: Dynamic support for language types*/}
<Highlight theme={themes.github} code={fileContent} language='rust'>
{({ style, tokens, getLineProps, getTokenProps }) => (
<pre
style={{
...style,
backgroundColor: '#fff',
padding: '16px',
paddingTop: '0px'
paddingTop: '30px',
userSelect: 'text'
}}
className='overflow-x-auto whitespace-pre rounded-lg bg-gray-100 p-4 text-sm'
className='overflow-x-auto whitespace-pre rounded-lg p-4 text-sm'
>
{!lfs &&
tokens.map((line, i) => (
Expand All @@ -56,13 +171,40 @@ const CodeContent = ({ fileContent }) => {
key={i}
{...getLineProps({ line })}
// @ts-ignore
ref={(el) => lineRef.current[i] = el as HTMLDivElement}
ref={(el) => (lineRef.current[i] = el as HTMLDivElement)}
style={{
backgroundColor: selectedLine === i ? '#f0f7ff' : 'transparent'
}}
>
<span className={styles.codeLineNumber}>{i + 1}</span>
{line.map((token, key) => (
// eslint-disable-next-line react/no-array-index-key
<span key={key} {...getTokenProps({ token })} />
))}
<div className='h-6 justify-center items-center align-middle'>
<span className='inline-block w-8 text-center justify-center items-center'>
{selectedLine === i ?
<Dropdown
menu={{
...menuItems,
onClick: (props) => {
if(props.key === '1') {
handleCopyLine(line.map(i=>i.content).join(''))
}
}
}}
className='bg-gray-100 border rounded border-gray-200 justify-center'

>
<Button icon={<ListIcon />} size={'small'}/>
</Dropdown>
:
null
}
</span>
<span className={styles.codeLineNumber} onClick={() => handleLineClick(i)}>
{i + 1}
</span>
{line.map((token, key) => (
// eslint-disable-next-line react/no-array-index-key
<span key={key} {...getTokenProps({ token })} />
))}
</div>
</div>
))}
{lfs && <span>(Sorry about that, but we can’t show files that are this big right now.)</span>}
Expand All @@ -73,4 +215,4 @@ const CodeContent = ({ fileContent }) => {
)
}

export default CodeContent;
export default CodeContent;
4 changes: 2 additions & 2 deletions moon/apps/web/components/CodeView/TreeView/BreadCrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ const Bread = ({ path }:any) => {
});

return (
<div className='m-4 flex items-center overflow-x-auto pb-2 no-scrollbar'>
<div className='flex items-center overflow-x-auto p-2 no-scrollbar mt-2'>

{breadCrumbItems?.map((item: { isLast: any; title: string; href: string | UrlObject; }, index: number) => (
<React.Fragment key={item.title}>
{/* displayed after the home item and before non-last items */}
{index > 0 && (
<span className="text-gray-400 mx-1">/</span>
<span className="text-gray-400">/</span>
)}
{/* Current breadcrumb item */}
{item.isLast ? (
Expand Down
20 changes: 7 additions & 13 deletions moon/apps/web/pages/[org]/code/blob/[...path].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ import { CommentSection } from '@/components/CodeView/BlobView/CommentSection'
const codeStyle = {
borderRadius: 8,
width: 'calc(85% - 8px)',
background: '#fff'
}

const breadStyle = {
minHeight: 30,
borderRadius: 8,
overflow: 'hidden',
width: 'calc(100% - 8px)',
background: '#fff'
background: '#fff',
border: '1px solid #d1d9e0',
margin: '0 10px'
}

interface Comment {
Expand Down Expand Up @@ -85,11 +79,11 @@ function BlobPage() {
return (
<div style={{overflow: 'auto'}}>
<Flex gap='middle' wrap>
<Layout style={breadStyle}>
<Layout>
<Bread path={path} />
</Layout>
<Layout style={codeStyle}>
<CodeContent fileContent={fileContent} />
<CodeContent fileContent={fileContent} path={path} />
</Layout>
<Layout>
{/* @ts-ignore */}
Expand All @@ -105,7 +99,7 @@ BlobPage.getProviders = (
| string
| number
| boolean
| React.ReactElement<any, string | React.JSXElementConstructor<any>>
| React.ReactElement
| Iterable<React.ReactNode>
| React.ReactPortal
| Promise<React.AwaitedReactNode>
Expand All @@ -120,4 +114,4 @@ BlobPage.getProviders = (
)
}

export default BlobPage
export default BlobPage