Fix Cmd:W to delete block (#238)

This commit is contained in:
Evan Simkowitz 2024-08-16 11:40:10 -07:00 committed by GitHub
parent a23dd67805
commit b0a09db4d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 67 deletions

View File

@ -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<Tab>(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<string, Bounds> = new Map();
const addlProps = globalStore.get(layoutModel.additionalProps);
const blockPositions: Map<string, Bounds> = 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);

View File

@ -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: {

View File

@ -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.

View File

@ -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 };