Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
manav-gopal committed Jun 23, 2024
2 parents 791c6cd + 0df0723 commit cb18dd0
Show file tree
Hide file tree
Showing 29 changed files with 3,368 additions and 3,146 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
node_modules
temp
.idea
*.tgz
.parcel-cache
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"build-core": "yarn workspace iv-viewer bundle",
"start-core": "yarn workspace iv-viewer start",
"build": "yarn build-core && yarn build-react"
},
"devDependencies": {
"all-contributors-cli": "^6.26.1"
}
}
}
4 changes: 2 additions & 2 deletions packages/iv-viewer/.babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"presets": ["@babel/preset-env"],
"plugins": [
"@babel/plugin-proposal-class-properties",
["@babel/plugin-proposal-object-rest-spread", {}, "rest-spread"]
"@babel/plugin-transform-class-properties",
["@babel/plugin-transform-object-rest-spread", {}, "rest-spread"]
]
}
5 changes: 4 additions & 1 deletion packages/iv-viewer/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
*.swp
.DS_Store
.vscode
.idea
*.tgz

#ignore gitignore
.gitignore

#ignore build files
rollup.config.js
rollup.config.mjs
build-css.js
.babelrc
.eslintrc
Expand All @@ -19,3 +21,4 @@ node_modules/
temp/
assets/
example/
.parcel-cache
288 changes: 288 additions & 0 deletions packages/iv-viewer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,288 @@
# iv-viewer
[![All Contributors](https://img.shields.io/badge/all_contributors-4-orange.svg?style=flat-square)](#contributors-)
A zooming and panning plugin inspired by google photos for your web images.

### Features
<ul>
<li>Smooth dragging and panning of images.</li>
<li>Support touch devices.</li>
<li>Double tap to zoom in/zoom out.</li>
<li>Pinch in/out to control zoom.</li>
<li>Snap view for better panning and zooming experience.</li>
<li>Allow quick display of loaded image then loading of high quality image progressively.</li>
<li>Exposed API to control zoom programmatically.</li>
<li>Custom Events to listen for the state changes.</li>
</ul>

![alt tag](assets/imageviewer.jpg)

### Install
Through npm
```
npm install iv-viewer --save
```

Or get compiled development and production version (css and js) from ./dist

### Usage
Import ImageViewer and it's style.
```js
import ImageViewer from 'iv-viewer';

// or
const ImageViewer = require('iv-viewer').default;

// Import css
import 'iv-viewer/dist/iv-viewer.css';
```
You can choose to import css file inside your scss/less files.

You can also use the standalone UMD build by including dist/iv-viewer.js and dist/iv-viewer.css in your page.
```html
<script src="https://unpkg.com/iv-viewer/dist/iv-viewer.js"></script>

<link rel="stylesheet" href="https://unpkg.com/iv-viewer/dist/iv-viewer.css">
```

### Three different modes

#### Full-Screen Mode
If you want to show images in full screen, with predefined styles. You can use FullScreenViewer.
```js
import { FullScreenViewer } from 'iv-viewer';

const viewer = new FullScreenViewer(options); // check options section for supported options

viewer.show('images/low-res-img', 'images/hi-res-img'); //second option is optional. Check better image loading section
```

#### Container Mode
If you have your own container to show images (you might have custom layout or gallery), you can use image-viewer in a container mode.

```html
<div id="image-container"></div>
```

```js
import ImageViewer from 'iv-viewer';

const container = document.querySelector('#image-container');
const viewer = new ImageViewer(container, options); //check options section for supported options

viewer.load('images/low-res-img', 'images/hi-res-img'); //second option is optional. Check better image loading section
```

#### Image Mode
If you just want to add zoom and pan gesture to your images in a image-viewer style, you can use image-viewer in image mode.
```html
<img id="image" src="image.png" data-high-res-src="hi-res-image.png" />
```

```js
import ImageViewer from 'iv-viewer';

const image = document.querySelector('#image');
const viewer = new ImageViewer(image, options); // check options section for supported options
```

### Options
| Option | Allowed Value | Default | Description |
| ------------- |-------------| -----| -------- |
| zoomValue | number in percentage | 100 | It defines the initial zoom value of the image. |
| maxZoom | number in percentage | 500 | It defines the max limit for the zoom value of the image. |
| snapView | boolean | true | If true will show snap view. |
| refreshOnResize | boolean | true | Defines whether to refresh the viewer on resize of window. This is available only for Container and Image mode. On FullScreen mode it will refresh on window resize by default.|
| zoomOnMouseWheel | boolean | true | Defines weather to allow zoom with mouse scroll or not. |
| hasZoomButtons | boolean | true | Defines weather to add zoom buttons or not |
| zoomStep | number | 50 | The number of which the zoom should increase/decrease when the buttons are clicked |
| listeners | object | null | multiple useful callbacks that could use in-order to get the current state of the viewer|

### The Listeners
There are multiple listeners you can register with each viewer instance
```js
import ImageViewer from 'iv-viewer';

const viewer = new ImageViewer(element, {
listeners: {
onInit: callback(data), // called when the instance is initiated
onDestroy: callback(), // called when the instance is destroyed
onImageLoaded: callback(data), // called on image load
onZoomChange: callback(data), // called on zoom in/out
}
});
```
### Callback Data
The data passed to each callback is very useful, it contains the current instance with more info that you can use to react to the instance state

| Option | dataType | Description |
| ------------- |-------------| -------- |
| container | HTMLElement | The current container of the viewer |
| snapView | HTMLElement | The snap view element in the viewer |
| zoomValue | Number | The current zoom value |
| reachedMin | boolean | A boolean value that determine if the zoom value reached the initial zoom.|
| reachedMax | boolean | A boolean value that determine if the zoom value reached the maximum zoom. |
| instance | ImageViewer | The current instance which contains all other info if needed |

### API (ImageViewer)

Creating an instance

```js
import ImageViewer from 'iv-viewer';

const viewer = new ImageViewer(element, options);
```
Here the first argument is the element, which can be container where viewer will be loaded, or it can be a image in which case viewer will be initialized in a image mode.

You can also pass a selector directly instead of a DOM element.
```js
const viewer = new ImageViewer('#element', options);
```

Second argument is to provide configuration options for the ImageViewer. This argument is optional.

#### Instance methods
**load(imgSrc, highResImg)**

Load an image to the viewer. You can pass two different resolution of the image as first and second argument (optional). See why do you need it at [better image loading](#better-image-loading) section.

```js
viewer.load('image.png', 'hi-res-image.png');
```
Note that this is just required for the container mode, as in image mode you can just use `src` and `data-high-res-src` attribute and the image will load by itself. See [image mode](#image-mode) example

**zoom(zoomValue, point)**

zoomValue: A percentage value to which you want to zoom the image.

point(optional): Point {x, y} is the coordinate of the container which would act as the center for the zoom. If not defined, it will take the center of the container as the zooming point.

```js
viewer.zoom(300, { x: 500, y: 500 });
```

**resetZoom()**

Reset zoom to the default or provided initial zoomValue.

```js
viewer.resetZoom();
```

**refresh()**

Method to manually refresh the viewer. It will reset the zoom and pan. It will also recalculate the dimension of container, window and image in case if that is changed.
```js
viewer.refresh();
```

**destroy()**

Destroys the plugin instance and unbind all events. It will also reset the container or the image(if ImageViewer is used in image mode). It returns null which you should assign to viewer variable to completely free up memory.
```js
viewer = viewer.destroy();
```

### API (FullScreenViewer)
FullScreenViewer is extended from ImageViewer. So it shares the same ImageViewer api along with some FullScreenViewer API.

Creating an instance

```js
import { FullScreenViewer } from 'iv-viewer';

const viewer = new FullScreenViewer(options);
```
Unlike ImageViewer you don't have to pass container for the viewer as it will be initialized in pre-defined full screen container.

First argument is to provide configuration options for the FullScreenViewer. This argument is optional.

#### Instance methods (FullScreenViewer)
FullScreenViewer inherits all the instance method of ImageViewer. In additional to that it has following methods.

**show(imgSrc, highResImg)**

Show the full screen viewer with passed image on the show method. You can pass two different resolution of the image as first and second argument (optional). See why do you need it at [better image loading](#better-image-loading) section.

```js
viewer.show('image.png', 'hi-res-image.png');
```

**hide()**

Hide/Close the fullScreen viewer.
```js
viewer.hide();
```

### Better image loading
To improve the perceived experience, it is always recommended to show the already loaded image or the low quality image on the viewer and let the ImageViewer load high quality image in parallel.

It will also try to preview the high quality image while it's loading so it will give a progressive loading effect.

ImageViewer provides api to do this.
Container mode
```js
viewer.load('image.png', 'hi-res-image.png');
```

FullScreen mode
```js
viewer.show('image.png', 'hi-res-image.png');
```

Image Mode
```html
<img id="image" src="image.png" data-high-res-src="hi-res-image.png" />
```

In all of the above example it will first try to display the first image and then load the second image (if passed) in parallel.

The second image is optional, which you should pass when you feel you can improve the image loading experience by first showing low quality image and then progressively update it with high quality image.

### Demo
codesandbox preview link: [https://8ypwzryom0.codesandbox.io/](https://8ypwzryom0.codesandbox.io/)

codesandbox link: [https://codesandbox.io/s/8ypwzryom0](https://codesandbox.io/s/8ypwzryom0)

### Like this
[:star: this repo](https://github.com/s-yadav/iv-viewer)

### Major updates

#### v2.2.0

- Includes TypeScript type declarations
- Bugfixing

#### v2.1.0

- It's a complete rewrite of ImageViewer with no jQuery requirement in ES6.
- While the options and instance method are same the way you use a ImageViewer and FullScreenView is changed. The v1 API will no longer be supported.
- Published on the npm. V1 was not available on npm.
- Changed the package name to iv-viewer. image-viewer name was not available on npm.
- Added some listeners
- Added zoom in/out buttons
- Some SCSS variable for easier overrides.

## Contributors ✨

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="http://mozillians.org/en-US/u/ruchikabgosain/"><img src="https://avatars2.githubusercontent.com/u/30324532?v=4" width="100px;" alt="Ruchika"/><br /><sub><b>Ruchika</b></sub></a><br /><a href="https://github.com/s-yadav/iv-viewer/commits?author=ruchikabgosain" title="Documentation">📖</a></td>
<td align="center"><a href="https://twitter.com/_syadav"><img src="https://avatars1.githubusercontent.com/u/3096766?v=4" width="100px;" alt="Sudhanshu Yadav"/><br /><sub><b>Sudhanshu Yadav</b></sub></a><br /><a href="https://github.com/s-yadav/iv-viewer/commits?author=s-yadav" title="Documentation">📖</a></td>
<td align="center"><a href="https://hockeycommunity.com"><img src="https://avatars0.githubusercontent.com/u/2039539?v=4" width="100px;" alt="Amrit Kahlon"/><br /><sub><b>Amrit Kahlon</b></sub></a><br /><a href="https://github.com/s-yadav/iv-viewer/commits?author=amritk" title="Documentation">📖</a></td>
<td align="center"><a href="https://github.com/10000"><img src="https://avatars2.githubusercontent.com/u/3347256?v=4" width="100px;" alt="10000"/><br /><sub><b>10000</b></sub></a><br /><a href="https://github.com/s-yadav/iv-viewer/commits?author=10000" title="Documentation">📖</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->
<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
26 changes: 26 additions & 0 deletions packages/iv-viewer/build-css.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');
const util = require('util');
const sass = require('sass');
const autoprefixer = require('autoprefixer');
const cssnano = require('cssnano');
const postcss = require('postcss');

const writeFile = util.promisify(fs.writeFile);

// build css
const filePath = './src/scss/build.scss';
const css = sass.compile(filePath).css;

async function compileCss () {
const unminifiedCssPromise = postcss([autoprefixer]).process(css, { from: undefined });
const minifiedCssPromise = postcss([cssnano, autoprefixer]).process(css, { from: undefined });
const [unminifiedCss, minifiedCss] = await Promise.all([unminifiedCssPromise, minifiedCssPromise]);

const distUnminified = writeFile('./dist/iv-viewer.css', unminifiedCss.css);
const distMinified = writeFile('./dist/iv-viewer.min.css', minifiedCss.css);
const libUnminified = writeFile('./lib/image-viewer.css', unminifiedCss.css);

return Promise.all([distUnminified, distMinified, libUnminified]);
}

compileCss();
8 changes: 7 additions & 1 deletion packages/iv-viewer/build-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ async function compileCss () {
const unminifiedCssPromise = postcss([autoprefixer]).process(css, { from: undefined });
const minifiedCssPromise = postcss([cssnano, autoprefixer]).process(css, { from: undefined });
const [unminifiedCss, minifiedCss] = await Promise.all([unminifiedCssPromise, minifiedCssPromise]);
<<<<<<<< HEAD:packages/iv-viewer/build-css.js
const distUnminified = writeFile('./dist/iv-viewer.css', unminifiedCss.css);
const distMinified = writeFile('./dist/iv-viewer.min.css', minifiedCss.css);
========
const distUnminified = writeFile('./dist/iv-viewer.css', unminifiedCss.toString());
const distMinified = writeFile('./dist/iv-viewer.min.css', minifiedCss.toString());
const libUnminified = writeFile('./lib/image-viewer.css', unminifiedCss.toString());
>>>>>>>> upstream/master:build-css.cjs

return Promise.all([distUnminified, distMinified]);
return Promise.all([distUnminified, distMinified, libUnminified]);
}

compileCss();
3 changes: 3 additions & 0 deletions packages/iv-viewer/dist/iv-viewer.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
.iv-container {
overflow: hidden;
position: relative;
-webkit-user-select: none;
-moz-user-select: none;
user-select: none;
}
.iv-fullscreen {
position: fixed;
Expand Down
Loading

0 comments on commit cb18dd0

Please sign in to comment.