a tool for shared writing and social publishing
at feature/footnotes 43 lines 1.2 kB view raw
1import { EditorView } from "prosemirror-view"; 2import { v7 } from "uuid"; 3import { Replicache } from "replicache"; 4import type { ReplicacheMutators } from "src/replicache"; 5import { schema } from "./schema"; 6import { generateKeyBetween } from "fractional-indexing"; 7import { scanIndex } from "src/replicache/utils"; 8 9export async function insertFootnote( 10 view: EditorView, 11 blockID: string, 12 rep: Replicache<ReplicacheMutators>, 13 permissionSet: string, 14) { 15 let footnoteEntityID = v7(); 16 17 let existingFootnotes = await rep.query(async (tx) => { 18 let scan = scanIndex(tx); 19 return scan.eav(blockID, "block/footnote"); 20 }); 21 let lastPosition = 22 existingFootnotes.length > 0 23 ? existingFootnotes 24 .map((f) => f.data.position) 25 .sort() 26 .at(-1)! 27 : null; 28 let position = generateKeyBetween(lastPosition, null); 29 30 await rep.mutate.createFootnote({ 31 footnoteEntityID, 32 blockID, 33 permission_set: permissionSet, 34 position, 35 }); 36 37 let node = schema.nodes.footnote.create({ footnoteEntityID }); 38 let { from } = view.state.selection; 39 let tr = view.state.tr.insert(from, node); 40 view.dispatch(tr); 41 42 return footnoteEntityID; 43}