From 80e20b1cdc70ae1cf8fe168012477b50ca09c0a0 Mon Sep 17 00:00:00 2001 From: unanmed <1319491857@qq.com> Date: Sat, 18 Nov 2023 11:33:24 +0800 Subject: [PATCH] fix: hotkey scope --- src/core/main/custom/hotkey.ts | 6 ++++-- src/plugin/utils.ts | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index 8e24766..e2fb619 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -1,6 +1,6 @@ import { KeyCode } from '@/plugin/keyCodes'; import { getLocFromMouseLoc } from '@/plugin/ui/fixed'; -import { deleteWith, generateBinary, has, tip } from '@/plugin/utils'; +import { deleteWith, generateBinary, has, spliceBy, tip } from '@/plugin/utils'; import { EmitableEvent, EventEmitter } from '../../common/eventEmitter'; import { GameStorage } from '../storage'; @@ -81,6 +81,7 @@ export class Hotkey extends EventEmitter { * @param symbol 当前作用域的symbol */ use(symbol: symbol) { + spliceBy(this.scopeStack, symbol); this.scopeStack.push(symbol); this.scope = symbol; for (const key of Object.values(this.data)) { @@ -89,13 +90,14 @@ export class Hotkey extends EventEmitter { } /** - * 释放一个作用域,释放后不能调用{@link realize}函数,除非重新调用{@link use}函数 + * 释放一个作用域,释放后作用域将退回至删除的作用域的上一级 * @param symbol 要释放的作用域的symbol */ dispose(symbol: symbol) { for (const key of Object.values(this.data)) { key.func.delete(symbol); } + spliceBy(this.scopeStack, symbol); this.scope = this.scopeStack.pop() ?? Symbol(); } diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index ada4279..27f3768 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -271,6 +271,13 @@ export function deleteWith(arr: T[], ele: T): T[] { return arr; } +export function spliceBy(arr: T[], from: T): T[] { + const index = arr.indexOf(from); + if (index === -1) return arr; + arr.splice(index); + return arr; +} + export async function triggerFullscreen(full: boolean) { const { maxGameScale } = core.plugin.utils; if (!!document.fullscreenElement && !full) {