Rich text
A BcrdtText carries formatting as well as characters: marks such as bold or italic that occupy no
position in the text, merge like any other edit, and read back as a Quill-style delta. Two replicas of one
paragraph, each with a plain editor, formatting buttons and the formatted result rendered from that delta.
Two replicas, formatted
Select some words in either editor, then press Bold, Italic or Underline - the
buttons format the text you last selected in that panel. The editor itself stays plain, because a
<textarea> cannot show formatting; under it, Rendered is the same text drawn from
ToDelta(), and Delta is that delta step by step.
Opening two replicas…
Formatting is not a character
Format(index, length, attributes) does not touch the characters it formats. It places a mark at
each end of the run, and the marks take up no position: the character count, every caret and every index
stay exactly where they were, which is why the plain editors above are not disturbed when a word turns bold.
A mark is still a block in the document like any character, though, so it is sent, merged and undone the
same way - and text typed between two marks takes on their formatting, whoever typed it.
BcrdtText text = document.Text("rich"); text.Format(4, 11, new Dictionary<string, object?> { ["bold"] = true }); // null removes an attribute text.Insert(0, "Note: ", new Dictionary<string, object?> { ["italic"] = true }); text.InsertEmbed(text.Count, new Dictionary<string, object?> { ["image"] = "chart.png" }); IReadOnlyList<BcrdtDeltaOperation> delta = text.ToDelta(); // what an editor renders text.ApplyDelta(changeFromTheEditor); // what an editor reports back string plain = text.ToString(); // formatting and embeds left out
Attribute names and values are yours to choose - bold, a link's address, a colour - as long as
the values are ones a document can store. An embed - an image, a mention, a nested type - occupies exactly
one position.
Bringing your own editor
bit Bcrdt ships no rich-text editor component - BcrdtTextArea is plain text - but the delta is the
shape Quill and most editors built like it already speak, so the bridge is two calls. Outbound, render
ToDelta() once, then patch the editor with each change's own delta from Observe,
which says exactly what moved. Inbound, hand what the editor reports to ApplyDelta: a retain
leaves the underlying blocks alone, so concurrent edits inside a retained span survive - it is an edit, not a
replacement.
text.Observe((change, transaction) =>
{
if (!transaction.Local) editor.UpdateContents(change.Delta); // a collaborator's change
});
editor.OnTextChange(delta => text.ApplyDelta(delta)); // the user's change
There is no XML or tree type for ProseMirror-style editors: what this models is a run of text with marks and embeds, the Quill model.