🎨 Attribute Panel - Database supports display database block floating window https://github.com/siyuan-note/siyuan/issues/9285

This commit is contained in:
Daniel 2023-11-02 16:24:50 +08:00
parent 960cf3fe04
commit da65f608a5
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
3 changed files with 156 additions and 116 deletions

View file

@ -25,7 +25,6 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/88250/gulu"
@ -33,7 +32,6 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/vmihailenco/msgpack/v5"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
@ -586,120 +584,6 @@ func NewAttributeView(id string) (ret *AttributeView) {
return
}
var (
attributeViewBlocksLock = sync.Mutex{}
)
func IsMirror(avID string) bool {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return false
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return false
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return false
}
blockIDs := avBlocks[avID]
return nil != blockIDs && 1 < len(blockIDs)
}
func RemoveBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
blockIDs := avBlocks[avID]
if nil == blockIDs {
return
}
var newBlockIDs []string
for _, v := range blockIDs {
if v != blockID {
newBlockIDs = append(newBlockIDs, v)
}
}
avBlocks[avID] = newBlockIDs
data, err = msgpack.Marshal(avBlocks)
if nil != err {
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
return
}
if err = filelock.WriteFile(blocks, data); nil != err {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
}
func UpsertBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
avBlocks := map[string][]string{}
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
if err := os.MkdirAll(filepath.Dir(blocks), 0755); nil != err {
logging.LogErrorf("create attribute view dir failed: %s", err)
return
}
} else {
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
}
blockIDs := avBlocks[avID]
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = blockIDs
data, err := msgpack.Marshal(avBlocks)
if nil != err {
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
return
}
if err = filelock.WriteFile(blocks, data); nil != err {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
}
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
avJSONPath := GetAttributeViewDataPath(avID)
if !gulu.File.IsExist(avJSONPath) {

152
kernel/av/mirror.go Normal file
View file

@ -0,0 +1,152 @@
package av
import (
"os"
"path/filepath"
"sync"
"github.com/88250/gulu"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/util"
"github.com/vmihailenco/msgpack/v5"
)
var (
attributeViewBlocksLock = sync.Mutex{}
)
func GetMirrorBlockIDs(avID string) []string {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return nil
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return nil
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return nil
}
blockIDs := avBlocks[avID]
return blockIDs
}
func IsMirror(avID string) bool {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return false
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return false
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return false
}
blockIDs := avBlocks[avID]
return nil != blockIDs && 1 < len(blockIDs)
}
func RemoveBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
return
}
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
avBlocks := map[string][]string{}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
blockIDs := avBlocks[avID]
if nil == blockIDs {
return
}
var newBlockIDs []string
for _, v := range blockIDs {
if v != blockID {
newBlockIDs = append(newBlockIDs, v)
}
}
avBlocks[avID] = newBlockIDs
data, err = msgpack.Marshal(avBlocks)
if nil != err {
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
return
}
if err = filelock.WriteFile(blocks, data); nil != err {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
}
func UpsertBlockRel(avID, blockID string) {
attributeViewBlocksLock.Lock()
defer attributeViewBlocksLock.Unlock()
avBlocks := map[string][]string{}
blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
if !gulu.File.IsExist(blocks) {
if err := os.MkdirAll(filepath.Dir(blocks), 0755); nil != err {
logging.LogErrorf("create attribute view dir failed: %s", err)
return
}
} else {
data, err := filelock.ReadFile(blocks)
if nil != err {
logging.LogErrorf("read attribute view blocks failed: %s", err)
return
}
if err = msgpack.Unmarshal(data, &avBlocks); nil != err {
logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
return
}
}
blockIDs := avBlocks[avID]
blockIDs = append(blockIDs, blockID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[avID] = blockIDs
data, err := msgpack.Marshal(avBlocks)
if nil != err {
logging.LogErrorf("marshal attribute view blocks failed: %s", err)
return
}
if err = filelock.WriteFile(blocks, data); nil != err {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
}

View file

@ -36,6 +36,7 @@ import (
type BlockAttributeViewKeys struct {
AvID string `json:"avID"`
AvName string `json:"avName"`
BlockIDs []string `json:"blockIDs"`
KeyValues []*av.KeyValues `json:"keyValues"`
}
@ -140,9 +141,12 @@ func GetBlockAttributeViewKeys(blockID string) (ret []*BlockAttributeViewKeys) {
})
}
blockIDs := av.GetMirrorBlockIDs(avID)
ret = append(ret, &BlockAttributeViewKeys{
AvID: avID,
AvName: attrView.Name,
BlockIDs: blockIDs,
KeyValues: keyValues,
})
}