1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-09 17:44:59 +09:00
anytype-heart/util/internalflag/flag.go
2024-07-30 21:57:10 +02:00

84 lines
1.7 KiB
Go

package internalflag
import (
"github.com/gogo/protobuf/types"
"github.com/anyproto/anytype-heart/core/block/editor/state"
"github.com/anyproto/anytype-heart/pkg/lib/bundle"
"github.com/anyproto/anytype-heart/pkg/lib/pb/model"
"github.com/anyproto/anytype-heart/util/pbtypes"
)
const relationKey = bundle.RelationKeyInternalFlags
type Set struct {
flags []int
}
func NewFromState(st *state.State) Set {
flags := st.Details().GetIntListOrDefault(relationKey, nil)
return Set{
flags: flags,
}
}
func (s *Set) Add(flag model.InternalFlagValue) {
if !s.Has(flag) {
s.flags = append(s.flags, int(flag))
}
}
func (s *Set) Has(flag model.InternalFlagValue) bool {
for _, f := range s.flags {
if f == int(flag) {
return true
}
}
return false
}
func (s *Set) Remove(flag model.InternalFlagValue) {
res := s.flags[:0]
for _, f := range s.flags {
if f == int(flag) {
continue
}
res = append(res, f)
}
s.flags = res
}
func (s *Set) AddToState(st *state.State) {
if len(s.flags) == 0 {
st.RemoveDetail(relationKey)
return
}
st.SetDetailAndBundledRelation(relationKey, pbtypes.IntList(s.flags...))
}
func (s *Set) IsEmpty() bool {
return len(s.flags) == 0
}
func PutToDetails(details *types.Struct, flags []*model.InternalFlag) *types.Struct {
ints := make([]int, 0, len(flags))
for _, f := range flags {
ints = append(ints, int(f.Value))
}
return putToDetails(details, ints)
}
func putToDetails(details *types.Struct, flags []int) *types.Struct {
if details == nil {
details = &types.Struct{
Fields: map[string]*types.Value{},
}
}
if details.Fields == nil {
details.Fields = map[string]*types.Value{}
}
details.Fields[relationKey.String()] = pbtypes.IntList(flags...)
return details
}