-
Notifications
You must be signed in to change notification settings - Fork 29
Text operations
The text type is much simpler & faster than the JSON OT type for editing plaintext documents.
Ops are lists of components which scan over the document and can edit it as they go. Each component in the list is either:
- Number N: Skip forward N characters in the document
- "str": Insert "str" at the current position in the document
- {d:N}: Delete N characters at the current position in the document
Unlike the old text-composable type on which it is based, the operation does not have to skip the last characters in the document.
Document snapshots are strings.
Like other text types, selection ranges are either a single number (the cursor position) or a pair of [anchor, focus] numbers (aka [start, end]) of the selection range. Be aware that end can be before start.
Eg:
This operation: [1, 'hi', 2, {d:3}] will skip the first character, insert 'hi', skip 2 more characters then delete the next 3 characters. If applied to "ABCDEFG", that operation would produce "AhiBCG"
There is also a C implementation of the text OT type here which is insanely fast. They are almost the same, except javascript counts characters using 16 bit words and the C implementation counts characters using UTF8 codepoints. This means that if you have any characters in the astral plane in your document, cursor positions won't make sense. See here for more information.