wsh deleteblock

This commit is contained in:
sawka 2024-07-18 18:13:59 -07:00
parent f0263865bd
commit fd3581fc42
8 changed files with 121 additions and 1 deletions

View File

@ -0,0 +1,54 @@
// Copyright 2024, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/wavetermdev/thenextwave/pkg/wshrpc"
)
var deleteBlockCmd = &cobra.Command{
Use: "deleteblock",
Short: "delete a block",
Args: cobra.ExactArgs(1),
Run: deleteBlockRun,
}
func init() {
rootCmd.AddCommand(deleteBlockCmd)
}
func deleteBlockRun(cmd *cobra.Command, args []string) {
oref := args[0]
if oref == "" {
fmt.Println("oref is required")
return
}
err := validateEasyORef(oref)
if err != nil {
fmt.Printf("%v\n", err)
return
}
setTermRawMode()
fullORef, err := resolveSimpleId(oref)
if err != nil {
fmt.Printf("error resolving oref: %v\r\n", err)
return
}
if fullORef.OType != "block" {
fmt.Printf("oref is not a block\r\n")
return
}
deleteBlockData := &wshrpc.CommandDeleteBlockData{
BlockId: fullORef.OID,
}
_, err = RpcClient.SendRpcRequest(wshrpc.Command_DeleteBlock, deleteBlockData, 2000)
if err != nil {
fmt.Printf("error deleting block: %v\r\n", err)
return
}
fmt.Print("block deleted\r\n")
}

View File

@ -22,6 +22,11 @@ class WshServerType {
return WOS.wshServerRpcHelper_call("createblock", data, opts); return WOS.wshServerRpcHelper_call("createblock", data, opts);
} }
// command "deleteblock" [call]
DeleteBlockCommand(data: CommandDeleteBlockData, opts?: WshRpcCommandOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("deleteblock", data, opts);
}
// command "file:append" [call] // command "file:append" [call]
AppendFileCommand(data: CommandAppendFileData, opts?: WshRpcCommandOpts): Promise<void> { AppendFileCommand(data: CommandAppendFileData, opts?: WshRpcCommandOpts): Promise<void> {
return WOS.wshServerRpcHelper_call("file:append", data, opts); return WOS.wshServerRpcHelper_call("file:append", data, opts);

View File

@ -10,7 +10,7 @@
border-radius: 3px; border-radius: 3px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
font-family: Inter; font: var(--base-font);
.dir-table-head { .dir-table-head {
.dir-table-head-row { .dir-table-head-row {
display: flex; display: flex;

View File

@ -95,6 +95,11 @@ declare global {
rtopts: RuntimeOpts; rtopts: RuntimeOpts;
}; };
// wshrpc.CommandDeleteBlockData
type CommandDeleteBlockData = {
blockid: string;
};
// wshrpc.CommandGetMetaData // wshrpc.CommandGetMetaData
type CommandGetMetaData = { type CommandGetMetaData = {
oref: ORef; oref: ORef;

View File

@ -29,6 +29,12 @@ func CreateBlockCommand(w *wshutil.WshRpc, data wshrpc.CommandCreateBlockData, o
return resp, err return resp, err
} }
// command "deleteblock", wshserver.DeleteBlockCommand
func DeleteBlockCommand(w *wshutil.WshRpc, data wshrpc.CommandDeleteBlockData, opts *wshrpc.WshRpcCommandOpts) error {
_, err := sendRpcRequestCallHelper[any](w, "deleteblock", data, opts)
return err
}
// command "file:append", wshserver.AppendFileCommand // command "file:append", wshserver.AppendFileCommand
func AppendFileCommand(w *wshutil.WshRpc, data wshrpc.CommandAppendFileData, opts *wshrpc.WshRpcCommandOpts) error { func AppendFileCommand(w *wshutil.WshRpc, data wshrpc.CommandAppendFileData, opts *wshrpc.WshRpcCommandOpts) error {
_, err := sendRpcRequestCallHelper[any](w, "file:append", data, opts) _, err := sendRpcRequestCallHelper[any](w, "file:append", data, opts)

View File

@ -25,6 +25,7 @@ const (
Command_AppendIJson = "file:appendijson" Command_AppendIJson = "file:appendijson"
Command_ResolveIds = "resolveids" Command_ResolveIds = "resolveids"
Command_CreateBlock = "createblock" Command_CreateBlock = "createblock"
Command_DeleteBlock = "deleteblock"
) )
type MetaDataType = map[string]any type MetaDataType = map[string]any
@ -133,3 +134,7 @@ type CommandAppendIJsonData struct {
FileName string `json:"filename"` FileName string `json:"filename"`
Data ijson.Command `json:"data"` Data ijson.Command `json:"data"`
} }
type CommandDeleteBlockData struct {
BlockId string `json:"blockid" wshcontext:"BlockId"`
}

View File

@ -44,6 +44,7 @@ var WshServerCommandToDeclMap = map[string]*WshServerMethodDecl{
wshrpc.Command_BlockInput: GetWshServerMethod(wshrpc.Command_BlockInput, wshutil.RpcType_Call, "BlockInputCommand", WshServerImpl.BlockInputCommand), wshrpc.Command_BlockInput: GetWshServerMethod(wshrpc.Command_BlockInput, wshutil.RpcType_Call, "BlockInputCommand", WshServerImpl.BlockInputCommand),
wshrpc.Command_AppendFile: GetWshServerMethod(wshrpc.Command_AppendFile, wshutil.RpcType_Call, "AppendFileCommand", WshServerImpl.AppendFileCommand), wshrpc.Command_AppendFile: GetWshServerMethod(wshrpc.Command_AppendFile, wshutil.RpcType_Call, "AppendFileCommand", WshServerImpl.AppendFileCommand),
wshrpc.Command_AppendIJson: GetWshServerMethod(wshrpc.Command_AppendIJson, wshutil.RpcType_Call, "AppendIJsonCommand", WshServerImpl.AppendIJsonCommand), wshrpc.Command_AppendIJson: GetWshServerMethod(wshrpc.Command_AppendIJson, wshutil.RpcType_Call, "AppendIJsonCommand", WshServerImpl.AppendIJsonCommand),
wshrpc.Command_DeleteBlock: GetWshServerMethod(wshrpc.Command_DeleteBlock, wshutil.RpcType_Call, "DeleteBlockCommand", WshServerImpl.DeleteBlockCommand),
"streamtest": RespStreamTest_MethodDecl, "streamtest": RespStreamTest_MethodDecl,
} }
@ -294,3 +295,37 @@ func (ws *WshServer) AppendIJsonCommand(ctx context.Context, data wshrpc.Command
}) })
return nil return nil
} }
func (ws *WshServer) DeleteBlockCommand(ctx context.Context, data wshrpc.CommandDeleteBlockData) error {
ctx = wstore.ContextWithUpdates(ctx)
tabId, err := wstore.DBFindTabForBlockId(ctx, data.BlockId)
if err != nil {
return fmt.Errorf("error finding tab for block: %w", err)
}
if tabId == "" {
return fmt.Errorf("no tab found for block")
}
windowId, err := wstore.DBFindWindowForTabId(ctx, tabId)
if err != nil {
return fmt.Errorf("error finding window for tab: %w", err)
}
if windowId == "" {
return fmt.Errorf("no window found for tab")
}
err = wstore.DeleteBlock(ctx, tabId, data.BlockId)
if err != nil {
return fmt.Errorf("error deleting block: %w", err)
}
eventbus.SendEventToWindow(windowId, eventbus.WSEventType{
EventType: eventbus.WSEvent_LayoutAction,
Data: &eventbus.WSLayoutActionData{
ActionType: "delete",
TabId: tabId,
BlockId: data.BlockId,
},
})
blockcontroller.StopBlockController(data.BlockId)
updates := wstore.ContextGetUpdatesRtn(ctx)
sendWStoreUpdatesToEventBus(updates)
return nil
}

View File

@ -267,3 +267,13 @@ func DBFindWindowForTabId(ctx context.Context, tabId string) (string, error) {
return tx.GetString(query, tabId), nil return tx.GetString(query, tabId), nil
}) })
} }
func DBFindTabForBlockId(ctx context.Context, blockId string) (string, error) {
return WithTxRtn(ctx, func(tx *TxWrap) (string, error) {
query := `
SELECT t.oid
FROM db_tab t, json_each(data->'blockids') je
WHERE je.value = ?;`
return tx.GetString(query, blockId), nil
})
}