From cfd6585828192128411aa675aead668c13805b6e Mon Sep 17 00:00:00 2001 From: ZiuChen <457353192@qq.com> Date: Wed, 7 Sep 2022 09:19:29 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=B6=E8=97=8F=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E8=AE=A1=E5=85=A5=E5=88=B0=E6=9D=A1=E6=95=B0?= =?UTF-8?q?=E9=99=90=E5=88=B6=E4=B8=AD=20=E4=B8=8D=E4=BC=9A=E8=A2=AB?= =?UTF-8?q?=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/preload.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/public/preload.js b/public/preload.js index 2beadc3..49d5d96 100644 --- a/public/preload.js +++ b/public/preload.js @@ -37,10 +37,12 @@ class DB { // 读取磁盘记录到内存 const dataBase = JSON.parse(data) this.dataBase = dataBase - // 将超过14天的数据删除 + // 将超过14天的数据删除 排除掉收藏 const now = new Date().getTime() const deleteTime = now - '\u0031\u0034' * '\u0032\u0034' * 60 * 60 * 1000 // unicode - this.dataBase.data = this.dataBase.data.filter((item) => item.updateTime > deleteTime) + this.dataBase.data = this.dataBase.data?.filter( + (item) => item.updateTime > deleteTime || item.collect + ) this.updateDataBaseLocal() } catch (err) { utools.showNotification('读取剪切板出错' + err) @@ -72,14 +74,16 @@ class DB { addItem(cItem) { this.dataBase.data.unshift(cItem) this.updateDataBase() - // unicode - if (this.dataBase.data.length > '\u0035\u0030\u0030') { - // 达到条数限制 - this.dataBase.data.pop() - // 仍然大于: 超出了不止一条 - if (this.dataBase.data.length > '\u0035\u0030\u0030') { - this.dataBase.data = this.dataBase.data.splice(0, 499) + const exceedCount = this.dataBase.data.length - '\u0035\u0030\u0030' + if (exceedCount > 0) { + // 达到条数限制 在收藏条数限制内遍历非收藏历史并删除 + // 所有被移除的 item都存入tempList + const tmpList = [] + for (let i = 0; i < exceedCount; i++) { + const item = this.dataBase.data.pop() + tmpList.push(item) } + tmpList.forEach((item) => !item.collect || this.dataBase.data.push(item)) // 收藏内容 重新入栈 } this.updateDataBaseLocal() } @@ -197,7 +201,7 @@ const focus = () => { const toTop = () => (document.scrollingElement.scrollTop = 0) const resetNav = () => document.querySelectorAll('.clip-switch-item')[0]?.click() -let timmer = watchClipboard(db, (item) => { +watchClipboard(db, (item) => { // 此函数不断执行 if (!item) return if (db.updateItemViaId(item.id)) {