waveterm/pkg/wstore/wstore.go

81 lines
2.2 KiB
Go
Raw Normal View History

// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package wstore
import (
2024-05-22 06:15:11 +02:00
"context"
2024-05-21 20:09:22 +02:00
"fmt"
2024-09-05 23:25:45 +02:00
"github.com/wavetermdev/waveterm/pkg/util/utilfn"
"github.com/wavetermdev/waveterm/pkg/waveobj"
)
2024-05-26 20:59:14 +02:00
func init() {
2024-08-20 23:56:48 +02:00
for _, rtype := range waveobj.AllWaveObjTypes() {
2024-05-27 08:05:11 +02:00
waveobj.RegisterType(rtype)
}
2024-05-26 20:59:14 +02:00
}
2024-06-21 19:23:04 +02:00
func UpdateTabName(ctx context.Context, tabId, name string) error {
return WithTx(ctx, func(tx *TxWrap) error {
2024-08-20 23:56:48 +02:00
tab, _ := DBGet[*waveobj.Tab](tx.Context(), tabId)
2024-06-21 19:23:04 +02:00
if tab == nil {
return fmt.Errorf("tab not found: %q", tabId)
}
if tabId != "" {
tab.Name = name
DBUpdate(tx.Context(), tab)
}
return nil
})
}
func UpdateObjectMeta(ctx context.Context, oref waveobj.ORef, meta waveobj.MetaMapType, mergeSpecial bool) error {
return WithTx(ctx, func(tx *TxWrap) error {
if oref.IsEmpty() {
return fmt.Errorf("empty object reference")
}
obj, _ := DBGetORef(tx.Context(), oref)
if obj == nil {
return ErrNotFound
}
objMeta := waveobj.GetMeta(obj)
if objMeta == nil {
objMeta = make(map[string]any)
}
newMeta := waveobj.MergeMeta(objMeta, meta, mergeSpecial)
waveobj.SetMeta(obj, newMeta)
DBUpdate(tx.Context(), obj)
return nil
})
}
func MoveBlockToTab(ctx context.Context, currentTabId string, newTabId string, blockId string) error {
return WithTx(ctx, func(tx *TxWrap) error {
2024-10-24 07:47:29 +02:00
block, _ := DBGet[*waveobj.Block](tx.Context(), blockId)
if block == nil {
return fmt.Errorf("block not found: %q", blockId)
}
2024-08-20 23:56:48 +02:00
currentTab, _ := DBGet[*waveobj.Tab](tx.Context(), currentTabId)
if currentTab == nil {
return fmt.Errorf("current tab not found: %q", currentTabId)
}
2024-08-20 23:56:48 +02:00
newTab, _ := DBGet[*waveobj.Tab](tx.Context(), newTabId)
if newTab == nil {
return fmt.Errorf("new tab not found: %q", newTabId)
}
2024-12-02 19:56:56 +01:00
blockIdx := utilfn.FindStringInSlice(currentTab.BlockIds, blockId)
if blockIdx == -1 {
return fmt.Errorf("block not found in current tab: %q", blockId)
}
currentTab.BlockIds = utilfn.RemoveElemFromSlice(currentTab.BlockIds, blockId)
newTab.BlockIds = append(newTab.BlockIds, blockId)
2024-10-24 07:47:29 +02:00
block.ParentORef = waveobj.MakeORef(waveobj.OType_Tab, newTabId).String()
DBUpdate(tx.Context(), block)
DBUpdate(tx.Context(), currentTab)
DBUpdate(tx.Context(), newTab)
return nil
})
}