QuoteCard.jsx 8.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
import React, { useState, useRef } from 'react';
import styled from 'styled-components';
import html2canvas from 'html2canvas';

// 更新中文字体数组,包含显示名称和 CSS 字体族名称
const chineseFonts = [
    { name: '系统默认', value: '-apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif' },
    { name: '无衬线', value: '"PingFang SC", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif' },
    { name: '衬线', value: 'Georgia, "Nimbus Roman No9 L", "Songti SC", "Noto Serif CJK SC", "Source Han Serif SC", "Source Han Serif CN", STSong, "AR PL New Sung", "AR PL SungtiL GB", NSimSun, SimSun, serif' },
    { name: '等宽', value: '"SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, "Noto Sans Mono CJK SC", monospace' },
  ];

// 定义可选的英文字体
const englishFonts = [
  { name: 'Arial', value: 'Arial' },
  { name: 'Times New Roman', value: 'Times New Roman' },
  { name: 'Georgia', value: 'Georgia' },
  { name: 'Verdana', value: 'Verdana' },
  { name: 'Courier New', value: 'Courier New' },
];

// 定义可选的背景颜色
const backgroundOptions = [
  { name: '白色', value: '#FFFFFF' },
  { name: '黑色', value: '#333333' }, // 修改为深灰色
  { name: '黄色纸张', value: '#fdf6e3' },
  { name: '自定义', value: 'custom' },
];

const Container = styled.div`
  display: flex;
  flex-direction: row;
  gap: 20px;
  padding: 20px;
  justify-content: center;
  background-color: #f5f5f5;
  min-height: 100vh;
`;

const TitleLabel = styled.label`
  font-size: 16px; // 增大字体大小
  color: #1677FF; // 设置字体颜色为深色
`;


const InputContainer = styled.div`
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 15px;
`;

const PreviewContainer = styled.div`
  flex: 1;
  background-color: ${(props) => props.$bgColor || '#333333'}; // 使用短暂属性
  padding: 20px;
  border-radius: 12px;
  border: 1px solid #333;
  color: ${(props) => props.$fontColor || '#b8b83b'}; // 使用短暂属性
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  width: 100%;
  height: 100%;
`;

const QuoteText = styled.div`
  font-size: clamp(16px, 2.5vw, 24px);
  margin-bottom: 16px;
  color: ${(props) => props.$color || '#b8b83b'}; // 使用短暂属性
  text-align: center;
  line-height: 1.5;
  font-family: ${(props) => props.$fontFamily}, serif; // 使用短暂属性
  white-space: pre-wrap;
  word-wrap: break-word;
`;

const QuoteAuthor = styled.div`
  font-size: clamp(14px, 2vw, 18px);
  color: ${(props) => props.$color || '#888'}; // 使用短暂属性
  text-align: center;
  font-family: ${(props) => props.$fontFamily}, sans-serif; // 使用短暂属性
  margin-top: 20px;
`;

const InputText = styled.textarea`
  width: 100%;
  height: 80px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #dadce0;
  font-size: 16px;
  font-family: Arial, sans-serif;
  resize: vertical;
`;

const InputField = styled.input`
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #dadce0;
  font-size: 16px;
`;

const Select = styled.select`
  width: 100%;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #dadce0;
  font-size: 16px;
  background-color: #fff;
`;

const ColorInput = styled.input`
  width: 100%;
  padding: 5px;
  border: none;
  background-color: transparent;
`;

const Label = styled.label`
  font-size: 14px;
  margin-bottom: 5px;
`;

const DownloadButton = styled.button`
  padding: 10px 20px;
130
  background-color: #1677ff;
131 132 133 134 135 136 137 138
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
  margin-top: 20px;

  &:hover {
139
    background-color: #2f86ff;
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  }
`;

function QuoteCard() {
  const [chineseText, setChineseText] = useState('');
  const [englishText, setEnglishText] = useState('');
  const [author, setAuthor] = useState('');

  const [chineseFont, setChineseFont] = useState('SimSun');
  const [englishFont, setEnglishFont] = useState('Arial');
  const [fontColor, setFontColor] = useState('#b8b83b');
  const [authorColor, setAuthorColor] = useState('#b8b83b');
  const [bgColor, setBgColor] = useState('#333333');
  const [customBgColor, setCustomBgColor] = useState('#FFFFFF');

  const previewRef = useRef(null);

  const handleBackgroundChange = (e) => {
    const value = e.target.value;
    if (value !== 'custom') {
      setBgColor(value);
    }
  };

  const handleDownload = () => {
    if (previewRef.current) {
      html2canvas(previewRef.current, {
        backgroundColor: null,
        useCORS: true,
      }).then((canvas) => {
        const link = document.createElement('a');
        link.download = 'quote_card.png';
        link.href = canvas.toDataURL('image/png');
        link.click();
      }).catch((err) => {
        console.error('图片下载失败:', err);
      });
    }
  };

  return (
    <Container>
      {/* 左侧输入区域 */}
      <InputContainer>
        <TitleLabel>名言卡片生成器</TitleLabel>
        <InputText
          placeholder="请输入中文名人名言"
          value={chineseText}
          onChange={(e) => setChineseText(e.target.value)}
        />
        <InputText
          placeholder="请输入英文翻译"
          value={englishText}
          onChange={(e) => setEnglishText(e.target.value)}
        />
        <InputField
          placeholder="请输入作者姓名"
          value={author}
          onChange={(e) => setAuthor(e.target.value)}
        />

        {/* 字体选择 */}
        <Label>选择中文字体:</Label>
        <Select value={chineseFont} onChange={(e) => setChineseFont(e.target.value)}>
          {chineseFonts.map((font) => (
            <option key={font.value} value={font.value}>
              {font.name}
            </option>
          ))}
        </Select>

        <Label>选择英文字体:</Label>
        <Select value={englishFont} onChange={(e) => setEnglishFont(e.target.value)}>
          {englishFonts.map((font) => (
            <option key={font.value} value={font.value}>
              {font.name}
            </option>
          ))}
        </Select>

        {/* 字体颜色选择 */}
        <Label>选择字体颜色:</Label>
        <ColorInput
          type="color"
          value={fontColor}
          onChange={(e) => setFontColor(e.target.value)}
        />

        {/* 作者颜色选择 */}
        <Label>选择作者颜色:</Label>
        <ColorInput
          type="color"
          value={authorColor}
          onChange={(e) => setAuthorColor(e.target.value)}
        />

        {/* 背景色选择 */}
        <Label>选择背景颜色:</Label>
        <Select
          value={
            backgroundOptions.some((option) => option.value === bgColor)
              ? bgColor
              : 'custom'
          }
          onChange={handleBackgroundChange}
        >
          {backgroundOptions.map((option) => (
            <option key={option.name} value={option.value}>
              {option.name}
            </option>
          ))}
        </Select>
        {bgColor === 'custom' && (
          <>
            <Label>选择自定义背景颜色:</Label>
            <ColorInput
              type="color"
              value={customBgColor}
              onChange={(e) => setBgColor(e.target.value)}
              title="选择自定义背景颜色"
            />
          </>
        )}

        {/* 下载按钮 */}
        <DownloadButton onClick={handleDownload}>
          下载图片
        </DownloadButton>
      </InputContainer>

      {/* 右侧预览区域 */}
      <PreviewContainer
        $bgColor={bgColor}
        $fontColor={fontColor}
        ref={previewRef}
      >
        <QuoteText $color={fontColor} $fontFamily={chineseFont}>
          {chineseText || '请输入中文名人名言'}
        </QuoteText>
        <QuoteText $color={fontColor} $fontFamily={englishFont}>
          {englishText || 'Enter the English translation here.'}
        </QuoteText>
        <QuoteAuthor $color={authorColor} $fontFamily={englishFont}>
          —— {author || '作者姓名'}
        </QuoteAuthor>
      </PreviewContainer>
    </Container>
  );
}

export default QuoteCard;