KB1RD/matrix-notepad

Rich Text

Opened this issue · 0 comments

KB1RD commented

Rich text refers to having inline embedded content (such as mentions), block embedded content (such as images), and marks in the text (such as bold or italic). This could actually happen really soon. Like in a few days if I really wanted to go quickly. Why?

TL;DR: Since the algorithm is data-agnostic, I can push anything through. I did a bit of future-proofing in the event representation, so all I really need to do is stop being lazy.

Here's an example event:

{
  "type": "net.kb1rd.anchorlogoot0.ins",
  "sender": "@kb1rd:kb1rd.net",
  "content": {
    "a": {
      "v": 0,
      "o": []
    },
    "d": [
      "Hello"
    ],
    "c": 0
  }
}

(For reference, the a contains the anchors, which are undefined in this case, so it anchors to DocStart and DocEnd. d corresponds to the data. c corresponds to the Lamport clock)

If you look closely, you'll notice something odd: The data is actually an array of strings. Why? Well, because you can't embed an object in a string. This is future-proofing to allow individual characters to be efficiently mixed with objects. If one wishes to embed one or more objects in the string, they could add an array of objects to the current data array. On the receiving end, each element in the data array would be merged end-to-end. It's easier to see an example:

{
  "type": "net.kb1rd.anchorlogoot0.ins",
  "sender": "@kb1rd:kb1rd.net",
  "content": {
    "a": {
      "v": 0,
      "o": []
    },
    "d": [
      "Hi ",
      [ { "type": "net.kb1rd.image-block", "url": "mxc://myimage" } ]
    ],
    "c": 0
  }
}

I could just as easily make that an array of characters, but the JSON representation would be less efficient. You may be wondering why I need the array brackets around the object. The reason for this is because it lets the clients know that each element of that array is an atom and cannot be broken. It is still algorithmically correct to distinguish between a string that is a single atom and a string that is a set of atoms. So, if you wanted to send a single-atom string (though most clients would likely discard it), you could set "d": ["each letter is an atom", ["this is an atom"] ].