/
AppTwitterQuote.ts
98 lines (92 loc) · 2.57 KB
/
AppTwitterQuote.ts
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
import {
CreateElement,
VNode,
RenderContext,
FunctionalComponentOptions,
} from 'vue'
export interface Props {
tweet: string
author: string
}
const twitterDependencySrc = 'https://platform.twitter.com/widgets.js'
const addTwitterDependency = (evt: HTMLElementEventMap['mouseenter']) => {
let maybeScriptDep = null
try {
if (evt && evt.view && evt.view && evt.view.document) {
const d = evt.view.document
maybeScriptDep = [
...d.querySelectorAll(`[src="${twitterDependencySrc}"]`),
]
if (maybeScriptDep && maybeScriptDep.length < 1) {
const scriptElement = d.createElement('script')
scriptElement.async = true
scriptElement.src = twitterDependencySrc
d.body.appendChild(scriptElement)
}
}
} catch (_) {
// Failing silently
}
}
/**
* Quote a Twitter tweet.
*
* ```html
* <blockquote class="twitter-tweet">
* <p lang="en" dir="ltr">
* I just came up with my shortest summary for leveraging Vue,React,Svelte; "X as a UI Runtime"<br/><br/>UI generated by code that's reading data, that's describing views embedded with metadata about what and how to get content for itself. <a href="https://overreacted.io/react-as-a-ui-runtime/">overreacted.io/react-as-a-ui-runtime</a> <br><br>Thanks <a href="https://twitter.com/dan_abramov?ref_src=twsrc%5Etfw">@dan_abramov</a>
* </p>
* — Renoir Boulanger (@renoirb)
* <a href="https://twitter.com/renoirb/status/1134646209392648193?ref_src=twsrc%5Etfw">
* June 1, 2019
* </a>
* </blockquote>
* ```
*/
const AppTwitterQuote: FunctionalComponentOptions<Props> = {
name: 'AppTwitterQuote' /* app-twitter-quote */,
functional: true,
props: {
tweet: {
type: String,
required: true,
},
author: {
type: String,
required: false,
default: 'renoirb',
},
},
render(h: CreateElement, ctx: RenderContext<Props>): VNode {
const tweet = ctx.props.tweet
const author = ctx.props.author
const cite = `https://twitter.com/${author}/status/${tweet}?ref_src=twsrc%5Etfw`
return h(
'blockquote',
{
class: ['twitter-tweet'],
on: {
mouseenter: addTwitterDependency,
},
...ctx.data,
attrs: {
cite,
...(ctx.data.attrs ? ctx.data.attrs : {}),
},
},
[
h('p', {}, [...ctx.children]),
h(
'a',
{
attrs: {
href: cite,
},
},
['Source'],
),
],
)
},
}
export default AppTwitterQuote