/**
* 朋友圈模块
* 处理朋友圈页面的显示和交互逻辑
* - 每个联系人有独立的朋友圈
* - 评论来自角色世界书中的人物
* - 用户评论后角色会回复
*/
import { saveSettingsDebounced } from '../../../../script.js';
import { getContext } from '../../../extensions.js';
import { getSettings } from './config.js';
import { showToast, showNotificationBanner } from './toast.js';
import { sleep } from './utils.js';
// 当前正在查看的联系人索引
let currentContactIndex = null;
let currentMomentId = null;
let currentReplyTo = null; // 当前回复的评论者名称
// 消息计数器(用于保底机制)
let messageCounters = {};
/**
* 初始化朋友圈模块
*/
export function initMoments() {
const settings = getSettings();
// 初始化朋友圈数据结构
if (!settings.momentsData) {
settings.momentsData = {};
}
// 绑定事件
bindMomentsEvents();
console.log('[可乐] 朋友圈模块初始化完成');
}
/**
* 绑定朋友圈相关事件
*/
function bindMomentsEvents() {
// 返回按钮
const backBtn = document.getElementById('wechat-moments-back-btn');
if (backBtn) {
backBtn.addEventListener('click', closeMomentsPage);
}
// 相机按钮 - 用户发自己的朋友圈
const cameraBtn = document.getElementById('wechat-moments-camera-btn');
if (cameraBtn) {
cameraBtn.addEventListener('click', () => {
showUserPostMomentModal();
});
}
// 封面点击更换
const cover = document.getElementById('wechat-moments-cover');
if (cover) {
cover.addEventListener('click', changeMomentsCover);
}
// 评论发送按钮
const commentSend = document.getElementById('wechat-moments-comment-send');
if (commentSend) {
commentSend.addEventListener('click', sendUserComment);
}
// 评论输入框回车发送
const commentInput = document.getElementById('wechat-moments-comment-text');
if (commentInput) {
commentInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
sendUserComment();
}
});
}
// 点击页面其他地方关闭弹窗
const momentsPage = document.getElementById('wechat-moments-page');
if (momentsPage) {
momentsPage.addEventListener('click', (e) => {
if (!e.target.closest('.wechat-moment-action-btn') &&
!e.target.closest('.wechat-moments-action-popup')) {
hideActionPopup();
}
});
}
}
/**
* 打开朋友圈页面(查看指定联系人的朋友圈)
* @param {number} contactIndex - 联系人索引,null 表示查看所有
*/
export function openMomentsPage(contactIndex = null) {
currentContactIndex = contactIndex;
const page = document.getElementById('wechat-moments-page');
if (page) {
page.classList.remove('hidden');
updateMomentsProfile(contactIndex);
renderMomentsList(contactIndex);
}
}
/**
* 关闭朋友圈页面
*/
export function closeMomentsPage() {
const page = document.getElementById('wechat-moments-page');
if (page) {
page.classList.add('hidden');
}
hideActionPopup();
hideCommentInput();
currentContactIndex = null;
}
/**
* 更新朋友圈用户资料显示
*/
function updateMomentsProfile(contactIndex) {
const settings = getSettings();
let userName, userAvatar, coverImage;
if (contactIndex !== null && settings.contacts[contactIndex]) {
// 显示特定联系人的信息
const contact = settings.contacts[contactIndex];
userName = contact.name || '未知';
userAvatar = contact.avatar;
coverImage = contact.momentsCover;
} else {
// 显示用户自己的信息
const context = getContext();
userName = context?.name1 || settings.wechatId || 'User';
userAvatar = settings.userAvatar;
coverImage = settings.momentsCover;
}
// 更新用户名
const usernameEl = document.getElementById('wechat-moments-username');
if (usernameEl) {
usernameEl.textContent = userName;
}
// 更新头像
const avatarEl = document.getElementById('wechat-moments-avatar');
if (avatarEl) {
if (userAvatar) {
avatarEl.innerHTML = ``;
} else {
const firstChar = userName.charAt(0) || '?';
avatarEl.innerHTML = `