Skip to content

Commit 595aae4

Browse files
committed
新增常用表情功能
1 parent 9221cb9 commit 595aae4

File tree

6 files changed

+152
-61
lines changed

6 files changed

+152
-61
lines changed

src/config/view.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ <h2>启用本地表情包</h2>
122122
<li class="vertical-list-item">
123123
<div>
124124
<h2>常用表情</h2>
125-
<span class="secondary-text">最多记录50个常用表情显示在顶部</span>
125+
<span class="secondary-text">最多记录20个常用表情显示在顶部</span>
126126
</div>
127127
<div class="switchCommonlyEmoticons q-switch">
128128
<span class="q-switch__handle"></span>

src/main.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { replaceArk } = require("./main_modules/replaceArk");
1616
const { debounce } = require("./main_modules/debounce");
1717
const { log } = require("./main_modules/log");
1818

19-
let mainMessage, recordMessageRecallIdList, messageRecallPath, messageRecallJson;
19+
let mainMessage, recordMessageRecallIdList, messageRecallPath, messageRecallJson, localEmoticonsPath;
2020

2121
const listenList = []; // 所有打开过的窗口对象
2222
const catchMsgList = new LimitedMap(2000); // 内存缓存消息记录-用于根据消息id获取撤回原始内容
@@ -30,7 +30,6 @@ let options, localEmoticonsConfig; // 配置数据
3030
function onLoad(plugin) {
3131
const pluginDataPath = plugin.path.data;
3232
const settingsPath = path.join(pluginDataPath, "settings.json");
33-
const localEmoticonsPath = path.join(pluginDataPath, "localEmoticonsConfig.json");
3433
const styleSassPath = path.join(plugin.path.plugin, "src/style.scss");
3534
const stylePath = path.join(plugin.path.plugin, "src/style.css");
3635
const globalScssPath = path.join(plugin.path.plugin, "src/global.scss");
@@ -39,6 +38,7 @@ function onLoad(plugin) {
3938
const settingPath = path.join(plugin.path.plugin, "src/config/view.css");
4039
messageRecallPath = path.join(pluginDataPath, "/messageRecall");
4140
messageRecallJson = path.join(pluginDataPath, "/messageRecall/latestRecallMessage.json");
41+
localEmoticonsPath = path.join(pluginDataPath, "localEmoticonsConfig.json");
4242

4343
// 初始化配置文件路径
4444
if (!fs.existsSync(pluginDataPath)) {
@@ -126,18 +126,11 @@ function onLoad(plugin) {
126126
log(
127127
"%c轻量工具箱已加载",
128128
"border-radius: 8px;padding:10px 20px;font-size:18px;background:linear-gradient(to right, #3f7fe8, #03ddf2);color:#fff;",
129-
plugin,
130129
);
131130

132131
// 监听本地表情包文件夹内的更新
133132
onUpdateEmoticons((emoticonsList) => {
134133
console.log("本地表情包更新", emoticonsList.length);
135-
if (options.localEmoticons.commonlyEmoticons) {
136-
emoticonsList.unshift({
137-
name: "常用表情",
138-
list: localEmoticonsConfig.commonlyEmoticons,
139-
});
140-
}
141134
globalBroadcast(listenList, "LiteLoader.lite_tools.updateEmoticons", emoticonsList);
142135
localEmoticonsList = emoticonsList;
143136
});
@@ -164,6 +157,7 @@ function onLoad(plugin) {
164157
// 返回本地表情包数据
165158
ipcMain.handle("LiteLoader.lite_tools.getLocalEmoticonsList", (event) => {
166159
log("返回本地表情包数据");
160+
globalBroadcast(listenList, "LiteLoader.lite_tools.updateLocalEmoticonsConfig", localEmoticonsConfig);
167161
return localEmoticonsList;
168162
});
169163

@@ -217,6 +211,12 @@ function onLoad(plugin) {
217211
resetCommonlyEmoticons(); // 重置常用表情
218212
loadEmoticons(opt.localEmoticons.localPath);
219213
}
214+
// 判断是否开启了常用表情
215+
if (opt.localEmoticons.commonlyEmoticons) {
216+
globalBroadcast(listenList, "LiteLoader.lite_tools.updateLocalEmoticonsConfig", localEmoticonsConfig);
217+
} else {
218+
globalBroadcast(listenList, "LiteLoader.lite_tools.updateLocalEmoticonsConfig", { commonlyEmoticons: [] });
219+
}
220220
}
221221
options = opt;
222222
fs.writeFileSync(settingsPath, JSON.stringify(options, null, 4));
@@ -234,6 +234,9 @@ function onLoad(plugin) {
234234
log("%c轻量工具箱 [渲染进程]: ", "background:#272829;color:#fff;", ...message);
235235
});
236236

237+
// 更新常用表情列表
238+
ipcMain.on("LiteLoader.lite_tools.addCommonlyEmoticons", addCommonlyEmoticons);
239+
237240
// 获取全局样式
238241
ipcMain.handle("LiteLoader.lite_tools.getGlobalStyle", (event) => {
239242
try {
@@ -331,7 +334,7 @@ function onBrowserWindowCreated(window, plugin) {
331334
// 监听页面加载完成事件
332335
window.webContents.on("did-stop-loading", () => {
333336
if (window.webContents.getURL().indexOf("#/main/message") !== -1) {
334-
log("捕获到主窗口", window);
337+
log("捕获到主窗口");
335338
mainMessage = window;
336339
}
337340
});
@@ -401,7 +404,7 @@ function onBrowserWindowCreated(window, plugin) {
401404
// 捕获消息列表
402405
const msgList = args[1]?.msgList;
403406
if (msgList && msgList.length) {
404-
log("解析到消息数据", msgList);
407+
log("解析到消息数据");
405408
// 遍历消息列表中的所有消息
406409
if (options.message.showMsgTime || options.message.convertMiniPrgmArk || options.message.preventMessageRecall) {
407410
msgList.forEach((msgItem, index) => {
@@ -630,7 +633,7 @@ function onBrowserWindowCreated(window, plugin) {
630633
}
631634
}
632635
}
633-
// 记录下可能会用到的时间名称
636+
// 记录下可能会用到的事件名称
634637

635638
// 视频加载完成事件
636639
// cmdName: "nodeIKernelMsgListener/onRichMediaDownloadComplete";
@@ -646,7 +649,28 @@ function onBrowserWindowCreated(window, plugin) {
646649

647650
// 重置常用表情列表
648651
function resetCommonlyEmoticons() {
652+
log("重置常用表情");
649653
localEmoticonsConfig.commonlyEmoticons = [];
654+
globalBroadcast(listenList, "LiteLoader.lite_tools.updateLocalEmoticonsConfig", localEmoticonsConfig);
655+
fs.writeFileSync(localEmoticonsPath, JSON.stringify(localEmoticonsConfig, null, 4));
656+
}
657+
// 增加常用表情
658+
function addCommonlyEmoticons(event, src) {
659+
if (!options.localEmoticons.commonlyEmoticons) {
660+
return;
661+
}
662+
log("更新常用表情", localEmoticonsPath);
663+
const newSet = new Set(localEmoticonsConfig.commonlyEmoticons);
664+
// 如果已经有这个表情了,则更新位置
665+
newSet.delete(src);
666+
localEmoticonsConfig.commonlyEmoticons = Array.from(newSet);
667+
localEmoticonsConfig.commonlyEmoticons.unshift(src);
668+
// 删除多余的值
669+
if (localEmoticonsConfig.commonlyEmoticons.length > 20) {
670+
localEmoticonsConfig.commonlyEmoticons.pop();
671+
}
672+
log("send2", localEmoticonsConfig);
673+
globalBroadcast(listenList, "LiteLoader.lite_tools.updateLocalEmoticonsConfig", localEmoticonsConfig);
650674
fs.writeFileSync(localEmoticonsPath, JSON.stringify(localEmoticonsConfig, null, 4));
651675
}
652676

src/main_modules/LimitedMap.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class LimitedMap {
2020
has(key) {
2121
return this.map.has(key);
2222
}
23+
toArray() {
24+
return Array.from(this.map);
25+
}
2326
delete(key) {
2427
const index = this.keys.indexOf(key);
2528
if (index !== -1) {

src/main_modules/test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { LimitedMap } = require("./LimitedMap");
2+
3+
const a = {
4+
test: new LimitedMap(50),
5+
};
6+
console.log(JSON.stringify(a));

src/preload.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ contextBridge.exposeInMainWorld("lite_tools", {
1717
onMessageRecall: (callback) => ipcRenderer.on("LiteLoader.lite_tools.onMessageRecall", callback),
1818
// 监听本地表情更新
1919
updateEmoticons: (callback) => ipcRenderer.on("LiteLoader.lite_tools.updateEmoticons", callback),
20+
// 监听常用表情列表更新
21+
updateLocalEmoticonsConfig: (callback) => ipcRenderer.on("LiteLoader.lite_tools.updateLocalEmoticonsConfig", callback),
2022
// 主动获取本地表情列表
2123
getLocalEmoticonsList: () => ipcRenderer.invoke("LiteLoader.lite_tools.getLocalEmoticonsList"),
2224
// 打开选择本地表情文件夹窗口
@@ -63,7 +65,7 @@ contextBridge.exposeInMainWorld("lite_tools", {
6365
commentElements: [],
6466
},
6567
undefined,
66-
]
68+
],
6769
);
6870
},
6971
// 消息窗口向主进程发送输入框上方功能列表
@@ -78,4 +80,6 @@ contextBridge.exposeInMainWorld("lite_tools", {
7880
openWeb: (url) => ipcRenderer.send("LiteLoader.lite_tools.openWeb", url),
7981
// 在主进程的终端打印渲染进程日志
8082
log: (...msg) => ipcRenderer.send("LiteLoader.lite_tools.log", ...msg),
83+
// 更新常用表情列表
84+
addCommonlyEmoticons: (src) => ipcRenderer.send("LiteLoader.lite_tools.addCommonlyEmoticons", src),
8185
});

0 commit comments

Comments
 (0)