misskey/packages/frontend/src/components/MkUrlPreview.vue

387 lines
9.0 KiB
Vue
Raw Normal View History

<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
<template>
<template v-if="player.url && playerEnabled">
<div
:class="$style.player"
:style="player.width ? `padding: ${(player.height || 0) / player.width * 100}% 0 0` : `padding: ${(player.height || 0)}px 0 0`"
>
<iframe
v-if="player.url?.startsWith('http://') || player.url?.startsWith('https://')"
sandbox="allow-popups allow-scripts allow-storage-access-by-user-activation allow-same-origin"
scrolling="no"
:allow="player.allow.join(';')"
:class="$style.playerIframe"
:src="player.url + (player.url.match(/\?/) ? '&autoplay=1&auto_play=1' : '?autoplay=1&auto_play=1')"
:style="{ border: 0 }"
></iframe>
<span v-else>invalid url</span>
</div>
<div :class="$style.action">
<MkButton :small="true" inline @click="playerEnabled = false">
<i class="ti ti-x"></i> {{ i18n.ts.disablePlayer }}
</MkButton>
</div>
</template>
<template v-else-if="tweetId && tweetExpanded">
<div ref="twitter">
<iframe
ref="tweet"
allow="fullscreen;web-share"
sandbox="allow-popups allow-scripts allow-same-origin"
scrolling="no"
:style="{ position: 'relative', width: '100%', height: `${tweetHeight}px`, border: 0 }"
:src="`https://platform.twitter.com/embed/index.html?embedId=${embedId}&amp;hideCard=false&amp;hideThread=false&amp;lang=en&amp;theme=${defaultStore.state.darkMode ? 'dark' : 'light'}&amp;id=${tweetId}`"
></iframe>
</div>
<div :class="$style.action">
<MkButton :small="true" inline @click="tweetExpanded = false">
<i class="ti ti-x"></i> {{ i18n.ts.close }}
</MkButton>
</div>
</template>
<div v-else>
<component :is="self ? 'MkA' : 'a'" :class="[$style.link, { [$style.compact]: compact }]" :[attr]="self ? url.substring(local.length) : url" rel="nofollow noopener" :target="target" :title="url">
<div v-if="thumbnail" :class="$style.thumbnail" :style="defaultStore.state.enableDataSaverMode ? '' : `background-image: url('${thumbnail}')`">
2022-12-30 14:24:00 +09:00
</div>
2023-01-14 11:59:08 +09:00
<article :class="$style.body">
<header :class="$style.header">
<h1 v-if="unknownUrl" :class="$style.title">{{ url }}</h1>
<h1 v-else-if="fetching" :class="$style.title"><MkEllipsis/></h1>
<h1 v-else :class="$style.title" :title="title ?? undefined">{{ title }}</h1>
2022-12-30 14:24:00 +09:00
</header>
<p v-if="unknownUrl" :class="$style.text">{{ i18n.ts.failedToPreviewUrl }}</p>
2023-01-14 11:59:08 +09:00
<p v-else-if="fetching" :class="$style.text"><MkEllipsis/></p>
<p v-else-if="description" :class="$style.text" :title="description">{{ description.length > 85 ? description.slice(0, 85) + '…' : description }}</p>
<footer :class="$style.footer">
<img v-if="icon" :class="$style.siteIcon" :src="icon"/>
<p v-if="unknownUrl" :class="$style.siteName">{{ requestUrl.host }}</p>
2023-01-14 11:59:08 +09:00
<p v-else-if="fetching" :class="$style.siteName"><MkEllipsis/></p>
<p v-else :class="$style.siteName" :title="sitename ?? requestUrl.host">{{ sitename ?? requestUrl.host }}</p>
2022-12-30 14:24:00 +09:00
</footer>
</article>
</component>
<template v-if="showActions">
<div v-if="tweetId" :class="$style.action">
<MkButton :small="true" inline @click="tweetExpanded = true">
<i class="ti ti-brand-x"></i> {{ i18n.ts.expandTweet }}
</MkButton>
</div>
<div v-if="!playerEnabled && player.url" :class="$style.action">
<MkButton :small="true" inline @click="playerEnabled = true">
<i class="ti ti-player-play"></i> {{ i18n.ts.enablePlayer }}
</MkButton>
<MkButton v-if="!isMobile" :small="true" inline @click="openPlayer()">
<i class="ti ti-picture-in-picture"></i> {{ i18n.ts.openInWindow }}
</MkButton>
</div>
</template>
</div>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, onUnmounted } from 'vue';
import type { summaly } from 'summaly';
2023-09-19 16:37:43 +09:00
import { url as local } from '@/config.js';
import { i18n } from '@/i18n.js';
import * as os from '@/os.js';
import { deviceKind } from '@/scripts/device-kind.js';
2022-12-28 18:22:46 +09:00
import MkButton from '@/components/MkButton.vue';
2023-09-19 16:37:43 +09:00
import { versatileLang } from '@/scripts/intl-const.js';
import { defaultStore } from '@/store.js';
type SummalyResult = Awaited<ReturnType<typeof summaly>>;
const props = withDefaults(defineProps<{
url: string;
detail?: boolean;
compact?: boolean;
showActions?: boolean;
}>(), {
detail: false,
compact: false,
showActions: true,
});
const MOBILE_THRESHOLD = 500;
const isMobile = $ref(deviceKind === 'smartphone' || window.innerWidth <= MOBILE_THRESHOLD);
const self = props.url.startsWith(local);
const attr = self ? 'to' : 'href';
const target = self ? null : '_blank';
let fetching = $ref(true);
let title = $ref<string | null>(null);
let description = $ref<string | null>(null);
let thumbnail = $ref<string | null>(null);
let icon = $ref<string | null>(null);
let sitename = $ref<string | null>(null);
let player = $ref<SummalyResult['player']>({
url: null,
width: null,
height: null,
allow: [],
});
let playerEnabled = $ref(false);
let tweetId = $ref<string | null>(null);
let tweetExpanded = $ref(props.detail);
const embedId = `embed${Math.random().toString().replace(/\D/, '')}`;
let tweetHeight = $ref(150);
2022-12-30 14:24:00 +09:00
let unknownUrl = $ref(false);
const requestUrl = new URL(props.url);
if (!['http:', 'https:'].includes(requestUrl.protocol)) throw new Error('invalid url');
if (requestUrl.hostname === 'twitter.com' || requestUrl.hostname === 'mobile.twitter.com' || requestUrl.hostname === 'x.com' || requestUrl.hostname === 'mobile.x.com') {
const m = requestUrl.pathname.match(/^\/.+\/status(?:es)?\/(\d+)/);
if (m) tweetId = m[1];
}
if (requestUrl.hostname === 'music.youtube.com' && requestUrl.pathname.match('^/(?:watch|channel)')) {
requestUrl.hostname = 'www.youtube.com';
}
requestUrl.hash = '';
window.fetch(`/url?url=${encodeURIComponent(requestUrl.href)}&lang=${versatileLang}`)
.then(res => {
if (!res.ok) {
fetching = false;
unknownUrl = true;
return;
}
return res.json();
})
.then((info: SummalyResult) => {
if (info.url == null) {
fetching = false;
unknownUrl = true;
return;
}
fetching = false;
unknownUrl = false;
title = info.title;
description = info.description;
thumbnail = info.thumbnail;
icon = info.icon;
sitename = info.sitename;
player = info.player;
});
function 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 !== embedId) return;
const height = embed?.params[0]?.height;
if (height) tweetHeight = height;
}
const openPlayer = (): void => {
build(#10336): Storybook & Chromatic & msw (#10365) * build(#10336): init * fix(#10336): invalid name conversion * build(#10336): load locales and vite config * refactor(#10336): remove unused imports * build(#10336): separate definitions and generated codes * refactor(#10336): remove hatches * refactor(#10336): module semantics * refactor(#10336): remove unused common preferences * fix: typo * build(#10336): mock assets * build(#10336): impl `SatisfiesExpression` * build(#10336): control themes * refactor(#10336): semantics * build(#10336): make .storybook as an individual TypeScript project * style(#10336): use single quote * build(#10336): avoid intrinsic component names * chore: suppress linter * style: typing * build(#10336): update dependencies * docs: note about Storybook * build(#10336): sync * build(#10336): full reload server on change * chore: use defaultStore instead * build(#10336): show popups on Story * refactor(#10336): remove redundant div * docs: fix * build(#10336): interactions * build(#10336): add an interaction test for `<MkA/>` * build(#10336): bump storybook * docs(#10336): mention to pre-build misskey-js * build(#10336): write stories for `MkAcct` * build(#10336): write stories for `MkAd` * build(#10336): fix missing type definition * build(#10336): use `toHaveTextContent` * build(#10336): write some stories * build(#10336): hide internal args * build(#10336): generate `components/global` stories only * build(#10336): write stories for `MkMisskeyFlavoredMarkdown` * fix: conflict errors * build(#10336): subcomponents on sidebar * refactor: restore `SatisfiesExpression` * docs(#10336): note development status * build(#10336): use chokidar-cli * docs(#10336): note chokidar-cli mode * chore(#10336): untrack generated stories files * fix: pointer handling * build(#10336): finalize * chore: add static option to `MkLoading` * refactor(#10336): bind to local args * fix: missing case * revert: restore `SatisfiesExpression` This reverts commit f246699f38a28befbfccc11e9eade22cbaace4f3. * build(#10336): make storybook buildable * build(#10336): staticify assets * build(#10336): staticified directory structure * build(#10336): normalize path for Windows * ci(#10336): create actions * build(#10336): ignore tsc errors * build(#10336): ignore tsc errors * build(#10336): missing dependencies * build(#10336): missing dependencies * build(#10336): use fast-glob * fix: invalid lockfile * ci(#10336): increase heap size * build(#10336): use unpkg for storybook tabler icons * build(#10336): use unpkg for storybook twemojis * build(#10336): disable `ProfilePageCat` * build(#10336): blur `MkA` before interaction ends * ci(#10336): stabilize * ci(#10336): fetch-depth * build(#10336): isChromatic * ci(#10336): notify on changes * ci(#10336): fix typo * ci(#10336): missing working directory * ci(#10336): skip build * ci(#10336): fix path * build(#10336): fails on Windows * build(#10336): available on Windows * ci(#10336): disable animation on chromatic * ci(#10336): add static option to `PageHeader.tabs` * chore: void * ci(#10336): change parameters * docs(#10336): update CONTRIBUTING * docs(#10336): note about meta overriding and etc. * ci(#10336): use Chromatic for checks * ci(#10336): use `pull_request` instead of `pull_request_target` for now * ci(#10336): use `exitOnceUploaded` * ci(#10336): reuse built storybook * ci(#10336): back to `pull_request_target` * chore: unused dependencies * style(#10336): reduce prettier indents * style: note about `TSSatisfiesExpression`
2023-04-04 09:38:34 +09:00
os.popup(defineAsyncComponent(() => import('@/components/MkYouTubePlayer.vue')), {
url: requestUrl.href,
});
};
(window as any).addEventListener('message', adjustTweetHeight);
onUnmounted(() => {
(window as any).removeEventListener('message', adjustTweetHeight);
});
</script>
2023-01-14 11:59:08 +09:00
<style lang="scss" module>
.player {
position: relative;
width: 100%;
2023-01-14 11:59:08 +09:00
}
2023-01-14 11:59:08 +09:00
.disablePlayer {
position: absolute;
top: -1.5em;
right: 0;
font-size: 1em;
width: 1.5em;
height: 1.5em;
padding: 0;
margin: 0;
color: var(--fg);
background: rgba(128, 128, 128, 0.2);
opacity: 0.7;
&:hover {
opacity: 0.9;
}
2023-01-14 11:59:08 +09:00
}
2023-01-14 11:59:08 +09:00
.playerIframe {
height: 100%;
left: 0;
position: absolute;
top: 0;
width: 100%;
}
2023-01-14 11:59:08 +09:00
.link {
position: relative;
display: block;
font-size: 14px;
box-shadow: 0 0 0 1px var(--divider);
border-radius: 8px;
overflow: clip;
&:hover {
text-decoration: none;
border-color: rgba(0, 0, 0, 0.2);
> .body > .header > .title {
text-decoration: underline;
}
2023-01-14 11:59:08 +09:00
}
2023-01-14 11:59:08 +09:00
&.compact {
> .body {
> .header .title, .text, .footer {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
2023-01-14 11:59:08 +09:00
}
}
2023-01-14 11:59:08 +09:00
.thumbnail {
position: absolute;
width: 100px;
height: 100%;
background-position: center;
background-size: cover;
background-color: var(--bg);
2023-01-14 11:59:08 +09:00
display: flex;
justify-content: center;
align-items: center;
& + .body {
left: 100px;
width: calc(100% - 100px);
}
}
2023-01-14 11:59:08 +09:00
.body {
position: relative;
box-sizing: border-box;
padding: 16px;
}
2023-01-14 11:59:08 +09:00
.header {
margin-bottom: 8px;
}
2023-01-14 11:59:08 +09:00
.title {
margin: 0;
font-size: 1em;
}
2023-01-14 11:59:08 +09:00
.text {
margin: 0;
font-size: 0.8em;
}
2023-01-14 11:59:08 +09:00
.footer {
margin-top: 8px;
height: 16px;
}
2023-01-14 11:59:08 +09:00
.siteIcon {
display: inline-block;
width: 16px;
height: 16px;
margin-right: 4px;
vertical-align: top;
}
2023-01-14 11:59:08 +09:00
.siteName {
display: inline-block;
margin: 0;
color: var(--urlPreviewInfo);
font-size: 0.8em;
line-height: 16px;
vertical-align: top;
}
2022-12-28 18:22:46 +09:00
2023-01-14 11:59:08 +09:00
.action {
display: flex;
gap: 6px;
flex-wrap: wrap;
margin-top: 6px;
}
@container (max-width: 400px) {
2023-01-14 11:59:08 +09:00
.link {
font-size: 12px;
}
2023-01-14 11:59:08 +09:00
.thumbnail {
height: 80px;
}
2023-01-14 11:59:08 +09:00
.body {
padding: 12px;
}
}
@container (max-width: 350px) {
2023-01-14 11:59:08 +09:00
.link {
font-size: 10px;
2023-01-14 11:59:08 +09:00
&.compact {
> .thumbnail {
2023-01-14 11:59:08 +09:00
position: absolute;
width: 56px;
height: 100%;
}
2023-01-14 11:59:08 +09:00
> .body {
left: 56px;
width: calc(100% - 56px);
padding: 4px;
2023-01-14 11:59:08 +09:00
> .header {
margin-bottom: 2px;
}
2023-01-14 11:59:08 +09:00
> .footer {
margin-top: 2px;
}
}
2023-01-14 11:59:08 +09:00
}
}
2023-01-14 11:59:08 +09:00
.thumbnail {
height: 70px;
}
2023-01-14 11:59:08 +09:00
.body {
padding: 8px;
}
2023-01-14 11:59:08 +09:00
.header {
margin-bottom: 4px;
}
2023-01-14 11:59:08 +09:00
.footer {
margin-top: 4px;
}
.siteIcon {
width: 12px;
height: 12px;
}
}
</style>