mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00: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
|
/// #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:非自我应用主会话广播
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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{}{
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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")
|
||||||
|
|
|
||||||
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
|
|
|
||||||
|
|
@ -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,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,6 @@ const (
|
||||||
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 {
|
||||||
|
|
@ -39,7 +38,6 @@ type Result struct {
|
||||||
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"`
|
||||||
|
|
@ -50,19 +48,17 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue