Usage example
See this sync example.
Api
Document
Extends: EventEmitter
Emits: update
-
Document ⇐
EventEmitter
- new Document(id, doc)
-
instance
-
.id :
string
-
.doc :
Y.Doc
-
.content :
DocumentContent
-
.versions :
DocumentVersions
-
.docState :
Uint8Array
- .doc
- .init()
- .destroy()
-
.clone() ⇒
Document
- .applyUpdate(update, origin)
-
.updateDiff(otherDoc) ⇒
Uint8Array
- .equals(otherDocument)
- .transact(fn, origin)
- "update" (data, documentContent)
- "update" (data, document)
-
.id :
-
static
-
.encode(document) ⇒
Uint8Array
-
.decode(encodedDocument) ⇒
Document
-
.encode(document) ⇒
new Document(id, doc)
Creates a new Document.
Param | Type | Description |
---|---|---|
id | string |
document id |
doc | Y.Doc |
document doc instance |
string
document.id : Document id.
Y.Doc
document.doc : YJS Doc instance.
DocumentContent
document.content : Content representation.
DocumentVersions
document.versions : Versions for this document.
Uint8Array
document.docState :
document.doc
encoded as YJS update.
document.doc
Sets YJS document
Param | Type | Description |
---|---|---|
doc | Y.Doc |
YJS document |
document.init()
Initialize current doc. On events.
document.destroy()
Destroy current doc. Off events.
Document
document.clone() ⇒ Clone this document and return a new one.
document.applyUpdate(update, origin)
Param | Type |
---|---|
update | Uint8Array |
origin | any |
Uint8Array
document.updateDiff(otherDoc) ⇒ Computes differences between doc states and return diff as update.
Returns: Uint8Array
- diff update.
Param | Type | Description |
---|---|---|
otherDoc | Document |
Document to compare. |
document.equals(otherDocument)
Param | Type |
---|---|
otherDocument | Document |
document.transact(fn, origin)
Creates a transaction in wich the function
fn
will be executed
Param | Type | Description |
---|---|---|
fn | function |
Function to execute in a transaction |
origin | any |
Origin for this transaction |
"update" (data, documentContent)
Param | Type | Description |
---|---|---|
data | object |
|
data.events | Array.<Y.Event> |
list of events for this update. |
data.transaction | Y.Transaction |
transaction for this update. |
documentContent | DocumentContent |
DocumentContent updated. |
"update" (data, document)
Param | Type | Description |
---|---|---|
data | object |
|
data.update | Uint8Array |
Update applied. |
data.origin | any |
Origin for this update. |
document | Document |
Document updated. |
Uint8Array
Document.encode(document) ⇒ Encode a Document instance.
Returns: Uint8Array
- encoded document
Param | Type |
---|---|
document | Document |
Document
Document.decode(encodedDocument) ⇒ Decodes an encoded Document instance.
Returns: Document
- document
Param | Type |
---|---|
encodedDocument | Uint8Array |
DocumentContent
DocumentContent class for content
Extends: EventEmitter
Emits: DocumentContent#event:update
-
DocumentContent ⇐
EventEmitter
- new DocumentContent(document)
-
.document :
Document
-
.xmlFragment :
Y.XmlFragment
-
.length :
number
- .init()
- .destroy()
-
.equals(otherContent) ⇒
boolean
- .insert(index, content, [origin])
- .delete(index, [length], [origin])
-
.toString() ⇒
string
-
.fromMarkdown(markdown) ⇒
Document
-
.toMarkdown() ⇒
string
-
.toHtml() ⇒
string
new DocumentContent(document)
Creates a DocumentContent for a Document instance.
Param | Type | Description |
---|---|---|
document | Document |
document for this content |
Document
documentContent.document : Document for this content.
Y.XmlFragment
documentContent.xmlFragment : Y.XmlFragment content
number
documentContent.length : Content length.
documentContent.init()
Initialize content. On events.
documentContent.destroy()
Destroy current content. Off events.
boolean
documentContent.equals(otherContent) ⇒ Compares two DocumentContent instances.
Returns: boolean
- same
Param | Type |
---|---|
otherContent | DocumentContent |
documentContent.insert(index, content, [origin])
Inserts new content at an index.
Param | Type | Default | Description |
---|---|---|---|
index | number |
The index to insert content at | |
content | Array.<(YXmlElement|YXmlText)> |
The array of content | |
[origin] | object |
{} |
Origin extra data for transaction |
documentContent.delete(index, [length], [origin])
Deletes elements starting from an index.
Param | Type | Default | Description |
---|---|---|---|
index | number |
Index at which to start deleting elements | |
[length] | number |
1 |
The number of elements to remove. Defaults to 1 |
[origin] | object |
{} |
Origin extra data for transaction |
string
documentContent.toString() ⇒ Get the string representation of this content.
Returns: string
- The string representation of this content.
Document
documentContent.fromMarkdown(markdown) ⇒ Set current content based on
markdown
. Current content will be replaced.
Returns: Document
- document
Param | Type |
---|---|
markdown | string |
string
documentContent.toMarkdown() ⇒ Markdown representation of this content.
Returns: string
- Content as Markdown
string
documentContent.toHtml() ⇒ HTML representation of this content.
Returns: string
- Content as HTML
DocumentVersions
DocumentVersions class for document
-
DocumentVersions
-
.document :
Document
-
.versions :
Object.<string, version>
-
.currentVersionId :
string
-
.current :
version
-
.count :
number
- .currentVersionId
- .init()
- .destroy()
-
.create(name, [origin]) ⇒
version
-
.restore(versionId, [origin]) ⇒
version
-
.document :
Document
documentVersions.document : Document for this content.
Object.<string, version>
documentVersions.versions : Available versions.
string
documentVersions.currentVersionId : Current version id for
documentVersions.document
.
version
documentVersions.current : Current version for document.
number
documentVersions.count : Versions count.
documentVersions.currentVersionId
Sets current version id.
Param | Type | Description |
---|---|---|
currentVersionId | string |
id for current version. |
documentVersions.init()
Initialize versions. On events.
documentVersions.destroy()
Destroy current versions. Off events.
version
documentVersions.create(name, [origin]) ⇒ Creates and sets a new version as current. Current version will be freezed.
Returns: version
- Created and current version.
Param | Type | Default | Description |
---|---|---|---|
name | string |
Name for new version. | |
[origin] | object |
{} |
Origin extra data for transaction |
version
documentVersions.restore(versionId, [origin]) ⇒ Restore
#documentVersions.document
to a previous version.
Returns: version
- Restored version data.
Param | Type | Default | Description |
---|---|---|---|
versionId | string |
desired id version to restore. | |
[origin] | object |
{} |
Origin extra data for transaction |