@wirelineio/document v1.0.4
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)
- static
- .encode(document) ⇒ Uint8Array
- .decode(encodedDocument) ⇒ Document
new Document(id, doc)
Creates a new Document.
Param | Type | Description |
---|---|---|
id | string | document id |
doc | Y.Doc | document doc instance |
document.id : string
Document id.
document.doc : Y.Doc
YJS Doc instance.
document.content : DocumentContent
Content representation.
document.versions : DocumentVersions
Versions for this document.
document.docState : Uint8Array
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.clone() ⇒ Document
Clone this document and return a new one.
document.applyUpdate(update, origin)
Param | Type |
---|---|
update | Uint8Array |
origin | any |
document.updateDiff(otherDoc) ⇒ Uint8Array
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. |
Document.encode(document) ⇒ Uint8Array
Encode a Document instance.
Returns: Uint8Array - encoded document
Param | Type |
---|---|
document | Document |
Document.decode(encodedDocument) ⇒ Document
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 |
documentContent.document : Document
Document for this content.
documentContent.xmlFragment : Y.XmlFragment
Y.XmlFragment content
documentContent.length : number
Content length.
documentContent.init()
Initialize content. On events.
documentContent.destroy()
Destroy current content. Off events.
documentContent.equals(otherContent) ⇒ boolean
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 |
documentContent.toString() ⇒ string
Get the string representation of this content.
Returns: string - The string representation of this content.
documentContent.fromMarkdown(markdown) ⇒ Document
Set current content based on
markdown
. Current content will be replaced.
Returns: Document - document
Param | Type |
---|---|
markdown | string |
documentContent.toMarkdown() ⇒ string
Markdown representation of this content.
Returns: string - Content as Markdown
documentContent.toHtml() ⇒ string
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
documentVersions.document : Document
Document for this content.
documentVersions.versions : Object.<string, version>
Available versions.
documentVersions.currentVersionId : string
Current version id for
documentVersions.document
.
documentVersions.current : version
Current version for document.
documentVersions.count : number
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.
documentVersions.create(name, origin) ⇒ version
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 |
documentVersions.restore(versionId, origin) ⇒ version
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 |