Skip to content

Commit 3fe2746

Browse files
committed
Image viewer navigation controls
1 parent 7400573 commit 3fe2746

File tree

10 files changed

+101
-31
lines changed

10 files changed

+101
-31
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-document-viewer",
3-
"version": "2.3.1",
3+
"version": "2.3.2",
44
"description": "React-based component which provides epub,mobi,pdf,xls,xlsx,docx and image viewer capabilities for your browser",
55
"main": "src/index.js",
66
"author": "Sergey Ko <u306060@gmail.com>",

readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
<p align="center">
1+
<p align="center">
22

3-
![ReactDocViewer Logo](./doc/logo.png)
3+
![ReactDocViewer Logo](./doc/logo.png)
44

55
</p>
66

7-
![React Document Viewer Version](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Fway5%2Freact-document-viewer%2Frefs%2Fheads%2Fdev%2Fpackage.json&query=%24.version&label=ReactDocViewer:&color=%23F86071)
7+
![React Document Viewer Version](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fraw.githubusercontent.com%2Fway5%2Freact-document-viewer%2Frefs%2Fheads%2Fmaster%2Fpackage.json&query=%24.version&label=ReactDocViewer:&color=%23F86071)
88

99
### + INTRO
1010

1111
Accepted document formats:
1212

13-
- docx
13+
- docx
1414
- xls, xlsx
1515
- pdf
1616
- epub

src/index.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ if (process.env.NODE_ENV === 'development') {
1212
files: [
1313
{ src: '/docs/test.jpg', name: '', type: '' },
1414
{ src: '/docs/test.webp', name: 'Image #2', type: '' },
15-
{ src: '/docs/test.jpg', name: '', type: '' },
16-
{ src: '/docs/test.webp', name: 'Image #3', type: '' },
17-
{ src: '/docs/test.jpg', name: '', type: '' },
18-
{ src: '/docs/test.webp', name: 'Image #4', type: '' }
1915
],
2016
downloadInNewWindow: false,
2117
noNavbar: false,

src/locales/en-US.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"pageWidthOption": "Page Width",
2222
"sidebarToggle": "Toggle Sidebar",
2323
"viewThumbnail": "Show Thumbnails",
24+
"gotoFirst": "To the beginning",
25+
"gotoLast": "To the end",
26+
"navbarClickToDownload": "Click to download",
2427
"resetView": "Reset view",
2528
"formatInfoDocx": "Please upload a file in DOCX format",
2629
"formatInfoPPTx": "PPT(X) format is not supported at the moment",

src/locales/es-ES.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"pageWidthOption": "Ajuste de anchura",
2222
"sidebarToggle": "Barra lateral",
2323
"viewThumbnail": "Ver miniaturas",
24+
"gotoFirst": "Ir al inicio",
25+
"gotoLast": "Ir al final",
26+
"navbarClickToDownload": "Haga clic para descargar",
2427
"resetView": "Restablecer vista",
2528
"formatInfoDocx": "Por favor abra el documento en formato DOCX",
2629
"formatInfoPPTx": "PPT(X) formato no soportado actualmente",

src/locales/ru-RU.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"pageWidthOption": "Подогнать по ширине",
2222
"sidebarToggle": "Показать/скрыть боковую панель",
2323
"viewThumbnail": "Показать/скрыть миниатюры",
24+
"gotoFirst": "В начало",
25+
"gotoLast": "В конец",
26+
"navbarClickToDownload": "Нажмите, чтобы отобразить изображение",
2427
"resetView": "Сброс представления",
2528
"formatInfoDocx": "Пожалуйста, загрузите документ в формате DOCX",
2629
"formatInfoPPTx": "На данный момент PPT(X) формат не поддерживается",

src/locales/zh-CN.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
"pageWidthOption": "适合页宽",
2222
"sidebarToggle": "切换侧栏",
2323
"viewThumbnail": "显示缩略图",
24+
"gotoFirst": "第一的",
25+
"gotoLast": "去最后",
26+
"navbarClickToDownload": "点击下载",
2427
"formatInfoDocx": "请打开DOC格式文档",
2528
"formatInfoPPTx": "暂不支持PPT(X)文件预览,请关注后续更新!",
2629
"supportFileTypes": "目前仅支持DOCX,XLS(X),PDF,EPUB,FB2,MOBI格式的文件!",

src/plugins/img/Navigation.tsx

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import React, { ReactElement, ReactHTMLElement } from 'react';
1+
import React, { ReactElement } from 'react';
22
import { ImageViewerNavProps } from '../../definitions';
3-
import { TbPhotoDown } from 'react-icons/tb';
3+
import { TbChevronLeftPipe, TbChevronRightPipe, TbPhotoDown } from 'react-icons/tb';
4+
import { useTranslation } from 'react-i18next';
5+
import { Tooltip } from 'react-tooltip';
46

57
/**
68
* Navigation block
@@ -10,12 +12,13 @@ import { TbPhotoDown } from 'react-icons/tb';
1012
* @returns {*}
1113
*/
1214
export default function ImageViewerNavigation(props: ImageViewerNavProps) {
15+
const { t } = useTranslation();
1316
const { activeIndex = 0, thumbWidth = 50, thumbHeight = undefined, thumbSpace = 10 } = props;
1417
const [listMarginLeft, setListMarginLeft] = React.useState<number>(0);
1518

1619
React.useEffect(() => {
17-
if(props.images && props.images[activeIndex]) {
18-
let margin = ((activeIndex + 1) * (thumbWidth + thumbSpace)) - (thumbWidth/2);
20+
if (props.images && props.images[activeIndex]) {
21+
let margin = (activeIndex + 1) * (thumbWidth + thumbSpace) - thumbWidth / 2;
1922
setListMarginLeft(margin);
2023
}
2124
}, [props.images]);
@@ -30,22 +33,31 @@ export default function ImageViewerNavigation(props: ImageViewerNavProps) {
3033
const imagesList = () => {
3134
let elements: ReactElement[] = [];
3235
let styles = {};
33-
if(thumbHeight)
34-
styles = { ...styles, height: `${thumbHeight}px` };
36+
if (thumbHeight) styles = { ...styles, height: `${thumbHeight}px` };
3537
styles = { ...styles, width: `${thumbWidth}px` };
3638
for (let i = 0; i < props.imagesTotal; i++) {
3739
let liElement = (
3840
<li
3941
key={i}
40-
className={(i === activeIndex) ? 'active' : ''}
41-
style={{marginLeft: `${thumbSpace}px`}}
42+
className={i === activeIndex ? 'active' : ''}
43+
style={{ marginLeft: `${thumbSpace}px` }}
4244
onClick={e => {
4345
handleChangeImg(i);
4446
}}>
4547
{props.images && props.images[i] ? (
46-
<img style={styles} src={props.images[i].src} alt={props.images[i].alt} />
48+
<img
49+
style={styles}
50+
src={props.images[i].src}
51+
alt={props.images[i].alt}
52+
data-tooltip-id='navbar-tooltip'
53+
data-tooltip-content={props.images[i].alt}
54+
/>
4755
) : (
48-
<div className="placeholder" style={styles} >
56+
<div
57+
className='placeholder'
58+
style={styles}
59+
data-tooltip-id='navbar-tooltip'
60+
data-tooltip-content={t('navbarClickToDownload')}>
4961
<TbPhotoDown />
5062
</div>
5163
)}
@@ -58,10 +70,41 @@ export default function ImageViewerNavigation(props: ImageViewerNavProps) {
5870
};
5971

6072
return (
61-
<div className='navbar'>
62-
<ul className='list list-transition' style={{ marginLeft: `calc(50% - ${listMarginLeft}px)` }}>
63-
{imagesList()}
64-
</ul>
65-
</div>
73+
<>
74+
<div className='navbar'>
75+
<button
76+
type='button'
77+
className='navbar-goto-first'
78+
onClick={e => {
79+
handleChangeImg(0);
80+
}}
81+
disabled={activeIndex === 0}
82+
data-tooltip-id='navbar-tooltip'
83+
data-tooltip-content={t('gotoFirst')}
84+
data-tooltip-place='right'
85+
data-tooltip-hidden={activeIndex === 0}>
86+
<TbChevronLeftPipe />
87+
</button>
88+
<div className='wrapper'>
89+
<ul className='list list-transition' style={{ marginLeft: `calc(50% - ${listMarginLeft}px)` }}>
90+
{imagesList()}
91+
</ul>
92+
</div>
93+
<button
94+
type='button'
95+
className='navbar-goto-last'
96+
onClick={e => {
97+
handleChangeImg(props.imagesTotal - 1);
98+
}}
99+
disabled={activeIndex === props.imagesTotal - 1}
100+
data-tooltip-id='navbar-tooltip'
101+
data-tooltip-content={t('gotoLast')}
102+
data-tooltip-place='left'
103+
data-tooltip-hidden={activeIndex === props.imagesTotal - 1}>
104+
<TbChevronRightPipe />
105+
</button>
106+
</div>
107+
<Tooltip id='navbar-tooltip' />
108+
</>
66109
);
67110
}

src/plugins/img/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export default (props: ViewerPluginProps) => {
201201
const imageUrl = _getBlobUrlFromBuffer(fileBuffer, fileType.extension);
202202
image.src = imageUrl;
203203
}
204+
image.alt = fileName;
204205

205206
function loadImgSuccess(imgWidth: number, imgHeight: number, success: boolean) {
206207
if (currentActiveIndex !== currentIndex.current) {

src/scss/index.scss

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,31 +236,48 @@ div:not(span):where(.doc-viewer),
236236
}
237237

238238
.navbar {
239-
@apply overflow-x-auto w-full flex items-center justify-start scrollbar-none;
239+
@apply w-full scrollbar-none relative;
240240

241-
&.drag {
242-
@apply cursor-grabbing;
241+
.navbar-goto-first {
242+
@apply left-4;
243+
}
244+
245+
.navbar-goto-last {
246+
@apply right-4;
247+
}
248+
249+
.navbar-goto-first,
250+
.navbar-goto-last {
251+
@apply absolute top-[calc(50%-theme('height.4'))] h-8 w-8 rounded-full border-2 border-solid cursor-pointer flex items-center justify-center
252+
[&:not(:disabled)]:active:scale-95 disabled:cursor-not-allowed z-10;
253+
@extend .border-primary;
254+
@extend .text-primary;
255+
@extend .button-toolbar;
243256
}
244257

258+
.wrapper {
259+
@apply w-full h-full overflow-x-scroll flex items-center justify-start ;
260+
245261
.list {
246262
@apply inline-flex;
247263

248264
li {
249-
@apply flex cursor-pointer box-border select-none border border-solid;
250-
@extend .border-primary;
265+
@apply flex cursor-pointer select-none;
251266

252267
img {
253-
@apply opacity-50 select-none;
268+
@apply opacity-50 select-none box-border border border-solid;
269+
@extend .border-primary;
254270
}
255271

256272
&.active > img {
257273
opacity: 1;
258274
}
259275

260276
.placeholder {
261-
@apply flex items-center justify-center min-w-10;
277+
@apply flex items-center justify-center min-w-10 box-border border border-solid;
262278
@extend .toolbar-background;
263279
@extend .text-primary;
280+
@extend .border-primary;
264281
}
265282
}
266283

@@ -279,6 +296,7 @@ div:not(span):where(.doc-viewer),
279296
}
280297
}
281298
}
299+
}
282300
}
283301
}
284302

0 commit comments

Comments
 (0)