Document
Abstract Class defined in model/Document.js#55 inherits from

Abstract class used for deriving a custom article implementation. Requires a model/DocumentSchema to be provided on construction.

Example

    import { Document } from 'substance'

    class MyArticle extends Document
      foo() {

      }
    }

new Document()
Constructor defined in model/Document.js#57
this.getSchema()
Method defined in model/Document.js#108
Returns

model/DocumentSchema | the document's schema.

this.contains()
Method defined in model/Document.js#117

Check if this storage contains a node with given id.

Returns

Boolean | `true` if a node with id exists, `false` otherwise.

this.get(path)
Method defined in model/Document.js#127

Get a node or value via path.

Parameters

path | String|String[] | node id or path to property.

Returns

DocumentNode|any | a Node instance, a value or undefined if not found.

this.getNodes()
Method defined in model/Document.js#134
Returns

Object | A hash of {@link model/DocumentNode} instances.

this.import(importer)
Method defined in model/Document.js#173

Creates a context like a transaction for importing nodes. This is important in presence of cyclic dependencies. Indexes will not be updated during the import but will afterwards when all nodes are have been created.

Parameters

importer | Function | a `function(doc)`, where with `doc` is a `model/AbstractDocument`

Example

Consider the following example from our documentation generator: We want to have a member index, which keeps track of members of namespaces, modules, and classes. grouped by type, and in the case of classes, also grouped by 'instance' and 'class'.

    ui
      - class
        - ui/Component
    ui/Component
      - class
        - method
          - mount
      - instance
        - method
          - render

To decide which grouping to apply, the parent type of a member needs to be considered. Using an incremental approach, this leads to the problem, that the parent must exist before the child. At the same time, e.g. when deserializing, the parent has already a field with all children ids. This cyclic dependency is best address, by turning off all listeners (such as indexes) until the data is consistent.

this.create(plain)
Method defined in model/Document.js#202

Create a node from the given data.

Parameters

plain | Object | node data.

Returns

DocumentNode | The created node.

Example

    doc.transaction(function(tx) {
      tx.create({
        id: 'p1',
        type: 'paragraph',
        content: 'Hi I am a Substance paragraph.'
      })
    })

this.delete(nodeId)
Method defined in model/Document.js#227

Delete the node with given id.

Parameters

nodeId | String |

Returns

DocumentNode | The deleted node.

Example

    doc.transaction(function(tx) {
      tx.delete('p1')
    })

this.set(property, newValue)
Method defined in model/Document.js#251

Set a property to a new value.

Parameters

property | String[] | path
newValue | any |

Returns

DocumentNode | The deleted node.

Example

    doc.transaction(function(tx) {
      tx.set(['p1', 'content'], "Hello there! I'm a new paragraph.")
    })

this.update(property, diff)
Method defined in model/Document.js#294

Update a property incrementally.

Parameters

property | Array | path
diff | Object |

Returns

any | The value before applying the update.

Example

Inserting text into a string property:

    doc.update(['p1', 'content'], { insert: {offset: 3, value: "fee"} })

would turn "Foobar" into "Foofeebar".

Deleting text from a string property:

    doc.update(['p1', 'content'], { delete: {start: 0, end: 3} })

would turn "Foobar" into "bar".

Inserting into an array:

    doc.update(['p1', 'content'], { insert: {offset: 2, value: 0} })

would turn `[1,2,3,4]` into `[1,2,0,3,4]`.

Deleting from an array:

    doc.update(['body', 'nodes'], { delete: 2 })

would turn `[1,2,3,4]` into `[1,2,4]`.

this.addIndex(name, index)
Method defined in model/Document.js#308

Add a document index.

Parameters

name | String |
index | DocumentIndex |

this.getIndex(name)
Method defined in model/Document.js#316
Parameters

name | String |

Returns

DocumentIndex | the node index with given name.

this.createSelection(sel)
Method defined in model/Document.js#358

Creates a selection which is attached to this document. Every selection implementation provides its own parameter format which is basically a JSON representation.

Parameters

sel | Selection | An object describing the selection.

Example

Creating a PropertySelection:

    doc.createSelection([ 'text1', 'content'], 10, 20)

Creating a ContainerSelection:

    doc.createSelection('main', [ 'p1', 'content'], 10, [ 'p2', 'content'], 20)

Creating a NullSelection:

    doc.createSelection(null)

You can also call this method with JSON data

    doc.createSelection({
      type: 'property',
      path: [ 'p1', 'content'],
      startOffset: 10,
      endOffset: 20
    })

this.loadSeed()
Method defined in model/Document.js#426

- DEPRECATED: We will drop support as this should be done in a more

-             controlled fashion using an importer.

- @skip

this.toJSON()
Method defined in model/Document.js#442

Convert to JSON.

Returns

Object | Plain content.