mirror of https://github.com/vector-im/riot-web
Initial code for dynamic minZoom
Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>pull/21833/head
parent
c09d4f4a78
commit
dad7a22055
|
@ -31,8 +31,7 @@ limitations under the License.
|
||||||
|
|
||||||
.mx_ImageView_image {
|
.mx_ImageView_image {
|
||||||
pointer-events: all;
|
pointer-events: all;
|
||||||
max-width: 95%;
|
flex-shrink: 0;
|
||||||
max-height: 95%;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.mx_ImageView_panel {
|
.mx_ImageView_panel {
|
||||||
|
|
|
@ -36,13 +36,15 @@ import {normalizeWheelEvent} from "../../../utils/Mouse";
|
||||||
|
|
||||||
const MIN_ZOOM = 100;
|
const MIN_ZOOM = 100;
|
||||||
const MAX_ZOOM = 300;
|
const MAX_ZOOM = 300;
|
||||||
|
// Max scale to keep gaps around the image
|
||||||
|
const MAX_SCALE = 0.95;
|
||||||
// This is used for the buttons
|
// This is used for the buttons
|
||||||
const ZOOM_STEP = 10;
|
const ZOOM_STEP = 10;
|
||||||
// This is used for mouse wheel events
|
// This is used for mouse wheel events
|
||||||
const ZOOM_COEFFICIENT = 0.5;
|
const ZOOM_COEFFICIENT = 0.5;
|
||||||
// If we have moved only this much we can zoom
|
// If we have moved only this much we can zoom
|
||||||
const ZOOM_DISTANCE = 10;
|
const ZOOM_DISTANCE = 10;
|
||||||
|
const IMAGE_WRAPPER_CLASS = "mx_ImageView_image_wrapper";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
src: string, // the source of the image being displayed
|
src: string, // the source of the image being displayed
|
||||||
|
@ -62,8 +64,9 @@ interface IProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IState {
|
interface IState {
|
||||||
rotation: number,
|
|
||||||
zoom: number,
|
zoom: number,
|
||||||
|
minZoom: number,
|
||||||
|
rotation: number,
|
||||||
translationX: number,
|
translationX: number,
|
||||||
translationY: number,
|
translationY: number,
|
||||||
moving: boolean,
|
moving: boolean,
|
||||||
|
@ -75,8 +78,9 @@ export default class ImageView extends React.Component<IProps, IState> {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
this.state = {
|
this.state = {
|
||||||
|
zoom: 0,
|
||||||
|
minZoom: 100,
|
||||||
rotation: 0,
|
rotation: 0,
|
||||||
zoom: MIN_ZOOM,
|
|
||||||
translationX: 0,
|
translationX: 0,
|
||||||
translationY: 0,
|
translationY: 0,
|
||||||
moving: false,
|
moving: false,
|
||||||
|
@ -99,12 +103,29 @@ export default class ImageView extends React.Component<IProps, IState> {
|
||||||
// We have to use addEventListener() because the listener
|
// We have to use addEventListener() because the listener
|
||||||
// needs to be passive in order to work with Chromium
|
// needs to be passive in order to work with Chromium
|
||||||
this.focusLock.current.addEventListener('wheel', this.onWheel, { passive: false });
|
this.focusLock.current.addEventListener('wheel', this.onWheel, { passive: false });
|
||||||
|
window.addEventListener("resize", this.onWindowResize);
|
||||||
|
this.calculateMinZoom();
|
||||||
}
|
}
|
||||||
|
|
||||||
componentWillUnmount() {
|
componentWillUnmount() {
|
||||||
this.focusLock.current.removeEventListener('wheel', this.onWheel);
|
this.focusLock.current.removeEventListener('wheel', this.onWheel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private onWindowResize = (ev) => {
|
||||||
|
this.calculateMinZoom();
|
||||||
|
}
|
||||||
|
|
||||||
|
private calculateMinZoom() {
|
||||||
|
// TODO: What if we don't have width and height props?
|
||||||
|
const imageWrapper = document.getElementsByClassName(IMAGE_WRAPPER_CLASS)[0];
|
||||||
|
const zoomX = (imageWrapper.clientWidth / this.props.width) * 100;
|
||||||
|
const zoomY = (imageWrapper.clientHeight / this.props.height) * 100;
|
||||||
|
const zoom = Math.min(zoomX, zoomY) * MAX_SCALE;
|
||||||
|
|
||||||
|
if (this.state.zoom <= this.state.minZoom) this.setState({zoom: zoom});
|
||||||
|
this.setState({minZoom: zoom});
|
||||||
|
}
|
||||||
|
|
||||||
private onKeyDown = (ev: KeyboardEvent) => {
|
private onKeyDown = (ev: KeyboardEvent) => {
|
||||||
if (ev.key === Key.ESCAPE) {
|
if (ev.key === Key.ESCAPE) {
|
||||||
ev.stopPropagation();
|
ev.stopPropagation();
|
||||||
|
@ -427,7 +448,7 @@ export default class ImageView extends React.Component<IProps, IState> {
|
||||||
{this.renderContextMenu()}
|
{this.renderContextMenu()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="mx_ImageView_image_wrapper">
|
<div className={IMAGE_WRAPPER_CLASS}>
|
||||||
<img
|
<img
|
||||||
src={this.props.src}
|
src={this.props.src}
|
||||||
title={this.props.name}
|
title={this.props.name}
|
||||||
|
|
Loading…
Reference in New Issue