-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
112 lines (100 loc) · 3.03 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import React from 'react';
import axios from 'axios';
class PluginSettings {
constructor(data) {
/**
* @type {number}
*/
this.width = data.width == null ? 600 : data.width;
/**
* @type {number}
*/
this.height = data.height == null ? 340 : data.height;
}
}
class PostWillRenderEmbed extends React.Component {
static plugin;
/**
* @type {PluginSettings}
*/
static settings;
render() {
let title = '';
let description = '';
let iframeWidth = 600, iframeHeight = 340;
try {
iframeWidth = PostWillRenderEmbed.settings.width;
iframeHeight = PostWillRenderEmbed.settings.height;
} catch {
try {
iframeWidth = PostWillRenderEmbed.plugin.props.iframeWidth;
iframeHeight = PostWillRenderEmbed.plugin.props.iframeHeight;
} catch { }
}
try {
title = this.props.embed.data.title;
description = this.props.embed.data.description;
} catch {
}
let url = '';
if (!this.props.embed.url.includes('player.vimeo.com')) {
url = this.props.embed.url.replace('vimeo.com', 'player.vimeo.com/video');
} else {
url = this.props.embed.url;
}
return (
<div>
<h5>
Vimeo -
<a href={this.props.embed.url} target="_blank">
{title}
</a>
</h5>
<div>
<small>{description}</small>
</div>
<iframe src={url} width={iframeWidth} height={iframeHeight}>
</iframe>
</div>
);
}
}
class VimeoPlugin {
static apiUrl = '/plugins/vimeo';
initialize(registry, store) {
PostWillRenderEmbed.plugin = store.getState().plugins.plugins.vimeo;
axios.get(`${VimeoPlugin.apiUrl}/settings`)
.then(res => {
/**
* @type {PluginSettings}
*/
PostWillRenderEmbed.settings = new PluginSettings(res.data);
registry.registerPostWillRenderEmbedComponent(
this.handleEmbed,
PostWillRenderEmbed,
false,
);
})
.catch(err => {
/**
* @type {PluginSettings}
*/
PostWillRenderEmbed.settings = new PluginSettings();
registry.registerPostWillRenderEmbedComponent(
this.handleEmbed,
PostWillRenderEmbed,
false,
);
});
}
handleEmbed(embed) {
if (embed.type == 'opengraph' && embed.url.includes('vimeo.com')) {
return true;
}
return false;
}
uninitialize() {
// No clean up required.
}
}
window.registerPlugin('vimeo', new VimeoPlugin());