1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | };
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
16 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
17 | };
|
18 | Object.defineProperty(exports, "__esModule", { value: true });
|
19 | exports.ImagePreview = void 0;
|
20 | var react_1 = __importDefault(require("react"));
|
21 | function tokenToURL(token) {
|
22 | if (token.type !== 'string') {
|
23 | return;
|
24 | }
|
25 | var value = token.string.slice(1).slice(0, -1).trim();
|
26 | try {
|
27 | var location_1 = window.location;
|
28 | return new URL(value, location_1.protocol + '//' + location_1.host);
|
29 | }
|
30 | catch (err) {
|
31 | return;
|
32 | }
|
33 | }
|
34 | function isImageURL(url) {
|
35 | return /(bmp|gif|jpeg|jpg|png|svg)$/.test(url.pathname);
|
36 | }
|
37 | var ImagePreview = (function (_super) {
|
38 | __extends(ImagePreview, _super);
|
39 | function ImagePreview() {
|
40 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
41 | _this._node = null;
|
42 | _this.state = {
|
43 | width: null,
|
44 | height: null,
|
45 | src: null,
|
46 | mime: null,
|
47 | };
|
48 | return _this;
|
49 | }
|
50 | ImagePreview.shouldRender = function (token) {
|
51 | var url = tokenToURL(token);
|
52 | return url ? isImageURL(url) : false;
|
53 | };
|
54 | ImagePreview.prototype.componentDidMount = function () {
|
55 | this._updateMetadata();
|
56 | };
|
57 | ImagePreview.prototype.componentDidUpdate = function () {
|
58 | this._updateMetadata();
|
59 | };
|
60 | ImagePreview.prototype.render = function () {
|
61 | var _this = this;
|
62 | var _a;
|
63 | var dims = null;
|
64 | if (this.state.width !== null && this.state.height !== null) {
|
65 | var dimensions = this.state.width + 'x' + this.state.height;
|
66 | if (this.state.mime !== null) {
|
67 | dimensions += ' ' + this.state.mime;
|
68 | }
|
69 | dims = react_1.default.createElement("div", null, dimensions);
|
70 | }
|
71 | return (react_1.default.createElement("div", null,
|
72 | react_1.default.createElement("img", { onLoad: function () { return _this._updateMetadata(); }, ref: function (node) {
|
73 | _this._node = node;
|
74 | }, src: (_a = tokenToURL(this.props.token)) === null || _a === void 0 ? void 0 : _a.href }),
|
75 | dims));
|
76 | };
|
77 | ImagePreview.prototype._updateMetadata = function () {
|
78 | var _this = this;
|
79 | if (!this._node) {
|
80 | return;
|
81 | }
|
82 | var width = this._node.naturalWidth;
|
83 | var height = this._node.naturalHeight;
|
84 | var src = this._node.src;
|
85 | if (src !== this.state.src) {
|
86 | this.setState({ src: src });
|
87 | fetch(src, { method: 'HEAD' }).then(function (response) {
|
88 | _this.setState({
|
89 | mime: response.headers.get('Content-Type'),
|
90 | });
|
91 | });
|
92 | }
|
93 | if (width !== this.state.width || height !== this.state.height) {
|
94 | this.setState({ height: height, width: width });
|
95 | }
|
96 | };
|
97 | return ImagePreview;
|
98 | }(react_1.default.Component));
|
99 | exports.ImagePreview = ImagePreview;
|
100 |
|
\ | No newline at end of file |