diff --git a/src/renderer/src/plugins/shareStore.js b/src/renderer/src/plugins/shareStore.js index 0db8119..c4c2567 100644 --- a/src/renderer/src/plugins/shareStore.js +++ b/src/renderer/src/plugins/shareStore.js @@ -5,6 +5,8 @@ const isNode = typeof require !== 'undefined' // 是否支持node函数 const { ipcRenderer } = isNode?require('electron'):{} // app使用 import { sessionStore } from '@/utils/store' import CircularJSON from 'circular-json' +import _ from 'lodash' + // import { diff } from 'jsondiffpatch' // const Remote = isNode?require('@electron/remote'):{} // 远程模块 @@ -19,19 +21,14 @@ export function shareStorePlugin({store}) { const storeName = mutation.storeId // 用于多窗口共享(需要共享的状态名称) const names = ['tool'] - if (names.includes(storeName)) { - const { storeId: storeName, payload, events, type } = mutation // direct - // if (!Object.keys(payload).length) return - // if (type != 'direct' || !events || Array.isArray(events) || !events.key) return - stateSyncWatch(storeName, state) // 需要同步 - } + if (names.includes(storeName)) stateSyncWatch(storeName, state) // 需要同步 }) // 暴露方法-手动同步 store.stateSync = (storeName, key, value) => { const state = store.$state if (!storeName && !!key && !!value) stateSync(storeName, key, value, state) - else stateSyncAll(store, state) + else stateSyncAll(store) } // 暴露方法-发送当前状态-新窗口 store.stateSyncInit = wid => stateSyncInit(wid, store) @@ -58,21 +55,42 @@ function stateSync(storeName, key, value, state) { function stateSyncWatch(storeName, newState) { const oldState = sessionStore.store // 旧数据 const diffData = findDifferences(oldState, newState) - // console.log('state-change-diffData', diffData) + if(!_.keys(diffData).length) return // 没有变化就终止执行 + // 数据处理: 找出差异 + console.log('state-change-diffData', diffData) try { + let pinaValue = {} // store pina状态管理需要的数据格式 + // 数据转换处理 for(const key in diffData) { const value = diffData[key] || null const newValue = {} // 重新组装pinia需要的数据 {a:{b:1}} 这种 const keyArr = key.split('.') || [] keyArr.reduce((o,c,i)=>{o[c] = i === keyArr.length-1 ? value : {};return o[c]}, newValue) - const jsonStr = JSON.stringify(newValue) // 从新组装-json数据 - // 更新本地数据-session - // console.log('state-change-update:', key, value) - sessionStore.set(key, value) - // 通知主线程更新 - ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr) - // console.log('======',key, value, jsonStr ) + // 合并数据 loadsh _.merge() 函数 + _.merge(pinaValue, newValue) } + + // 数据处理: electron-store + const [key, value] = _.toPairs(pinaValue)[0] // 对象转换为数组 {a:1} toPairs [['a',1]] + // 无数据就终止执行 + if (!key || !value) return + + // 更新本地数据-session + // 直接获取当前最新值(整体更新),上面获取到value是差异值,并不能知道删除还是新增 + const newValAll = _.get(newState, key) + const oldValAll = sessionStore.get(key) + + // 没变化也终止执行 + if (_.isEqual(oldValAll, newValAll)) return + + // 更新本地数据-session + sessionStore.set(key, newValAll) + + // 数据处理: pina-store + const jsonStr = JSON.stringify(pinaValue) // 从新组装-json数据 + // 通知主线程更新 + ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr) + // console.log('======',key, value, jsonStr ) } catch (error) { console.log('state-change-error', error) } diff --git a/src/renderer/src/store/modules/tool.js b/src/renderer/src/store/modules/tool.js index 3f5c5d3..8c818cd 100644 --- a/src/renderer/src/store/modules/tool.js +++ b/src/renderer/src/store/modules/tool.js @@ -23,7 +23,7 @@ export const useToolState = defineStore('tool', { }), actions: { async resetDef() { // 重置数据-下课 - // this.model = 'select' // 悬浮球-当前模式 + this.model = 'select' // 悬浮球-当前模式 await sleep(50) // 休眠50ms this.showBoardAll = false // 全屏画板-是否显示 await sleep(50) // 休眠50ms diff --git a/src/renderer/src/views/desktop/index.vue b/src/renderer/src/views/desktop/index.vue index 679fc19..c92a5be 100644 --- a/src/renderer/src/views/desktop/index.vue +++ b/src/renderer/src/views/desktop/index.vue @@ -8,7 +8,7 @@
{{ item.name }}