forked from misskey-dev/misskey
-
Notifications
You must be signed in to change notification settings - Fork 12
/
url-preview.vue
365 lines (301 loc) · 7.61 KB
/
url-preview.vue
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<template>
<div v-if="playerEnabled" class="player" :style="`padding: ${(player.height || 0) / (player.width || 1) * 100}% 0 0`">
<button class="disablePlayer" @click="playerEnabled = false" :title="$t('disable-player')"><fa icon="times"/></button>
<iframe :src="player.url + (player.url.match(/\?/) ? '&autoplay=1&auto_play=1' : '?autoplay=1&auto_play=1')" :width="player.width || '100%'" :heigth="player.height || 250" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen />
</div>
<div v-else-if="tweetId && tweetExpanded" class="twitter" ref="twitter">
<iframe ref="tweet" scrolling="no" frameborder="no" :style="{ 'margin-top': '8px', left: `${tweetLeft}px`, width: `${tweetLeft < 0 ? 'auto' : '100%'}`, height: `${tweetHeight}px` }" :src="`https://platform.twitter.com/embed/index.html?embedId=${embedId}&hideCard=false&hideThread=false&lang=en&theme=${$store.state.device.darkmode ? 'dark' : 'light'}&id=${tweetId}`"></iframe>
</div>
<div v-else class="mk-url-preview">
<a :class="{ mini: narrow, compact }" :href="url" rel="nofollow noopener" target="_blank" :title="url" v-if="!fetching">
<div class="thumbnail" v-if="thumbnail && (!sensitive || $store.state.device.alwaysShowNsfw)" :style="`background-image: url('${thumbnail}')`">
<button v-if="!playerEnabled && player.url" @click.prevent="playerEnabled = true" :title="$t('enable-player')"><fa :icon="['far', 'play-circle']"/></button>
</div>
<article>
<header>
<h1 :title="title">{{ title }}</h1>
</header>
<p v-if="description" :title="description">{{ description.length > 85 ? description.slice(0, 85) + '…' : description }}</p>
<footer>
<img class="icon" v-if="icon" :src="icon"/>
<p :title="sitename">{{ sitename }}</p>
</footer>
</article>
</a>
<div class="expandTweet" v-if="tweetId">
<a @click="tweetExpanded = true">
<fa :icon="faTwitter"/> {{ $t('expandTweet') }}
</a>
</div>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../i18n';
import { url as misskeyUrl, lang } from '../../../config';
import { faTwitter } from '@fortawesome/free-brands-svg-icons';
export default Vue.extend({
i18n: i18n('common/views/components/url-preview.vue'),
props: {
url: {
type: String,
require: true
},
detail: {
type: Boolean,
required: false,
default: false
},
compact: {
type: Boolean,
required: false,
default: false
},
},
inject: {
narrow: {
default: false
}
},
data() {
return {
fetching: true,
title: null,
description: null,
thumbnail: null,
icon: null,
sitename: null,
sensitive: false,
player: {
url: null,
width: null,
height: null
},
tweetId: null,
tweetExpanded: this.detail,
embedId: `embed${Math.random().toString().replace(/\D/,'')}`,
tweetHeight: 150,
tweetLeft: 0,
playerEnabled: false,
misskeyUrl,
faTwitter
};
},
created() {
const requestUrl = new URL(this.url);
if (this.isBlokedUrl(requestUrl)) {
return;
}
if (requestUrl.hostname == 'twitter.com') {
const m = requestUrl.pathname.match(/^\/.+\/status(?:es)?\/(\d+)/);
if (m) this.tweetId = m[1];
}
if (requestUrl.hostname === 'music.youtube.com' && requestUrl.pathname.match('^/(?:watch|channel)')) {
requestUrl.hostname = 'www.youtube.com';
}
const requestLang = (lang || 'ja-JP').replace('ja-KS', 'ja-JP');
requestUrl.hash = '';
fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${requestLang}`).then(res => {
res.json().then(info => {
if (info.url == null) return;
this.title = info.title;
this.description = info.description;
this.thumbnail = info.thumbnail;
this.icon = info.icon;
this.sitename = info.sitename;
this.sensitive = !!info.sensitive;
this.fetching = false;
this.player = info.player;
})
});
(window as any).addEventListener('message', this.adjustTweetHeight);
},
mounted() {
// 300pxないと絶対右にはみ出るので左に移動してしまう
const areaWidth = (this.$el as HTMLElement)?.clientWidth;
if (areaWidth && areaWidth < 300) this.tweetLeft = areaWidth - 290;
},
methods: {
isBlokedUrl(url: URL) {
if (url.pathname.match(/\.(?:jpg|gif|png)$/)) return true;
if (url.pathname.match(/^\/media\/(?:[\w-]{19})$/)) return true;
return false;
},
adjustTweetHeight(message: any) {
if (message.origin !== 'https://platform.twitter.com') return;
const embed = message.data?.['twttr.embed'];
if (embed?.method !== 'twttr.private.resize') return;
if (embed?.id !== this.embedId) return;
const height = embed?.params[0]?.height;
if (height) this.tweetHeight = height;
},
},
beforeDestroy() {
(window as any).removeEventListener('message', this.adjustTweetHeight);
},
});
</script>
<style lang="stylus" scoped>
.player
position relative
width 100%
> button
position absolute
top -1.5em
right 0
font-size 1em
width 1.5em
height 1.5em
padding 0
margin 0
color var(--text)
background rgba(128, 128, 128, 0.2)
opacity 0.7
&:hover
opacity 0.9
> iframe
height 100%
left 0
position absolute
top 0
width 100%
.mk-url-preview
> a
display block
font-size 14px
border solid var(--lineWidth) var(--urlPreviewBorder)
border-radius 4px
overflow hidden
&:hover
text-decoration none
border-color var(--urlPreviewBorderHover)
> article > header > h1
text-decoration underline
> .thumbnail
position absolute
width 100px
height 100%
background-position center
background-size cover
display flex
justify-content center
align-items center
> button
font-size 3.5em
opacity: 0.7
&:hover
font-size 4em
opacity 0.9
& + article
left 100px
width calc(100% - 100px)
> article
padding 16px
> header
margin-bottom 8px
> h1
margin 0
font-size 1em
color var(--urlPreviewTitle)
> p
margin 0
color var(--urlPreviewText)
font-size 0.8em
> footer
margin-top 8px
height 16px
> img
display inline-block
width 16px
height 16px
margin-right 4px
vertical-align top
> p
display inline-block
margin 0
color var(--urlPreviewInfo)
font-size 0.8em
line-height 16px
vertical-align top
@media (max-width 700px)
> .thumbnail
position relative
width 100%
height 100px
& + article
left 0
width 100%
@media (max-width 550px)
font-size 12px
> .thumbnail
height 80px
> article
padding 12px
@media (max-width 500px)
font-size 10px
> .thumbnail
height 70px
> article
padding 8px
> header
margin-bottom 4px
> footer
margin-top 4px
> img
width 12px
height 12px
&.compact
> .thumbnail
position: absolute
width 56px
height 100%
> article
left 56px
width calc(100% - 56px)
padding 4px
> header
margin-bottom 2px
> footer
margin-top 2px
&.mini
font-size 10px
> .thumbnail
position relative
width 100%
height 60px
> article
left 0
width 100%
padding 8px
> header
margin-bottom 4px
> footer
margin-top 4px
> img
width 12px
height 12px
&.compact
> .thumbnail
position absolute
width 56px
height 100%
> article
left 56px
width calc(100% - 56px)
padding 4px
> header
margin-bottom 2px
> footer
margin-top 2px
&.compact
> article
> header h1, p, footer
overflow hidden
white-space nowrap
text-overflow ellipsis
> .expandTweet
display flex
> a
font-size small
color var(--text)
</style>