1616
1717<script setup lang="ts">
1818import HeaderView from ' @r/views/header/header.vue'
19- import { onMounted , onUnmounted , ref , watch } from ' vue'
19+ import { onMounted , onUnmounted , ref , watch , nextTick } from ' vue'
2020import { ElMessage , ElNotification } from ' element-plus'
21- import { useDataStore } from ' ./stores/data'
21+ import { DataSet , useDataStore } from ' ./stores/data'
2222import { useProjectStore } from ' ./stores/project'
2323import { useWindowSize } from ' @vueuse/core'
2424import { useGlobalStart } from ' ./stores/runtime'
@@ -27,6 +27,7 @@ import { useDark } from '@vueuse/core'
2727import { VxeUI } from ' vxe-table'
2828import { bus } from ' wujie'
2929import log from ' electron-log'
30+ import { cloneDeep } from ' lodash'
3031const data = useDataStore ()
3132const project = useProjectStore ()
3233const pluginStore = usePluginStore ()
@@ -35,29 +36,25 @@ const globalStart = useGlobalStart()
3536const isDark = useDark ()
3637const params = ref <any >({})
3738
38- bus .$on (
39- ' update:modelValue' ,
40- ({ pluginId , id , data : val }: { pluginId: string ; id: string ; data: any }) => {
41- log .info (' plugin data update' , {
42- pluginId ,
43- id ,
44- val
45- })
46- if (pluginId == id ) {
47- // single data
48- data .pluginData [pluginId ] = val
49- } else {
50- // multi data
51- if (data .pluginData [pluginId ]) {
52- data .pluginData [pluginId ][id ] = val
53- } else {
54- data .pluginData [pluginId ] = {
55- [id ]: val
56- }
57- }
58- }
39+ let isExternalUpdate = false
40+ // 监听插件对 store 的修改,同步到主应用
41+ bus .$on (' update:dataStore' , (newStore : DataSet ) => {
42+ // 使用 $patch 批量更新 store(Pinia 推荐方式)
43+ isExternalUpdate = true
44+ data .$patch (() => {
45+ return newStore
46+ })
47+ })
48+
49+ // 监听主应用 store 的变化,同步到插件
50+ data .$subscribe ((mutation , state ) => {
51+ if (isExternalUpdate ) {
52+ isExternalUpdate = false
53+ return
5954 }
60- )
55+ console .log (' mainEmit' )
56+ bus .$emit (' update:dataStore:fromMain' , cloneDeep (data .getData ()))
57+ })
6158
6259// Watch for dark theme changes
6360watch (isDark , (value ) => {
0 commit comments