fix: bind skin loading to the current page and choice

This commit is contained in:
illusia committed 2026-10-01 17:00:05 +08:00
1 parent 15e22d3099
commit 9dfded0fb7
13 files changed
+639 -195

No files matched your search

+8 -52
View File
@@ -32,15 +32,14 @@
</template>
<script setup lang="ts">
import { computed, onMounted, ref, shallowRef } from "vue";
import { computed, onMounted, ref } from "vue";
import Icon from "../components/Icon.vue";
import LanguageSwitcher from "../components/LanguageSwitcher.vue";
import SkinSwitcher, { type SkinOption } from "../components/SkinSwitcher.vue";
import { listInstalledSkins, loadLocalPreferences, saveLocalPreferences } from "../services/local-persistence.js";
import { BUILTIN_ILLUSIA_SKIN_ID, getPublicDefaultSkinId, isPublicSkinEnabled, listPublicSkins, type SkinCatalogEntry } from "../services/skin-catalog.js";
import { activateSkin, BUILTIN_DARK_SKIN, BUILTIN_LIGHT_SKIN, getStoredSkinId } from "../services/skin-runtime.js";
import type { InstalledSkin } from "../services/skin-pack.js";
import { getStoredTheme, isDarkTheme } from "../services/theme.js";
import { saveLocalPreferences } from "../services/local-persistence.js";
import { BUILTIN_ILLUSIA_SKIN_ID, isPublicSkinEnabled } from "../services/skin-catalog.js";
import { BUILTIN_DARK_SKIN, BUILTIN_LIGHT_SKIN } from "../services/skin-runtime.js";
import { usePublicSkin } from "../composables/usePublicSkin.js";
type Language = "zh" | "en" | "de" | "ru" | "ja";
const illusiaSkinLabels: Record<Language, string> = { zh: "ILLUSIA 风", en: "ILLUSIA style", de: "ILLUSIA-Stil", ru: "Стиль ILLUSIA", ja: "ILLUSIA スタイル" };
@@ -49,12 +48,8 @@ type Tab = "channel" | "server";
const storedLanguage = localStorage.getItem("webspeak:language");
const language = ref<Language>(storedLanguage === "en" || storedLanguage === "de" || storedLanguage === "ru" || storedLanguage === "ja" ? storedLanguage : "zh");
const baseCopy = computed(() => language.value === "zh" ? zh : language.value === "de" ? de : language.value === "ru" ? ru : language.value === "ja" ? ja : en);
const activeSkin = shallowRef<InstalledSkin | null>(null);
const storedSkinId = getStoredSkinId();
const activeSkinId = ref(storedSkinId ?? (isDarkTheme(getStoredTheme()) ? BUILTIN_DARK_SKIN : BUILTIN_LIGHT_SKIN));
const skinReady = ref(storedSkinId === BUILTIN_LIGHT_SKIN || storedSkinId === BUILTIN_DARK_SKIN);
const installedSkins = ref<InstalledSkin[]>([]);
const catalogSkins = ref<SkinCatalogEntry[]>([]);
const { activeSkin, activeSkinId, skinReady, installedSkins, catalogSkins,
select: onSkinChange, initialize: initializeSkin } = usePublicSkin();
const skinOptions = computed<SkinOption[]>(() => [
...catalogSkins.value.map((skin) => ({
value: skin.id,
@@ -103,46 +98,7 @@ const selectedChannel = computed(() => channels.find((channel) => channel.id ===
const visibleMessages = computed(() => activeTab.value === "channel" ? messages.value : []);
function persistLanguage() { localStorage.setItem("webspeak:language", language.value); void saveLocalPreferences({ schemaVersion: 1, language: language.value }); }
async function onSkinChange(skinId: string) {
localStorage.setItem("webspeak:skin-choice", skinId);
const catalogSkin = catalogSkins.value.find((skin) => skin.id === skinId);
activeSkin.value = await activateSkin(skinId, catalogSkin?.version);
activeSkinId.value = getStoredSkinId() ?? skinId;
void saveLocalPreferences({ schemaVersion: 1, skinId: activeSkinId.value });
}
onMounted(async () => {
const catalogPromise = listPublicSkins().catch(() => []);
try {
const [preferences, installed, available] = await Promise.all([loadLocalPreferences(), listInstalledSkins(), catalogPromise]);
installedSkins.value = installed;
catalogSkins.value = available;
// Only a deliberate choice should override the instance default. The active
// skin and local preference also contain automatically applied defaults.
const savedSkinId = localStorage.getItem("webspeak:skin-choice");
const selectedId = savedSkinId && isPublicSkinEnabled(savedSkinId) ? savedSkinId : getPublicDefaultSkinId();
const selectedSkin = available.find((skin) => skin.id === selectedId);
activeSkinId.value = selectedId;
activeSkin.value = await activateSkin(selectedId, selectedSkin?.version);
activeSkinId.value = getStoredSkinId() ?? selectedId;
} catch {
activeSkin.value = null;
activeSkinId.value = isDarkTheme(getStoredTheme()) ? BUILTIN_DARK_SKIN : BUILTIN_LIGHT_SKIN;
await activateSkin(activeSkinId.value).catch(() => undefined);
} finally {
skinReady.value = true;
}
void catalogPromise.then(async (available) => {
catalogSkins.value = available;
const selected = available.find((skin) => skin.id === activeSkinId.value);
const installed = installedSkins.value.find((skin) => skin.id === activeSkinId.value);
if (!selected || !installed || installed.version === selected.version) return;
activeSkin.value = await activateSkin(selected.id, selected.version);
if (getStoredSkinId() !== selected.id) return;
activeSkinId.value = getStoredSkinId() ?? selected.id;
installedSkins.value = await listInstalledSkins();
}).catch(() => undefined);
});
onMounted(() => { void initializeSkin(); });
function selectChannel(id: string) { selectedChannelId.value = id; activeTab.value = "channel"; }
function toggleSpeaking() { speakingId.value = speakingId.value ? "" : selectedChannel.value.members[0]?.id ?? ""; }
function messageText(message: DemoMessage): string { return language.value === "zh" ? message.zhText ?? message.text : language.value === "ru" ? message.ruText ?? message.enText ?? message.text : language.value === "ja" ? message.jaText ?? message.enText ?? message.text : message.enText ?? message.text; }
+8 -84
View File
@@ -389,6 +389,7 @@ import { computed, onMounted, onUnmounted, reactive, ref, shallowRef, watch } fr
import Icon from "../components/Icon.vue";
import WebClientHeader from "../components/web-client/WebClientHeader.vue";
import IdentityImportDialog from "../components/web-client/IdentityImportDialog.vue";
import { usePublicSkin } from "../composables/usePublicSkin.js";
import { useWebClientIdentity } from "../composables/useWebClientIdentity.js";
import LanguageSwitcher from "../components/LanguageSwitcher.vue";
import SkinSwitcher, { type SkinOption } from "../components/SkinSwitcher.vue";
@@ -404,10 +405,10 @@ import { useWebClientI18n } from "../composables/useWebClientI18n.js";
import { useWebClientPublicConfig } from "../composables/useWebClientPublicConfig.js";
import { useWebClientServerHistory } from "../composables/useWebClientServerHistory.js";
import { getInitialLanguage, type Language } from "../i18n/web-client.js";
import { clearLocalData as clearStoredLocalData, isLocalPersistenceAvailable, listInstalledSkins, loadLocalPreferences, loadStoredIdentity, removeStoredIdentity, saveLocalPreferences, saveStoredIdentity } from "../services/local-persistence.js";
import { clearLocalData as clearStoredLocalData, isLocalPersistenceAvailable, loadLocalPreferences, loadStoredIdentity, removeStoredIdentity, saveLocalPreferences, saveStoredIdentity } from "../services/local-persistence.js";
import type { InstalledSkin, SkinHomeCopy } from "../services/skin-pack.js";
import { getPublicDefaultSkinId, isPublicSkinEnabled, listPublicSkins, type SkinCatalogEntry } from "../services/skin-catalog.js";
import { activateSkin, BUILTIN_DARK_SKIN, BUILTIN_LIGHT_SKIN, clearCustomSkinStyle, getStoredSkinId } from "../services/skin-runtime.js";
import { isPublicSkinEnabled } from "../services/skin-catalog.js";
import { BUILTIN_DARK_SKIN, BUILTIN_LIGHT_SKIN } from "../services/skin-runtime.js";
import { applyTheme, getStoredTheme, isDarkTheme, saveTheme, type ThemeMode } from "../services/theme.js";
import { DEFAULT_TEAM_SPEAK_PORT, splitTeamSpeakTarget } from "../services/teamspeak-target.js";
@@ -585,11 +586,8 @@ function resolveSkinMode(theme: ThemeMode): SkinMode {
const themeMode = ref<SkinMode>(resolveSkinMode(getStoredTheme()));
applyTheme(themeMode.value);
if (localStorage.getItem("webspeak:theme") === "system") saveTheme(themeMode.value);
const storedSkinId = getStoredSkinId();
const activeSkinId = ref(storedSkinId ?? (themeMode.value === "dark" ? BUILTIN_DARK_SKIN : BUILTIN_LIGHT_SKIN));
const skinReady = ref(storedSkinId === BUILTIN_LIGHT_SKIN || storedSkinId === BUILTIN_DARK_SKIN);
const installedSkins = ref<InstalledSkin[]>([]);
const catalogSkins = ref<SkinCatalogEntry[]>([]);
const publicSkin = usePublicSkin({ activeSkin, themeMode, appVersion: () => appVersion.value });
const { activeSkinId, skinReady, installedSkins, catalogSkins, select: onSkinChange, initialize: initializeSkin } = publicSkin;
const skinOptions = computed<SkinOption[]>(() => [
...catalogSkins.value.map((skin) => ({
value: skin.id,
@@ -685,75 +683,6 @@ function persistLanguage() {
void saveLocalPreferences({ schemaVersion: 1, language: language.value });
}
async function onSkinChange(skinId: string) {
localStorage.setItem("webspeak:skin-choice", skinId);
activeSkinId.value = skinId;
const catalogSkin = catalogSkins.value.find((skin) => skin.id === skinId);
activeSkin.value = await activateSkin(skinId, catalogSkin?.version, appVersion.value);
activeSkinId.value = getStoredSkinId() ?? skinId;
if (skinId === BUILTIN_LIGHT_SKIN) themeMode.value = "light";
else if (skinId === BUILTIN_DARK_SKIN) themeMode.value = "dark";
else themeMode.value = resolveSkinMode(getStoredTheme());
void saveLocalPreferences({ schemaVersion: 1, theme: themeMode.value, skinId: activeSkinId.value });
}
async function initializeSkin(): Promise<void> {
const catalogPromise = listPublicSkins().catch(() => []);
try {
const [preferences, skins, availableSkins] = await Promise.all([loadLocalPreferences(), listInstalledSkins(), catalogPromise]);
installedSkins.value = skins;
catalogSkins.value = availableSkins;
if (!localStorage.getItem("webspeak:theme")) {
if (preferences.theme === "system" || preferences.theme === "light" || preferences.theme === "dark") {
themeMode.value = resolveSkinMode(preferences.theme);
}
saveTheme(themeMode.value);
}
// Only a deliberate choice should override the instance default. The active
// skin and local preference also contain automatically applied defaults.
const savedSkinId = localStorage.getItem("webspeak:skin-choice");
const preferredSkinId = savedSkinId && isPublicSkinEnabled(savedSkinId)
? savedSkinId
: getPublicDefaultSkinId();
const preferredSkin = availableSkins.find((skin) => skin.id === preferredSkinId);
activeSkinId.value = preferredSkinId;
// Apply the locally cached package (or fetch it if absent) before revealing
// the page. The network catalog/version check must not block the first paint.
activeSkin.value = await activateSkin(preferredSkinId, preferredSkin?.version, appVersion.value);
activeSkinId.value = getStoredSkinId() ?? preferredSkinId;
if (activeSkinId.value === BUILTIN_LIGHT_SKIN) themeMode.value = "light";
else if (activeSkinId.value === BUILTIN_DARK_SKIN) themeMode.value = "dark";
else themeMode.value = resolveSkinMode(getStoredTheme());
void saveLocalPreferences({ schemaVersion: 1, theme: themeMode.value, skinId: activeSkinId.value });
} catch {
// Storage or package loading can fail on restricted browsers. Do not leave
// the application hidden, and restore a usable built-in palette instead.
activeSkin.value = null;
activeSkinId.value = themeMode.value === "dark" ? BUILTIN_DARK_SKIN : BUILTIN_LIGHT_SKIN;
try {
await activateSkin(activeSkinId.value, undefined, appVersion.value);
} catch {
applyTheme(themeMode.value);
}
} finally {
// The active skin's CSS and asset URLs are installed before this becomes
// visible, so the built-in day/night palette is never shown in between.
skinReady.value = true;
}
void catalogPromise.then(async (availableSkins) => {
catalogSkins.value = availableSkins;
const selectedSkin = availableSkins.find((skin) => skin.id === activeSkinId.value);
const installedSkin = installedSkins.value.find((skin) => skin.id === activeSkinId.value);
if (!selectedSkin || !installedSkin || installedSkin.version === selectedSkin.version) return;
const updatedSkin = await activateSkin(selectedSkin.id, selectedSkin.version, appVersion.value);
if (getStoredSkinId() !== selectedSkin.id) return;
activeSkin.value = updatedSkin;
activeSkinId.value = getStoredSkinId() ?? selectedSkin.id;
installedSkins.value = await listInstalledSkins();
}).catch(() => undefined);
}
const screenShareIndicatorBars = [5, 10, 7, 12, 8, 10];
const {
playerElement: screenSharePlayerEl,
@@ -1032,15 +961,10 @@ function doShare() {
async function clearBrowserData(): Promise<void> {
if (!window.confirm(t("clearLocalDataConfirm"))) return;
resetIdentityOperations();
publicSkin.cancel();
await clearStoredLocalData();
for (const key of ["webspeak:nickname", "webspeak:language", "webspeak:theme", "webspeak:active-skin", "webspeak:skin-choice", "webspeak:input-device", "webspeak:output-device", "webspeak:remember-identity"]) localStorage.removeItem(key);
clearCustomSkinStyle();
activeSkin.value = null;
installedSkins.value = [];
catalogSkins.value = await listPublicSkins();
themeMode.value = resolveSkinMode("system");
applyTheme(themeMode.value);
activeSkinId.value = themeMode.value === "dark" ? BUILTIN_DARK_SKIN : BUILTIN_LIGHT_SKIN;
await publicSkin.reset();
identityMaterial.value = "";
rememberIdentity.value = false;
clearServerHistory();