fix type errors of MkPageHeader

This commit is contained in:
Lhcfl 2024-04-04 16:53:02 +08:00
parent 3902b82470
commit 995d679afd
3 changed files with 28 additions and 16 deletions

View file

@ -19,7 +19,8 @@
"rules": { "rules": {
"style": { "style": {
"useImportType": "warn", "useImportType": "warn",
"useShorthandFunctionType": "warn" "useShorthandFunctionType": "warn",
"useTemplate": "warn"
} }
} }
} }

View file

@ -3,8 +3,8 @@
v-if="show" v-if="show"
ref="el" ref="el"
class="fdidabkb" class="fdidabkb"
:class="{ thin: thin_, tabs: tabs?.length > 0 }" :class="{ thin: thin_, tabs: isTabs(tabs)}"
:style="{ background: bg }" :style="{ background: bg || undefined }"
@click="onClick" @click="onClick"
> >
<div class="left"> <div class="left">
@ -70,14 +70,14 @@
</div> </div>
<template v-if="metadata"> <template v-if="metadata">
<nav <nav
v-if="hasTabs" v-if="isTabs(tabs)"
ref="tabsEl" ref="tabsEl"
class="tabs" class="tabs"
:class="{ collapse: hasTabs && tabs.length > 3 }" :class="{ collapse: tabs.length > 3 }"
> >
<button <button
v-for="tab in tabs" v-for="tab in tabs"
:ref="(el) => (tabRefs[tab.key] = el)" :ref="(el) => (tab.key && (tabRefs[tab.key] = el))"
v-tooltip.noDelay="tab.title" v-tooltip.noDelay="tab.title"
v-vibrate="5" v-vibrate="5"
class="tab _button" class="tab _button"
@ -157,6 +157,7 @@ const props = defineProps<{
actions?: { actions?: {
text: string; text: string;
icon: string; icon: string;
highlighted?: boolean;
handler: (ev: MouseEvent) => void; handler: (ev: MouseEvent) => void;
}[]; }[];
thin?: boolean; thin?: boolean;
@ -171,7 +172,7 @@ const displayBackButton =
inject("shouldBackButton", true); inject("shouldBackButton", true);
const emit = defineEmits<{ const emit = defineEmits<{
(ev: "update:tab", key: string); "update:tab": [key: string];
}>(); }>();
const metadata = injectPageMetadata(); const metadata = injectPageMetadata();
@ -183,9 +184,14 @@ const el = ref<HTMLElement | null>(null);
const tabRefs = {}; const tabRefs = {};
const tabHighlightEl = ref<HTMLElement | null>(null); const tabHighlightEl = ref<HTMLElement | null>(null);
const tabsEl = ref<HTMLElement | null>(null); const tabsEl = ref<HTMLElement | null>(null);
const bg = ref(null); const bg = ref<string | null | number>(null);
const narrow = ref(false); const narrow = ref(false);
const hasTabs = computed(() => props.tabs && props.tabs.length > 0); const hasTabs = computed(() => props.tabs && props.tabs.length > 0);
function isTabs(t: Tab[] | undefined): t is Tab[] {
return t != null && t.length > 0;
}
const hasActions = computed(() => props.actions && props.actions.length > 0); const hasActions = computed(() => props.actions && props.actions.length > 0);
const show = computed(() => { const show = computed(() => {
return !hideTitle || hasTabs.value || hasActions.value; return !hideTitle || hasTabs.value || hasActions.value;
@ -201,7 +207,7 @@ const openAccountMenu = (ev: MouseEvent) => {
}; };
const showTabsPopup = (ev: MouseEvent) => { const showTabsPopup = (ev: MouseEvent) => {
if (!hasTabs.value) return; if (!isTabs(props.tabs)) return;
if (!narrow.value) return; if (!narrow.value) return;
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
@ -213,7 +219,7 @@ const showTabsPopup = (ev: MouseEvent) => {
onTabClick(tab, ev); onTabClick(tab, ev);
}, },
})); }));
popupMenu(menu, ev.currentTarget ?? ev.target); popupMenu(menu, (ev.currentTarget ?? ev.target) as HTMLElement);
}; };
const preventDrag = (ev: TouchEvent) => { const preventDrag = (ev: TouchEvent) => {
@ -224,8 +230,10 @@ const onClick = () => {
if (props.to) { if (props.to) {
location.href = props.to; location.href = props.to;
} else { } else {
if (el.value) {
scrollToTop(el.value, { behavior: "smooth" }); scrollToTop(el.value, { behavior: "smooth" });
} }
}
}; };
function onTabMousedown(tab: Tab, ev: MouseEvent): void { function onTabMousedown(tab: Tab, ev: MouseEvent): void {
@ -257,6 +265,8 @@ onMounted(() => {
() => [props.tab, props.tabs], () => [props.tab, props.tabs],
() => { () => {
nextTick(() => { nextTick(() => {
if (props.tab == null) return;
if (!isTabs(props.tabs)) return;
const tabEl = tabRefs[props.tab]; const tabEl = tabRefs[props.tab];
if (tabEl && tabHighlightEl.value) { if (tabEl && tabHighlightEl.value) {
// offsetWidth offsetLeft getBoundingClientRect 使 // offsetWidth offsetLeft getBoundingClientRect 使
@ -266,7 +276,8 @@ onMounted(() => {
tabEl.style = `--width: ${tabSizeX}px`; tabEl.style = `--width: ${tabSizeX}px`;
} }
setTimeout(() => { setTimeout(() => {
tabHighlightEl.value.style.width = tabSizeX + "px"; if (tabHighlightEl.value == null) return;
tabHighlightEl.value.style.width = `${tabSizeX}px`;
tabHighlightEl.value.style.transform = `translateX(${tabEl.offsetLeft}px)`; tabHighlightEl.value.style.transform = `translateX(${tabEl.offsetLeft}px)`;
window.requestAnimationFrame(() => { window.requestAnimationFrame(() => {
tabsEl.value?.scrollTo({ tabsEl.value?.scrollTo({
@ -283,10 +294,10 @@ onMounted(() => {
}, },
); );
if (el.value && el.value.parentElement) { if (el.value?.parentElement) {
narrow.value = el.value.parentElement.offsetWidth < 500; narrow.value = el.value.parentElement.offsetWidth < 500;
ro = new ResizeObserver((entries, observer) => { ro = new ResizeObserver((_entries, _observer) => {
if (el.value.parentElement && document.body.contains(el.value)) { if (el.value?.parentElement && document.body.contains(el.value)) {
narrow.value = el.value.parentElement.offsetWidth < 500; narrow.value = el.value.parentElement.offsetWidth < 500;
} }
}); });

View file

@ -9,7 +9,7 @@ export interface PageMetadata {
title: string; title: string;
subtitle?: string; subtitle?: string;
icon?: string | null; icon?: string | null;
avatar?: entities.User | null; avatar?: entities.UserDetailed | null;
userName?: entities.User | null; userName?: entities.User | null;
bg?: string; bg?: string;
} }