refactor(customCursor.ts): rename addActiveCallback to active.addWatcher for better readability and consistency

refactor(stageNav.ts): rename getIsAnimating to isAnimating.get for better readability and consistency
refactor(stageNav.ts): rename addActiveCallback to active.addWatcher for better readability and consistency
feat(stageNav.ts): add check for isOpen.get() and isAnimating.get() before handling key events to prevent unwanted actions
This commit is contained in:
Sped0n
2023-10-29 02:42:04 +08:00
parent 817b543ee0
commit e93bca55d5
2 changed files with 9 additions and 15 deletions

View File

@@ -1,4 +1,4 @@
import { addActiveCallback } from './stage'
import { active } from './stage'
let cursor: HTMLDivElement
@@ -24,8 +24,8 @@ export function initCustomCursor(): void {
// bind mousemove event to window
window.addEventListener('mousemove', onMouse)
// add active callback
addActiveCallback((active) => {
if (active) {
active.addWatcher(() => {
if (active.get()) {
cursor.classList.add('active')
} else {
cursor.classList.remove('active')

View File

@@ -1,13 +1,7 @@
import { setCustomCursor } from './customCursor'
import { decIndex, incIndex, getState } from './state'
import { increment, decrement } from './utils'
import {
cordHist,
isOpen,
getIsAnimating,
minimizeImage,
addActiveCallback
} from './stage'
import { cordHist, isOpen, isAnimating, active, minimizeImage } from './stage'
type NavItem = (typeof navItems)[number]
const navItems = ['prev', 'close', 'next'] as const
@@ -29,7 +23,7 @@ function handleClick(type: NavItem) {
}
function handleKey(e: KeyboardEvent) {
if (isOpen.get() || getIsAnimating()) return
if (isOpen.get() || isAnimating.get()) return
switch (e.key) {
case 'ArrowLeft':
prevImage()
@@ -57,8 +51,8 @@ export function initStageNav() {
overlay.addEventListener('focus', () => setCustomCursor(navItem))
navOverlay.append(overlay)
}
addActiveCallback((active) => {
if (active) {
active.addWatcher(() => {
if (active.get()) {
navOverlay.classList.add('active')
} else {
navOverlay.classList.remove('active')
@@ -71,7 +65,7 @@ export function initStageNav() {
// hepler
function nextImage() {
if (getIsAnimating()) return
if (isAnimating.get()) return
cordHist.set(
cordHist.get().map((item) => {
return { ...item, i: increment(item.i, getState().length) }
@@ -82,7 +76,7 @@ function nextImage() {
}
function prevImage() {
if (getIsAnimating()) return
if (isAnimating.get()) return
cordHist.set(
cordHist.get().map((item) => {
return { ...item, i: decrement(item.i, getState().length) }