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
Returns
model/DocumentSchema

the document's schema.

Check if this storage contains a node with given id.

Returns
Boolean

true if a node with id exists, false otherwise.

Get a node or value via path.

Parameters
pathString|String[]

node id or path to property.

Returns
DocumentNode|any

a Node instance, a value or undefined if not found.

Returns
Object

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

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
importerFunction

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.

Create a node from the given data.

Parameters
plainObject

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.'
  })
})

Delete the node with given id.

Parameters
nodeIdString
Returns
DocumentNode

The deleted node.

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

Set a property to a new value.

Parameters
propertyString[]

path

newValueany
Returns
DocumentNode

The deleted node.

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

Update a property incrementally.

Parameters
propertyArray

path

diffObject
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].

Add a document index.

Parameters
nameString
indexDocumentIndex
Parameters
nameString
Returns
DocumentIndex

the node index with given name.

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

Parameters
selSelection

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
})
  • DEPRECATED: We will drop support as this should be done in a more
  •         controlled fashion using an importer.
    
  • @skip

Convert to JSON.

Returns
Object

Plain content.