-
Notifications
You must be signed in to change notification settings - Fork 7
/
content-hints.js
49 lines (42 loc) · 1.17 KB
/
content-hints.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
/*
* Plugin to manage events for content hint buttons
*/
import _ from '../util/lodash'
// The plugin
export default class PluginContentHints {
constructor (pm, options) {
this.boundOnDocChanged = this.onDocChanged.bind(this)
this.debouncedDocChanged = _.debounce(this.boundOnDocChanged, 500)
this.pm = pm
this.ed = options.ed
this.updater = pm.updateScheduler([pm.on.change], this.debouncedDocChanged)
this.updater.force()
}
detach () {
this.updater.detach()
}
onDocChanged () {
// Should use debounced version
const doc = this.pm.doc
let hasCover = false
let hasFold = false
doc.forEach(function (node, offset, index) {
const {name} = node.type
if (name === 'media') {
const {type} = node.attrs
if (type === 'image' || type === 'placeholder') {
hasCover = true
}
}
if (name === 'horizontal_rule') {
hasFold = true
}
})
this.ed.trigger('plugin.contenthints', {hasCover, hasFold})
// Signal widgets initialized if first
if (!this.initialized) {
this.initialized = true
this.ed.trigger('plugin.contenthints.initialized')
}
}
}