Skip to content

Commit fd3df1f

Browse files
committed
重构:将Hero组件模块化,拆分样式和功能代码
1 parent 7794c0d commit fd3df1f

File tree

5 files changed

+184
-145
lines changed

5 files changed

+184
-145
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import React from 'react';
2+
import {
3+
CodeBox,
4+
CodeHeader,
5+
CodeDots
6+
} from '../../styles/components/CodeExample.styles';
7+
import { VisualContainer } from '../../styles/components/Hero.styles';
8+
import CustomCodeBlock from '../CustomCodeBlock';
9+
10+
// 示例代码
11+
const codeExample = `// ==UserScript==
12+
// @name Typescript Userscript Template
13+
// @namespace https://github.com/JSREI/typescript-userscript-template
14+
// @version 1.0.0
15+
// @description A modern userscript template using TypeScript
16+
// @author Your Name
17+
// @match *://*/*
18+
// @run-at document-start
19+
// @grant GM_getValue
20+
// @grant GM_setValue
21+
// @grant GM_xmlhttpRequest
22+
// ==/UserScript==
23+
24+
import { storeData, retrieveData } from "./gm_api_utils";
25+
26+
class MyUserScript {
27+
private config = {
28+
enableLogging: true,
29+
theme: "dark"
30+
};
31+
32+
constructor() {
33+
this.initialize();
34+
}
35+
36+
private async initialize(): Promise<void> {
37+
console.log("UserScript initialized!");
38+
39+
// 存储并获取上次运行时间
40+
await storeData("lastRun", new Date().toISOString());
41+
const lastRun = await retrieveData<string>("lastRun", "从未运行");
42+
43+
if (this.config.enableLogging) {
44+
console.log(\`上次运行时间: \${lastRun}\`);
45+
}
46+
47+
this.setupEventListeners();
48+
}
49+
50+
private setupEventListeners(): void {
51+
document.addEventListener("click", this.handleClick);
52+
}
53+
54+
private handleClick = (e: MouseEvent): void => {
55+
if (this.config.enableLogging) {
56+
console.log("Clicked:", e.target);
57+
}
58+
};
59+
}
60+
61+
// 启动脚本
62+
new MyUserScript();`;
63+
64+
const CodeExample: React.FC = () => {
65+
return (
66+
<VisualContainer>
67+
<CodeBox>
68+
<CodeHeader>
69+
<CodeDots>
70+
<span></span>
71+
<span></span>
72+
<span></span>
73+
</CodeDots>
74+
</CodeHeader>
75+
<CustomCodeBlock
76+
code={codeExample}
77+
language="typescript"
78+
showLineNumbers={true}
79+
/>
80+
</CodeBox>
81+
</VisualContainer>
82+
);
83+
};
84+
85+
export default CodeExample;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react';
2+
import {
3+
ContentLeft,
4+
Title,
5+
Subtitle,
6+
ButtonGroup,
7+
PrimaryButton,
8+
SecondaryButton
9+
} from '../../styles/components/Hero.styles';
10+
import CodeExample from './CodeExample';
11+
12+
const HeroContent: React.FC = () => {
13+
return (
14+
<>
15+
<ContentLeft>
16+
<Title>Typescript Userscript 模板</Title>
17+
<Subtitle>
18+
现代化的油猴脚本开发模板,集成TypeScript、Rollup和自动化工具,让你的UserScript开发更加高效和愉快。
19+
</Subtitle>
20+
<ButtonGroup>
21+
<PrimaryButton href="https://github.com/JSREI/typescript-userscript-template" target="_blank">
22+
获取模板
23+
</PrimaryButton>
24+
<SecondaryButton href="https://github.com/JSREI/typescript-userscript-template#readme" target="_blank">
25+
查看文档
26+
</SecondaryButton>
27+
</ButtonGroup>
28+
</ContentLeft>
29+
30+
<CodeExample />
31+
</>
32+
);
33+
};
34+
35+
export default HeroContent;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from 'react';
2+
import { HeroSection, HeroContent as StyledHeroContent } from '../../styles/components/Hero.styles';
3+
import HeroContent from './HeroContent';
4+
5+
const Hero: React.FC = () => {
6+
return (
7+
<HeroSection id="home">
8+
<StyledHeroContent>
9+
<HeroContent />
10+
</StyledHeroContent>
11+
</HeroSection>
12+
);
13+
};
14+
15+
export default Hero;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import styled from 'styled-components';
2+
3+
export const CodeBox = styled.div`
4+
border-radius: ${({ theme }) => theme.borderRadius.medium};
5+
overflow: hidden;
6+
box-shadow: ${({ theme }) => theme.boxShadow.large};
7+
font-family: 'Fira Code', monospace;
8+
`;
9+
10+
export const CodeHeader = styled.div`
11+
background-color: #1e1e1e;
12+
padding: 0.75rem 1rem;
13+
display: flex;
14+
align-items: center;
15+
`;
16+
17+
export const CodeDots = styled.div`
18+
display: flex;
19+
gap: 0.5rem;
20+
21+
span {
22+
display: block;
23+
width: 12px;
24+
height: 12px;
25+
border-radius: 50%;
26+
27+
&:nth-child(1) {
28+
background-color: #ff5f56;
29+
}
30+
31+
&:nth-child(2) {
32+
background-color: #ffbd2e;
33+
}
34+
35+
&:nth-child(3) {
36+
background-color: #27c93f;
37+
}
38+
}
39+
`;

0 commit comments

Comments
 (0)