Clear previous layout when applying portable layout to tab (#382)

This commit is contained in:
Evan Simkowitz 2024-09-16 17:26:37 -07:00 committed by GitHub
parent 822920bd2c
commit fd6e92ee2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import { createRef, CSSProperties } from "react";
import { debounce } from "throttle-debounce"; import { debounce } from "throttle-debounce";
import { balanceNode, findNode, newLayoutNode, walkNodes } from "./layoutNode"; import { balanceNode, findNode, newLayoutNode, walkNodes } from "./layoutNode";
import { import {
clearTree,
computeMoveNode, computeMoveNode,
deleteNode, deleteNode,
focusNode, focusNode,
@ -25,6 +26,7 @@ import {
LayoutNodeAdditionalProps, LayoutNodeAdditionalProps,
LayoutTreeAction, LayoutTreeAction,
LayoutTreeActionType, LayoutTreeActionType,
LayoutTreeClearTreeAction,
LayoutTreeComputeMoveNodeAction, LayoutTreeComputeMoveNodeAction,
LayoutTreeDeleteNodeAction, LayoutTreeDeleteNodeAction,
LayoutTreeFocusNodeAction, LayoutTreeFocusNodeAction,
@ -354,6 +356,9 @@ export class LayoutModel {
case LayoutTreeActionType.MagnifyNodeToggle: case LayoutTreeActionType.MagnifyNodeToggle:
magnifyNodeToggle(this.treeState, action as LayoutTreeMagnifyNodeToggleAction); magnifyNodeToggle(this.treeState, action as LayoutTreeMagnifyNodeToggleAction);
break; break;
case LayoutTreeActionType.ClearTree: {
clearTree(this.treeState);
}
default: default:
console.error("Invalid reducer action", this.treeState, action); console.error("Invalid reducer action", this.treeState, action);
} }
@ -431,6 +436,11 @@ export class LayoutModel {
this.treeReducer(insertAction); this.treeReducer(insertAction);
break; break;
} }
case LayoutTreeActionType.ClearTree: {
this.treeReducer({
type: LayoutTreeActionType.ClearTree,
} as LayoutTreeClearTreeAction);
}
default: default:
console.warn("unsupported layout action", action); console.warn("unsupported layout action", action);
break; break;

View File

@ -417,3 +417,11 @@ export function magnifyNodeToggle(layoutState: LayoutTreeState, action: LayoutTr
} }
layoutState.generation++; layoutState.generation++;
} }
export function clearTree(layoutState: LayoutTreeState) {
layoutState.rootNode = undefined;
layoutState.leafOrder = undefined;
layoutState.focusedNodeId = undefined;
layoutState.magnifiedNodeId = undefined;
layoutState.generation++;
}

View File

@ -69,6 +69,7 @@ export enum LayoutTreeActionType {
DeleteNode = "delete", DeleteNode = "delete",
FocusNode = "focus", FocusNode = "focus",
MagnifyNodeToggle = "magnify", MagnifyNodeToggle = "magnify",
ClearTree = "clear",
} }
/** /**
@ -236,6 +237,13 @@ export interface LayoutTreeMagnifyNodeToggleAction extends LayoutTreeAction {
nodeId: string; nodeId: string;
} }
/**
* Action for clearing all nodes from the layout tree.
*/
export interface LayoutTreeClearTreeAction extends LayoutTreeAction {
type: LayoutTreeActionType.ClearTree;
}
/** /**
* Represents a single node in the layout tree. * Represents a single node in the layout tree.
*/ */

View File

@ -18,6 +18,7 @@ const (
LayoutActionDataType_Insert = "insert" LayoutActionDataType_Insert = "insert"
LayoutActionDataType_InsertAtIndex = "insertatindex" LayoutActionDataType_InsertAtIndex = "insertatindex"
LayoutActionDataType_Remove = "delete" LayoutActionDataType_Remove = "delete"
LayoutActionDataType_ClearTree = "clear"
) )
type PortableLayout []struct { type PortableLayout []struct {
@ -119,7 +120,8 @@ func QueueLayoutActionForTab(ctx context.Context, tabId string, actions ...waveo
} }
func ApplyPortableLayout(ctx context.Context, tabId string, layout PortableLayout) error { func ApplyPortableLayout(ctx context.Context, tabId string, layout PortableLayout) error {
actions := make([]waveobj.LayoutActionData, len(layout)) actions := make([]waveobj.LayoutActionData, len(layout)+1)
actions[0] = waveobj.LayoutActionData{ActionType: LayoutActionDataType_ClearTree}
for i := 0; i < len(layout); i++ { for i := 0; i < len(layout); i++ {
layoutAction := layout[i] layoutAction := layout[i]
@ -128,7 +130,7 @@ func ApplyPortableLayout(ctx context.Context, tabId string, layout PortableLayou
return fmt.Errorf("unable to create block to apply portable layout to tab %s: %w", tabId, err) return fmt.Errorf("unable to create block to apply portable layout to tab %s: %w", tabId, err)
} }
actions[i] = waveobj.LayoutActionData{ actions[i+1] = waveobj.LayoutActionData{
ActionType: LayoutActionDataType_InsertAtIndex, ActionType: LayoutActionDataType_InsertAtIndex,
BlockId: blockData.OID, BlockId: blockData.OID,
IndexArr: &layoutAction.IndexArr, IndexArr: &layoutAction.IndexArr,