···44import { scanIndex } from "src/replicache/utils";
55import { getBlocksWithType } from "src/hooks/queries/useBlocks";
66import { focusBlock } from "src/utils/focusBlock";
77+import { UndoManager } from "src/undoManager";
7889export async function deleteBlock(
910 entities: string[],
1011 rep: Replicache<ReplicacheMutators>,
1212+ undoManager?: UndoManager,
1113) {
1214 // get what pagess we need to close as a result of deleting this block
1315 let pagesToClose = [] as string[];
···3234 }
3335 }
34363535- // the next and previous blocks in the block list
3636- // if the focused thing is a page and not a block, return
3737+ // figure out what to focus
3738 let focusedBlock = useUIState.getState().focusedEntity;
3839 let parent =
3940 focusedBlock?.entityType === "page"
···4445 let parentType = await rep?.query((tx) =>
4546 scanIndex(tx).eav(parent, "page/type"),
4647 );
4848+ // if the page is a canvas, focus the page
4749 if (parentType[0]?.data.value === "canvas") {
4850 useUIState
4951 .getState()
5052 .setFocusedBlock({ entityType: "page", entityID: parent });
5153 useUIState.getState().setSelectedBlocks([]);
5254 } else {
5555+ // if the page is a doc, focus the previous block (or if there isn't a prev block, focus the next block)
5356 let siblings =
5457 (await rep?.query((tx) => getBlocksWithType(tx, parent))) || [];
5558···105108 }
106109 }
107110111111+ // close the pages
108112 pagesToClose.forEach((page) => page && useUIState.getState().closePage(page));
113113+ undoManager && undoManager.startGroup();
114114+115115+ // delete the blocks
109116 await Promise.all(
110117 entities.map((entity) =>
111118 rep?.mutate.removeBlock({
···113120 }),
114121 ),
115122 );
123123+ undoManager && undoManager.endGroup();
116124}