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
if (slice !== undefined) {
RichText.serializeSlice(slice) // → string for the clipboard
RichText.withFreshIds(slice, mint) // identities that cannot collide
}
RichText.deserializeSlice(payload) // → Slice | undefined
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
splits the caret's block and lands the content between the halves. The first
pasted block joins the text before the caret and the last joins the text after
it, when each can be one block with it, so a copied word or line lands inside the
sentence and several lines keep the sentence's two ends. A block that cannot join
(an image, a table, a list) stands between the halves, and a half left empty goes,
except the line after a pasted image or table, which is where writing goes on.
Into a kind whose text keeps line breaks (code), the paste is its plain text. A
range selection is replaced first, and the caret lands at the end of the pasted
text. foldkit-richtext-dom reads the clipboard into a slice (its own payload,
then HTML, then plain text) and sends this command.