📄 Lectures: pdf.js viewer в модалке вместо Google Viewer
- vite.config.ts: копирование pdf.worker + cMaps - LecturesPage.tsx: react-pdf viewer с навигацией по страницам - Скачивание через Telegram.WebApp.downloadFile (без изменений) - Убрал Google Docs Viewer — приватность
This commit is contained in:
+254
-46
@@ -1,14 +1,18 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useState, useMemo } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Document, Page, pdfjs } from 'react-pdf';
|
||||
import { useLanguage } from '../i18n/LanguageContext';
|
||||
import { fonts, radius } from '../styles/theme';
|
||||
import { getLectures, type LectureListItem } from '../api';
|
||||
|
||||
// pdf.js worker + cMaps (для кириллицы и польских букв)
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = '/pdf-worker/pdf.worker.min.mjs';
|
||||
|
||||
/**
|
||||
* LecturesPage — список лекций по уровню пользователя.
|
||||
* LecturesPage — список лекций + модалка с pdf.js viewer.
|
||||
*
|
||||
* Открытие: через Telegram.WebApp.openLink() — открывает PDF в браузере.
|
||||
* Скачивание: через Telegram.WebApp.downloadFile() — скачивает файл.
|
||||
* PDF рендерится через pdf.js в canvas — работает в Telegram WebView.
|
||||
* Скачивание — через Telegram.WebApp.downloadFile.
|
||||
*/
|
||||
const LecturesPage: React.FC = () => {
|
||||
const navigate = useNavigate();
|
||||
@@ -17,6 +21,14 @@ const LecturesPage: React.FC = () => {
|
||||
const [level, setLevel] = useState('A1');
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [activeLecture, setActiveLecture] = useState<LectureListItem | null>(null);
|
||||
|
||||
// Состояние pdf.js viewer
|
||||
const [numPages, setNumPages] = useState<number>(0);
|
||||
const [pageNumber, setPageNumber] = useState<number>(1);
|
||||
const [pdfLoading, setPdfLoading] = useState(false);
|
||||
const [pdfError, setPdfError] = useState<string | null>(null);
|
||||
const [pageWidth, setPageWidth] = useState<number>(window.innerWidth - 40);
|
||||
|
||||
const brandColor = '#2ECC71';
|
||||
const lectureColor = '#4FA9E8';
|
||||
@@ -32,6 +44,12 @@ const LecturesPage: React.FC = () => {
|
||||
...extra,
|
||||
});
|
||||
|
||||
// pdf.js options — обязательно вне компонента или useMemo
|
||||
const pdfOptions = useMemo(() => ({
|
||||
cMapUrl: '/cmaps/',
|
||||
cMapPacked: true,
|
||||
}), []);
|
||||
|
||||
useEffect(() => {
|
||||
getLectures()
|
||||
.then((data) => {
|
||||
@@ -46,32 +64,38 @@ const LecturesPage: React.FC = () => {
|
||||
});
|
||||
}, []);
|
||||
|
||||
// Обновляем ширину страницы при ресайзе
|
||||
useEffect(() => {
|
||||
const handleResize = () => {
|
||||
setPageWidth(window.innerWidth - 40);
|
||||
};
|
||||
window.addEventListener('resize', handleResize);
|
||||
return () => window.removeEventListener('resize', handleResize);
|
||||
}, []);
|
||||
|
||||
const getFileUrl = (lecture: LectureListItem): string => {
|
||||
return `https://api.filwordspl.com${lecture.file_url}`;
|
||||
};
|
||||
|
||||
/**
|
||||
* Открыть лекцию: в Telegram — через openLink (Safari/Chrome),
|
||||
* в браузере — через window.open.
|
||||
*/
|
||||
const handleOpenLecture = (lecture: LectureListItem) => {
|
||||
const url = getFileUrl(lecture);
|
||||
setActiveLecture(lecture);
|
||||
setPageNumber(1);
|
||||
setNumPages(0);
|
||||
setPdfError(null);
|
||||
setPdfLoading(true);
|
||||
};
|
||||
|
||||
if (window.Telegram?.WebApp?.openLink) {
|
||||
window.Telegram.WebApp.openLink(url);
|
||||
} else {
|
||||
window.open(url, '_blank');
|
||||
}
|
||||
const handleCloseLecture = () => {
|
||||
setActiveLecture(null);
|
||||
setPageNumber(1);
|
||||
setNumPages(0);
|
||||
setPdfError(null);
|
||||
setPdfLoading(false);
|
||||
};
|
||||
|
||||
/**
|
||||
* Скачать лекцию: в Telegram — через downloadFile,
|
||||
* в браузере — через скрытую ссылку.
|
||||
*/
|
||||
const handleDownloadLecture = (lecture: LectureListItem) => {
|
||||
const url = getFileUrl(lecture);
|
||||
|
||||
// Telegram WebApp имеет свой метод downloadFile
|
||||
if (window.Telegram?.WebApp?.downloadFile) {
|
||||
window.Telegram.WebApp.downloadFile({
|
||||
url,
|
||||
@@ -80,7 +104,6 @@ const LecturesPage: React.FC = () => {
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback — обычное скачивание через ссылку
|
||||
const a = document.createElement('a');
|
||||
a.href = url;
|
||||
a.download = `${lecture.title}.${lecture.file_type}`;
|
||||
@@ -99,7 +122,7 @@ const LecturesPage: React.FC = () => {
|
||||
fontFamily: fonts.main,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
padding: '24px',
|
||||
padding: '16px',
|
||||
position: 'relative',
|
||||
overflow: 'hidden',
|
||||
}}>
|
||||
@@ -114,26 +137,29 @@ const LecturesPage: React.FC = () => {
|
||||
|
||||
{/* Заголовок */}
|
||||
<div style={{
|
||||
display: 'flex', alignItems: 'center', gap: '10px',
|
||||
marginBottom: '24px',
|
||||
display: 'flex', alignItems: 'center', gap: '8px',
|
||||
marginBottom: '16px',
|
||||
}}>
|
||||
<button
|
||||
onClick={() => navigate('/learning')}
|
||||
style={{
|
||||
...glass({ padding: '10px 14px', borderRadius: radius.md }),
|
||||
...glass({ padding: '8px 12px', borderRadius: radius.md }),
|
||||
color: textColor, fontSize: '14px', fontWeight: 600, cursor: 'pointer',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
←
|
||||
</button>
|
||||
<h1 style={{
|
||||
fontSize: '22px', fontWeight: 800, margin: 0, color: textColor, flex: 1,
|
||||
fontSize: '18px', fontWeight: 800, margin: 0, color: textColor, flex: 1,
|
||||
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
||||
}}>
|
||||
📄 {t('lectures') || 'Лекции'}
|
||||
</h1>
|
||||
<span style={{
|
||||
...glass({ padding: '6px 12px', borderRadius: radius.sm }),
|
||||
fontSize: '14px', fontWeight: 800, color: brandColor,
|
||||
...glass({ padding: '4px 10px', borderRadius: radius.sm }),
|
||||
fontSize: '13px', fontWeight: 800, color: brandColor,
|
||||
flexShrink: 0,
|
||||
}}>
|
||||
{level}
|
||||
</span>
|
||||
@@ -149,9 +175,7 @@ const LecturesPage: React.FC = () => {
|
||||
⚠️ {error}
|
||||
</div>
|
||||
) : lectures.length === 0 ? (
|
||||
<div style={{
|
||||
textAlign: 'center', padding: '60px 20px', color: subColor,
|
||||
}}>
|
||||
<div style={{ textAlign: 'center', padding: '60px 20px', color: subColor }}>
|
||||
<div style={{ fontSize: '64px', marginBottom: '16px' }}>📭</div>
|
||||
<p style={{ fontSize: '16px', marginBottom: '8px', fontWeight: 600 }}>
|
||||
{t('lecturesEmpty') || 'Лекции в разработке'}
|
||||
@@ -161,14 +185,12 @@ const LecturesPage: React.FC = () => {
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div style={{
|
||||
display: 'flex', flexDirection: 'column', gap: '12px',
|
||||
}}>
|
||||
<div style={{ display: 'flex', flexDirection: 'column', gap: '10px' }}>
|
||||
{lectures.map((lecture) => (
|
||||
<div
|
||||
key={lecture.id}
|
||||
style={{
|
||||
padding: '18px 20px',
|
||||
padding: '14px 16px',
|
||||
borderRadius: radius.lg,
|
||||
border: '1px solid rgba(255,255,255,0.7)',
|
||||
background: 'rgba(255,255,255,0.55)',
|
||||
@@ -177,17 +199,19 @@ const LecturesPage: React.FC = () => {
|
||||
boxShadow: '0 6px 20px rgba(31,38,73,0.08), inset 0 1px 0 rgba(255,255,255,0.85)',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: '16px',
|
||||
gap: '10px',
|
||||
width: '100%',
|
||||
boxSizing: 'border-box',
|
||||
}}
|
||||
>
|
||||
{/* Клик по иконке/названию — открыть */}
|
||||
<button
|
||||
onClick={() => handleOpenLecture(lecture)}
|
||||
style={{
|
||||
flex: 1,
|
||||
minWidth: 0,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: '16px',
|
||||
gap: '12px',
|
||||
border: 'none',
|
||||
background: 'transparent',
|
||||
cursor: 'pointer',
|
||||
@@ -196,31 +220,29 @@ const LecturesPage: React.FC = () => {
|
||||
fontFamily: fonts.main,
|
||||
}}
|
||||
>
|
||||
<div style={{ fontSize: '32px', flexShrink: 0 }}>
|
||||
<div style={{ fontSize: '28px', flexShrink: 0 }}>
|
||||
{lecture.file_type === 'pdf' ? '📕' : lecture.file_type === 'html' ? '🌐' : '📄'}
|
||||
</div>
|
||||
<div style={{ flex: 1, minWidth: 0 }}>
|
||||
<div style={{
|
||||
fontSize: '16px', fontWeight: 700, color: textColor,
|
||||
marginBottom: '4px',
|
||||
fontSize: '15px', fontWeight: 700, color: textColor,
|
||||
marginBottom: '2px',
|
||||
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
||||
}}>
|
||||
{lecture.title}
|
||||
</div>
|
||||
<div style={{
|
||||
fontSize: '11px', color: subColor, opacity: 0.7,
|
||||
}}>
|
||||
<div style={{ fontSize: '11px', color: subColor, opacity: 0.7 }}>
|
||||
{lecture.file_type.toUpperCase()} · {lecture.file_size_kb} KB
|
||||
</div>
|
||||
</div>
|
||||
</button>
|
||||
|
||||
{/* Кнопка скачать */}
|
||||
<button
|
||||
onClick={() => handleDownloadLecture(lecture)}
|
||||
style={{
|
||||
width: '44px',
|
||||
height: '44px',
|
||||
width: '40px',
|
||||
height: '40px',
|
||||
minWidth: '40px',
|
||||
borderRadius: radius.md,
|
||||
border: `1px solid ${brandColor}30`,
|
||||
background: `${brandColor}15`,
|
||||
@@ -248,6 +270,192 @@ const LecturesPage: React.FC = () => {
|
||||
Filwords PL © 2026
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* ============ МОДАЛКА С PDF.JS VIEWER ============ */}
|
||||
{activeLecture && (
|
||||
<div
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: 0, left: 0, right: 0, bottom: 0,
|
||||
background: 'rgba(20,22,35,0.95)',
|
||||
zIndex: 1000,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
padding: '8px',
|
||||
boxSizing: 'border-box',
|
||||
}}
|
||||
>
|
||||
{/* Заголовок модалки */}
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: '8px',
|
||||
padding: '8px 12px',
|
||||
background: 'rgba(255,255,255,0.95)',
|
||||
borderRadius: radius.md,
|
||||
marginBottom: '8px',
|
||||
flexShrink: 0,
|
||||
}}>
|
||||
<button
|
||||
onClick={handleCloseLecture}
|
||||
style={{
|
||||
padding: '6px 12px',
|
||||
borderRadius: radius.sm,
|
||||
border: 'none',
|
||||
background: 'rgba(0,0,0,0.05)',
|
||||
color: textColor,
|
||||
fontSize: '14px',
|
||||
fontWeight: 600,
|
||||
cursor: 'pointer',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
←
|
||||
</button>
|
||||
<div style={{
|
||||
flex: 1, minWidth: 0,
|
||||
fontSize: '13px', fontWeight: 700, color: textColor,
|
||||
overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap',
|
||||
}}>
|
||||
{activeLecture.title}
|
||||
</div>
|
||||
<button
|
||||
onClick={() => handleDownloadLecture(activeLecture)}
|
||||
style={{
|
||||
padding: '6px 12px',
|
||||
borderRadius: radius.sm,
|
||||
border: 'none',
|
||||
background: `${brandColor}20`,
|
||||
color: brandColor,
|
||||
fontSize: '13px',
|
||||
fontWeight: 700,
|
||||
cursor: 'pointer',
|
||||
flexShrink: 0,
|
||||
}}
|
||||
>
|
||||
⬇
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* PDF viewer */}
|
||||
<div style={{
|
||||
flex: 1,
|
||||
background: '#FFFFFF',
|
||||
borderRadius: radius.md,
|
||||
overflow: 'auto',
|
||||
minHeight: 0,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
alignItems: 'center',
|
||||
padding: '8px 0',
|
||||
position: 'relative',
|
||||
}}>
|
||||
{pdfLoading && (
|
||||
<div style={{
|
||||
position: 'absolute', top: '20px',
|
||||
padding: '8px 16px',
|
||||
background: 'rgba(0,0,0,0.7)',
|
||||
color: '#FFF',
|
||||
borderRadius: radius.md,
|
||||
fontSize: '13px',
|
||||
zIndex: 10,
|
||||
}}>
|
||||
⏳ Загрузка PDF...
|
||||
</div>
|
||||
)}
|
||||
|
||||
{pdfError && (
|
||||
<div style={{
|
||||
padding: '40px 20px',
|
||||
color: '#E74C3C',
|
||||
fontSize: '14px',
|
||||
textAlign: 'center',
|
||||
}}>
|
||||
⚠️ {pdfError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Document
|
||||
file={getFileUrl(activeLecture)}
|
||||
options={pdfOptions}
|
||||
onLoadSuccess={({ numPages }) => {
|
||||
setNumPages(numPages);
|
||||
setPdfLoading(false);
|
||||
}}
|
||||
onLoadError={(err) => {
|
||||
console.error('PDF load error:', err);
|
||||
setPdfError('Не удалось загрузить PDF');
|
||||
setPdfLoading(false);
|
||||
}}
|
||||
loading=""
|
||||
>
|
||||
<Page
|
||||
pageNumber={pageNumber}
|
||||
width={pageWidth}
|
||||
renderTextLayer={false}
|
||||
renderAnnotationLayer={false}
|
||||
/>
|
||||
</Document>
|
||||
|
||||
{/* Навигация по страницам */}
|
||||
{numPages > 1 && (
|
||||
<div style={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
gap: '12px',
|
||||
padding: '12px',
|
||||
position: 'sticky',
|
||||
bottom: '0',
|
||||
background: 'rgba(255,255,255,0.95)',
|
||||
borderRadius: radius.md,
|
||||
marginTop: '12px',
|
||||
}}>
|
||||
<button
|
||||
onClick={() => setPageNumber(p => Math.max(1, p - 1))}
|
||||
disabled={pageNumber <= 1}
|
||||
style={{
|
||||
padding: '8px 16px',
|
||||
borderRadius: radius.sm,
|
||||
border: 'none',
|
||||
background: pageNumber <= 1 ? 'rgba(0,0,0,0.05)' : brandColor,
|
||||
color: pageNumber <= 1 ? '#999' : '#FFF',
|
||||
fontSize: '16px',
|
||||
fontWeight: 700,
|
||||
cursor: pageNumber <= 1 ? 'default' : 'pointer',
|
||||
}}
|
||||
>
|
||||
←
|
||||
</button>
|
||||
<div style={{
|
||||
fontSize: '14px',
|
||||
fontWeight: 700,
|
||||
color: textColor,
|
||||
minWidth: '80px',
|
||||
textAlign: 'center',
|
||||
}}>
|
||||
{pageNumber} / {numPages}
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setPageNumber(p => Math.min(numPages, p + 1))}
|
||||
disabled={pageNumber >= numPages}
|
||||
style={{
|
||||
padding: '8px 16px',
|
||||
borderRadius: radius.sm,
|
||||
border: 'none',
|
||||
background: pageNumber >= numPages ? 'rgba(0,0,0,0.05)' : brandColor,
|
||||
color: pageNumber >= numPages ? '#999' : '#FFF',
|
||||
fontSize: '16px',
|
||||
fontWeight: 700,
|
||||
cursor: pageNumber >= numPages ? 'default' : 'pointer',
|
||||
}}
|
||||
>
|
||||
→
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user