feat(contextualGuides): add multiple contextual guide components for agent and knowledge base creation

- Introduced new components: AgentCreateContextualGuide, KbCreateContextualGuide, TenantModelsGuide, and SpotlightGuide to enhance user onboarding.
- Implemented dynamic step configurations for each guide, allowing tailored user experiences based on context.
- Enhanced the existing NewUserGuide component to utilize the new SpotlightGuide for improved visual guidance.
- Updated localization files to include new strings for contextual guides, ensuring a comprehensive user experience across languages.
- Refactored existing components to integrate with the new guide system, improving maintainability and user interaction.
This commit is contained in:
wizardchen
2026-06-03 14:41:12 +08:00
committed by lyingbug
parent 291d72565f
commit f4af9cca97
22 changed files with 2483 additions and 579 deletions
@@ -0,0 +1,174 @@
<template>
<SpotlightGuide v-model:active="active" :steps="guideSteps" step-i18n-prefix="contextualGuide.agentCreate.steps"
labels-prefix="contextualGuide" @finish="onFinish" @dismiss="onFinish" />
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import SpotlightGuide from '@/components/SpotlightGuide.vue'
import {
AGENT_EDITOR_FOCUS_SECTION_EVENT,
markContextualGuideDone,
isContextualGuideDone,
isGlobalUserGuideDone,
} from '@/config/contextualGuides'
import type { SpotlightGuideStep } from '@/types/spotlightGuide'
const props = defineProps<{
when: boolean
isAgentMode: boolean
}>()
const active = ref(false)
const focusSection = (section: string) => {
window.dispatchEvent(
new CustomEvent(AGENT_EDITOR_FOCUS_SECTION_EVENT, { detail: { section } }),
)
}
const guideSteps = computed<SpotlightGuideStep[]>(() => {
const steps: SpotlightGuideStep[] = [
{
key: 'mode',
target: '[data-guide="agent-create-mode"]',
placement: 'right',
before: () => focusSection('basic'),
},
{
key: 'agentType',
target: '[data-guide="agent-create-agent-type"]',
placement: 'right',
before: () => focusSection('basic'),
optional: true,
},
{
key: 'name',
target: '[data-guide="agent-create-name"]',
placement: 'right',
before: () => focusSection('basic'),
},
{
key: 'navModel',
target: '[data-guide="agent-editor-nav-model"]',
placement: 'right',
before: () => focusSection('model'),
},
{
key: 'model',
target: '[data-guide="agent-create-model"]',
placement: 'right',
before: () => focusSection('model'),
},
{
key: 'navKnowledge',
target: '[data-guide="agent-editor-nav-knowledge"]',
placement: 'right',
before: () => focusSection('knowledge'),
},
{
key: 'knowledge',
target: '[data-guide="agent-create-knowledge"]',
placement: 'right',
before: () => focusSection('knowledge'),
},
{
key: 'navWebsearch',
target: '[data-guide="agent-editor-nav-websearch"]',
placement: 'right',
before: () => focusSection('websearch'),
optional: true,
},
{
key: 'navMultimodal',
target: '[data-guide="agent-editor-nav-multimodal"]',
placement: 'right',
before: () => focusSection('multimodal'),
optional: true,
},
{
key: 'multimodal',
target: '[data-guide="agent-create-multimodal"]',
placement: 'right',
before: () => focusSection('multimodal'),
optional: true,
},
]
if (props.isAgentMode) {
steps.push({
key: 'navTools',
target: '[data-guide="agent-editor-nav-tools"]',
placement: 'right',
before: () => focusSection('tools'),
optional: true,
})
}
steps.push({
key: 'submit',
target: '[data-guide="agent-create-submit"]',
placement: 'top',
before: () => focusSection('basic'),
interact: true,
})
return steps
})
let openTimer: ReturnType<typeof setTimeout> | null = null
let waitGlobalTimer: ReturnType<typeof setTimeout> | null = null
const onFinish = () => {
markContextualGuideDone('agentCreate')
}
const tryOpen = () => {
if (!props.when || isContextualGuideDone('agentCreate') || active.value) return
openTimer = setTimeout(() => {
if (!props.when || isContextualGuideDone('agentCreate')) return
active.value = true
}, 450)
}
const scheduleOpen = () => {
if (openTimer) {
clearTimeout(openTimer)
openTimer = null
}
if (waitGlobalTimer) {
clearTimeout(waitGlobalTimer)
waitGlobalTimer = null
}
if (!props.when || isContextualGuideDone('agentCreate')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
const poll = () => {
if (!props.when || isContextualGuideDone('agentCreate')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
waitGlobalTimer = setTimeout(poll, 400)
}
waitGlobalTimer = setTimeout(poll, 400)
}
watch(
() => props.when,
(val) => {
if (!val) {
if (openTimer) clearTimeout(openTimer)
if (waitGlobalTimer) clearTimeout(waitGlobalTimer)
openTimer = null
waitGlobalTimer = null
active.value = false
return
}
scheduleOpen()
},
{ immediate: true },
)
</script>
+100
View File
@@ -0,0 +1,100 @@
<template>
<SpotlightGuide v-model:active="active" :steps="config.steps" :step-i18n-prefix="config.stepI18nPrefix"
labels-prefix="contextualGuide" @finish="onFinish" @dismiss="onFinish" />
</template>
<script setup lang="ts">
import { onBeforeUnmount, ref, watch } from 'vue'
import SpotlightGuide from '@/components/SpotlightGuide.vue'
import {
CONTEXTUAL_GUIDE_TOURS,
isContextualGuideDone,
isGlobalUserGuideDone,
type ContextualGuideTourConfig,
type ContextualGuideTourId,
} from '@/config/contextualGuides'
const props = defineProps<{
tour: ContextualGuideTourId
/** 为 true 且未完成过该情境引导时,在满足全局引导已结束后自动打开 */
when: boolean
}>()
const config: ContextualGuideTourConfig = CONTEXTUAL_GUIDE_TOURS[props.tour]
const active = ref(false)
let openTimer: ReturnType<typeof setTimeout> | null = null
let waitGlobalTimer: ReturnType<typeof setTimeout> | null = null
const clearTimers = () => {
if (openTimer) {
clearTimeout(openTimer)
openTimer = null
}
if (waitGlobalTimer) {
clearTimeout(waitGlobalTimer)
waitGlobalTimer = null
}
}
const tryOpen = () => {
if (active.value) return
if (!props.when) return
if (isContextualGuideDone(props.tour)) return
if (!isGlobalUserGuideDone()) return
openTimer = setTimeout(() => {
if (!props.when || isContextualGuideDone(props.tour) || active.value) return
active.value = true
}, config.openDelayMs)
}
const scheduleOpen = () => {
clearTimers()
if (!props.when || isContextualGuideDone(props.tour)) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
// 等待全局新手引导结束后再展示情境引导,避免两层遮罩叠加
const poll = () => {
if (!props.when || isContextualGuideDone(props.tour)) {
clearTimers()
return
}
if (isGlobalUserGuideDone()) {
waitGlobalTimer = null
tryOpen()
return
}
waitGlobalTimer = setTimeout(poll, 400)
}
waitGlobalTimer = setTimeout(poll, 400)
}
const onFinish = () => {
localStorage.setItem(config.storageKey, '1')
config.alsoCompleteTours?.forEach((id) => {
localStorage.setItem(CONTEXTUAL_GUIDE_TOURS[id].storageKey, '1')
})
}
watch(
() => props.when,
(val) => {
if (val) {
scheduleOpen()
} else {
clearTimers()
active.value = false
}
},
{ immediate: true },
)
onBeforeUnmount(() => {
clearTimers()
})
</script>
+3 -3
View File
@@ -2099,7 +2099,7 @@ defineExpose({
<input ref="imageInputRef" type="file" accept="image/jpeg,image/png,image/gif,image/webp" multiple
style="display:none" @change="handleImageSelect" />
<!-- 富文本输入框容器 -->
<div class="rich-input-container">
<div class="rich-input-container" data-guide="chat-input">
<!-- 图片预览区域 -->
<div v-if="uploadedImages.length > 0" class="image-preview-bar">
<div v-for="(img, idx) in uploadedImages" :key="idx" class="image-preview-item">
@@ -2255,7 +2255,7 @@ defineExpose({
allSelectedItems.length
}) : $t('input.knowledgeBase') }}</span>
</template>
<div ref="atButtonRef" class="control-btn kb-btn" :class="{
<div ref="atButtonRef" class="control-btn kb-btn" data-guide="chat-kb-mention" :class="{
'active': allSelectedItems.length > 0,
'disabled': isKnowledgeBaseDisabledByAgent
}" @click.stop @mousedown.prevent="triggerMention">
@@ -2332,7 +2332,7 @@ defineExpose({
</t-tooltip>
<!-- 发送按钮 -->
<div v-if="!isReplying" @click="createSession(query)" class="control-btn send-btn"
<div v-if="!isReplying" @click="createSession(query)" class="control-btn send-btn" data-guide="chat-send"
:class="{ 'disabled': !query.length }">
<img src="../assets/img/sending-aircraft.svg" :alt="$t('input.send')" />
</div>
@@ -0,0 +1,202 @@
<template>
<SpotlightGuide v-model:active="active" :steps="guideSteps" step-i18n-prefix="contextualGuide.kbCreate.steps"
labels-prefix="contextualGuide" @finish="onFinish" @dismiss="onFinish" />
</template>
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import SpotlightGuide from '@/components/SpotlightGuide.vue'
import {
KB_EDITOR_FOCUS_SECTION_EVENT,
markContextualGuideDone,
isContextualGuideDone,
isGlobalUserGuideDone,
} from '@/config/contextualGuides'
import type { SpotlightGuideStep } from '@/types/spotlightGuide'
const props = defineProps<{
when: boolean
isFaq: boolean
needsEmbedding: boolean
}>()
const active = ref(false)
const focusSection = (section: string) => {
window.dispatchEvent(
new CustomEvent(KB_EDITOR_FOCUS_SECTION_EVENT, { detail: { section } }),
)
}
const guideSteps = computed<SpotlightGuideStep[]>(() => {
const steps: SpotlightGuideStep[] = [
{
key: 'type',
target: '[data-guide="kb-create-type"]',
placement: 'right',
before: () => focusSection('basic'),
},
{
key: 'name',
target: '[data-guide="kb-create-name"]',
placement: 'right',
before: () => focusSection('basic'),
},
]
if (!props.isFaq) {
steps.push({
key: 'indexing',
target: '[data-guide="kb-create-indexing"]',
placement: 'right',
before: () => focusSection('basic'),
})
}
steps.push(
{
key: 'navModels',
target: '[data-guide="kb-editor-nav-models"]',
placement: 'right',
before: () => focusSection('models'),
},
{
key: 'llm',
target: '[data-guide="kb-create-llm"]',
placement: 'right',
before: () => focusSection('models'),
},
)
if (props.needsEmbedding) {
steps.push({
key: 'embedding',
target: '[data-guide="kb-create-embedding"]',
placement: 'right',
before: () => focusSection('models'),
optional: true,
})
}
if (!props.isFaq) {
steps.push(
{
key: 'parser',
target: '[data-guide="kb-editor-nav-parser"]',
placement: 'right',
before: () => focusSection('parser'),
optional: true,
},
{
key: 'chunking',
target: '[data-guide="kb-editor-nav-chunking"]',
placement: 'right',
before: () => focusSection('chunking'),
optional: true,
},
{
key: 'storage',
target: '[data-guide="kb-editor-nav-storage"]',
placement: 'right',
before: () => focusSection('storage'),
optional: true,
},
{
key: 'navMultimodal',
target: '[data-guide="kb-editor-nav-multimodal"]',
placement: 'right',
before: () => focusSection('multimodal'),
optional: true,
},
{
key: 'multimodalToggle',
target: '[data-guide="kb-create-multimodal-toggle"]',
placement: 'right',
before: () => focusSection('multimodal'),
optional: true,
},
{
key: 'multimodalVllm',
target: '[data-guide="kb-create-multimodal-vllm"]',
placement: 'right',
before: () => focusSection('multimodal'),
optional: true,
},
)
} else {
steps.push({
key: 'faq',
target: '[data-guide="kb-editor-nav-faq"]',
placement: 'right',
before: () => focusSection('faq'),
optional: true,
})
}
steps.push({
key: 'submit',
target: '[data-guide="kb-create-submit"]',
placement: 'top',
before: () => focusSection('basic'),
interact: true,
})
return steps
})
let openTimer: ReturnType<typeof setTimeout> | null = null
let waitGlobalTimer: ReturnType<typeof setTimeout> | null = null
const onFinish = () => {
markContextualGuideDone('kbCreate')
}
const tryOpen = () => {
if (!props.when || isContextualGuideDone('kbCreate') || active.value) return
openTimer = setTimeout(() => {
if (!props.when || isContextualGuideDone('kbCreate')) return
active.value = true
}, 450)
}
const scheduleOpen = () => {
if (openTimer) {
clearTimeout(openTimer)
openTimer = null
}
if (waitGlobalTimer) {
clearTimeout(waitGlobalTimer)
waitGlobalTimer = null
}
if (!props.when || isContextualGuideDone('kbCreate')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
const poll = () => {
if (!props.when || isContextualGuideDone('kbCreate')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
waitGlobalTimer = setTimeout(poll, 400)
}
waitGlobalTimer = setTimeout(poll, 400)
}
watch(
() => props.when,
(val) => {
if (!val) {
if (openTimer) clearTimeout(openTimer)
if (waitGlobalTimer) clearTimeout(waitGlobalTimer)
openTimer = null
waitGlobalTimer = null
active.value = false
return
}
scheduleOpen()
},
{ immediate: true },
)
</script>
+19 -549
View File
@@ -1,94 +1,21 @@
<template>
<Teleport to="body">
<Transition name="guide-fade">
<div v-if="active" class="guide" role="dialog" aria-modal="true"
:aria-label="t('newUserGuide.steps.welcome.title')" @keydown.esc.prevent="dismiss" @keydown.left.prevent="prev"
@keydown.right.prevent="next" tabindex="-1" ref="rootRef">
<!-- 四块暗色挡板围住高亮区:中间留出真实空洞(DOM 上无任何元素),
点击直接穿透到下层控件;四周挡板拦截点击,避免引导期间误触。 -->
<template v-if="hole">
<!-- box-shadow 镂空:内缘与描边同为圆角,避免矩形挖洞 + 圆角描边的直角缺口 -->
<div class="guide__spot" :style="spotStyle" aria-hidden="true" />
<div v-for="(piece, i) in backdropPieces" :key="i" class="guide__backdrop guide__backdrop--hit"
:style="piece" />
</template>
<div v-else class="guide__backdrop guide__backdrop--full" />
<!-- 高亮描边 -->
<div v-if="hole" class="guide__ring" :style="ringStyle" aria-hidden="true" />
<!-- 说明卡片 -->
<div ref="cardRef" class="guide__card" :class="{ 'guide__card--center': !hole }" :style="cardStyle">
<button type="button" class="guide__close" :aria-label="t('newUserGuide.skip')" @click="dismiss">
<t-icon name="close" size="18px" />
</button>
<div class="guide__progress">
<span v-for="(s, i) in steps" :key="s.key" class="guide__dot"
:class="{ 'is-active': i === index, 'is-done': i < index }" />
</div>
<p class="guide__step-label">{{ t('newUserGuide.stepOf', { current: index + 1, total: steps.length }) }}</p>
<h3 class="guide__title">{{ stepTitle }}</h3>
<p class="guide__desc">{{ stepDesc }}</p>
<div class="guide__actions">
<button type="button" class="guide__skip" @click="dismiss">{{ t('newUserGuide.skip') }}</button>
<div class="guide__actions-main">
<t-button v-if="index > 0" size="small" variant="outline" @click="prev">
{{ t('newUserGuide.prev') }}
</t-button>
<t-button v-if="!isLast" size="small" theme="primary" @click="next">
{{ t('newUserGuide.next') }}
</t-button>
<t-button v-else size="small" theme="primary" @click="finish">
{{ t('newUserGuide.done') }}
</t-button>
</div>
</div>
</div>
</div>
</Transition>
</Teleport>
<SpotlightGuide v-model:active="active" :steps="steps" step-i18n-prefix="newUserGuide.steps"
labels-prefix="newUserGuide" @finish="onFinish" @step-change="onStepChange" />
</template>
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import SpotlightGuide from '@/components/SpotlightGuide.vue'
import { GLOBAL_USER_GUIDE_KEY } from '@/config/contextualGuides'
import { useUIStore } from '@/stores/ui'
import type { SpotlightGuideStep } from '@/types/spotlightGuide'
const STORAGE_KEY = 'weknora:new-user-guide-done:v1'
const OPEN_EVENT = 'weknora:open-new-user-guide'
const CARD_WIDTH = 340
const GAP = 16
const EDGE = 16
const PAD = 8
/** 与侧栏 menu_item(4px)+ 内边距协调,略大于目标圆角即可 */
const holeRadius = 8
const BACKDROP_COLOR = 'rgba(15, 18, 22, 0.58)'
type Placement = 'right' | 'left' | 'bottom' | 'top'
interface GuideStep {
key: string
/** 高亮目标的 CSS 选择器;缺省表示居中卡片(欢迎 / 结束) */
target?: string
/** 优先放置方位 */
placement?: Placement
/** 进入该步骤前的准备动作(如展开侧栏、切换路由) */
before?: () => void | Promise<void>
/** 目标不存在时是否跳过该步骤(角色/版本可能隐藏入口) */
optional?: boolean
}
const { t } = useI18n()
const uiStore = useUIStore()
let settingsOpenedByGuide = false
// 顺序按真实使用依赖编排:先配模型(硬前置)→ 建知识库 → 对话 → 智能体
// → 设置入口 → 完成。
const steps = computed<GuideStep[]>(() => [
const steps = computed<SpotlightGuideStep[]>(() => [
{ key: 'welcome' },
{
key: 'knowledge',
@@ -116,7 +43,6 @@ const steps = computed<GuideStep[]>(() => [
before: () => uiStore.expandSidebar(),
},
{
// 模型配置在「设置」弹窗内:引导自动打开弹窗并高亮「添加模型」。
key: 'models',
target: '[data-guide="settings-add-model"], [data-guide="settings-models"]',
placement: 'left',
@@ -129,226 +55,6 @@ const steps = computed<GuideStep[]>(() => [
])
const active = ref(false)
const index = ref(0)
const vw = ref(window.innerWidth)
const vh = ref(window.innerHeight)
const targetRect = ref<DOMRect | null>(null)
const targetEl = ref<HTMLElement | null>(null)
const cardSize = ref({ width: CARD_WIDTH, height: 220 })
type HoleRect = { x: number; y: number; width: number; height: number }
/** 目标与相邻节点之间的可用间距(不含 margin 折叠到 border 外的部分) */
const measureNeighborGap = (el: HTMLElement, r: DOMRect) => {
let above = PAD
const prev = el.previousElementSibling
if (prev) {
above = Math.max(0, r.top - prev.getBoundingClientRect().bottom)
}
let below = PAD
const next = el.nextElementSibling
if (next) {
below = Math.max(0, next.getBoundingClientRect().top - r.bottom)
} else {
const mb = parseFloat(getComputedStyle(el).marginBottom) || 0
below = Math.max(0, PAD - mb)
}
return { above, below }
}
/**
* 高亮框四边留白一致:受相邻项间距限制时同步缩小;
* 贴边 clamp 时同步收窄宽高,避免侧栏靠左时「左贴边、右留白」。
*/
const computeHighlightHole = (el: HTMLElement, r: DOMRect): HoleRect => {
const { above, below } = measureNeighborGap(el, r)
const inset = Math.min(PAD, above, below)
let x = r.left - inset
let y = r.top - inset
let width = r.width + inset * 2
let height = r.height + inset * 2
if (x < 0) {
width += x
x = 0
}
if (y < 0) {
height += y
y = 0
}
const rightOverflow = x + width - vw.value
if (rightOverflow > 0) {
width -= rightOverflow
}
const bottomOverflow = y + height - vh.value
if (bottomOverflow > 0) {
height -= bottomOverflow
}
return { x, y, width, height }
}
const rootRef = ref<HTMLElement | null>(null)
const cardRef = ref<HTMLElement | null>(null)
let retryTimer: ReturnType<typeof setTimeout> | null = null
const step = computed(() => steps.value[index.value] ?? steps.value[0])
const isLast = computed(() => index.value === steps.value.length - 1)
const stepTitle = computed(() => t(`newUserGuide.steps.${step.value.key}.title`))
const stepDesc = computed(() => t(`newUserGuide.steps.${step.value.key}.desc`))
const hole = computed(() => {
const el = targetEl.value
const r = targetRect.value
if (!el || !r) return null
return computeHighlightHole(el, r)
})
const backdropPieces = computed(() => {
const h = hole.value
if (!h) return []
const w = vw.value
const v = vh.value
return [
// 上
{ top: '0px', left: '0px', width: `${w}px`, height: `${h.y}px` },
// 下
{ top: `${h.y + h.height}px`, left: '0px', width: `${w}px`, height: `${Math.max(0, v - h.y - h.height)}px` },
// 左
{ top: `${h.y}px`, left: '0px', width: `${h.x}px`, height: `${h.height}px` },
// 右
{ top: `${h.y}px`, left: `${h.x + h.width}px`, width: `${Math.max(0, w - h.x - h.width)}px`, height: `${h.height}px` },
]
})
const holeFrameStyle = computed(() => {
if (!hole.value) return {}
return {
left: `${hole.value.x}px`,
top: `${hole.value.y}px`,
width: `${hole.value.width}px`,
height: `${hole.value.height}px`,
borderRadius: `${holeRadius}px`,
}
})
const spotStyle = computed(() => ({
...holeFrameStyle.value,
boxShadow: `0 0 0 9999px ${BACKDROP_COLOR}`,
}))
const ringStyle = holeFrameStyle
const overlaps = (
a: { left: number; top: number; right: number; bottom: number },
b: { left: number; top: number; right: number; bottom: number },
) => !(a.right <= b.left || a.left >= b.right || a.bottom <= b.top || a.top >= b.bottom)
const cardStyle = computed(() => {
const w = Math.min(CARD_WIDTH, vw.value - EDGE * 2)
const h = cardSize.value.height
const h0 = hole.value
if (!h0) {
return {
width: `${w}px`,
left: `${(vw.value - w) / 2}px`,
top: `${Math.max(EDGE, vh.value * 0.32 - h / 2)}px`,
}
}
const holeBox = { left: h0.x, top: h0.y, right: h0.x + h0.width, bottom: h0.y + h0.height }
const order: Placement[] = (() => {
const pref = step.value.placement ?? 'right'
const all: Placement[] = ['right', 'left', 'bottom', 'top']
return [pref, ...all.filter((p) => p !== pref)]
})()
const candidates: Record<Placement, { left: number; top: number }> = {
right: { left: holeBox.right + GAP, top: h0.y + h0.height / 2 - h / 2 },
left: { left: holeBox.left - w - GAP, top: h0.y + h0.height / 2 - h / 2 },
bottom: { left: h0.x + h0.width / 2 - w / 2, top: holeBox.bottom + GAP },
top: { left: h0.x + h0.width / 2 - w / 2, top: holeBox.top - h - GAP },
}
for (const place of order) {
const c = candidates[place]
const left = Math.min(Math.max(EDGE, c.left), vw.value - w - EDGE)
const top = Math.min(Math.max(EDGE, c.top), vh.value - h - EDGE)
const cardBox = { left, top, right: left + w, bottom: top + h }
if (!overlaps(cardBox, holeBox)) {
return { width: `${w}px`, left: `${left}px`, top: `${top}px` }
}
}
// 兜底:底部居中
const left = Math.min(Math.max(EDGE, (vw.value - w) / 2), vw.value - w - EDGE)
const top = Math.min(Math.max(EDGE, holeBox.bottom + GAP), vh.value - h - EDGE)
return { width: `${w}px`, left: `${left}px`, top: `${top}px` }
})
// 支持以逗号分隔的多个候选选择器,按书写顺序优先匹配(用于「优先高亮按钮、
// 否则退化为标题」这类带降级的目标)。
const queryTarget = (selector?: string): HTMLElement | null => {
if (!selector) return null
for (const part of selector.split(',').map((s) => s.trim()).filter(Boolean)) {
const el = document.querySelector<HTMLElement>(part)
if (!el) continue
const r = el.getBoundingClientRect()
if (r.width > 2 && r.height > 2) return el
}
return null
}
const measureCard = async () => {
await nextTick()
if (cardRef.value) {
cardSize.value = {
width: cardRef.value.offsetWidth,
height: cardRef.value.offsetHeight,
}
}
}
const locate = async (retry = 0) => {
vw.value = window.innerWidth
vh.value = window.innerHeight
const cur = step.value
if (!cur.target) {
targetEl.value = null
targetRect.value = null
await measureCard()
return
}
const el = queryTarget(cur.target)
if (!el) {
if (retry < 12) {
if (retryTimer) clearTimeout(retryTimer)
retryTimer = setTimeout(() => locate(retry + 1), 120)
return
}
// 找不到目标:可选步骤跳过,否则退化为居中卡片
if (cur.optional) {
goTo(index.value + 1)
return
}
targetEl.value = null
targetRect.value = null
await measureCard()
return
}
el.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'smooth' })
targetEl.value = el
targetRect.value = el.getBoundingClientRect()
await measureCard()
}
const closeGuideSettings = () => {
if (settingsOpenedByGuide) {
@@ -357,56 +63,19 @@ const closeGuideSettings = () => {
}
}
const goTo = async (i: number) => {
if (i < 0 || i >= steps.value.length) return
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
index.value = i
// 离开模型步骤时,关闭由引导自己打开的设置弹窗
if (step.value.key !== 'models') {
const onFinish = () => {
localStorage.setItem(GLOBAL_USER_GUIDE_KEY, '1')
closeGuideSettings()
}
const onStepChange = ({ toKey }: { toKey: string }) => {
if (toKey !== 'models') {
closeGuideSettings()
}
await step.value.before?.()
// 等路由/侧栏/弹窗过渡稳定
await new Promise((r) => setTimeout(r, step.value.before ? 280 : 0))
await locate()
await nextTick()
rootRef.value?.focus()
}
const next = () => goTo(index.value + 1)
const prev = () => goTo(index.value - 1)
const open = async () => {
const open = () => {
active.value = true
index.value = 0
await nextTick()
await goTo(0)
}
const close = () => {
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
closeGuideSettings()
active.value = false
targetEl.value = null
targetRect.value = null
}
const finish = () => {
localStorage.setItem(STORAGE_KEY, '1')
close()
}
const dismiss = () => finish()
const onViewportChange = () => {
if (!active.value) return
locate()
}
const handleOpenEvent = () => {
@@ -415,217 +84,18 @@ const handleOpenEvent = () => {
}
onMounted(() => {
window.addEventListener('resize', onViewportChange)
window.addEventListener('scroll', onViewportChange, true)
window.addEventListener(OPEN_EVENT, handleOpenEvent)
if (localStorage.getItem(STORAGE_KEY) !== '1') {
if (localStorage.getItem(GLOBAL_USER_GUIDE_KEY) !== '1') {
window.setTimeout(() => {
if (localStorage.getItem(STORAGE_KEY) !== '1') open()
if (localStorage.getItem(GLOBAL_USER_GUIDE_KEY) !== '1') {
open()
}
}, 700)
}
})
onBeforeUnmount(() => {
window.removeEventListener('resize', onViewportChange)
window.removeEventListener('scroll', onViewportChange, true)
window.removeEventListener(OPEN_EVENT, handleOpenEvent)
closeGuideSettings()
if (retryTimer) clearTimeout(retryTimer)
})
</script>
<style lang="less" scoped>
.guide {
position: fixed;
inset: 0;
z-index: 5000;
outline: none;
pointer-events: none;
}
// 四块透明挡板拦截点击;中间空洞无元素,点击穿透到下层控件。
.guide__backdrop {
position: fixed;
pointer-events: auto;
background: rgba(15, 18, 22, 0.58);
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1);
&--full {
inset: 0;
}
&--hit {
background: transparent;
}
}
// 仅负责圆角遮罩绘制;不拦截指针(box-shadow 本身也不参与命中)。
.guide__spot {
position: fixed;
box-sizing: border-box;
pointer-events: none;
background: transparent;
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1),
border-radius 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.guide__ring {
position: fixed;
box-sizing: border-box;
pointer-events: none;
border: 2px solid var(--td-brand-color);
box-shadow: 0 0 0 4px rgba(7, 192, 95, 0.18);
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.guide__card {
position: fixed;
z-index: 1;
pointer-events: auto;
display: flex;
flex-direction: column;
gap: 8px;
padding: 18px 18px 14px;
border-radius: 14px;
background: var(--td-bg-color-container);
border: 1px solid var(--td-component-stroke);
box-shadow: 0 20px 48px rgba(0, 0, 0, 0.18);
color: var(--td-text-color-primary);
max-height: calc(100vh - 32px);
overflow-y: auto;
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1);
&--center {
max-width: calc(100vw - 32px);
}
}
.guide__close {
position: absolute;
top: 10px;
right: 10px;
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
padding: 0;
border: none;
border-radius: 8px;
background: transparent;
color: var(--td-text-color-secondary);
cursor: pointer;
&:hover {
background: var(--td-bg-color-container-hover);
color: var(--td-text-color-primary);
}
}
.guide__progress {
display: flex;
gap: 5px;
}
.guide__dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--td-bg-color-component);
transition: width 0.2s ease, background 0.2s ease;
&.is-active {
width: 16px;
border-radius: 999px;
background: var(--td-brand-color);
}
&.is-done {
background: rgba(7, 192, 95, 0.4);
}
}
.guide__step-label {
margin: 6px 0 0;
font-size: 12px;
color: var(--td-text-color-placeholder);
}
.guide__title {
margin: 0;
padding-right: 24px;
font-size: 18px;
font-weight: 600;
line-height: 26px;
}
.guide__desc {
margin: 0;
font-size: 14px;
line-height: 22px;
color: var(--td-text-color-secondary);
}
.guide__actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 4px;
padding-top: 10px;
border-top: 1px solid var(--td-component-stroke);
}
.guide__skip {
border: none;
background: transparent;
padding: 0;
font-size: 13px;
color: var(--td-text-color-placeholder);
cursor: pointer;
&:hover {
color: var(--td-text-color-secondary);
}
}
.guide__actions-main {
display: flex;
gap: 8px;
}
.guide-fade-enter-active,
.guide-fade-leave-active {
transition: opacity 0.2s ease;
}
.guide-fade-enter-from,
.guide-fade-leave-to {
opacity: 0;
}
@media (max-width: 720px) {
.guide__card {
left: 16px !important;
right: 16px;
width: auto !important;
top: auto !important;
bottom: 16px;
max-height: 56vh;
}
}
</style>
+600
View File
@@ -0,0 +1,600 @@
<template>
<Teleport to="body">
<Transition name="guide-fade">
<div v-if="active" class="guide" role="dialog" aria-modal="true" :aria-label="stepTitle"
@keydown.esc.prevent="dismiss" @keydown.left.prevent="prev" @keydown.right.prevent="next" tabindex="-1"
ref="rootRef">
<template v-if="hole">
<div class="guide__spot" :style="spotStyle" aria-hidden="true" />
<div v-for="(piece, i) in backdropPieces" :key="i" class="guide__backdrop guide__backdrop--hit"
:style="piece" />
</template>
<div v-else class="guide__backdrop guide__backdrop--full" />
<div v-if="hole" class="guide__ring" :style="ringStyle" aria-hidden="true" />
<div ref="cardRef" class="guide__card" :class="{ 'guide__card--center': !hole }" :style="cardStyle">
<button type="button" class="guide__close" :aria-label="t(`${labelsPrefix}.skip`)" @click="dismiss">
<t-icon name="close" size="18px" />
</button>
<div class="guide__progress">
<span v-for="(s, i) in steps" :key="s.key" class="guide__dot"
:class="{ 'is-active': i === index, 'is-done': i < index }" />
</div>
<p class="guide__step-label">{{ t(`${labelsPrefix}.stepOf`, { current: index + 1, total: steps.length }) }}
</p>
<h3 class="guide__title">{{ stepTitle }}</h3>
<p class="guide__desc">{{ stepDesc }}</p>
<p v-if="step.interact" class="guide__interact-hint">{{ t(`${labelsPrefix}.interactHint`) }}</p>
<div class="guide__actions">
<button type="button" class="guide__skip" @click="dismiss">{{ t(`${labelsPrefix}.skip`) }}</button>
<div v-if="!step.interact" class="guide__actions-main">
<t-button v-if="index > 0" size="small" variant="outline" @click="prev">
{{ t(`${labelsPrefix}.prev`) }}
</t-button>
<t-button v-if="!isLast" size="small" theme="primary" @click="next">
{{ t(`${labelsPrefix}.next`) }}
</t-button>
<t-button v-else size="small" theme="primary" @click="finish">
{{ t(`${labelsPrefix}.done`) }}
</t-button>
</div>
</div>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup lang="ts">
import { computed, nextTick, onBeforeUnmount, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type { SpotlightGuideStep } from '@/types/spotlightGuide'
const CARD_WIDTH = 340
const GAP = 16
const EDGE = 16
const PAD = 8
const holeRadius = 8
const BACKDROP_COLOR = 'rgba(15, 18, 22, 0.58)'
const props = withDefaults(
defineProps<{
active: boolean
steps: SpotlightGuideStep[]
/** i18n 前缀,步骤文案为 `${stepI18nPrefix}.${key}.title|desc` */
stepI18nPrefix: string
/** skip/prev/next/done/stepOf 所在前缀,默认 newUserGuide */
labelsPrefix?: string
/** 每步 before 执行后的等待毫秒数 */
beforeDelayMs?: number
}>(),
{
labelsPrefix: 'newUserGuide',
beforeDelayMs: 280,
},
)
const emit = defineEmits<{
'update:active': [value: boolean]
finish: []
dismiss: []
'step-change': [payload: { fromKey?: string; toKey: string; index: number }]
}>()
const { t } = useI18n()
const index = ref(0)
const vw = ref(window.innerWidth)
const vh = ref(window.innerHeight)
const targetRect = ref<DOMRect | null>(null)
const targetEl = ref<HTMLElement | null>(null)
const cardSize = ref({ width: CARD_WIDTH, height: 220 })
type HoleRect = { x: number; y: number; width: number; height: number }
const measureNeighborGap = (el: HTMLElement, r: DOMRect) => {
let above = PAD
const prev = el.previousElementSibling
if (prev) {
above = Math.max(0, r.top - prev.getBoundingClientRect().bottom)
}
let below = PAD
const next = el.nextElementSibling
if (next) {
below = Math.max(0, next.getBoundingClientRect().top - r.bottom)
} else {
const mb = parseFloat(getComputedStyle(el).marginBottom) || 0
below = Math.max(0, PAD - mb)
}
return { above, below }
}
const computeHighlightHole = (el: HTMLElement, r: DOMRect): HoleRect => {
const { above, below } = measureNeighborGap(el, r)
const inset = Math.min(PAD, above, below)
let x = r.left - inset
let y = r.top - inset
let width = r.width + inset * 2
let height = r.height + inset * 2
if (x < 0) {
width += x
x = 0
}
if (y < 0) {
height += y
y = 0
}
const rightOverflow = x + width - vw.value
if (rightOverflow > 0) {
width -= rightOverflow
}
const bottomOverflow = y + height - vh.value
if (bottomOverflow > 0) {
height -= bottomOverflow
}
return { x, y, width, height }
}
const rootRef = ref<HTMLElement | null>(null)
const cardRef = ref<HTMLElement | null>(null)
let retryTimer: ReturnType<typeof setTimeout> | null = null
const step = computed(() => props.steps[index.value] ?? props.steps[0])
const isLast = computed(() => index.value === props.steps.length - 1)
const stepTitle = computed(() => t(`${props.stepI18nPrefix}.${step.value.key}.title`))
const stepDesc = computed(() => t(`${props.stepI18nPrefix}.${step.value.key}.desc`))
const hole = computed(() => {
const el = targetEl.value
const r = targetRect.value
if (!el || !r) return null
return computeHighlightHole(el, r)
})
const backdropPieces = computed(() => {
const h = hole.value
if (!h) return []
const w = vw.value
const v = vh.value
return [
{ top: '0px', left: '0px', width: `${w}px`, height: `${h.y}px` },
{
top: `${h.y + h.height}px`,
left: '0px',
width: `${w}px`,
height: `${Math.max(0, v - h.y - h.height)}px`,
},
{ top: `${h.y}px`, left: '0px', width: `${h.x}px`, height: `${h.height}px` },
{
top: `${h.y}px`,
left: `${h.x + h.width}px`,
width: `${Math.max(0, w - h.x - h.width)}px`,
height: `${h.height}px`,
},
]
})
const holeFrameStyle = computed(() => {
if (!hole.value) return {}
return {
left: `${hole.value.x}px`,
top: `${hole.value.y}px`,
width: `${hole.value.width}px`,
height: `${hole.value.height}px`,
borderRadius: `${holeRadius}px`,
}
})
const spotStyle = computed(() => ({
...holeFrameStyle.value,
boxShadow: `0 0 0 9999px ${BACKDROP_COLOR}`,
}))
const ringStyle = holeFrameStyle
const overlaps = (
a: { left: number; top: number; right: number; bottom: number },
b: { left: number; top: number; right: number; bottom: number },
) => !(a.right <= b.left || a.left >= b.right || a.bottom <= b.top || a.top >= b.bottom)
const cardStyle = computed(() => {
const w = Math.min(CARD_WIDTH, vw.value - EDGE * 2)
const h = cardSize.value.height
const h0 = hole.value
if (!h0) {
return {
width: `${w}px`,
left: `${(vw.value - w) / 2}px`,
top: `${Math.max(EDGE, vh.value * 0.32 - h / 2)}px`,
}
}
const holeBox = { left: h0.x, top: h0.y, right: h0.x + h0.width, bottom: h0.y + h0.height }
type Placement = 'right' | 'left' | 'bottom' | 'top'
const order: Placement[] = (() => {
const pref = step.value.placement ?? 'right'
const all: Placement[] = ['right', 'left', 'bottom', 'top']
return [pref, ...all.filter((p) => p !== pref)]
})()
const candidates: Record<Placement, { left: number; top: number }> = {
right: { left: holeBox.right + GAP, top: h0.y + h0.height / 2 - h / 2 },
left: { left: holeBox.left - w - GAP, top: h0.y + h0.height / 2 - h / 2 },
bottom: { left: h0.x + h0.width / 2 - w / 2, top: holeBox.bottom + GAP },
top: { left: h0.x + h0.width / 2 - w / 2, top: holeBox.top - h - GAP },
}
for (const place of order) {
const c = candidates[place]
const left = Math.min(Math.max(EDGE, c.left), vw.value - w - EDGE)
const top = Math.min(Math.max(EDGE, c.top), vh.value - h - EDGE)
const cardBox = { left, top, right: left + w, bottom: top + h }
if (!overlaps(cardBox, holeBox)) {
return { width: `${w}px`, left: `${left}px`, top: `${top}px` }
}
}
const left = Math.min(Math.max(EDGE, (vw.value - w) / 2), vw.value - w - EDGE)
const top = Math.min(Math.max(EDGE, holeBox.bottom + GAP), vh.value - h - EDGE)
return { width: `${w}px`, left: `${left}px`, top: `${top}px` }
})
const queryTarget = (selector?: string): HTMLElement | null => {
if (!selector) return null
for (const part of selector.split(',').map((s) => s.trim()).filter(Boolean)) {
const el = document.querySelector<HTMLElement>(part)
if (!el) continue
const r = el.getBoundingClientRect()
if (r.width > 2 && r.height > 2) return el
}
return null
}
const measureCard = async () => {
await nextTick()
if (cardRef.value) {
cardSize.value = {
width: cardRef.value.offsetWidth,
height: cardRef.value.offsetHeight,
}
}
}
const locate = async (retry = 0) => {
vw.value = window.innerWidth
vh.value = window.innerHeight
const cur = step.value
if (!cur.target) {
targetEl.value = null
targetRect.value = null
await measureCard()
return
}
const el = queryTarget(cur.target)
if (!el) {
if (retry < 12) {
if (retryTimer) clearTimeout(retryTimer)
retryTimer = setTimeout(() => locate(retry + 1), 120)
return
}
if (cur.optional) {
goTo(index.value + 1)
return
}
targetEl.value = null
targetRect.value = null
await measureCard()
return
}
el.scrollIntoView({ block: 'nearest', inline: 'nearest', behavior: 'smooth' })
targetEl.value = el
targetRect.value = el.getBoundingClientRect()
await measureCard()
}
const goTo = async (i: number) => {
if (i < 0 || i >= props.steps.length) return
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
const fromKey = props.steps[index.value]?.key
index.value = i
emit('step-change', { fromKey, toKey: step.value.key, index: i })
await step.value.before?.()
const delay = step.value.before ? props.beforeDelayMs : 0
if (delay > 0) {
await new Promise((r) => setTimeout(r, delay))
}
await locate()
await nextTick()
rootRef.value?.focus()
}
const next = () => goTo(index.value + 1)
const prev = () => goTo(index.value - 1)
const close = () => {
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
emit('update:active', false)
targetEl.value = null
targetRect.value = null
}
const finish = () => {
emit('finish')
close()
}
const dismiss = () => {
emit('dismiss')
finish()
}
const onViewportChange = () => {
if (!props.active) return
locate()
}
const open = async () => {
index.value = 0
emit('update:active', true)
await nextTick()
await goTo(0)
}
watch(
() => props.active,
(val, old) => {
if (val && !old) {
open()
} else if (!val && old) {
if (retryTimer) {
clearTimeout(retryTimer)
retryTimer = null
}
}
},
)
onBeforeUnmount(() => {
window.removeEventListener('resize', onViewportChange)
window.removeEventListener('scroll', onViewportChange, true)
if (retryTimer) clearTimeout(retryTimer)
})
watch(
() => props.active,
(val) => {
if (val) {
window.addEventListener('resize', onViewportChange)
window.addEventListener('scroll', onViewportChange, true)
} else {
window.removeEventListener('resize', onViewportChange)
window.removeEventListener('scroll', onViewportChange, true)
}
},
{ immediate: true },
)
defineExpose({ open, close })
</script>
<style lang="less" scoped>
.guide {
position: fixed;
inset: 0;
z-index: 5000;
outline: none;
pointer-events: none;
}
.guide__backdrop {
position: fixed;
pointer-events: auto;
background: rgba(15, 18, 22, 0.58);
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1);
&--full {
inset: 0;
}
&--hit {
background: transparent;
}
}
.guide__spot {
position: fixed;
box-sizing: border-box;
pointer-events: none;
background: transparent;
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1),
border-radius 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.guide__ring {
position: fixed;
box-sizing: border-box;
pointer-events: none;
border: 2px solid var(--td-brand-color);
box-shadow: 0 0 0 4px rgba(7, 192, 95, 0.18);
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1),
width 0.28s cubic-bezier(0.4, 0, 0.2, 1),
height 0.28s cubic-bezier(0.4, 0, 0.2, 1);
}
.guide__card {
position: fixed;
z-index: 1;
pointer-events: auto;
display: flex;
flex-direction: column;
gap: 8px;
padding: 18px 18px 14px;
border-radius: 14px;
background: var(--td-bg-color-container);
border: 1px solid var(--td-component-stroke);
box-shadow: 0 20px 48px rgba(0, 0, 0, 0.18);
color: var(--td-text-color-primary);
max-height: calc(100vh - 32px);
overflow-y: auto;
transition:
top 0.28s cubic-bezier(0.4, 0, 0.2, 1),
left 0.28s cubic-bezier(0.4, 0, 0.2, 1);
&--center {
max-width: calc(100vw - 32px);
}
}
.guide__close {
position: absolute;
top: 10px;
right: 10px;
display: flex;
align-items: center;
justify-content: center;
width: 28px;
height: 28px;
padding: 0;
border: none;
border-radius: 8px;
background: transparent;
color: var(--td-text-color-secondary);
cursor: pointer;
&:hover {
background: var(--td-bg-color-container-hover);
color: var(--td-text-color-primary);
}
}
.guide__progress {
display: flex;
gap: 5px;
}
.guide__dot {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--td-bg-color-component);
transition: width 0.2s ease, background 0.2s ease;
&.is-active {
width: 16px;
border-radius: 999px;
background: var(--td-brand-color);
}
&.is-done {
background: rgba(7, 192, 95, 0.4);
}
}
.guide__step-label {
margin: 6px 0 0;
font-size: 12px;
color: var(--td-text-color-placeholder);
}
.guide__title {
margin: 0;
padding-right: 24px;
font-size: 18px;
font-weight: 600;
line-height: 26px;
}
.guide__desc {
margin: 0;
font-size: 14px;
line-height: 22px;
color: var(--td-text-color-secondary);
}
.guide__interact-hint {
margin: 0;
font-size: 13px;
line-height: 20px;
color: var(--td-brand-color);
font-weight: 500;
}
.guide__actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
margin-top: 4px;
padding-top: 10px;
border-top: 1px solid var(--td-component-stroke);
}
.guide__skip {
border: none;
background: transparent;
padding: 0;
font-size: 13px;
color: var(--td-text-color-placeholder);
cursor: pointer;
&:hover {
color: var(--td-text-color-secondary);
}
}
.guide__actions-main {
display: flex;
gap: 8px;
}
.guide-fade-enter-active,
.guide-fade-leave-active {
transition: opacity 0.2s ease;
}
.guide-fade-enter-from,
.guide-fade-leave-to {
opacity: 0;
}
@media (max-width: 720px) {
.guide__card {
left: 16px !important;
right: 16px;
width: auto !important;
top: auto !important;
bottom: 16px;
max-height: 56vh;
}
}
</style>
@@ -0,0 +1,127 @@
<template>
<SpotlightGuide v-model:active="active" :steps="steps" :step-i18n-prefix="stepI18nPrefix"
labels-prefix="contextualGuide" @finish="onFinish" @dismiss="onFinish" @step-change="onStepChange" />
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, ref, watch } from 'vue'
import SpotlightGuide from '@/components/SpotlightGuide.vue'
import {
isContextualGuideDone,
isGlobalUserGuideDone,
markContextualGuideDone,
} from '@/config/contextualGuides'
import { useUIStore } from '@/stores/ui'
import type { SpotlightGuideStep } from '@/types/spotlightGuide'
const props = withDefaults(
defineProps<{
when: boolean
/** documentKb:需对话+Embedding;agent:仅需对话模型 */
variant?: 'documentKb' | 'agent'
}>(),
{ variant: 'documentKb' },
)
const stepI18nPrefix = computed(() =>
props.variant === 'agent'
? 'contextualGuide.tenantModels.stepsAgent'
: 'contextualGuide.tenantModels.steps',
)
const uiStore = useUIStore()
const active = ref(false)
let settingsOpenedByGuide = false
const steps: SpotlightGuideStep[] = [
{ key: 'intro' },
{
key: 'addModel',
target: '[data-guide="settings-add-model"], [data-guide="settings-models"]',
placement: 'left',
before: () => {
uiStore.openSettings('models')
settingsOpenedByGuide = true
},
},
{ key: 'done' },
]
let openTimer: ReturnType<typeof setTimeout> | null = null
const closeGuideSettings = () => {
if (settingsOpenedByGuide) {
uiStore.closeSettings()
settingsOpenedByGuide = false
}
}
const onFinish = () => {
markContextualGuideDone('tenantModels')
closeGuideSettings()
}
const onStepChange = ({ toKey }: { toKey: string }) => {
if (toKey !== 'addModel') {
closeGuideSettings()
}
}
let waitGlobalTimer: ReturnType<typeof setTimeout> | null = null
const tryOpen = () => {
if (active.value || !props.when || isContextualGuideDone('tenantModels')) return
openTimer = setTimeout(() => {
if (!props.when || isContextualGuideDone('tenantModels')) return
active.value = true
}, 500)
}
const scheduleOpen = () => {
if (openTimer) {
clearTimeout(openTimer)
openTimer = null
}
if (waitGlobalTimer) {
clearTimeout(waitGlobalTimer)
waitGlobalTimer = null
}
if (!props.when || isContextualGuideDone('tenantModels')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
const poll = () => {
if (!props.when || isContextualGuideDone('tenantModels')) return
if (isGlobalUserGuideDone()) {
tryOpen()
return
}
waitGlobalTimer = setTimeout(poll, 400)
}
waitGlobalTimer = setTimeout(poll, 400)
}
watch(
() => props.when,
(val) => {
if (!val) {
if (openTimer) clearTimeout(openTimer)
if (waitGlobalTimer) clearTimeout(waitGlobalTimer)
openTimer = null
waitGlobalTimer = null
active.value = false
closeGuideSettings()
return
}
scheduleOpen()
},
{ immediate: true },
)
onBeforeUnmount(() => {
if (openTimer) clearTimeout(openTimer)
if (waitGlobalTimer) clearTimeout(waitGlobalTimer)
closeGuideSettings()
})
</script>