Skip to content

Commit f4aab04

Browse files
committed
add: complete tweet component
1 parent 71f0148 commit f4aab04

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

src/blocks/helpers/tweet.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { reactive, onBeforeMount } from "vue"
2+
3+
const store = reactive({
4+
callbacks: [] as any[],
5+
})
6+
7+
export const useTwttr = (cb: () => any) => {
8+
onBeforeMount(() => {
9+
let twttrScript = document.getElementById("twitter-widgets-js")
10+
if (!twttrScript) {
11+
store.callbacks.push(cb)
12+
var s = document.createElement("script")
13+
s.id = "twitter-widgets-js"
14+
s.src = "https://platform.twitter.com/widgets.js"
15+
s.onload = () => store.callbacks.forEach((cb) => cb())
16+
document.body.appendChild(s)
17+
} else {
18+
store.callbacks.push(cb)
19+
}
20+
})
21+
}

src/blocks/tweet.vue

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
<script setup lang="ts">
22
import { useNotionBlock, defineNotionProps } from "@/lib/blockable"
3-
import { computed, nextTick, onBeforeMount, onMounted, ref, watch } from "vue"
3+
import { computed, onBeforeMount, getCurrentInstance, ref, onMounted } from "vue"
4+
import { useTwttr } from "./helpers/tweet"
45
56
const props = defineProps({ ...defineNotionProps })
67
//@ts-ignore
7-
const { properties, pass } = useNotionBlock(props)
8+
const { properties } = useNotionBlock(props)
9+
810
const tweetId = computed(() => properties.value?.source?.[0]?.[0].split("status/")?.[1])
911
const el = ref<HTMLElement>()
12+
const error = ref()
1013
1114
const renderTweet = () => {
1215
//@ts-ignore
1316
const twttr = window["twttr"]
1417
//@ts-ignore
1518
twttr?.ready().then(({ widgets }) => {
16-
if (el.value) {
17-
el.value.innerHTML = ""
18-
}
19-
widgets.createTweetEmbed(tweetId.value, el.value, {})
19+
widgets
20+
.createTweetEmbed(tweetId.value, el.value, {})
21+
.then((element: any) => {
22+
error.value = element ? undefined : "error"
23+
})
24+
.catch((err: any) => {
25+
error.value = err
26+
})
2027
})
2128
}
22-
23-
onBeforeMount(() => {
24-
let twttrScript = document.getElementById("twitter-widgets-js")
25-
if (!twttrScript) {
26-
var s = document.createElement("script")
27-
s.id = "twitter-widgets-js"
28-
s.src = "https://platform.twitter.com/widgets.js"
29-
document.body.appendChild(s)
30-
}
29+
useTwttr(renderTweet)
30+
onMounted(() => {
31+
renderTweet()
3132
})
3233
</script>
3334

@@ -38,5 +39,6 @@ export default {
3839
</script>
3940

4041
<template>
41-
<div ref="el"></div>
42+
<div v-if="!error" class="notion-tweet" ref="el"></div>
43+
<div v-else class="notion-tweet-error">Error loading Tweet</div>
4244
</template>

src/components/block.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import NotionToggle from "@/blocks/toggle.vue"
1919
import NotionTableOfContents from "@/blocks/table-of-contents.vue"
2020
import NotionSyncBlock from "@/blocks/sync-block.vue"
2121
import NotionSyncPointerBlock from "@/blocks/sync-pointer-block.vue"
22-
import NotionTweet from "@/blocks/tweet.vue"
2322
2423
const NotionCode = defineAsyncComponent(() => import("@/blocks/code.vue"))
24+
const NotionTweet = defineAsyncComponent(() => import("@/blocks/tweet.vue"))
2525
2626
const props = defineProps({ ...defineNotionProps })
2727
//@ts-ignore

src/style.css

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,17 @@ svg.notion-page-icon {
24702470
text-decoration-color: var(--fg-color-1);
24712471
}
24722472

2473+
.notion-tweet {
2474+
display: flex;
2475+
justify-content: center;
2476+
}
2477+
.notion-tweet-error {
2478+
font-size: 0.8rem;
2479+
padding: 0.5rem 1rem;
2480+
border: 1px solid rgb(233, 233, 231);
2481+
border-radius: 3px;
2482+
}
2483+
24732484
.token.operator,
24742485
.token.entity,
24752486
.token.url,

0 commit comments

Comments
 (0)