-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathValueModel.js
64 lines (51 loc) · 1.26 KB
/
ValueModel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { isNil, getKeyForPath } from 'substance'
import { throwMethodIsAbstract } from '../shared'
export default class ValueModel {
constructor (api, path) {
this._api = api
this._path = path
}
get id () {
return getKeyForPath(this._path)
}
get type () {
throwMethodIsAbstract()
}
getPath () {
return this._path
}
// EXPERIMENTAL: a third kind of path, which is [<type>, <prop-name>]
_getPropertySelector () {
if (!this._selector) {
let doc = this._api.getDocument()
let node = doc.get(this._path[0])
this._selector = [node.type].concat(this._path.slice(1))
}
return this._selector
}
hasTargetType (name) {
return false
}
getValue () {
return this._api.getDocument().get(this._path)
}
setValue (val) {
// TODO this should go into API
let api = this._api
api.getEditorSession().transaction(tx => {
tx.set(this._path, val)
tx.setSelection(api._createValueSelection(this._path))
})
}
getSchema () {
return this._api.getDocument().getProperty(this._path)
}
isEmpty () {
return isNil(this.getValue())
}
_resolveId (id) {
return this._api.getDocument().get(id)
}
get _value () { return this.getValue() }
get _isValue () { return true }
}