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

46 lines
1.2 KiB
JavaScript

// 聊天室字号偏好控制,保留旧的 localStorage key 以兼容已有用户设置。
export const CHAT_FONT_SIZE_STORAGE_KEY = "chat_font_size";
/**
* 应用字号到聊天消息窗口,并保存到 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;
}