The page
richtext
A rich text editor as an element, for writing mail and documents: bold,
italic, code, links, headings, lists, quotes and pictures, with a toolbar.
Its document is a value the program holds, not the page's DOM: the page
keeps it in state, sends it, makes mail of it (richtext.html,
richtext.text) and can apply edits made elsewhere (richtext.apply).
Several people can edit one document at once, through a server that keeps
their edits sealed and only orders them (below).
Written from nothing (runtime/page/richtext.mote, part RX; the document's
own runtime/shared/richtext.mote, part RXM; the merge,
runtime/shared/richmerge.mote, part RXO); a page that does not use it
does not carry it.
Examplea whole program
state doc: json = richtext.from("Hello Ada,\n\nThe plans are attached.")column gap=12 padding=16 richtext value=doc placeholder="Write your message…" aria-label="Message" height=280 text richtext.text(doc) white-space=pre-wrap color=dimAttributes#
Events#
Examplea whole program
shape Attached ref: string name: string type: string data: bytesstate doc: json = nonestate attached: Attached[] = []state sent = 0action took(p: Attached) attached.push(p)action edited(ops: json[]) sent += ops.lengthcolumn gap=8 padding=16 richtext value=doc picture=took edit=edited aria-label="Message" height=240 text `${attached.length} pictures, ${sent} operations` color=dimKeys#
The document#
Examplea whole program
state doc: json = {blocks: [{kind: "heading", level: 2, text: "Plans"}, {kind: "item", list: "number", text: "Draw them", marks: [{kind: "bold", from: 0, to: 4}]}, {kind: "paragraph", text: "Thanks", quote: 1}]}richtext value=doc aria-label="Plans" height=200Mail of it#
Operations#
Several people at once#
How: operations moved past each other, in the server's order#
The editor#
Examplea whole program
shape Stored seq: number blob: stringderive room = "plans"state words = ""state key: bytes = bytes.new(0)state seen = 0// Everyone begins with the same document: the list's entry 0, kept with the room when it was made.state entries: json[] = [{seq: 0, doc: {blocks: [{id: "start", kind: "heading", level: 1, text: "Plans"}]}}]state outbox: json[] = []state flushing = falsestate doc: json = none// The server keeps blobs in the order they come, numbered: it cannot read one.server fn post(room: string, blob: string): number let d = sql.open("rooms")? sql.run(d, "create table if not exists unit(room text, seq integer, blob text, primary key (room, seq))")? sql.run(d, "insert into unit select ?, coalesce(max(seq), 0) + 1, ? from unit where room = ?", [room, blob, room])? changed("since", room) return 1server live fn since(room: string, after: number): Stored[] let d = sql.open("rooms")? sql.run(d, "create table if not exists unit(room text, seq integer, blob text, primary key (room, seq))")? let rows: Stored[] = sql.run(d, "select seq, blob from unit where room = ? and seq > ? order by seq limit 500", [room, after])? return rowslive fresh: Stored[] = since(room, seen) else []action join key = crypto.argon2id(words, `the room ${room}`, {bytes: true})?// What came, opened, to the editor; and the next part of the list asked for.watch fresh let got: json[] = [] for e in fresh if e.seq > seen let all = bytes.base64(e.blob)? let unit: json = JSON.parse(crypto.aes.open(key, all.slice(0, 12), all.slice(12), {aad: room})?.text()) got.push({...unit, seq: e.seq}) if got.length entries = got seen = got[got.length - 1].seqaction send(unit: json) outbox.push(unit) flush()// Sealed and stored, one at a time; what could not be stored goes when the line is back.action flush if flushing return flushing = true while outbox.length let nonce = crypto.randomBytes(12) let sealed = crypto.aes.seal(key, nonce, JSON.stringify(outbox[0]), {aad: room}) else none let ok = sealed ? post(room, bytes.join([nonce, sealed]).base64()) else 0 : 0 if ok == 0 flushing = false return outbox.shift() flushing = falsewatch net.online if net.online flush()column gap=8 padding=16 if key.length == 0 input value=words placeholder="The room's passphrase" submit=join else richtext value=doc log=entries send=send aria-label="Plans" height=300Without the editor: the calls#
Examplea whole program
state a: json = nonestate b: json = nonestate list: json[] = []state said = ""action go // Two copies of one document (the same ids: one document, begun twice). let start = richtext.from("one two") a = richtext.copy(start) b = richtext.copy(start) let first = start.blocks[0].id a = richtext.edit(a, [{op: "insert", block: first, at: 3, text: " and"}]) b = richtext.edit(b, [{op: "mark", block: first, from: 0, to: 7, kind: "bold", on: true}]) list.push({...a.out, seq: 1}) list.push({...b.out, seq: 2}) a = richtext.take(a, list) b = richtext.take(b, list) said = `${richtext.text(a.doc)} · the same: ${JSON.stringify(a.doc) == JSON.stringify(b.doc)}`button "Merge" press=gotext saidWhat it does when two edit one thing#
What it does not do yet#
What was tested of it#
How it takes the keyboard#
Paste, drop, copy#
What a screen reader is told#
What was tested, and what was not#
What it is not#
Edit this page:
docs/kit/richtext.md