Abstract class used for deriving a custom article implementation. Requires a model/DocumentSchema to be provided on construction.
import { Document } from 'substance'
class MyArticle extends Document
foo() {
}
}
model/DocumentSchema | the document's schema. |
Check if this storage contains a node with given id.
Boolean |
|
Get a node or value via path.
path | String|String[] | node id or path to property. |
DocumentNode|any | a Node instance, a value or undefined if not found. |
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.
importer | Function | a |
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.
plain | Object | node data. |
DocumentNode | The created node. |
doc.transaction(function(tx) {
tx.create({
id: 'p1',
type: 'paragraph',
content: 'Hi I am a Substance paragraph.'
})
})
Delete the node with given id.
nodeId | String |
DocumentNode | The deleted node. |
doc.transaction(function(tx) {
tx.delete('p1')
})
Set a property to a new value.
property | String[] | path |
newValue | any |
DocumentNode | The deleted node. |
doc.transaction(function(tx) {
tx.set(['p1', 'content'], "Hello there! I'm a new paragraph.")
})
Update a property incrementally.
property | Array | path |
diff | Object |
any | The value before applying the update. |
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.
name | String | |
index | DocumentIndex |
name | String |
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.
sel | Selection | An object describing the selection. |
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