MCPcopy
hub / github.com/wavetermdev/waveterm / DeleteBlock

Function DeleteBlock

pkg/wcore/block.go:153–191  ·  view source on GitHub ↗

Must delete all blocks individually first. Also deletes LayoutState. recursive: if true, will recursively close parent tab, window, workspace, if they are empty. Returns new active tab id, error.

(ctx context.Context, blockId string, recursive bool)

Source from the content-addressed store, hash-verified

151// recursive: if true, will recursively close parent tab, window, workspace, if they are empty.
152// Returns new active tab id, error.
153func DeleteBlock(ctx context.Context, blockId string, recursive bool) error {
154 block, err := wstore.DBGet[*waveobj.Block](ctx, blockId)
155 if err != nil {
156 return fmt.Errorf("error getting block: %w", err)
157 }
158 if block == nil {
159 return nil
160 }
161 if len(block.SubBlockIds) > 0 {
162 for _, subBlockId := range block.SubBlockIds {
163 err := DeleteBlock(ctx, subBlockId, recursive)
164 if err != nil {
165 return fmt.Errorf("error deleting subblock %s: %w", subBlockId, err)
166 }
167 }
168 }
169 parentBlockCount, err := deleteBlockObj(ctx, blockId)
170 if err != nil {
171 return fmt.Errorf("error deleting block: %w", err)
172 }
173 log.Printf("DeleteBlock: parentBlockCount: %d", parentBlockCount)
174 parentORef := waveobj.ParseORefNoErr(block.ParentORef)
175
176 if recursive && parentORef.OType == waveobj.OType_Tab && parentBlockCount == 0 {
177 // if parent tab has no blocks, delete the tab
178 log.Printf("DeleteBlock: parent tab has no blocks, deleting tab %s", parentORef.OID)
179 parentWorkspaceId, err := wstore.DBFindWorkspaceForTabId(ctx, parentORef.OID)
180 if err != nil {
181 return fmt.Errorf("error finding workspace for tab to delete %s: %w", parentORef.OID, err)
182 }
183 newActiveTabId, err := DeleteTab(ctx, parentWorkspaceId, parentORef.OID, true)
184 if err != nil {
185 return fmt.Errorf("error deleting tab %s: %w", parentORef.OID, err)
186 }
187 SendActiveTabUpdate(ctx, parentWorkspaceId, newActiveTabId)
188 }
189 sendBlockCloseEvent(blockId)
190 return nil
191}
192
193// returns the updated block count for the parent object
194func deleteBlockObj(ctx context.Context, blockId string) (int, error) {

Callers 5

CreateBlockCommandMethod · 0.92
DeleteSubBlockCommandMethod · 0.92
DeleteBlockCommandMethod · 0.92
DeleteBlockMethod · 0.92
DeleteTabFunction · 0.85

Calls 7

DBGetFunction · 0.92
ParseORefNoErrFunction · 0.92
DBFindWorkspaceForTabIdFunction · 0.92
deleteBlockObjFunction · 0.85
DeleteTabFunction · 0.85
SendActiveTabUpdateFunction · 0.85
sendBlockCloseEventFunction · 0.85

Tested by

no test coverage detected