a tool for shared writing and social publishing
1import { create } from "zustand";
2import { Replicache } from "replicache";
3import { ReplicacheMutators } from "src/replicache";
4import { useUIState } from "src/useUIState";
5import { getBlocksWithType } from "src/hooks/queries/useBlocks";
6
7export const useSelectingMouse = create(() => ({
8 start: null as null | string,
9}));
10
11export const getSortedSelection = async (
12 rep: Replicache<ReplicacheMutators>,
13) => {
14 let selectedBlocks = useUIState.getState().selectedBlocks;
15 let foldedBlocks = useUIState.getState().foldedBlocks;
16 if (!selectedBlocks[0]) return [[], []];
17 let siblings =
18 (await rep?.query((tx) =>
19 getBlocksWithType(tx, selectedBlocks[0].parent),
20 )) || [];
21 let sortedBlocks = siblings.filter((s) => {
22 let selected = selectedBlocks.find((sb) => sb.value === s.value);
23 return selected;
24 });
25 let sortedBlocksWithChildren = siblings.filter((s) => {
26 let selected = selectedBlocks.find((sb) => sb.value === s.value);
27 if (s.listData && !selected) {
28 //Select the children of folded list blocks (in order to copy them)
29 return s.listData.path.find(
30 (p) =>
31 selectedBlocks.find((sb) => sb.value === p.entity) &&
32 foldedBlocks.includes(p.entity),
33 );
34 }
35 return selected;
36 });
37 return [
38 sortedBlocks,
39 siblings.filter(
40 (f) =>
41 !f.listData ||
42 !f.listData.path.find(
43 (p) => foldedBlocks.includes(p.entity) && p.entity !== f.value,
44 ),
45 ),
46 sortedBlocksWithChildren,
47 ];
48};