unis-oa/frontend/src/components/common/LinkText.vue

33 lines
1.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<span data-test="link-text">
<!-- 段与段之间不摆任何空白:值里本来是什么样子,上屏还是什么样子 -->
<template v-for="(seg, index) in segments" :key="index">
<a v-if="seg.href" :href="seg.href" target="_blank" rel="noopener noreferrer" class="break-all text-primary-600 underline decoration-primary-300/70 underline-offset-2 active:opacity-70 dark:text-primary-300" data-test="link-text-href" @click.stop>{{ seg.text }}</a>
<template v-else>{{ seg.text }}</template>
</template>
</span>
</template>
<script setup lang="ts">
/**
* 一串值里认得出的网址就地变成点得开的链接,认不出的照普通文字摆。
*
* 手机上尤其需要这一层:桌面能把光标挪到链接上看真地址、能划词复制,手机上点不到就是点不到。
* 判据不在这里,全在 utils/linkText,这一只组件只负责按段渲染。
*/
import { computed } from 'vue'
import { linkSegments } from '@/utils/linkText'
const props = withDefaults(
defineProps<{
/** 要上屏的那串值 */
text?: string | number | null
/** 这一格本来就是链接格子(`type === 'url'`):整串当一个地址看,裸域名也认 */
wholeUrl?: boolean
}>(),
{ text: '', wholeUrl: false }
)
const segments = computed(() => linkSegments(props.text, { wholeUrl: props.wholeUrl }))
</script>