2018-04-12 01:58:04 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-07-06 12:42:14 +03:00
|
|
|
Copyright 2019 Michael Telatynski <7t3chguy@gmail.com>
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-07-31 14:19:29 +03:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-12-21 00:13:46 +03:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2018-04-13 02:43:44 +03:00
|
|
|
import {formatDate} from '../../../DateUtils';
|
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-21 00:41:07 +03:00
|
|
|
import filesize from "filesize";
|
|
|
|
import AccessibleButton from "./AccessibleButton";
|
|
|
|
import Modal from "../../../Modal";
|
|
|
|
import * as sdk from "../../../index";
|
2019-12-16 20:14:03 +03:00
|
|
|
import {Key} from "../../../Keyboard";
|
2020-05-29 03:44:09 +03:00
|
|
|
import FocusLock from "react-focus-lock";
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-03-30 09:31:15 +03:00
|
|
|
export default class ImageView extends React.Component {
|
|
|
|
static propTypes = {
|
2019-07-31 14:19:29 +03:00
|
|
|
src: PropTypes.string.isRequired, // the source of the image being displayed
|
|
|
|
name: PropTypes.string, // the main title ('name') for the image
|
|
|
|
link: PropTypes.string, // the link (if any) applied to the name of the image
|
|
|
|
width: PropTypes.number, // width of the image src in pixels
|
|
|
|
height: PropTypes.number, // height of the image src in pixels
|
|
|
|
fileSize: PropTypes.number, // size of the image src in bytes
|
|
|
|
onFinished: PropTypes.func.isRequired, // callback when the lightbox is dismissed
|
2018-04-12 01:58:04 +03:00
|
|
|
|
|
|
|
// the event (if any) that the Image is displaying. Used for event-specific stuff like
|
|
|
|
// redactions, senders, timestamps etc. Other descriptors are taken from the explicit
|
|
|
|
// properties above, which let us use lightboxes to display images which aren't associated
|
|
|
|
// with events.
|
2019-07-31 14:19:29 +03:00
|
|
|
mxEvent: PropTypes.object,
|
2019-03-30 09:31:15 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-12-19 12:20:15 +03:00
|
|
|
this.state = {
|
2020-12-20 19:40:16 +03:00
|
|
|
rotation: 0,
|
2020-12-19 12:20:15 +03:00
|
|
|
zoom: 100,
|
2020-12-20 19:40:16 +03:00
|
|
|
translationX: 0,
|
|
|
|
translationY: 0,
|
|
|
|
moving: false,
|
2020-12-19 12:20:15 +03:00
|
|
|
};
|
2019-03-30 09:31:15 +03:00
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
initX = 0;
|
|
|
|
initY = 0;
|
|
|
|
lastX = 0;
|
|
|
|
lastY = 0;
|
|
|
|
|
2020-12-19 15:30:56 +03:00
|
|
|
componentDidMount() {
|
2020-12-20 22:05:27 +03:00
|
|
|
/* We have to use addEventListener() because the listener
|
|
|
|
* needs to be passive in order to work with Chromium */
|
2020-12-19 15:30:56 +03:00
|
|
|
this.focusLock.addEventListener('wheel', this.onWheel, { passive: false });
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.focusLock.removeEventListener('wheel', this.onWheel);
|
|
|
|
}
|
|
|
|
|
2019-03-30 09:31:15 +03:00
|
|
|
onKeyDown = (ev) => {
|
2019-12-16 20:14:03 +03:00
|
|
|
if (ev.key === Key.ESCAPE) {
|
2018-04-12 01:58:04 +03:00
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
this.props.onFinished();
|
|
|
|
}
|
2019-03-30 09:31:15 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-19 12:20:15 +03:00
|
|
|
onWheel = (ev) => {
|
2020-12-20 22:09:01 +03:00
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
|
|
|
const newZoom =this.state.zoom - ev.deltaY;
|
2020-12-20 19:40:16 +03:00
|
|
|
|
2020-12-20 22:09:01 +03:00
|
|
|
if (newZoom <= 100) {
|
2020-12-19 12:20:15 +03:00
|
|
|
this.setState({
|
2020-12-20 22:09:01 +03:00
|
|
|
zoom: 100,
|
|
|
|
translationX: 0,
|
|
|
|
translationY: 0,
|
2020-12-19 12:20:15 +03:00
|
|
|
});
|
2020-12-20 22:09:01 +03:00
|
|
|
return;
|
2020-12-19 12:20:15 +03:00
|
|
|
}
|
2020-12-20 22:09:01 +03:00
|
|
|
this.setState({
|
|
|
|
zoom: newZoom,
|
|
|
|
});
|
2020-12-19 12:20:15 +03:00
|
|
|
}
|
|
|
|
|
2019-03-30 09:31:15 +03:00
|
|
|
onRedactClick = () => {
|
2018-04-12 01:58:04 +03:00
|
|
|
const ConfirmRedactDialog = sdk.getComponent("dialogs.ConfirmRedactDialog");
|
|
|
|
Modal.createTrackedDialog('Confirm Redact Dialog', 'Image View', ConfirmRedactDialog, {
|
|
|
|
onFinished: (proceed) => {
|
|
|
|
if (!proceed) return;
|
2020-05-08 02:15:48 +03:00
|
|
|
this.props.onFinished();
|
2018-04-12 01:58:04 +03:00
|
|
|
MatrixClientPeg.get().redactEvent(
|
2018-10-27 06:50:35 +03:00
|
|
|
this.props.mxEvent.getRoomId(), this.props.mxEvent.getId(),
|
2018-04-12 01:58:04 +03:00
|
|
|
).catch(function(e) {
|
2018-10-27 06:50:35 +03:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
2018-04-12 01:58:04 +03:00
|
|
|
// display error message stating you couldn't delete this.
|
2018-10-27 06:50:35 +03:00
|
|
|
const code = e.errcode || e.statusCode;
|
2018-04-12 01:58:04 +03:00
|
|
|
Modal.createTrackedDialog('You cannot delete this image.', '', ErrorDialog, {
|
|
|
|
title: _t('Error'),
|
2018-10-27 06:50:35 +03:00
|
|
|
description: _t('You cannot delete this image. (%(code)s)', {code: code}),
|
2018-04-12 01:58:04 +03:00
|
|
|
});
|
2019-11-18 13:03:05 +03:00
|
|
|
});
|
2018-10-27 06:50:35 +03:00
|
|
|
},
|
2018-04-12 01:58:04 +03:00
|
|
|
});
|
2019-03-30 09:31:15 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2019-04-04 12:26:58 +03:00
|
|
|
getName() {
|
2018-10-27 06:50:35 +03:00
|
|
|
let name = this.props.name;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (name && this.props.link) {
|
2020-02-24 01:14:29 +03:00
|
|
|
name = <a href={ this.props.link } target="_blank" rel="noreferrer noopener">{ name }</a>;
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
return name;
|
2019-04-04 12:26:58 +03:00
|
|
|
}
|
2019-03-30 09:31:15 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onRotateCounterClockwiseClick = () => {
|
|
|
|
const cur = this.state.rotation;
|
2019-03-30 09:31:15 +03:00
|
|
|
const rotationDegrees = (cur - 90) % 360;
|
2020-12-20 19:40:16 +03:00
|
|
|
this.setState({ rotation: rotationDegrees });
|
2019-03-30 09:31:15 +03:00
|
|
|
};
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onRotateClockwiseClick = () => {
|
|
|
|
const cur = this.state.rotation;
|
2019-03-30 09:31:15 +03:00
|
|
|
const rotationDegrees = (cur + 90) % 360;
|
2020-12-20 19:40:16 +03:00
|
|
|
this.setState({ rotation: rotationDegrees });
|
2019-03-30 09:31:15 +03:00
|
|
|
};
|
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onZoomInClick = () => {
|
2020-12-19 12:20:15 +03:00
|
|
|
this.setState({
|
|
|
|
zoom: this.state.zoom + 10,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onZoomOutClick = () => {
|
|
|
|
if (this.state.zoom <= 100) {
|
|
|
|
this.setState({
|
|
|
|
zoom: 100,
|
|
|
|
translationX: 0,
|
|
|
|
translationY: 0,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-12-19 12:20:15 +03:00
|
|
|
this.setState({
|
|
|
|
zoom: this.state.zoom - 10,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onStartMoving = ev => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
if (this.state.zoom <= 100) return false;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
this.setState({moving: true});
|
|
|
|
this.initX = ev.pageX - this.lastX;
|
|
|
|
this.initY = ev.pageY - this.lastY;
|
|
|
|
}
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
onMoving = ev => {
|
|
|
|
ev.stopPropagation();
|
|
|
|
ev.preventDefault();
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
if (!this.state.moving) return false;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
this.lastX = ev.pageX - this.initX;
|
|
|
|
this.lastY = ev.pageY - this.initY;
|
|
|
|
this.setState({
|
|
|
|
translationX: this.lastX,
|
|
|
|
translationY: this.lastY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onEndMoving = ev => {
|
|
|
|
this.setState({moving: false});
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let mayRedact = false;
|
|
|
|
const showEventMeta = !!this.props.mxEvent;
|
2018-04-12 01:58:04 +03:00
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
let res;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (this.props.width && this.props.height) {
|
2020-12-19 12:20:15 +03:00
|
|
|
res = this.props.width + "x" + this.props.height + "px";
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
|
2018-10-27 06:50:35 +03:00
|
|
|
let size;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (this.props.fileSize) {
|
|
|
|
size = filesize(this.props.fileSize);
|
|
|
|
}
|
|
|
|
|
2019-07-06 12:42:14 +03:00
|
|
|
let sizeRes;
|
2018-04-12 01:58:04 +03:00
|
|
|
if (size && res) {
|
2019-07-06 12:42:14 +03:00
|
|
|
sizeRes = size + ", " + res;
|
2018-10-27 06:50:35 +03:00
|
|
|
} else {
|
2019-07-06 12:42:14 +03:00
|
|
|
sizeRes = size || res;
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
|
2020-12-19 11:26:09 +03:00
|
|
|
let metadata;
|
2018-10-27 06:50:35 +03:00
|
|
|
if (showEventMeta) {
|
2018-04-12 01:58:04 +03:00
|
|
|
// Figure out the sender, defaulting to mxid
|
|
|
|
let sender = this.props.mxEvent.getSender();
|
2019-07-06 12:42:14 +03:00
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
const room = cli.getRoom(this.props.mxEvent.getRoomId());
|
2018-04-12 01:58:04 +03:00
|
|
|
if (room) {
|
2019-07-06 12:42:14 +03:00
|
|
|
mayRedact = room.currentState.maySendRedactionForEvent(this.props.mxEvent, cli.credentials.userId);
|
2018-04-12 01:58:04 +03:00
|
|
|
const member = room.getMember(sender);
|
|
|
|
if (member) sender = member.name;
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:26:09 +03:00
|
|
|
metadata = (<div className="mx_ImageView_metadata">
|
2019-07-06 12:42:14 +03:00
|
|
|
{ _t('Uploaded on %(date)s by %(user)s', {
|
|
|
|
date: formatDate(new Date(this.props.mxEvent.getTs())),
|
|
|
|
user: sender,
|
|
|
|
}) }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>);
|
|
|
|
}
|
|
|
|
|
2020-12-19 11:26:09 +03:00
|
|
|
let redactButton;
|
2019-07-06 12:42:14 +03:00
|
|
|
if (mayRedact) {
|
2020-12-19 11:26:09 +03:00
|
|
|
redactButton = (
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Remove")} onClick={ this.onRedactClick }>
|
2020-12-19 14:54:55 +03:00
|
|
|
<img src={require("../../../../res/img/trash-red.svg")} alt={ _t('Remove') } width="18" height="18" />
|
2020-12-19 11:26:09 +03:00
|
|
|
</AccessibleButton>
|
|
|
|
);
|
2018-04-12 01:58:04 +03:00
|
|
|
}
|
|
|
|
|
2020-12-20 19:40:16 +03:00
|
|
|
const rotationDegrees = this.state.rotation + "deg";
|
|
|
|
const zoomPercentage = this.state.zoom/100;
|
|
|
|
const translatePixelsX = this.state.translationX + "px";
|
|
|
|
const translatePixelsY = this.state.translationY + "px";
|
2020-12-20 22:00:11 +03:00
|
|
|
/* The order of the values is important!
|
|
|
|
* First, we translate and only then we rotate, otherwise
|
|
|
|
* we would apply the translation to an already rotated
|
|
|
|
* image causing it translate in the wrong direction. */
|
2020-12-20 19:40:16 +03:00
|
|
|
const style = {
|
2020-12-20 22:00:11 +03:00
|
|
|
transform: `translateX(${translatePixelsX})
|
|
|
|
translateY(${translatePixelsY})
|
2020-12-20 19:40:16 +03:00
|
|
|
scale(${zoomPercentage})
|
2020-12-20 22:00:11 +03:00
|
|
|
rotate(${rotationDegrees})`,
|
2020-12-20 19:40:16 +03:00
|
|
|
};
|
2019-03-30 09:31:15 +03:00
|
|
|
|
2018-04-12 01:58:04 +03:00
|
|
|
return (
|
2020-05-29 03:44:09 +03:00
|
|
|
<FocusLock
|
|
|
|
returnFocus={true}
|
|
|
|
lockProps={{
|
|
|
|
onKeyDown: this.onKeyDown,
|
|
|
|
role: "dialog",
|
|
|
|
}}
|
|
|
|
className="mx_ImageView"
|
2020-12-19 15:30:56 +03:00
|
|
|
ref={ref => this.focusLock = ref}
|
2020-05-29 03:44:09 +03:00
|
|
|
>
|
2018-04-12 01:58:04 +03:00
|
|
|
<div className="mx_ImageView_content">
|
2020-12-20 21:48:24 +03:00
|
|
|
<div className="mx_ImageView_panel mx_ImageView_label">
|
|
|
|
<div className="mx_ImageView_name">
|
|
|
|
{ this.getName() }
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
2020-12-20 21:48:24 +03:00
|
|
|
{ metadata }
|
|
|
|
<span className="mx_ImageView_size">{ sizeRes }</span>
|
|
|
|
</div>
|
|
|
|
<div className="mx_ImageView_panel mx_ImageView_toolbar">
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Zoom in")} onClick={ this.onZoomInClick }>
|
|
|
|
<img src={require("../../../../res/img/plus-white.svg")} alt={ _t('Zoom in') } width="18" height="18" />
|
|
|
|
</AccessibleButton>
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Zoom out")} onClick={ this.onZoomOutClick }>
|
|
|
|
<img src={require("../../../../res/img/minus-white.svg")} alt={ _t('Zoom out') } width="18" height="18" />
|
|
|
|
</AccessibleButton>
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Rotate Left")} onClick={ this.onRotateCounterClockwiseClick }>
|
|
|
|
<img src={require("../../../../res/img/rotate-ccw.svg")} alt={ _t('Rotate counter-clockwise') } width="18" height="18" />
|
|
|
|
</AccessibleButton>
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Rotate Right")} onClick={ this.onRotateClockwiseClick }>
|
|
|
|
<img src={require("../../../../res/img/rotate-cw.svg")} alt={ _t('Rotate clockwise') } width="18" height="18" />
|
|
|
|
</AccessibleButton>
|
|
|
|
<a className="mx_ImageView_button" href={ this.props.src } download={ this.props.name } title={_t("Download")} target="_blank" rel="noopener">
|
|
|
|
<img src={require("../../../../res/img/download-white.svg")} width="18" height="18" alt={ _t('Download') } />
|
|
|
|
</a>
|
|
|
|
{ redactButton }
|
|
|
|
<AccessibleButton className="mx_ImageView_button" title={_t("Close")} onClick={ this.props.onFinished }>
|
|
|
|
<img src={require("../../../../res/img/cancel-white.svg")} width="18" height="18" alt={ _t('Close') } />
|
|
|
|
</AccessibleButton>
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
2020-12-20 20:19:11 +03:00
|
|
|
<img
|
|
|
|
src={this.props.src}
|
|
|
|
title={this.props.name}
|
|
|
|
style={style}
|
|
|
|
className="mainImage"
|
|
|
|
draggable={true}
|
|
|
|
onMouseDown={this.onStartMoving}
|
|
|
|
onMouseMove={this.onMoving}
|
|
|
|
onMouseUp={this.onEndMoving}
|
|
|
|
onMouseLeave={this.onEndMoving}
|
|
|
|
/>
|
2018-04-12 01:58:04 +03:00
|
|
|
</div>
|
2020-05-29 03:44:09 +03:00
|
|
|
</FocusLock>
|
2018-04-12 01:58:04 +03:00
|
|
|
);
|
2019-03-30 09:31:15 +03:00
|
|
|
}
|
|
|
|
}
|