mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-05-19 02:21:38 +08:00
🎨 桌面端支持同时打开多个工作空间 https://github.com/siyuan-note/siyuan/issues/4567
This commit is contained in:
parent
47c3dea234
commit
29c8b67a7d
@ -17,6 +17,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -53,6 +54,20 @@ func createWorkspaceDir(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workspacePaths, err := readWorkspacePaths()
|
||||||
|
if nil != err {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
workspacePaths = append(workspacePaths, path)
|
||||||
|
|
||||||
|
if err = writeWorkspacePaths(workspacePaths); nil != err {
|
||||||
|
ret.Code = -1
|
||||||
|
ret.Msg = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeWorkspaceDir(c *gin.Context) {
|
func removeWorkspaceDir(c *gin.Context) {
|
||||||
@ -66,29 +81,18 @@ func removeWorkspaceDir(c *gin.Context) {
|
|||||||
|
|
||||||
path := arg["path"].(string)
|
path := arg["path"].(string)
|
||||||
|
|
||||||
var workspacePaths []string
|
workspacePaths, err := readWorkspacePaths()
|
||||||
workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
|
|
||||||
data, err := os.ReadFile(workspaceConf)
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logging.LogErrorf("read workspace conf failed: %s", err)
|
|
||||||
} else {
|
|
||||||
if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err {
|
|
||||||
logging.LogErrorf("unmarshal workspace conf failed: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
|
||||||
if data, err = gulu.JSON.MarshalJSON(workspacePaths); nil != err {
|
|
||||||
msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err)
|
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = msg
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = gulu.File.WriteFileSafer(workspaceConf, data, 0644); nil != err {
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
||||||
msg := fmt.Sprintf("write workspace conf [%s] failed: %s", workspaceConf, err)
|
|
||||||
|
if err = writeWorkspacePaths(workspacePaths); nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = msg
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,17 +112,10 @@ func listWorkspaceDirs(c *gin.Context) {
|
|||||||
ret := gulu.Ret.NewResult()
|
ret := gulu.Ret.NewResult()
|
||||||
defer c.JSON(http.StatusOK, ret)
|
defer c.JSON(http.StatusOK, ret)
|
||||||
|
|
||||||
userHomeConfDir := filepath.Join(util.HomeDir, ".config", "siyuan")
|
workspacePaths, err := readWorkspacePaths()
|
||||||
workspaceConf := filepath.Join(userHomeConfDir, "workspace.json")
|
|
||||||
data, err := os.ReadFile(workspaceConf)
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logging.LogErrorf("read workspace conf [%s] failed: %s", workspaceConf, err)
|
ret.Code = -1
|
||||||
return
|
ret.Msg = err.Error()
|
||||||
}
|
|
||||||
|
|
||||||
var workspacePaths []string
|
|
||||||
if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err {
|
|
||||||
logging.LogErrorf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ret.Data = workspacePaths
|
ret.Data = workspacePaths
|
||||||
@ -151,15 +148,11 @@ func setWorkspaceDir(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var workspacePaths []string
|
workspacePaths, err := readWorkspacePaths()
|
||||||
workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
|
|
||||||
data, err := os.ReadFile(workspaceConf)
|
|
||||||
if nil != err {
|
if nil != err {
|
||||||
logging.LogErrorf("read workspace conf failed: %s", err)
|
ret.Code = -1
|
||||||
} else {
|
ret.Msg = err.Error()
|
||||||
if err = gulu.JSON.UnmarshalJSON(data, &workspacePaths); nil != err {
|
return
|
||||||
logging.LogErrorf("unmarshal workspace conf failed: %s", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
workspacePaths = append(workspacePaths, path)
|
workspacePaths = append(workspacePaths, path)
|
||||||
@ -167,18 +160,10 @@ func setWorkspaceDir(c *gin.Context) {
|
|||||||
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
workspacePaths = gulu.Str.RemoveElem(workspacePaths, path)
|
||||||
workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
|
workspacePaths = append(workspacePaths, path) // 切换的工作空间固定放在最后一个
|
||||||
|
|
||||||
if data, err = gulu.JSON.MarshalJSON(workspacePaths); nil != err {
|
if err = writeWorkspacePaths(workspacePaths); nil != err {
|
||||||
msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err)
|
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = msg
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
if err = gulu.File.WriteFileSafer(workspaceConf, data, 0644); nil != err {
|
|
||||||
msg := fmt.Sprintf("create workspace conf [%s] failed: %s", workspaceConf, err)
|
|
||||||
ret.Code = -1
|
|
||||||
ret.Msg = msg
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
|
if util.ContainerAndroid == util.Container || util.ContainerIOS == util.Container {
|
||||||
@ -187,3 +172,42 @@ func setWorkspaceDir(c *gin.Context) {
|
|||||||
model.Close(false, 1)
|
model.Close(false, 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readWorkspacePaths() (ret []string, err error) {
|
||||||
|
ret = []string{}
|
||||||
|
workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
|
||||||
|
data, err := os.ReadFile(workspaceConf)
|
||||||
|
if nil != err {
|
||||||
|
msg := fmt.Sprintf("read workspace conf [%s] failed: %s", workspaceConf, err)
|
||||||
|
logging.LogErrorf(msg)
|
||||||
|
err = errors.New(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = gulu.JSON.UnmarshalJSON(data, &ret); nil != err {
|
||||||
|
msg := fmt.Sprintf("unmarshal workspace conf [%s] failed: %s", workspaceConf, err)
|
||||||
|
logging.LogErrorf(msg)
|
||||||
|
err = errors.New(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeWorkspacePaths(workspacePaths []string) (err error) {
|
||||||
|
workspaceConf := filepath.Join(util.HomeDir, ".config", "siyuan", "workspace.json")
|
||||||
|
data, err := gulu.JSON.MarshalJSON(workspacePaths)
|
||||||
|
if nil != err {
|
||||||
|
msg := fmt.Sprintf("marshal workspace conf [%s] failed: %s", workspaceConf, err)
|
||||||
|
logging.LogErrorf(msg)
|
||||||
|
err = errors.New(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = os.WriteFile(workspaceConf, data, 0644); nil != err {
|
||||||
|
msg := fmt.Sprintf("write workspace conf [%s] failed: %s", workspaceConf, err)
|
||||||
|
logging.LogErrorf(msg)
|
||||||
|
err = errors.New(msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user