mirror of
https://github.com/EchoSixHIYA/WebSpeak-client-for-TeamSpeak.git
synced 2026-12-02 19:42:51 +08:00
refactor: extract settings dialogs and guard pending feedback
This commit is contained in:
1 parent
83b2430d18
commit
d2caaa8f3c
13 files changed
+497
-155
No files matched your search
@@ -0,0 +1,32 @@
|
||||
import { onMounted, onUnmounted, type Ref } from "vue";
|
||||
|
||||
export function useDialogFocus(dialog: Ref<HTMLElement | null>, close: () => void, fallbackFocus?: () => HTMLElement | null) {
|
||||
let previousFocus: HTMLElement | null = null;
|
||||
onMounted(() => {
|
||||
previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
const autofocus = dialog.value?.querySelector<HTMLElement>("[autofocus]:not(:disabled)");
|
||||
(autofocus ?? dialog.value)?.focus();
|
||||
});
|
||||
onUnmounted(() => {
|
||||
const target = previousFocus?.isConnected && previousFocus !== document.body ? previousFocus : fallbackFocus?.();
|
||||
target?.focus();
|
||||
});
|
||||
|
||||
function onDialogKeydown(event: KeyboardEvent): void {
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
close();
|
||||
} else if (event.key === "Tab" && dialog.value) {
|
||||
const controls = Array.from(dialog.value.querySelectorAll<HTMLElement>(
|
||||
'button:not(:disabled), input:not(:disabled), select:not(:disabled), textarea:not(:disabled), a[href], audio[controls], [tabindex="0"]',
|
||||
)).filter(element => element.getClientRects().length && getComputedStyle(element).visibility !== "hidden");
|
||||
const current = controls.indexOf(document.activeElement as HTMLElement);
|
||||
if (!controls.length || current < 0 || (event.shiftKey ? current === 0 : current === controls.length - 1)) {
|
||||
event.preventDefault();
|
||||
(event.shiftKey ? controls.at(-1) : controls[0])?.focus();
|
||||
}
|
||||
}
|
||||
}
|
||||
return { onDialogKeydown };
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { computed, ref, watch, type Ref } from "vue";
|
||||
import { computed, onScopeDispose, ref, watch, type Ref } from "vue";
|
||||
|
||||
interface UseWebClientAudioControlsOptions {
|
||||
settingsOpen: Ref<boolean>;
|
||||
@@ -62,6 +62,15 @@ export function useWebClientAudioControls({
|
||||
const settingsError = ref("");
|
||||
const whisperPttActive = ref(false);
|
||||
const micMeterBars = computed(() => Math.round(micLevel.value * 24));
|
||||
let settingsGeneration = 0;
|
||||
let settingsRequest = 0;
|
||||
|
||||
function beginSettingsRequest(): () => boolean {
|
||||
settingsError.value = "";
|
||||
const generation = settingsGeneration;
|
||||
const request = ++settingsRequest;
|
||||
return () => settingsOpen.value && generation === settingsGeneration && request === settingsRequest;
|
||||
}
|
||||
|
||||
function microphoneErrorMessage(error: unknown, fallback = "请检查浏览器权限"): string {
|
||||
const name = error instanceof DOMException ? error.name : "";
|
||||
@@ -96,30 +105,30 @@ export function useWebClientAudioControls({
|
||||
}
|
||||
|
||||
async function onInputDeviceChange(event: Event): Promise<void> {
|
||||
settingsError.value = "";
|
||||
const isCurrent = beginSettingsRequest();
|
||||
try {
|
||||
await setInputDevice((event.target as HTMLSelectElement).value);
|
||||
} catch (error: unknown) {
|
||||
settingsError.value = microphoneErrorMessage(error, "无法切换麦克风");
|
||||
if (isCurrent()) settingsError.value = microphoneErrorMessage(error, "无法切换麦克风");
|
||||
}
|
||||
}
|
||||
|
||||
async function onOutputDeviceChange(event: Event): Promise<void> {
|
||||
settingsError.value = "";
|
||||
const isCurrent = beginSettingsRequest();
|
||||
try {
|
||||
await setOutputDevice((event.target as HTMLSelectElement).value);
|
||||
} catch (error: unknown) {
|
||||
settingsError.value = localizedMessage(error instanceof Error ? error.message : "无法切换扬声器");
|
||||
if (isCurrent()) settingsError.value = localizedMessage(error instanceof Error ? error.message : "无法切换扬声器");
|
||||
}
|
||||
}
|
||||
|
||||
async function toggleMicTest(): Promise<void> {
|
||||
settingsError.value = "";
|
||||
const isCurrent = beginSettingsRequest();
|
||||
try {
|
||||
if (microphoneTestActive.value) stopMicrophoneTest();
|
||||
else await startMicrophoneTest();
|
||||
} catch (error: unknown) {
|
||||
settingsError.value = microphoneErrorMessage(error);
|
||||
if (isCurrent()) settingsError.value = microphoneErrorMessage(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -178,14 +187,19 @@ export function useWebClientAudioControls({
|
||||
}
|
||||
|
||||
watch(settingsOpen, (open) => {
|
||||
settingsGeneration++;
|
||||
if (open) {
|
||||
settingsError.value = "";
|
||||
const isCurrent = beginSettingsRequest();
|
||||
prepareInputDevices().catch((error: unknown) => {
|
||||
settingsError.value = microphoneErrorMessage(error);
|
||||
if (isCurrent()) settingsError.value = microphoneErrorMessage(error);
|
||||
});
|
||||
} else {
|
||||
stopMicrophoneTest();
|
||||
}
|
||||
}, { flush: "sync" });
|
||||
onScopeDispose(() => {
|
||||
settingsGeneration++;
|
||||
stopMicrophoneTest();
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@@ -22,6 +22,7 @@ interface UseWebClientConnectionOptions {
|
||||
accessMode: Readonly<Ref<string>>;
|
||||
isConnecting: Readonly<Ref<boolean>>;
|
||||
errorCode: Readonly<Ref<string>>;
|
||||
errorMessage: Readonly<Ref<string>>;
|
||||
channelSwitchedChannelId: Readonly<Ref<string>>;
|
||||
nickname: Ref<string>;
|
||||
channelName: Ref<string>;
|
||||
@@ -52,6 +53,7 @@ export function useWebClientConnection({
|
||||
accessMode,
|
||||
isConnecting,
|
||||
errorCode,
|
||||
errorMessage,
|
||||
channelSwitchedChannelId,
|
||||
nickname,
|
||||
channelName,
|
||||
@@ -140,7 +142,7 @@ export function useWebClientConnection({
|
||||
}
|
||||
|
||||
function submitChannelPassword(): void {
|
||||
if (!channelPasswordDialog.open || !channelPasswordDialog.channelId || !channelPasswordDialog.password) return;
|
||||
if (!channelPasswordDialog.open || channelPasswordDialog.submitting || !channelPasswordDialog.channelId || !channelPasswordDialog.password) return;
|
||||
channelPasswordDialog.error = "";
|
||||
channelPasswordDialog.submitting = true;
|
||||
switchChannel(channelPasswordDialog.channelId, channelPasswordDialog.password);
|
||||
@@ -163,6 +165,12 @@ export function useWebClientConnection({
|
||||
}
|
||||
|
||||
watch(errorCode, (code) => {
|
||||
if (code && code !== "CHANNEL_PASSWORD_REQUIRED" && channelPasswordDialog.open && channelPasswordDialog.submitting) {
|
||||
channelPasswordDialog.error = errorMessage.value;
|
||||
channelPasswordDialog.submitting = false;
|
||||
clearError();
|
||||
return;
|
||||
}
|
||||
if (code !== "CHANNEL_PASSWORD_REQUIRED" || !selectedChannelId.value) return;
|
||||
channelPasswordDialog.open = true;
|
||||
channelPasswordDialog.channelId = selectedChannelId.value;
|
||||
|
||||
Reference in new issue
Block a user