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

View File

@ -417,3 +417,11 @@ export function magnifyNodeToggle(layoutState: LayoutTreeState, action: LayoutTr
}
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",
FocusNode = "focus",
MagnifyNodeToggle = "magnify",
ClearTree = "clear",
}
/**
@ -236,6 +237,13 @@ export interface LayoutTreeMagnifyNodeToggleAction extends LayoutTreeAction {
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.
*/

View File

@ -18,6 +18,7 @@ const (
LayoutActionDataType_Insert = "insert"
LayoutActionDataType_InsertAtIndex = "insertatindex"
LayoutActionDataType_Remove = "delete"
LayoutActionDataType_ClearTree = "clear"
)
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 {
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++ {
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)
}
actions[i] = waveobj.LayoutActionData{
actions[i+1] = waveobj.LayoutActionData{
ActionType: LayoutActionDataType_InsertAtIndex,
BlockId: blockData.OID,
IndexArr: &layoutAction.IndexArr,