From e3acb7ce4e8533ec6d4d92f72c5b7ab90369d23b Mon Sep 17 00:00:00 2001 From: lyswhut Date: Fri, 17 Dec 2021 17:18:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E5=8A=9F=E8=83=BD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9=E5=88=97=E8=A1=A8=E9=A1=BA=E5=BA=8F=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E7=9A=84=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- publish/changeLog.md | 1 + src/main/modules/sync/server/syncList.js | 61 ++++++++++++++++++------ src/renderer/core/share/list.js | 7 ++- src/renderer/store/modules/list.js | 3 ++ 4 files changed, 56 insertions(+), 16 deletions(-) diff --git a/publish/changeLog.md b/publish/changeLog.md index 2e9f8eda..ea180727 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -11,6 +11,7 @@ - 优化列表性能,软件整体性能 - 调整Mac平台下的图标大小 +- 同步功能添加对列表顺序调整的控制,确保手动调整位置后的列表与不同的电脑同步时,列表位置不会被还原 ### 修复 diff --git a/src/main/modules/sync/server/syncList.js b/src/main/modules/sync/server/syncList.js index 6fa90328..3f5814c9 100644 --- a/src/main/modules/sync/server/syncList.js +++ b/src/main/modules/sync/server/syncList.js @@ -156,14 +156,26 @@ const mergeList = (sourceListData, targetListData) => { const userListDataObj = createUserListDataObj(sourceListData) newListData.userList = [...sourceListData.userList] - for (const list of targetListData.userList) { - const targetList = userListDataObj[list.id] - if (targetList) { - targetList.list = handleMergeList(targetList, list, addMusicLocationType).list + targetListData.userList.forEach((list, index) => { + const targetUpdateTime = list?.locationUpdateTime ?? 0 + const sourceList = userListDataObj[list.id] + if (sourceList) { + sourceList.list = handleMergeList(sourceList, list, addMusicLocationType).list + + const sourceUpdateTime = sourceList?.locationUpdateTime ?? 0 + if (targetUpdateTime >= sourceUpdateTime) return + // 调整位置 + const [newList] = newListData.userList.splice(newListData.userList.findIndex(l => l.id == list.id), 1) + newList.locationUpdateTime = targetUpdateTime + newListData.userList.splice(index, 0, newList) } else { - newListData.userList.push(list) + if (targetUpdateTime) { + newListData.userList.splice(index, 0, list) + } else { + newListData.userList.push(list) + } } - } + }) return newListData } @@ -175,11 +187,14 @@ const overwriteList = (sourceListData, targetListData) => { const userListDataObj = createUserListDataObj(sourceListData) newListData.userList = [...sourceListData.userList] - for (const list of targetListData.userList) { - const targetList = userListDataObj[list.id] - if (targetList) continue - newListData.userList.push(list) - } + targetListData.userList.forEach((list, index) => { + if (userListDataObj[list.id]) return + if (list?.locationUpdateTime) { + newListData.userList.splice(index, 0, list) + } else { + newListData.userList.push(list) + } + }) return newListData } @@ -340,10 +355,26 @@ const handleMergeListDataFromSnapshot = async(socket, snapshot) => { } newUserList.push(newList) } - for (const list of remoteListData.userList) { - if (removedListIds.has(list.id) || localUserListData[list.id]) continue - newUserList.push({ ...list }) - } + + remoteListData.userList.forEach((list, index) => { + if (removedListIds.has(list.id)) return + const remoteUpdateTime = list?.locationUpdateTime ?? 0 + if (localUserListData[list.id]) { + const localUpdateTime = localUserListData[list.id]?.locationUpdateTime ?? 0 + if (localUpdateTime >= remoteUpdateTime) return + // 调整位置 + const [newList] = newUserList.splice(newUserList.findIndex(l => l.id == list.id), 1) + newList.locationUpdateTime = localUpdateTime + newUserList.splice(index, 0, newList) + } else { + if (remoteUpdateTime) { + newUserList.splice(index, 0, { ...list }) + } else { + newUserList.push({ ...list }) + } + } + }) + newListData.userList = newUserList setLocalList(newListData) setRemotelList(socket, newListData) diff --git a/src/renderer/core/share/list.js b/src/renderer/core/share/list.js index deeffb2c..a1a9c876 100644 --- a/src/renderer/core/share/list.js +++ b/src/renderer/core/share/list.js @@ -51,7 +51,7 @@ export const tempList = reactive({ name: '临时列表', }) -export const userLists = reactive([]) +export const userLists = window.userLists = reactive([]) export const addUserList = ({ name, @@ -60,6 +60,7 @@ export const addUserList = ({ source, sourceListId, position, + locationUpdateTime, }) => { if (position == null) { userLists.push({ @@ -67,6 +68,7 @@ export const addUserList = ({ id, source, sourceListId, + locationUpdateTime, }) } else { userLists.splice(position + 1, 0, { @@ -74,6 +76,7 @@ export const addUserList = ({ id, source, sourceListId, + locationUpdateTime, }) } allListUpdate(id, list) @@ -85,6 +88,7 @@ export const updateList = ({ list, source, sourceListId, + locationUpdateTime, }) => { let targetList switch (id) { @@ -97,6 +101,7 @@ export const updateList = ({ targetList.name = name targetList.source = source targetList.sourceListId = sourceListId + if (locationUpdateTime) targetList.locationUpdateTime = locationUpdateTime break } allListUpdate(id, list) diff --git a/src/renderer/store/modules/list.js b/src/renderer/store/modules/list.js index 6c9865ea..1e282a34 100644 --- a/src/renderer/store/modules/list.js +++ b/src/renderer/store/modules/list.js @@ -320,6 +320,7 @@ const mutations = { position, sourceListId, } + if (position) newList.locationUpdateTime = Date.now() addUserList(newList) } this.commit('list/listAddMultiple', { id, list, isSync: true }) @@ -364,6 +365,7 @@ const mutations = { let targetList = userLists[index] userLists.splice(index, 1) userLists.splice(index - 1, 0, targetList) + targetList.locationUpdateTime = Date.now() window.eventHub.emit(eventListNames.listChange, [id]) }, movedownUserList(state, { id, isSync }) { @@ -378,6 +380,7 @@ const mutations = { let targetList = userLists[index] userLists.splice(index, 1) userLists.splice(index + 1, 0, targetList) + targetList.locationUpdateTime = Date.now() window.eventHub.emit(eventListNames.listChange, [id]) }, setMusicPosition(state, { id, position, list, isSync }) {