Newer
Older
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
'use strict'
var SubstanceContainerEditor = require('substance/ui/ContainerEditor')
var Surface = require('substance/ui/Surface')
var uuid = require('substance/util/uuid')
function ContainerEditor () {
ContainerEditor.super.apply(this, arguments)
}
ContainerEditor.Prototype = function () {
this.render = function ($$) {
var el = Surface.prototype.render.call(this, $$)
var doc = this.getDocument()
var containerId = this.getContainerId()
var containerNode = doc.get(containerId)
if (!containerNode) {
console.warn('No container node found for ', containerId)
}
el.addClass('sc-container-editor container-node ' + containerId)
.attr({
spellCheck: false,
'data-id': containerId
})
// if it IS empty, handle in didMount
if (!this.isEmpty()) {
containerNode.getNodes().forEach(function (node) {
el.append(this._renderNode($$, node).ref(node.id))
}.bind(this))
}
if (!this.props.disabled) {
el.addClass('sm-enabled')
el.setAttribute('contenteditable', true)
}
return el
}
this.didMount = function () {
Surface.prototype.didMount.apply(this, arguments)
this.container.on('nodes:changed', this.onContainerChange, this)
if (this.isEmpty()) this.createText()
}
// create an empty paragraph with an empty node
// then select it for cursor focus
this.createText = function () {
var newSel
this.transaction(function (tx) {
var container = tx.get(this.props.containerId)
var textType = tx.getSchema().getDefaultTextType()
var node = tx.create({
id: uuid(textType),
type: textType,
content: ''
})
container.show(node.id)
newSel = tx.createSelection({
type: 'property',
path: [ node.id, 'content' ],
startOffset: 0,
endOffset: 0
})
}.bind(this))
this.rerender()
this.setSelection(newSel)
}
}
SubstanceContainerEditor.extend(ContainerEditor)
module.exports = ContainerEditor