Skip to content

Commit

Permalink
feat: show tag hover card when hovering cursor on hashtag links (#2621)
Browse files Browse the repository at this point in the history
Co-authored-by: userquin <[email protected]>
  • Loading branch information
shuuji3 and userquin authored Mar 4, 2024
1 parent 0fa87f7 commit e44833b
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 28 deletions.
45 changes: 45 additions & 0 deletions components/account/TagHoverWrapper.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script setup lang="ts">
import type { mastodon } from 'masto'
defineOptions({
inheritAttrs: false,
})
const { tagName, disabled } = defineProps<{
tagName?: string
disabled?: boolean
}>()
const tag = ref<mastodon.v1.Tag>()
const tagHover = ref()
const hovered = useElementHover(tagHover)
watch(hovered, (newHovered) => {
if (newHovered && tagName) {
fetchTag(tagName).then((t) => {
tag.value = t
})
}
})
const userSettings = useUserSettings()
</script>

<template>
<span ref="tagHover">
<VMenu
v-if="!disabled && !getPreferences(userSettings, 'hideTagHoverCard')"
placement="bottom-start"
:delay="{ show: 500, hide: 100 }"
v-bind="$attrs"
:close-on-content-click="false"
>
<slot />
<template #popper>
<TagCardSkeleton v-if="!tag" />
<TagCard v-else :tag="tag" />
</template>
</VMenu>
<slot v-else />
</span>
</template>
28 changes: 14 additions & 14 deletions components/tag/TagCard.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<script lang="ts" setup>
<script setup lang="ts">
import type { mastodon } from 'masto'
const {
tag,
} = defineProps<{
const { tag } = defineProps<{
tag: mastodon.v1.Tag
}>()
Expand Down Expand Up @@ -32,19 +30,21 @@ function go(evt: MouseEvent | KeyboardEvent) {

<template>
<div
block p4 hover:bg-active flex justify-between cursor-pointer
block p4 hover:bg-active flex justify-between cursor-pointer flex-gap-2
@click="onclick"
@keydown.enter="onclick"
>
<div>
<h4 flex items-center text-size-base leading-normal font-medium line-clamp-1 break-all ws-pre-wrap>
<TagActionButton :tag="tag" />
<bdi>
<span>#</span>
<span hover:underline>{{ tag.name }}</span>
</bdi>
</h4>
<CommonTrending v-if="tag.history" :history="tag.history" text-sm text-secondary line-clamp-1 ws-pre-wrap break-all />
<div flex flex-gap-2>
<TagActionButton :tag="tag" />
<div>
<h4 flex items-center text-size-base leading-normal font-medium line-clamp-1 break-all ws-pre-wrap>
<bdi>
<span>#</span>
<span hover:underline>{{ tag.name }}</span>
</bdi>
</h4>
<CommonTrending v-if="tag.history" :history="tag.history" text-sm text-secondary line-clamp-1 ws-pre-wrap break-all />
</div>
</div>
<div v-if="tag.history" flex items-center>
<CommonTrendingCharts :history="tag.history" />
Expand Down
2 changes: 1 addition & 1 deletion components/tag/TagCardSkeleton.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div p4 flex justify-between>
<div p4 flex justify-between gap-4>
<div flex="~ col 1 gap-2">
<div flex class="skeleton-loading-bg" h-5 w-30 rounded />
<div flex class="skeleton-loading-bg" h-4 w-45 rounded />
Expand Down
22 changes: 22 additions & 0 deletions composables/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,23 @@ export async function fetchAccountByHandle(acct: string): Promise<mastodon.v1.Ac
return promise
}

export function fetchTag(tagName: string, force = false): Promise<mastodon.v1.Tag> {
const server = currentServer.value
const userId = currentUser.value?.account.id
const key = `${server}:${userId}:tag:${tagName}`
const cached = cache.get(key)
if (cached && !force)
return Promise.resolve(cached)

const promise = useMastoClient().v1.tags.$select(tagName).fetch()
.then((tag) => {
cacheTag(tag)
return tag
})
cache.set(key, promise)
return promise
}

export function useAccountById(id?: string | null) {
return useAsyncState(() => fetchAccountById(id), null).state
}
Expand All @@ -113,3 +130,8 @@ export function cacheAccount(account: mastodon.v1.Account, server = currentServe
setCached(`${server}:${userId}:account:${account.id}`, account, override)
setCached(`${server}:${userId}:account:${userAcct}`, account, override)
}

export function cacheTag(tag: mastodon.v1.Tag, server = currentServer.value, override?: boolean) {
const userId = currentUser.value?.account.id
setCached(`${server}:${userId}:tag:${tag.name}`, tag, override)
}
7 changes: 5 additions & 2 deletions composables/content-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Emoji from '~/components/emoji/Emoji.vue'
import ContentCode from '~/components/content/ContentCode.vue'
import ContentMentionGroup from '~/components/content/ContentMentionGroup.vue'
import AccountHoverWrapper from '~/components/account/AccountHoverWrapper.vue'
import TagHoverWrapper from '~/components/account/TagHoverWrapper.vue'

function getTextualAstComponents(astChildren: Node[]): string {
return astChildren
Expand Down Expand Up @@ -128,11 +129,13 @@ function handleMention(el: Node) {
addBdiNode(el)
return h(AccountHoverWrapper, { handle, class: 'inline-block' }, () => nodeToVNode(el))
}

const matchTag = href.match(TagLinkRE)
if (matchTag) {
const [, , name] = matchTag
const [, , tagName] = matchTag
addBdiNode(el)
el.attributes.href = `/${currentServer.value}/tags/${name}`
el.attributes.href = `/${currentServer.value}/tags/${tagName}`
return h(TagHoverWrapper, { tagName, class: 'inline-block' }, () => nodeToVNode(el))
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions composables/settings/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface PreferencesSettings {
hideTranslation: boolean
hideUsernameEmojis: boolean
hideAccountHoverCard: boolean
hideTagHoverCard: boolean
hideNews: boolean
grayscaleMode: boolean
enableAutoplay: boolean
Expand Down Expand Up @@ -70,6 +71,7 @@ export const DEFAULT__PREFERENCES_SETTINGS: PreferencesSettings = {
hideTranslation: false,
hideUsernameEmojis: false,
hideAccountHoverCard: false,
hideTagHoverCard: false,
hideNews: false,
grayscaleMode: false,
enableAutoplay: true,
Expand Down
1 change: 1 addition & 0 deletions locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@
"hide_follower_count": "Hide following/follower count",
"hide_news": "Hide news",
"hide_reply_count": "Hide reply count",
"hide_tag_hover_card": "Hide tag hover card",
"hide_translation": "Hide translation",
"hide_username_emojis": "Hide username emojis",
"hide_username_emojis_description": "Hides emojis from usernames in timelines. Emojis will still be visible in their profiles.",
Expand Down
1 change: 1 addition & 0 deletions locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@
"hide_follower_count": "Ocultar número de seguidores",
"hide_news": "Ocultar noticias",
"hide_reply_count": "Ocultar número de respuestas",
"hide_tag_hover_card": "Ocultar tarjeta flotante de etiqueta",
"hide_translation": "Ocultar traducción",
"hide_username_emojis": "Ocultar emojis en el nombre de usuario",
"hide_username_emojis_description": "Oculta los emojis de los nombres de usuarios en la línea de tiempo. Los emojis permanecerán visibles en sus perfiles.",
Expand Down
6 changes: 6 additions & 0 deletions pages/settings/preferences/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ const userSettings = useUserSettings()
>
{{ $t('settings.preferences.hide_account_hover_card') }}
</SettingsToggleItem>
<SettingsToggleItem
:checked="getPreferences(userSettings, 'hideTagHoverCard')"
@click="togglePreferences('hideTagHoverCard')"
>
{{ $t('settings.preferences.hide_tag_hover_card') }}
</SettingsToggleItem>
<SettingsToggleItem
:checked="getPreferences(userSettings, 'enableAutoplay')"
:disabled="getPreferences(userSettings, 'enableDataSaving')"
Expand Down
34 changes: 23 additions & 11 deletions tests/nuxt/__snapshots__/content-rich.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,18 @@ exports[`content-rich > handles html within code blocks 1`] = `
exports[`content-rich > hashtag adds bdi 1`] = `
"<p>
Testing bdi is added
<a
class="mention hashtag"
rel="nofollow noopener noreferrer"
to="/m.webtoo.ls/tags/turkey"
><bdi>#<span>turkey</span></bdi></a
<span
><VMenu
placement="bottom-start"
class="inline-block"
close-on-content-click="false"
><a
class="mention hashtag"
rel="nofollow noopener noreferrer"
to="/m.webtoo.ls/tags/turkey"
><bdi>#<span>turkey</span></bdi></a
></VMenu
></span
>
</p>
<p></p>
Expand All @@ -194,12 +201,17 @@ exports[`content-rich > hashtag adds bdi 1`] = `
exports[`content-rich > hashtag doesn't add 2 bdi 1`] = `
"<p>
Testing bdi not added
<a
class="mention hashtag"
rel="nofollow noopener noreferrer"
to="/m.webtoo.ls/tags/turkey"
><bdi></bdi
></a>
<span
><VMenu
placement="bottom-start"
class="inline-block"
close-on-content-click="false"
><a
class="mention hashtag"
rel="nofollow noopener noreferrer"
to="/m.webtoo.ls/tags/turkey"
><bdi></bdi></a></VMenu
></span>
</p>
<p></p>
"
Expand Down

0 comments on commit e44833b

Please sign in to comment.