🎨 localStorage 支持在多界面实例间同步 https://github.com/siyuan-note/siyuan/issues/6965

This commit is contained in:
Liang Ding 2023-01-01 14:09:36 +08:00
parent 3adb7d9ad0
commit 95bb18afe1
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
12 changed files with 49 additions and 55 deletions

View file

@ -14,6 +14,7 @@ export class Model {
/// #else /// #else
// @ts-ignore // @ts-ignore
public parent: any; public parent: any;
/// #endif /// #endif
constructor(options: { id: string, type?: TWS, callback?: () => void, msgCallback?: (data: IWebSocketData) => void }) { constructor(options: { id: string, type?: TWS, callback?: () => void, msgCallback?: (data: IWebSocketData) => void }) {
@ -79,8 +80,13 @@ export class Model {
cmd, cmd,
reqId: this.reqId, reqId: this.reqId,
param, param,
// pushMode 0: 广播1单播(默认)2广播不包含自己 // pushMode
// reloadPushMode 是否需要 reload 0: 广播1单播(默认)2广播不包含自己3不推送 // 0: 所有应用所有会话广播
// 1自我应用会话单播
// 2非自我会话广播
// 4非自我应用所有会话广播
// 5单个应用内所有会话广播
// 6非自我应用主会话广播
})); }));
} }
} }

View file

@ -288,7 +288,7 @@ func deleteBlock(c *gin.Context) {
} }
func broadcastTransactions(transactions []*model.Transaction) { func broadcastTransactions(transactions []*model.Transaction) {
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast, util.PushModeBroadcast) evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast)
evt.Data = transactions evt.Data = transactions
util.PushEvent(evt) util.PushEvent(evt)
} }

View file

@ -115,7 +115,7 @@ func heading2Doc(c *gin.Context) {
name := path.Base(targetPath) name := path.Base(targetPath)
box := model.Conf.Box(targetNotebook) box := model.Conf.Box(targetNotebook)
files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort) 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{}{ evt.Data = map[string]interface{}{
"box": box, "box": box,
"path": targetPath, "path": targetPath,
@ -159,7 +159,7 @@ func li2Doc(c *gin.Context) {
name := path.Base(targetPath) name := path.Base(targetPath)
box := model.Conf.Box(targetNotebook) box := model.Conf.Box(targetNotebook)
files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), model.Conf.FileTree.Sort) 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{}{ evt.Data = map[string]interface{}{
"box": box, "box": box,
"path": targetPath, "path": targetPath,
@ -465,7 +465,7 @@ func createDailyNote(c *gin.Context) {
if existed && "" != app { if existed && "" != app {
pushMode = util.PushModeBroadcastApp pushMode = util.PushModeBroadcastApp
} }
evt := util.NewCmdResult("createdailynote", 0, pushMode, util.PushModeNone) evt := util.NewCmdResult("createdailynote", 0, pushMode)
evt.AppId = app evt.AppId = app
name := path.Base(p) 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{}) { 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) name := path.Base(p)
files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort) files, _, _ := model.ListDocTree(box.ID, path.Dir(p), model.Conf.FileTree.Sort)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{

View file

@ -76,7 +76,7 @@ func renameNotebook(c *gin.Context) {
return return
} }
evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast, util.PushModeNone) evt := util.NewCmdResult("renamenotebook", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"box": notebook, "box": notebook,
"name": name, "name": name,
@ -101,7 +101,7 @@ func removeNotebook(c *gin.Context) {
return return
} }
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0) evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"box": notebook, "box": notebook,
} }
@ -137,7 +137,7 @@ func createNotebook(c *gin.Context) {
"notebook": model.Conf.Box(id), "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{}{ evt.Data = map[string]interface{}{
"box": model.Conf.Box(id), "box": model.Conf.Box(id),
"existed": existed, "existed": existed,
@ -164,7 +164,7 @@ func openNotebook(c *gin.Context) {
return return
} }
evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast, util.PushModeNone) evt := util.NewCmdResult("mount", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"box": model.Conf.Box(notebook), "box": model.Conf.Box(notebook),
"existed": existed, "existed": existed,

View file

@ -196,7 +196,7 @@ func setLocalStorage(c *gin.Context) {
} }
app := arg["app"].(string) app := arg["app"].(string)
evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp, util.PushModeBroadcastMainExcludeSelfApp) evt := util.NewCmdResult("setLocalStorage", 0, util.PushModeBroadcastMainExcludeSelfApp)
evt.AppId = app evt.AppId = app
evt.Data = val evt.Data = val
util.PushEvent(evt) util.PushEvent(evt)

View file

@ -93,7 +93,7 @@ func performTransactions(c *gin.Context) {
} }
func pushTransactions(app, session string, transactions []*model.Transaction) { 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.AppId = app
evt.SessionId = session evt.SessionId = session
evt.Data = transactions evt.Data = transactions

View file

@ -64,11 +64,7 @@ func NewCommand(cmdStr string, cmdId float64, param map[string]interface{}, sess
if pushModeParam := param["pushMode"]; nil != pushModeParam { if pushModeParam := param["pushMode"]; nil != pushModeParam {
pushMode = util.PushMode(pushModeParam.(float64)) pushMode = util.PushMode(pushModeParam.(float64))
} }
reloadPushMode := util.PushModeSingleSelf baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode)
if reloadPushModeParam := param["reloadPushMode"]; nil != reloadPushModeParam {
reloadPushMode = util.PushMode(reloadPushModeParam.(float64))
}
baseCmd.PushPayload = util.NewCmdResult(ret.Name(), cmdId, pushMode, reloadPushMode)
appId, _ := baseCmd.session.Get("app") appId, _ := baseCmd.session.Get("app")
baseCmd.PushPayload.AppId = appId.(string) baseCmd.PushPayload.AppId = appId.(string)
sid, _ := baseCmd.session.Get("id") sid, _ := baseCmd.session.Get("id")

View file

@ -148,7 +148,7 @@ func SetBlockAttrs(id string, nameValues map[string]string) (err error) {
} }
func pushBroadcastAttrTransactions(transactions []*Transaction) { func pushBroadcastAttrTransactions(transactions []*Transaction) {
evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast, util.PushModeBroadcast) evt := util.NewCmdResult("transactions", 0, util.PushModeBroadcast)
evt.Data = transactions evt.Data = transactions
util.PushEvent(evt) util.PushEvent(evt)
} }

View file

@ -1172,7 +1172,7 @@ func moveDoc(fromBox *Box, fromPath string, toBox *Box, toPath string) (newPath
moveSorts(tree.ID, fromBox.ID, toBox.ID) 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{}{ evt.Data = map[string]interface{}{
"fromNotebook": fromBox.ID, "fromNotebook": fromBox.ID,
"fromPath": fromPath, "fromPath": fromPath,
@ -1275,7 +1275,7 @@ func removeDoc(box *Box, p string) (err error) {
cache.RemoveDocIAL(p) cache.RemoveDocIAL(p)
evt := util.NewCmdResult("removeDoc", 0, util.PushModeBroadcast, util.PushModeNone) evt := util.NewCmdResult("removeDoc", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"ids": removeIDs, "ids": removeIDs,
} }
@ -1321,7 +1321,7 @@ func RenameDoc(boxID, p, title string) (err error) {
} }
refText := getNodeRefText(tree.Root) 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{}{ evt.Data = map[string]interface{}{
"box": boxID, "box": boxID,
"id": tree.Root.ID, "id": tree.Root.ID,

View file

@ -115,7 +115,7 @@ func Unmount(boxID string) {
WaitForWritingFiles() WaitForWritingFiles()
unmount0(boxID) unmount0(boxID)
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0) evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"box": boxID, "box": boxID,
} }

View file

@ -24,45 +24,41 @@ import (
type PushMode int type PushMode int
const ( const (
PushModeBroadcast PushMode = 0 // 所有应用所有会话广播 PushModeBroadcast PushMode = 0 // 所有应用所有会话广播
PushModeSingleSelf PushMode = 1 // 自我应用会话单播 PushModeSingleSelf PushMode = 1 // 自我应用会话单播
PushModeBroadcastExcludeSelf PushMode = 2 // 非自我会话广播 PushModeBroadcastExcludeSelf PushMode = 2 // 非自我会话广播
PushModeBroadcastExcludeSelfApp PushMode = 4 // 非自我应用所有会话广播 PushModeBroadcastExcludeSelfApp PushMode = 4 // 非自我应用所有会话广播
PushModeBroadcastApp PushMode = 5 // 单个应用内所有会话广播 PushModeBroadcastApp PushMode = 5 // 单个应用内所有会话广播
PushModeBroadcastMainExcludeSelfApp PushMode = 6 // 非自我应用主会话广播 PushModeBroadcastMainExcludeSelfApp PushMode = 6 // 非自我应用主会话广播
PushModeNone PushMode = 10 // 不进行 reload
) )
type Result struct { type Result struct {
Cmd string `json:"cmd"` Cmd string `json:"cmd"`
ReqId float64 `json:"reqId"` ReqId float64 `json:"reqId"`
AppId string `json:"app"` AppId string `json:"app"`
SessionId string `json:"sid"` SessionId string `json:"sid"`
PushMode PushMode `json:"pushMode"` PushMode PushMode `json:"pushMode"`
ReloadPushMode PushMode `json:"reloadPushMode"` Callback interface{} `json:"callback"`
Callback interface{} `json:"callback"` Code int `json:"code"`
Code int `json:"code"` Msg string `json:"msg"`
Msg string `json:"msg"` Data interface{} `json:"data"`
Data interface{} `json:"data"`
} }
func NewResult() *Result { func NewResult() *Result {
return &Result{Cmd: "", return &Result{Cmd: "",
ReqId: 0, ReqId: 0,
PushMode: 0, PushMode: 0,
ReloadPushMode: 0, Callback: "",
Callback: "", Code: 0,
Code: 0, Msg: "",
Msg: "", Data: nil}
Data: nil}
} }
func NewCmdResult(cmdName string, cmdId float64, pushMode, reloadPushMode PushMode) *Result { func NewCmdResult(cmdName string, cmdId float64, pushMode PushMode) *Result {
ret := NewResult() ret := NewResult()
ret.Cmd = cmdName ret.Cmd = cmdName
ret.ReqId = cmdId ret.ReqId = cmdId
ret.PushMode = pushMode ret.PushMode = pushMode
ret.ReloadPushMode = reloadPushMode
return ret return ret
} }

View file

@ -121,7 +121,7 @@ func ClosePushChan(id string) {
} }
func ReloadUI() { func ReloadUI() {
evt := NewCmdResult("reloadui", 0, PushModeBroadcast, 0) evt := NewCmdResult("reloadui", 0, PushModeBroadcast)
PushEvent(evt) PushEvent(evt)
} }
@ -208,7 +208,7 @@ func PushClearProgress() {
} }
func PushDownloadProgress(id string, percent float32) { func PushDownloadProgress(id string, percent float32) {
evt := NewCmdResult("downloadProgress", 0, PushModeBroadcast, 0) evt := NewCmdResult("downloadProgress", 0, PushModeBroadcast)
evt.Data = map[string]interface{}{ evt.Data = map[string]interface{}{
"id": id, "id": id,
"percent": percent, "percent": percent,
@ -219,9 +219,6 @@ func PushDownloadProgress(id string, percent float32) {
func PushEvent(event *Result) { func PushEvent(event *Result) {
msg := event.Bytes() msg := event.Bytes()
mode := event.PushMode mode := event.PushMode
if "reload" == event.Cmd {
mode = event.ReloadPushMode
}
switch mode { switch mode {
case PushModeBroadcast: case PushModeBroadcast:
Broadcast(msg) Broadcast(msg)
@ -235,7 +232,6 @@ func PushEvent(event *Result) {
broadcastApp(msg, event.AppId) broadcastApp(msg, event.AppId)
case PushModeBroadcastMainExcludeSelfApp: case PushModeBroadcastMainExcludeSelfApp:
broadcastOtherAppMains(msg, event.AppId) broadcastOtherAppMains(msg, event.AppId)
case PushModeNone:
} }
} }