Use the Y.Text type to share text content. The shared content can be bound to
Ace, CodeMirror, Monaco, or any HTML
input element (e.g. <input>, <textarea>, any element that has the contenteditable property)
// bind text to the first p element that is contenteditabley.share.text.bind(document.querySelector("p[contenteditable]"))
Use it!
Retrieve this with bower or npm.
Bower
bower install y-text --save
NPM
npm install y-text y-array --save
This type depends on y-array. So you have to
extend Yjs in the right order:
var Y = require('yjs')require('y-array')(Y)require('y-text')(Y)
Text Object
Reference
.insert(position, string)
Insert a string at a position
.delete(position, length)
Delete a substring. The length parameter is optional and defaults to 1
.get(i)
Retrieve the i-th character
.toString()
Return the content as a String
.bindTextarea(htmlElement)
Supports textareas, inputs, and any contenteditable element
aceClass: In case is not defined on the global object
A note on intention preservation
If two users insert something at the same position concurrently, the content
that was inserted by the user with the higher user-id will be to the right of
the other content. In the OT world we often speak of intention preservation,
which is very loosely defined in most cases. This type has the following notion
of intention preservation: When a user inserts content c after a set of
content C_left, and before a set of content C_right, then C_left will be
always to the left of c, and C_right will be always to the right of c. This
property will also hold when content is deleted or when a deletion is undone.
A note on time complexities
.insert(position, contents)
O(position + |contents|)
.toString()
O(this.length)
Apply an insert operation from another user
Yjs does not transform against operations that do not conflict with each
other.
An operation conflicts with another operation if it intends to be inserted
at the same position.
Overall worst case complexety: O(|conflicts|!)
Contribution
We thank @NathanaelA who sponsored the bindings to the Monaco editor!