Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140import { Replicache } from "replicache";import { ReplicacheMutators } from "src/replicache";import { useUIState } from "src/useUIState";import { scanIndex } from "src/replicache/utils";import { getPageBlocks } from "src/replicache/getBlocks";import { focusBlock } from "src/utils/focusBlock";import { UndoManager } from "src/undoManager";
export async function deleteBlock( entities: string[], rep: Replicache<ReplicacheMutators>, undoManager?: UndoManager,) { // get what pagess we need to close as a result of deleting this block let pagesToClose = [] as string[];
for (let entity of entities) { let [type] = await rep.query((tx) => scanIndex(tx).eav(entity, "block/type"), ); if (type.data.value === "card") { let [childPages] = await rep?.query( (tx) => scanIndex(tx).eav(entity, "block/card") || [], ); pagesToClose = [childPages?.data.value]; } if (type.data.value === "mailbox") { let [archive] = await rep?.query( (tx) => scanIndex(tx).eav(entity, "mailbox/archive") || [], ); let [draft] = await rep?.query( (tx) => scanIndex(tx).eav(entity, "mailbox/draft") || [], ); pagesToClose = [archive?.data.value, draft?.data.value]; } }
// the next and previous blocks in the block list // if the focused thing is a page and not a block, return let focusedBlock = useUIState.getState().focusedEntity; let parent = focusedBlock?.entityType === "page" ? focusedBlock.entityID : focusedBlock?.parent;
if (parent) { let parentType = await rep?.query((tx) => scanIndex(tx).eav(parent, "page/type"), ); // if the page is a canvas, focus the page if (parentType[0]?.data.value === "canvas") { useUIState .getState() .setFocusedBlock({ entityType: "page", entityID: parent }); useUIState.getState().setSelectedBlocks([]); } else { // if the page is a doc, focus the previous block (or if there isn't a prev block, focus the next block) let siblings = getPageBlocks(rep, parent);
let selectedBlocks = useUIState.getState().selectedBlocks; let firstSelected = selectedBlocks[0]; let lastSelected = selectedBlocks[entities.length - 1];
let prevBlock = siblings?.[ siblings.findIndex((s) => s.entityID === firstSelected?.entityID) - 1 ]; let prevBlockType = await rep?.query((tx) => scanIndex(tx).eav(prevBlock?.entityID, "block/type"), );
let nextBlock = siblings?.[ siblings.findIndex((s) => s.entityID === lastSelected.entityID) + 1 ]; let nextBlockType = await rep?.query((tx) => scanIndex(tx).eav(nextBlock?.entityID, "block/type"), );
if (prevBlock) { useUIState.getState().setSelectedBlock({ entityID: prevBlock.entityID, parent: prevBlock.parent, });
focusBlock( { entityID: prevBlock.entityID, type: prevBlockType?.[0].data.value, parent: prevBlock.parent, }, { type: "end" }, ); } else { useUIState.getState().setSelectedBlock({ entityID: nextBlock.entityID, parent: nextBlock.parent, });
focusBlock( { entityID: nextBlock.entityID, type: nextBlockType?.[0]?.data.value, parent: nextBlock.parent, }, { type: "start" }, ); } } }
// close the pages pagesToClose.forEach((page) => page && useUIState.getState().closePage(page));
// Footnote cleanup and block removal together form one undo step. let run = async () => { for (let entity of entities) { let footnotes = await rep.query((tx) => scanIndex(tx).eav(entity, "block/footnote"), ); for (let fn of footnotes) { await rep.mutate.deleteFootnote({ footnoteEntityID: fn.data.value, blockID: entity, }); } }
await Promise.all( entities.map((entity) => rep?.mutate.removeBlock({ blockEntity: entity, }), ), ); }; if (undoManager) await undoManager.withUndoGroup(run); else await run();}