Something went wrong. Try again.
leaflet fork for copy edit / bug fixes forked from leaflet.pub/leaflet
Something went wrong. Try again.
2.4 kB · 70 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import { create } from "zustand";import { Replicache } from "replicache";import { ReplicacheMutators } from "src/replicache";import { useUIState } from "src/useUIState";import { filterBlocksForZoom, getPageBlocks, isBlockHidden,} from "src/replicache/getBlocks";
export const useSelectingMouse = create(() => ({ start: null as null | string,}));
export const getViewBlocks = ( rep: Replicache<ReplicacheMutators>, page: string,) => filterBlocksForZoom( getPageBlocks(rep, page), useUIState.getState().zoomedBlocks[page], );
// Toolbar actions run against the selection, but the toolbar is also shown for// a block that has focus without ever having been selected (e.g. focused// programmatically after a block is created), so fall back to that block.export const getSelectedOrFocusedBlocks = async ( rep: Replicache<ReplicacheMutators>,) => { let [sortedBlocks] = await getSortedSelection(rep); if (sortedBlocks.length > 0) return sortedBlocks; let focused = useUIState.getState().focusedEntity; if (!focused || focused.entityType !== "block") return []; return getViewBlocks(rep, focused.parent).filter( (s) => s.entityID === focused.entityID, );};
export const getSortedSelection = async ( rep: Replicache<ReplicacheMutators>,) => { let selectedBlocks = useUIState.getState().selectedBlocks; if (!selectedBlocks[0]) return [[], []]; let siblings = getViewBlocks(rep, selectedBlocks[0].parent); let siblingIDs = new Set(siblings.map((block) => block.entityID)); let foldedBlocks = useUIState .getState() .foldedBlocks.filter((entity) => siblingIDs.has(entity)); let sortedBlocks = siblings.filter((s) => { let selected = selectedBlocks.find((sb) => sb.entityID === s.entityID); return selected; }); let sortedBlocksWithChildren = siblings.filter((s) => { let selected = selectedBlocks.find((sb) => sb.entityID === s.entityID); if (s.listData && !selected) { //Select the children of folded list blocks (in order to copy them) return s.listData.path.find( (p) => selectedBlocks.find((sb) => sb.entityID === p.entity) && foldedBlocks.includes(p.entity), ); } return selected; }); return [ sortedBlocks, siblings.filter((f) => !isBlockHidden(f, foldedBlocks)), sortedBlocksWithChildren, ];};