Something went wrong. Try again.
a tool for shared writing and social publishing
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243import { 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<ReplicacheMutators>, args: { parent: string; position: string | null; nextPosition: string | null; permission_set: string; type: Fact<"block/type">["data"]["value"]; },): Promise<string> { 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);}