Files
chatroom/resources/js/chat-room/font-size.js
T

67 lines
1.7 KiB
JavaScript
Raw Normal View History

// 聊天室字号偏好控制,保留旧的 localStorage key 以兼容已有用户设置。
export const CHAT_FONT_SIZE_STORAGE_KEY = "chat_font_size";
2026-04-25 03:40:30 +08:00
let fontSizeEventsBound = false;
/**
* 应用字号到聊天消息窗口,并保存到 localStorage。
*
* @param {string|number} size 字号大小
* @returns {boolean}
*/
export function applyFontSize(size) {
const px = Number.parseInt(size, 10);
if (Number.isNaN(px) || px < 10 || px > 30) {
return false;
}
const publicContainer = document.getElementById("chat-messages-container");
const privateContainer = document.getElementById("chat-messages-container2");
if (publicContainer) {
publicContainer.style.fontSize = `${px}px`;
}
if (privateContainer) {
privateContainer.style.fontSize = `${px}px`;
}
localStorage.setItem(CHAT_FONT_SIZE_STORAGE_KEY, String(px));
const selector = document.getElementById("font_size_select");
if (selector) {
selector.value = String(px);
}
return true;
}
/**
* 从 localStorage 恢复已保存的聊天室字号。
*
* @returns {boolean}
*/
export function restoreChatFontSize() {
const saved = localStorage.getItem(CHAT_FONT_SIZE_STORAGE_KEY);
return saved ? applyFontSize(saved) : false;
}
2026-04-25 03:40:30 +08:00
/**
* 绑定字号选择器事件。
*
* @returns {void}
*/
export function bindChatFontSizeControl() {
if (fontSizeEventsBound || typeof document === "undefined") {
return;
}
fontSizeEventsBound = true;
document.addEventListener("change", (event) => {
if (!(event.target instanceof HTMLSelectElement) || event.target.id !== "font_size_select") {
return;
}
applyFontSize(event.target.value);
});
}