Rich text / foldkit-richtext
Clipboard slices
Clipboard content is semantic, not HTML. A Slice is a versioned fragment with
its own identities:
const slice = RichText.sliceOf(document, selection) // → Slice | undefined
RichText.serializeSlice(slice) // → string for the clipboard
RichText.deserializeSlice(payload) // → Slice | undefined
RichText.withFreshIds(slice, mint) // identities that cannot collide
RichText.sliceFromText('plain\npaste', mint) // the text fallbacksliceOf takes a whole block for a node selection, and for a range only the
covered part of each touched block, with runs trimmed to the selection — so a
partial copy never drags in an untouched block. deserializeSlice returns
undefined for anything this version did not write (bad JSON, another version,
excess fields, duplicate identities inside the slice) rather than guessing.
Identities always come from the caller, like every other identity a live edit
mints.
Paste is a command ({ type: 'Paste', slice }): it remints every identity, then
places the content relative to the caret — above the block at its start, below
it at its end, and mid-block by splitting the block and landing the content
between the halves, so text after the caret stays below what was pasted. A range
selection is replaced first, and the caret lands at the end of the pasted text
(or at the start of what follows when the pasted content has none). The DOM
clipboard events that carry these payloads are not wired yet.