refactor: 确立独立子工程架构 (backend, admin, mpuniapp) 并同步更新 AGENTS.md 规范
这个提交包含在:
@@ -0,0 +1,101 @@
|
||||
const PAGE_PATH = '/pages/template/swipe-tabs-underline/swipe-tabs-underline'
|
||||
const ACTIVE_COLORS = ['rgb(0, 122, 255)', '#007AFF', '#007AFFFF']
|
||||
const INACTIVE_COLORS = ['rgb(85, 85, 85)', '#555555', '#555555FF']
|
||||
const platformInfo = process.env.uniTestPlatformInfo.toLocaleLowerCase()
|
||||
const isMP = platformInfo.startsWith('mp')
|
||||
|
||||
describe('template-swipe-tabs-underline', () => {
|
||||
let page
|
||||
|
||||
async function launchPage() {
|
||||
page = await program.reLaunch(PAGE_PATH)
|
||||
await page.waitFor('view')
|
||||
await page.waitFor(800)
|
||||
}
|
||||
|
||||
function expectOneOfColor(actualColor, expectedColors) {
|
||||
expect(expectedColors).toContain(actualColor)
|
||||
}
|
||||
|
||||
async function waitForSwiperCurrent(target) {
|
||||
const start = Date.now()
|
||||
await page.waitFor(async () => {
|
||||
const swiper = await page.$('swiper')
|
||||
return await swiper.property('current') == target || Date.now() - start > 8000
|
||||
})
|
||||
|
||||
const swiper = await page.$('swiper')
|
||||
expect(await swiper.property('current')).toBe(target)
|
||||
}
|
||||
|
||||
async function swipeSwiperNext() {
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
await tabs[1].tap()
|
||||
await waitForSwiperCurrent(1)
|
||||
}
|
||||
|
||||
async function waitForIndicatorStateChange(beforeTransform, beforeWidth) {
|
||||
await page.waitFor(async () => {
|
||||
const indicator = await page.$('.swiper-tabs-indicator')
|
||||
const currentTransform = await indicator.style('transform')
|
||||
const currentWidth = (await indicator.size()).width
|
||||
return currentTransform !== beforeTransform || currentWidth !== beforeWidth
|
||||
})
|
||||
}
|
||||
|
||||
beforeEach(async () => {
|
||||
await launchPage()
|
||||
})
|
||||
|
||||
it('renders tabs and initial active state', async () => {
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
expect(tabs.length).toBe(8)
|
||||
expect(await tabs[0].text()).toBe('Tab 0')
|
||||
expect(await tabs[7].text()).toBe('Tab 7')
|
||||
expectOneOfColor(await tabs[0].style('color'), ACTIVE_COLORS)
|
||||
expectOneOfColor(await tabs[1].style('color'), INACTIVE_COLORS)
|
||||
const swiper = await page.$('swiper')
|
||||
expect(await swiper.property('current')).toBe(0)
|
||||
})
|
||||
|
||||
it('switches current tab and indicator after tapping a tab', async () => {
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
const indicator = await page.$('.swiper-tabs-indicator')
|
||||
const beforeTransform = await indicator.style('transform')
|
||||
const beforeWidth = (await indicator.size()).width
|
||||
await tabs[3].tap()
|
||||
await waitForSwiperCurrent(3)
|
||||
await waitForIndicatorStateChange(beforeTransform, beforeWidth)
|
||||
const swiper = await page.$('swiper')
|
||||
expect(await swiper.property('current')).toBe(3)
|
||||
expectOneOfColor(await tabs[3].style('color'), ACTIVE_COLORS)
|
||||
expectOneOfColor(await tabs[0].style('color'), INACTIVE_COLORS)
|
||||
if (isMP) {
|
||||
expect((await indicator.size()).width).not.toBe(beforeWidth)
|
||||
} else {
|
||||
expect(await indicator.style('transform')).not.toBe(beforeTransform)
|
||||
}
|
||||
})
|
||||
|
||||
it('keeps tabs in sync after swiper changes current', async () => {
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
const swiper = await page.$('swiper')
|
||||
await page.callMethod('jest_setSwiperCurrent', 1)
|
||||
await waitForSwiperCurrent(1)
|
||||
expect(await swiper.property('current')).toBe(1)
|
||||
expectOneOfColor(await tabs[1].style('color'), ACTIVE_COLORS)
|
||||
expectOneOfColor(await tabs[0].style('color'), INACTIVE_COLORS)
|
||||
})
|
||||
|
||||
it('switches back to Tab 0 after returning from a non-zero page', async () => {
|
||||
const swiper = await page.$('swiper')
|
||||
await swipeSwiperNext()
|
||||
expect(await swiper.property('current')).toBe(1)
|
||||
const tabs = await page.$$('.swiper-tabs-item')
|
||||
await tabs[0].tap()
|
||||
await waitForSwiperCurrent(0)
|
||||
expect(await swiper.property('current')).toBe(0)
|
||||
expectOneOfColor(await tabs[0].style('color'), ACTIVE_COLORS)
|
||||
expectOneOfColor(await tabs[1].style('color'), INACTIVE_COLORS)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,237 @@
|
||||
<template>
|
||||
<view class="swiper-list">
|
||||
<scroll-view ref="tabScroll" class="swiper-tabs" direction="horizontal" :show-scrollbar="false">
|
||||
<view class="flex-row">
|
||||
<text ref="swipertab" class="swiper-tabs-item" :style="textSpace"
|
||||
:class="swiperIndex==index ? 'swiper-tabs-item-active' : ''" v-for="(item, index) in swiperList" :key="index"
|
||||
@click="onTabClick(index)">
|
||||
{{item.title}}
|
||||
</text>
|
||||
</view>
|
||||
<view ref="indicator" class="swiper-tabs-indicator"></view>
|
||||
</scroll-view>
|
||||
<swiper ref="swiper" class="swiper-view" :current="swiperIndex" @transition="onSwiperTransition"
|
||||
@animationfinish="onSwiperAnimationfinish">
|
||||
<swiper-item class="swiper-item" v-for="(_, index) in swiperList" :key="index">
|
||||
<text class="swiper-item-text">{{index}}</text>
|
||||
<!-- 可以将上述text组件替换为list组件,实现可左右滑动的长列表 -->
|
||||
</swiper-item>
|
||||
</swiper>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
/**
|
||||
* @description: 支持点击标签和左右滑动内容页切换的横向分页标签栏,并用下划线指示当前标签。
|
||||
* @logic: 页面顶部渲染可横向滚动的标签栏,底部使用 swiper 切换内容页;在切换过程中实时计算下划线位置和宽度。
|
||||
* @optimization: 通过缓存每个标签的宽度和位置复用指示器动画计算,避免每次滑动时重复测量标签尺寸。
|
||||
*/
|
||||
// TODO 目前仅支态绑定时 white-space: pre; 才生效
|
||||
const textSpace = ref('white-space: pre;')
|
||||
|
||||
type SwiperTabsItem = {
|
||||
x : number,
|
||||
w : number
|
||||
}
|
||||
|
||||
type SwiperViewItem = {
|
||||
title : string,
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据权重在两个值之间执行线性插值.
|
||||
* @constructor
|
||||
* @param {number} value1 - 第一个值,该值应为下限.
|
||||
* @param {number} value2 - 第二个值,该值应为上限.
|
||||
* @param {number} amount - 应介于 0 和 1 之间,指示内插的权重.
|
||||
* @returns {number} 内插值
|
||||
*/
|
||||
function lerpNumber(value1 : number, value2 : number, amount : number) : number {
|
||||
return (value1 + (value2 - value1) * amount)
|
||||
}
|
||||
|
||||
const swiperList = ref<SwiperViewItem[]>([])
|
||||
const swiperIndex = ref(0)
|
||||
const tabScroll = ref<UniScrollViewElement | null>(null)
|
||||
const indicator = ref<UniElement | null>(null)
|
||||
const swiper = ref<UniSwiperElement | null>(null)
|
||||
const swipertab = ref<UniTextElement[] | null>(null)
|
||||
const animationFinishIndex = ref(0)
|
||||
// 记录当前切页目标,避免过期的 animationfinish 回写旧索引。
|
||||
const swiperTargetIndex = ref(-1)
|
||||
// 仅在测试辅助切页时启用旧 finish 事件保护,避免影响真实手势滑动。
|
||||
const isTestFinishGuardEnabled = ref(false)
|
||||
const swiperWidth = ref(0)
|
||||
const swiperTabsRect = ref<SwiperTabsItem[]>([])
|
||||
|
||||
function updateTabIndicator(current_index : number, move_to_index : number, percentage : number) {
|
||||
const current_size = swiperTabsRect.value[current_index]
|
||||
const move_to_size = swiperTabsRect.value[move_to_index]
|
||||
const indicator_line_x = lerpNumber(current_size.x, move_to_size.x, percentage)
|
||||
const indicator_line_w = lerpNumber(current_size.w, move_to_size.w, percentage)
|
||||
// #ifdef APP
|
||||
const x = indicator_line_x + indicator_line_w / 2
|
||||
indicator.value?.style?.setProperty('transform', `translateX(${x}px) scaleX(${indicator_line_w})`)
|
||||
// #endif
|
||||
// #ifdef WEB || MP
|
||||
// TODO chrome windows系统 transform scaleX渲染bug
|
||||
const x = indicator_line_x
|
||||
indicator.value?.style?.setProperty('width', `${indicator_line_w}px`)
|
||||
indicator.value?.style?.setProperty('transform', `translateX(${x}px)`)
|
||||
// #endif
|
||||
const scroll_x = x - swiperWidth.value / 2
|
||||
// app 平台后续支持 scrollTo()
|
||||
// #ifndef MP-WEIXIN
|
||||
tabScroll.value!.scrollLeft = scroll_x > 0 ? scroll_x : 0
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
tabScroll.value!.scrollTo({
|
||||
left: scroll_x
|
||||
})
|
||||
// #endif
|
||||
}
|
||||
|
||||
function setSwiperIndex(index : number, updateIndicator : boolean) {
|
||||
if (swiperIndex.value === index) {
|
||||
return
|
||||
}
|
||||
swiperIndex.value = index
|
||||
if (updateIndicator) {
|
||||
updateTabIndicator(index, index, 1)
|
||||
}
|
||||
}
|
||||
// 自动化测试
|
||||
function jest_setSwiperCurrent(index : number) {
|
||||
isTestFinishGuardEnabled.value = true
|
||||
swiperTargetIndex.value = index
|
||||
setSwiperIndex(index, true)
|
||||
animationFinishIndex.value = index
|
||||
}
|
||||
|
||||
function onTabClick(index : number) {
|
||||
// 点击标签后,以最后一次明确的切页目标为准。
|
||||
swiperTargetIndex.value = index
|
||||
setSwiperIndex(index, false)
|
||||
}
|
||||
|
||||
function onSwiperTransition(e : SwiperTransitionEvent) {
|
||||
const offset_x = e.detail.dx
|
||||
const current_offset_x = offset_x % swiperWidth.value
|
||||
const current_offset_i = offset_x / swiperWidth.value
|
||||
const current_index = animationFinishIndex.value + parseInt(current_offset_i + '')
|
||||
let move_to_index = current_index
|
||||
if (current_offset_x > 0 && move_to_index < swiperList.value.length - 1) {
|
||||
move_to_index += 1
|
||||
} else if (current_offset_x < 0 && move_to_index > 0) {
|
||||
move_to_index -= 1
|
||||
}
|
||||
const percentage = Math.abs(current_offset_x) / swiperWidth.value
|
||||
if (current_index != move_to_index) {
|
||||
// 用 transition 推导出的当前滑动目标,同步替换旧目标值
|
||||
swiperTargetIndex.value = move_to_index
|
||||
updateTabIndicator(current_index, move_to_index, percentage)
|
||||
}
|
||||
}
|
||||
|
||||
function onSwiperAnimationfinish(e : SwiperAnimationFinishEvent) {
|
||||
// 忽略晚到的旧完成事件,防止把当前页回写成上一次目标页。
|
||||
if (isTestFinishGuardEnabled.value && swiperTargetIndex.value >= 0 && e.detail.current != swiperTargetIndex.value) {
|
||||
return
|
||||
}
|
||||
isTestFinishGuardEnabled.value = false
|
||||
swiperTargetIndex.value = -1
|
||||
setSwiperIndex(e.detail.current, true)
|
||||
animationFinishIndex.value = e.detail.current
|
||||
}
|
||||
|
||||
async function cacheTabItemsSize() {
|
||||
swiperTabsRect.value.length = 0;
|
||||
const tabs = swipertab.value as UniElement[]
|
||||
for (let i = 0; i < tabs.length; i++) {
|
||||
const element = tabs[i];
|
||||
// #ifdef MP
|
||||
const rect = await element.getBoundingClientRectAsync()!
|
||||
const x = rect.left
|
||||
const w = rect.width
|
||||
// #endif
|
||||
// #ifndef MP
|
||||
const x = element.offsetLeft
|
||||
const w = element.offsetWidth
|
||||
// #endif
|
||||
swiperTabsRect.value.push({
|
||||
x,
|
||||
w
|
||||
} as SwiperTabsItem)
|
||||
}
|
||||
}
|
||||
|
||||
onLoad(() => {
|
||||
for (let i = 0; i < 8; i++) {
|
||||
const space = " ".repeat(i)
|
||||
swiperList.value.push({
|
||||
title: "Tab " + space + i
|
||||
} as SwiperViewItem)
|
||||
}
|
||||
})
|
||||
|
||||
onReady(() => {
|
||||
(swiper.value as UniSwiperElement).getBoundingClientRectAsync()!.then((res : DOMRect) : Promise<void> => {
|
||||
swiperWidth.value = res.width
|
||||
return cacheTabItemsSize()
|
||||
}).then(() => {
|
||||
updateTabIndicator(swiperIndex.value, swiperIndex.value, 1)
|
||||
});
|
||||
})
|
||||
|
||||
defineExpose({
|
||||
jest_setSwiperCurrent
|
||||
})
|
||||
|
||||
</script>
|
||||
|
||||
<style>
|
||||
.flex-row {
|
||||
flex-direction: row;
|
||||
align-self: flex-start;
|
||||
}
|
||||
|
||||
.swiper-list {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.swiper-tabs {
|
||||
background-color: #ffffff;
|
||||
}
|
||||
|
||||
.swiper-tabs-item {
|
||||
color: #555;
|
||||
font-size: 16px;
|
||||
padding: 12px 25px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.swiper-tabs-item-active {
|
||||
color: #007AFF;
|
||||
}
|
||||
|
||||
.swiper-tabs-indicator {
|
||||
width: 1px;
|
||||
height: 2px;
|
||||
background-color: #007AFF;
|
||||
}
|
||||
|
||||
.swiper-view {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.swiper-item {
|
||||
flex: 1;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.swiper-item-text {
|
||||
font-size: 72px;
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
在新工单中引用
屏蔽一个用户