mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 14:40:12 +01:00
🎨 localStorage 支持在多界面实例间同步 https://github.com/siyuan-note/siyuan/issues/6965
This commit is contained in:
parent
3adb7d9ad0
commit
95bb18afe1
12 changed files with 49 additions and 55 deletions
|
|
@ -14,6 +14,7 @@ export class Model {
|
|||
/// #else
|
||||
// @ts-ignore
|
||||
public parent: any;
|
||||
|
||||
/// #endif
|
||||
|
||||
constructor(options: { id: string, type?: TWS, callback?: () => void, msgCallback?: (data: IWebSocketData) => void }) {
|
||||
|
|
@ -79,8 +80,13 @@ export class Model {
|
|||
cmd,
|
||||
reqId: this.reqId,
|
||||
param,
|
||||
// pushMode 0: 广播,1:单播(默认),2:广播(不包含自己)
|
||||
// reloadPushMode 是否需要 reload 0: 广播,1:单播(默认),2:广播(不包含自己),3:不推送
|
||||
// pushMode
|
||||
// 0: 所有应用所有会话广播
|
||||
// 1:自我应用会话单播
|
||||
// 2:非自我会话广播
|
||||
// 4:非自我应用所有会话广播
|
||||
// 5:单个应用内所有会话广播
|
||||
// 6:非自我应用主会话广播
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -288,7 +288,7 @@ func deleteBlock(c *gin.Context) {
|
|||
}
|
||||
|
||||
func broadcastTransactions(transactions []*model.Transaction) {
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast, util.PushModeBroadcast)
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast)
|
||||
evt.Data = transactions
|
||||
util.PushEvent(evt)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ func heading2Doc(c *gin.Context) {
|
|||
name := path.Base(targetPath)
|
||||
box := model.Conf.Box(targetNotebook)
|
||||
files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
|
||||
evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": box,
|
||||
"path": targetPath,
|
||||
|
|
@ -159,7 +159,7 @@ func li2Doc(c *gin.Context) {
|
|||
name := path.Base(targetPath)
|
||||
box := model.Conf.Box(targetNotebook)
|
||||
files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort)
|
||||
evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": box,
|
||||
"path": targetPath,
|
||||
|
|
@ -465,7 +465,7 @@ func createDailyNote(c *gin.Context) {
|
|||
if existed && "" != app {
|
||||
pushMode = util.PushModeBroadcastApp
|
||||
}
|
||||
evt := util.NewCmdResult("createdailynote", 0, pushMode, util.PushModeNone)
|
||||
evt := util.NewCmdResult("createdailynote", 0, pushMode)
|
||||
evt.AppId = app
|
||||
|
||||
name := path.Base(p)
|
||||
|
|
@ -750,7 +750,7 @@ func getDoc(c *gin.Context) {
|
|||
}
|
||||
|
||||
func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
|
||||
evt := util.NewCmdResult("create", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
|
||||
name := path.Base(p)
|
||||
files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
|
||||
evt.Data = map[string]interface{}{
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ func renameNotebook(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": notebook,
|
||||
"name": name,
|
||||
|
|
@ -101,7 +101,7 @@ func removeNotebook(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0)
|
||||
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": notebook,
|
||||
}
|
||||
|
|
@ -137,7 +137,7 @@ func createNotebook(c *gin.Context) {
|
|||
"notebook": model.Conf.Box(id),
|
||||
}
|
||||
|
||||
evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("createnotebook", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": model.Conf.Box(id),
|
||||
"existed": existed,
|
||||
|
|
@ -164,7 +164,7 @@ func openNotebook(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": model.Conf.Box(notebook),
|
||||
"existed": existed,
|
||||
|
|
|
|||
|
|
@ -196,7 +196,7 @@ func setLocalStorage(c *gin.Context) {
|
|||
}
|
||||
|
||||
app := arg["app"].(string)
|
||||
evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp, util.PushModeBroadcastMainExcludeSelfApp)
|
||||
evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp)
|
||||
evt.AppId = app
|
||||
evt.Data = val
|
||||
util.PushEvent(evt)
|
||||
|
|
|
|||
|
|
@ -93,7 +93,7 @@ func performTransactions(c *gin.Context) {
|
|||
}
|
||||
|
||||
func pushTransactions(app, session string, transactions []*model.Transaction) {
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcastExcludeSelf, util.PushModeBroadcastExcludeSelf)
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcastExcludeSelf)
|
||||
evt.AppId = app
|
||||
evt.SessionId = session
|
||||
evt.Data = transactions
|
||||
|
|
|
|||
|
|
@ -64,11 +64,7 @@ func NewCommand(cmdStr string, cmdId float64, param map[string]interface{}, sess
|
|||
if pushModeParam := param["pushMode"]; nil != pushModeParam {
|
||||
pushMode = util.PushMode(pushModeParam.(float64))
|
||||
}
|
||||
reloadPushMode := util.PushModeSingleSelf
|
||||
if reloadPushModeParam := param["reloadPushMode"]; nil != reloadPushModeParam {
|
||||
reloadPushMode = util.PushMode(reloadPushModeParam.(float64))
|
||||
}
|
||||
baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode, reloadPushMode)
|
||||
baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode)
|
||||
appId, _ := baseCmd.session.Get("app")
|
||||
baseCmd.PushPayload.AppId = appId.(string)
|
||||
sid, _ := baseCmd.session.Get("id")
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ func SetBlockAttrs(id string, nameValues map[string]string) (err error) {
|
|||
}
|
||||
|
||||
func pushBroadcastAttrTransactions(transactions []*Transaction) {
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast, util.PushModeBroadcast)
|
||||
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast)
|
||||
evt.Data = transactions
|
||||
util.PushEvent(evt)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1172,7 +1172,7 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string) (newPath
|
|||
moveSorts(tree.ID, fromBox.ID, toBox.ID)
|
||||
}
|
||||
|
||||
evt := util.NewCmdResult("moveDoc", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("moveDoc", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"fromNotebook": fromBox.ID,
|
||||
"fromPath": fromPath,
|
||||
|
|
@ -1275,7 +1275,7 @@ func removeDoc(box *Box, p string) (err error) {
|
|||
|
||||
cache.RemoveDocIAL(p)
|
||||
|
||||
evt := util.NewCmdResult("removeDoc", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("removeDoc", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"ids": removeIDs,
|
||||
}
|
||||
|
|
@ -1321,7 +1321,7 @@ func RenameDoc(boxID, p, title string) (err error) {
|
|||
}
|
||||
|
||||
refText := getNodeRefText(tree.Root)
|
||||
evt := util.NewCmdResult("rename", 0, util.PushModeBroadcast, util.PushModeNone)
|
||||
evt := util.NewCmdResult("rename", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": boxID,
|
||||
"id": tree.Root.ID,
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ func Unmount(boxID string) {
|
|||
WaitForWritingFiles()
|
||||
|
||||
unmount0(boxID)
|
||||
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0)
|
||||
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"box": boxID,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,45 +24,41 @@ import (
|
|||
type PushMode int
|
||||
|
||||
const (
|
||||
PushModeBroadcast PushMode = 0 // 所有应用所有会话广播
|
||||
PushModeSingleSelf PushMode = 1 // 自我应用会话单播
|
||||
PushModeBroadcastExcludeSelf PushMode = 2 // 非自我会话广播
|
||||
PushModeBroadcastExcludeSelfApp PushMode = 4 // 非自我应用所有会话广播
|
||||
PushModeBroadcastApp PushMode = 5 // 单个应用内所有会话广播
|
||||
PushModeBroadcastMainExcludeSelfApp PushMode = 6 // 非自我应用主会话广播
|
||||
PushModeNone PushMode = 10 // 不进行 reload
|
||||
PushModeBroadcast PushMode = 0 // 所有应用所有会话广播
|
||||
PushModeSingleSelf PushMode = 1 // 自我应用会话单播
|
||||
PushModeBroadcastExcludeSelf PushMode = 2 // 非自我会话广播
|
||||
PushModeBroadcastExcludeSelfApp PushMode = 4 // 非自我应用所有会话广播
|
||||
PushModeBroadcastApp PushMode = 5 // 单个应用内所有会话广播
|
||||
PushModeBroadcastMainExcludeSelfApp PushMode = 6 // 非自我应用主会话广播
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
Cmd string `json:"cmd"`
|
||||
ReqId float64 `json:"reqId"`
|
||||
AppId string `json:"app"`
|
||||
SessionId string `json:"sid"`
|
||||
PushMode PushMode `json:"pushMode"`
|
||||
ReloadPushMode PushMode `json:"reloadPushMode"`
|
||||
Callback interface{} `json:"callback"`
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
Cmd string `json:"cmd"`
|
||||
ReqId float64 `json:"reqId"`
|
||||
AppId string `json:"app"`
|
||||
SessionId string `json:"sid"`
|
||||
PushMode PushMode `json:"pushMode"`
|
||||
Callback interface{} `json:"callback"`
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data interface{} `json:"data"`
|
||||
}
|
||||
|
||||
func NewResult() *Result {
|
||||
return &Result{Cmd: "",
|
||||
ReqId: 0,
|
||||
PushMode: 0,
|
||||
ReloadPushMode: 0,
|
||||
Callback: "",
|
||||
Code: 0,
|
||||
Msg: "",
|
||||
Data: nil}
|
||||
ReqId: 0,
|
||||
PushMode: 0,
|
||||
Callback: "",
|
||||
Code: 0,
|
||||
Msg: "",
|
||||
Data: nil}
|
||||
}
|
||||
|
||||
func NewCmdResult(cmdName string, cmdId float64, pushMode, reloadPushMode PushMode) *Result {
|
||||
func NewCmdResult(cmdName string, cmdId float64, pushMode PushMode) *Result {
|
||||
ret := NewResult()
|
||||
ret.Cmd = cmdName
|
||||
ret.ReqId = cmdId
|
||||
ret.PushMode = pushMode
|
||||
ret.ReloadPushMode = reloadPushMode
|
||||
return ret
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ func ClosePushChan(id string) {
|
|||
}
|
||||
|
||||
func ReloadUI() {
|
||||
evt := NewCmdResult("reloadui", 0, PushModeBroadcast, 0)
|
||||
evt := NewCmdResult("reloadui", 0, PushModeBroadcast)
|
||||
PushEvent(evt)
|
||||
}
|
||||
|
||||
|
|
@ -208,7 +208,7 @@ func PushClearProgress() {
|
|||
}
|
||||
|
||||
func PushDownloadProgress(id string, percent float32) {
|
||||
evt := NewCmdResult("downloadProgress", 0, PushModeBroadcast, 0)
|
||||
evt := NewCmdResult("downloadProgress", 0, PushModeBroadcast)
|
||||
evt.Data = map[string]interface{}{
|
||||
"id": id,
|
||||
"percent": percent,
|
||||
|
|
@ -219,9 +219,6 @@ func PushDownloadProgress(id string, percent float32) {
|
|||
func PushEvent(event *Result) {
|
||||
msg := event.Bytes()
|
||||
mode := event.PushMode
|
||||
if "reload" == event.Cmd {
|
||||
mode = event.ReloadPushMode
|
||||
}
|
||||
switch mode {
|
||||
case PushModeBroadcast:
|
||||
Broadcast(msg)
|
||||
|
|
@ -235,7 +232,6 @@ func PushEvent(event *Result) {
|
|||
broadcastApp(msg, event.AppId)
|
||||
case PushModeBroadcastMainExcludeSelfApp:
|
||||
broadcastOtherAppMains(msg, event.AppId)
|
||||
case PushModeNone:
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue