From b0a09db4d13200c2e1215c744cd25d58c4f61382 Mon Sep 17 00:00:00 2001 From: Evan Simkowitz Date: Fri, 16 Aug 2024 11:40:10 -0700 Subject: [PATCH] Fix Cmd:W to delete block (#238) --- frontend/app/app.tsx | 44 ++++++++++-------------------- frontend/app/store/global.ts | 20 +++++++++----- frontend/layout/lib/layoutModel.ts | 14 ++++++++++ frontend/util/layoututil.ts | 30 -------------------- 4 files changed, 41 insertions(+), 67 deletions(-) delete mode 100644 frontend/util/layoututil.ts diff --git a/frontend/app/app.tsx b/frontend/app/app.tsx index fb33875b..c1b070ba 100644 --- a/frontend/app/app.tsx +++ b/frontend/app/app.tsx @@ -3,18 +3,12 @@ import { useWaveObjectValue } from "@/app/store/wos"; import { Workspace } from "@/app/workspace/workspace"; -import { - LayoutTreeActionType, - LayoutTreeDeleteNodeAction, - deleteLayoutModelForTab, - getLayoutModelForTab, -} from "@/layout/index"; +import { deleteLayoutModelForTab, getLayoutModelForTab } from "@/layout/index"; import { ContextMenuModel } from "@/store/contextmenu"; import { PLATFORM, WOS, atoms, globalStore, setBlockFocus } from "@/store/global"; import * as services from "@/store/services"; import { getWebServerEndpoint } from "@/util/endpoints"; import * as keyutil from "@/util/keyutil"; -import * as layoututil from "@/util/layoututil"; import * as util from "@/util/util"; import clsx from "clsx"; import Color from "color"; @@ -194,29 +188,24 @@ function switchBlock(tabId: string, offsetX: number, offsetY: number) { const tabAtom = WOS.getWaveObjectAtom(WOS.makeORef("tab", tabId)); const layoutModel = getLayoutModelForTab(tabAtom); const curBlockId = globalStore.get(atoms.waveWindow)?.activeblockid; - const curBlockLeafId = layoututil.findLeafIdFromBlockId(layoutModel, curBlockId); - if (curBlockLeafId == null) { - return; - } - const blockPos = readBoundsFromTransform(layoutModel.getNodeTransformById(curBlockLeafId)); - if (blockPos == null) { - return; - } - var blockPositions: Map = new Map(); + const addlProps = globalStore.get(layoutModel.additionalProps); + const blockPositions: Map = new Map(); for (const leaf of layoutModel.leafs) { - if (leaf.id == curBlockLeafId) { - continue; - } - const pos = readBoundsFromTransform(layoutModel.getNodeTransform(leaf)); - if (pos != null) { + const pos = readBoundsFromTransform(addlProps[leaf.id]?.transform); + if (pos) { blockPositions.set(leaf.data.blockId, pos); } } + const curBlockPos = blockPositions.get(curBlockId); + if (!curBlockPos) { + return; + } + blockPositions.delete(curBlockId); const maxX = boundsMapMaxX(blockPositions); const maxY = boundsMapMaxY(blockPositions); const moveAmount = 10; - let curX = blockPos.x + 1; - let curY = blockPos.y + 1; + let curX = curBlockPos.x + 1; + let curY = curBlockPos.y + 1; while (true) { curX += offsetX * moveAmount; curY += offsetY * moveAmount; @@ -344,13 +333,8 @@ function genericClose(tabId: string) { return; } const layoutModel = getLayoutModelForTab(tabAtom); - const curBlockLeafId = layoututil.findLeafIdFromBlockId(layoutModel, activeBlockId); - const deleteAction: LayoutTreeDeleteNodeAction = { - type: LayoutTreeActionType.DeleteNode, - nodeId: curBlockLeafId, - }; - layoutModel.treeReducer(deleteAction); - services.ObjectService.DeleteBlock(activeBlockId); + const curBlockLeafId = layoutModel.getNodeByBlockId(activeBlockId)?.id; + layoutModel.closeNodeById(curBlockLeafId); } const simpleControlShiftAtom = jotai.atom(false); diff --git a/frontend/app/store/global.ts b/frontend/app/store/global.ts index a221951e..64ad9448 100644 --- a/frontend/app/store/global.ts +++ b/frontend/app/store/global.ts @@ -10,7 +10,6 @@ import { newLayoutNode, } from "@/layout/index"; import { getWebServerEndpoint, getWSServerEndpoint } from "@/util/endpoints"; -import * as layoututil from "@/util/layoututil"; import { produce } from "immer"; import * as jotai from "jotai"; import * as rxjs from "rxjs"; @@ -274,12 +273,19 @@ function handleWSEventMessage(msg: WSEventType) { break; } case LayoutTreeActionType.DeleteNode: { - const leafId = layoututil.findLeafIdFromBlockId(layoutModel, layoutAction.blockid); - const deleteNodeAction = { - type: LayoutTreeActionType.DeleteNode, - nodeId: leafId, - }; - layoutModel.treeReducer(deleteNodeAction); + const leaf = layoutModel?.getNodeByBlockId(layoutAction.blockid); + if (leaf) { + const deleteNodeAction = { + type: LayoutTreeActionType.DeleteNode, + nodeId: leaf.id, + }; + layoutModel.treeReducer(deleteNodeAction); + } else { + console.error( + "Cannot apply eventbus layout action DeleteNode, could not find leaf node with blockId", + layoutAction.blockid + ); + } break; } case LayoutTreeActionType.InsertNodeAtIndex: { diff --git a/frontend/layout/lib/layoutModel.ts b/frontend/layout/lib/layoutModel.ts index 5a4257a1..bf4f1852 100644 --- a/frontend/layout/lib/layoutModel.ts +++ b/frontend/layout/lib/layoutModel.ts @@ -590,6 +590,11 @@ export class LayoutModel { await this.onNodeDelete?.(node.data); } + async closeNodeById(nodeId: string) { + const nodeToDelete = findNode(this.treeState.rootNode, nodeId); + await this.closeNode(nodeToDelete); + } + onDrop() { if (this.getter(this.pendingAction.currentValueAtom)) { this.treeReducer({ @@ -687,6 +692,15 @@ export class LayoutModel { } } + getNodeByBlockId(blockId: string) { + for (const leaf of this.leafs) { + if (leaf.data.blockId === blockId) { + return leaf; + } + } + return null; + } + /** * Get a jotai atom containing the additional properties associated with a given node. * @param nodeId The ID of the node for which to retrieve the additional properties. diff --git a/frontend/util/layoututil.ts b/frontend/util/layoututil.ts deleted file mode 100644 index 868a461f..00000000 --- a/frontend/util/layoututil.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2024, Command Line Inc. -// SPDX-License-Identifier: Apache-2.0 - -import { LayoutModel } from "@/layout/index"; - -function findLeafIdFromBlockId(layoutModel: LayoutModel, blockId: string): string { - if (layoutModel?.leafs == null) { - return null; - } - for (const leaf of layoutModel.leafs) { - if (leaf.data.blockId == blockId) { - return leaf.id; - } - } - return null; -} - -function isBlockMagnified(layoutModel: LayoutModel, blockId: string): boolean { - if (layoutModel?.leafs == null || layoutModel.treeState.magnifiedNodeId == null) { - return false; - } - for (const leaf of layoutModel.leafs) { - if (leaf.data.blockId == blockId) { - return layoutModel.treeState.magnifiedNodeId == leaf.id; - } - } - return false; -} - -export { findLeafIdFromBlockId, isBlockMagnified };