import { generateKeyBetween } from "fractional-indexing"; import { v7 } from "uuid"; import { Replicache } from "replicache"; import type { Fact, ReplicacheMutators } from "src/replicache"; import { elementId } from "src/utils/elementId"; // Insert a new block after `position` (and before `nextPosition`) in `parent`, // returning the new block's entity ID. export async function addBlockBelow( rep: Replicache, args: { parent: string; position: string | null; nextPosition: string | null; permission_set: string; type: Fact<"block/type">["data"]["value"]; }, ): Promise { let newEntityID = v7(); await rep.mutate.addBlock({ parent: args.parent, factID: v7(), permission_set: args.permission_set, type: args.type, position: generateKeyBetween(args.position, args.nextPosition), newEntityID, }); return newEntityID; } export function focusNewTextBlock(entityID: string, maxAttempts = 30) { setTimeout(() => { let attempts = 0; let tick = () => { let el = document.getElementById(elementId.block(entityID).text); if (el) return el.focus(); if (++attempts >= maxAttempts) return; requestAnimationFrame(tick); }; tick(); }, 10); }