Skip to content

Commit 28c5198

Browse files
committed
🎨 Automatically apply code snippets after data synchronization #16736
Signed-off-by: Daniel <[email protected]>
1 parent aea5690 commit 28c5198

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

kernel/api/setting.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func setConfSnippet(c *gin.Context) {
7979
model.Conf.Save()
8080

8181
ret.Data = snippet
82-
util.BroadcastByType("main", "setSnippet", 0, "", snippet)
82+
model.PushReloadSnippet(snippet)
8383
}
8484

8585
func addVirtualBlockRefExclude(c *gin.Context) {

kernel/model/push_reload.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ import (
3131
"github.com/emirpasic/gods/sets/hashset"
3232
"github.com/siyuan-note/logging"
3333
"github.com/siyuan-note/siyuan/kernel/av"
34+
"github.com/siyuan-note/siyuan/kernel/conf"
3435
"github.com/siyuan-note/siyuan/kernel/filesys"
3536
"github.com/siyuan-note/siyuan/kernel/sql"
3637
"github.com/siyuan-note/siyuan/kernel/task"
3738
"github.com/siyuan-note/siyuan/kernel/treenode"
3839
"github.com/siyuan-note/siyuan/kernel/util"
3940
)
4041

42+
func PushReloadSnippet(snippet *conf.Snpt) {
43+
util.BroadcastByType("main", "setSnippet", 0, "", snippet)
44+
}
45+
4146
func PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginNameSet, uninstallPluginNameSet *hashset.Set, excludeApp string) {
4247
// 集合去重
4348
if nil != uninstallPluginNameSet {

kernel/model/repository.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
15951595
var upserts, removes []string
15961596
var upsertTrees int
15971597
// 可能需要重新加载部分功能
1598-
var needReloadFlashcard, needReloadOcrTexts, needReloadPlugin bool
1598+
var needReloadFlashcard, needReloadOcrTexts, needReloadPlugin, needReloadSnippet bool
15991599
upsertCodePluginSet := hashset.New() // 插件代码变更 data/plugins/
16001600
upsertDataPluginSet := hashset.New() // 插件存储数据变更 data/storage/petal/
16011601
needUnindexBoxes, needIndexBoxes := map[string]bool{}, map[string]bool{}
@@ -1640,6 +1640,10 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16401640
absPath := filepath.Join(util.DataDir, file.Path)
16411641
HandleAssetsChangeEvent(absPath)
16421642
}
1643+
1644+
if file.Path == "/snippets/conf.json" {
1645+
needReloadSnippet = true
1646+
}
16431647
}
16441648

16451649
removeWidgetDirSet, unloadPluginSet, uninstallPluginSet := hashset.New(), hashset.New(), hashset.New()
@@ -1686,6 +1690,10 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
16861690
absPath := filepath.Join(util.DataDir, file.Path)
16871691
HandleAssetsRemoveEvent(absPath)
16881692
}
1693+
1694+
if file.Path == "/snippets/conf.json" {
1695+
needReloadSnippet = true
1696+
}
16891697
}
16901698

16911699
for _, upsertPetal := range mergeResult.UpsertPetals {
@@ -1710,6 +1718,10 @@ func processSyncMergeResult(exit, byHand bool, mergeResult *dejavu.MergeResult,
17101718
PushReloadPlugin(upsertCodePluginSet, upsertDataPluginSet, unloadPluginSet, uninstallPluginSet, "")
17111719
}
17121720

1721+
if needReloadSnippet {
1722+
PushReloadSnippet(Conf.Snippet)
1723+
}
1724+
17131725
for _, widgetDir := range removeWidgetDirSet.Values() {
17141726
widgetDirPath := filepath.Join(util.DataDir, "widgets", widgetDir.(string))
17151727
gulu.File.RemoveEmptyDirs(widgetDirPath)

0 commit comments

Comments
 (0)