front/src/views/site/SiteIntentFab.vue
zzh ba565f7384
All checks were successful
Build and Deploy Front / deploy (push) Successful in 3m27s
feat: 工作台美区 ZIP3 与官网/登录体验收敛
补齐客户工作台地图 ZIP3/港口数据与 keep-alive 重挂,并轻量化官网导航弹层与可拖意图 IP,登录页改为居中舞台。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-12-03 15:10:05 +08:00

314 lines
8.1 KiB
Vue

<template>
<div
v-show="visible"
ref="hostRef"
class="site-intent-fab"
:class="{
'is-open': open && !drag.moved,
'is-en': !isZh,
'is-dragging': drag.active && drag.moved,
'is-panel-right': panelRight,
}"
:style="hostStyle"
@pointerenter="onEnter"
@pointerleave="onLeave"
>
<div
class="site-intent-fab__panel"
role="menu"
:aria-label="ariaLabel"
:aria-hidden="open && !drag.moved ? 'false' : 'true'"
@pointerdown.stop
>
<p class="site-intent-fab__caption">{{ caption }}</p>
<button
v-for="(door, index) in doors"
:key="door.id"
type="button"
class="site-intent-fab__item"
:class="{ 'is-featured': door.id === 'agent' }"
:style="{ '--i': String(index) }"
role="menuitem"
:tabindex="open && !drag.moved ? 0 : -1"
:disabled="busy || !open || drag.moved"
:aria-label="itemAria(door)"
@click="onPick(door)"
>
<span class="site-intent-fab__icon" aria-hidden="true">
<SiteIllusIcon :name="door.icon" />
</span>
<span class="site-intent-fab__copy">
<span class="site-intent-fab__label">{{ isZh ? door.zh.title : door.en.title }}</span>
<span class="site-intent-fab__hint">{{ isZh ? door.zh.hint : door.en.hint }}</span>
</span>
</button>
</div>
<button
type="button"
class="site-intent-fab__core"
:aria-expanded="open && !drag.moved ? 'true' : 'false'"
:aria-label="coreLabel"
@pointerdown="onCorePointerDown"
@pointermove="onCorePointerMove"
@pointerup="onCorePointerUp"
@pointercancel="onCorePointerUp"
@click.prevent
>
<EdiAvatar size="lg" :pulse="false" />
</button>
</div>
</template>
<script lang="ts" setup>
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue';
import EdiAvatar from '@/components/EdiAssistant/EdiAvatar.vue';
import SiteIllusIcon from './SiteIllusIcon.vue';
import {
FULFILL_DOOR_IDS,
GROWTH_DOOR_IDS,
HOME_DOORS,
WELCOME_COPY,
} from './lib/site-welcome';
const EDGE = 16;
const DRAG_THRESHOLD = 6;
const PANEL_W = 292;
const POS_KEY = 'oms-site-intent-fab-pos';
const fabSize = ref(68);
const props = withDefaults(
defineProps<{
isZh: boolean;
busy?: boolean;
visible?: boolean;
}>(),
{
busy: false,
visible: true,
},
);
const emit = defineEmits<{
intent: [id: string];
}>();
const hostRef = ref<HTMLElement | null>(null);
const open = ref(false);
const hoverCapable = ref(true);
const left = ref(0);
const top = ref(0);
const panelRight = ref(false);
const drag = reactive({
active: false,
moved: false,
startX: 0,
startY: 0,
origLeft: 0,
origTop: 0,
});
let closeTimer: ReturnType<typeof setTimeout> | null = null;
const doors = computed(() => {
const fulfill = FULFILL_DOOR_IDS.map((id) => HOME_DOORS.find((d) => d.id === id)).filter(
Boolean,
);
const growth = GROWTH_DOOR_IDS.map((id) => HOME_DOORS.find((d) => d.id === id)).filter(
Boolean,
);
const agent = HOME_DOORS.find((d) => d.id === 'agent');
return [...fulfill, agent, ...growth].filter(
(d): d is (typeof HOME_DOORS)[number] => !!d,
);
});
const ariaLabel = computed(() =>
props.isZh ? WELCOME_COPY.zh.intentAria : WELCOME_COPY.en.intentAria,
);
const caption = computed(() => (props.isZh ? '快捷咨询' : 'Quick asks'));
const coreLabel = computed(() =>
props.isZh
? open.value
? '收起快捷入口'
: '展开快捷入口'
: open.value
? 'Collapse shortcuts'
: 'Open shortcuts',
);
const hostStyle = computed(() => ({
left: `${left.value}px`,
top: `${top.value}px`,
width: `${fabSize.value}px`,
height: `${fabSize.value}px`,
}));
function itemAria(door: (typeof HOME_DOORS)[number]) {
const title = props.isZh ? door.zh.title : door.en.title;
const hint = props.isZh ? door.zh.hint : door.en.hint;
return `${title}。${hint}`;
}
function syncPanelSide() {
panelRight.value = left.value < PANEL_W + EDGE + 24;
}
function syncFabSize() {
fabSize.value = window.matchMedia('(max-width: 780px)').matches ? 60 : 68;
}
function clampPos(x: number, y: number) {
const size = fabSize.value;
const maxX = Math.max(EDGE, window.innerWidth - size - EDGE);
const maxY = Math.max(EDGE, window.innerHeight - size - EDGE);
return {
x: Math.min(Math.max(x, EDGE), maxX),
y: Math.min(Math.max(y, EDGE), maxY),
};
}
function persistPos() {
try {
sessionStorage.setItem(
POS_KEY,
JSON.stringify({ left: left.value, top: top.value }),
);
} catch {
/* ignore */
}
}
function readStoredPos(): { left: number; top: number } | null {
try {
const raw = sessionStorage.getItem(POS_KEY);
if (!raw) return null;
const parsed = JSON.parse(raw) as { left?: number; top?: number };
if (typeof parsed.left !== 'number' || typeof parsed.top !== 'number') return null;
return { left: parsed.left, top: parsed.top };
} catch {
return null;
}
}
function initPos() {
syncFabSize();
const stored = readStoredPos();
const fallback = {
x: window.innerWidth - fabSize.value - 28,
y: Math.round(window.innerHeight * (2 / 3) - fabSize.value / 2),
};
const next = clampPos(stored?.left ?? fallback.x, stored?.top ?? fallback.y);
left.value = next.x;
top.value = next.y;
syncPanelSide();
}
function clearCloseTimer() {
if (closeTimer) {
clearTimeout(closeTimer);
closeTimer = null;
}
}
function onEnter() {
if (!hoverCapable.value || drag.active) return;
clearCloseTimer();
open.value = true;
}
function onLeave() {
if (!hoverCapable.value || drag.active) return;
clearCloseTimer();
closeTimer = setTimeout(() => {
open.value = false;
closeTimer = null;
}, 120);
}
function toggle() {
open.value = !open.value;
}
function onPick(door: (typeof HOME_DOORS)[number]) {
emit('intent', door.intentId);
open.value = false;
}
function onCorePointerDown(e: PointerEvent) {
if (e.button !== 0) return;
(e.currentTarget as HTMLElement | null)?.setPointerCapture?.(e.pointerId);
drag.active = true;
drag.moved = false;
drag.startX = e.clientX;
drag.startY = e.clientY;
drag.origLeft = left.value;
drag.origTop = top.value;
}
function onCorePointerMove(e: PointerEvent) {
if (!drag.active) return;
const dx = e.clientX - drag.startX;
const dy = e.clientY - drag.startY;
if (!drag.moved) {
if (Math.abs(dx) + Math.abs(dy) < DRAG_THRESHOLD) return;
drag.moved = true;
open.value = false;
clearCloseTimer();
}
const next = clampPos(drag.origLeft + dx, drag.origTop + dy);
left.value = next.x;
top.value = next.y;
syncPanelSide();
}
function onCorePointerUp() {
if (!drag.active) return;
const wasDrag = drag.moved;
drag.active = false;
if (wasDrag) {
persistPos();
drag.moved = false;
return;
}
drag.moved = false;
toggle();
}
function onDocPointerDown(e: PointerEvent) {
if (!open.value) return;
const root = e.target;
if (!(root instanceof Node)) return;
if (hostRef.value && !hostRef.value.contains(root)) open.value = false;
}
function onViewportResize() {
syncFabSize();
const next = clampPos(left.value, top.value);
left.value = next.x;
top.value = next.y;
syncPanelSide();
}
watch(
() => props.visible,
(v) => {
if (!v) open.value = false;
},
);
onMounted(() => {
hoverCapable.value = window.matchMedia('(hover: hover) and (pointer: fine)').matches;
initPos();
document.addEventListener('pointerdown', onDocPointerDown, true);
window.addEventListener('resize', onViewportResize);
});
onBeforeUnmount(() => {
clearCloseTimer();
document.removeEventListener('pointerdown', onDocPointerDown, true);
window.removeEventListener('resize', onViewportResize);
});
</script>