-
Notifications
You must be signed in to change notification settings - Fork 7
/
commands-interface.js
50 lines (43 loc) · 1.19 KB
/
commands-interface.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
import {edCommands} from '../menu/ed-menu'
function makeCommands (state) {
let commands = {}
let keys = Object.keys(edCommands)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
const item = edCommands[key]
commands[key] = makeCommand(item, state)
}
return commands
}
function makeCommand (item, state) {
let commandState = 'inactive'
let command = item.spec
if (command.active && command.active(state)) {
commandState = 'active'
}
if (command.select && command.select(state) === false) {
commandState = 'disabled'
}
if (command.class === 'flaggedFeature') {
commandState = 'flagged'
}
return commandState
}
export default {
state: {
init: function (config, state) {
const {ed} = this.options.edStuff
if (!ed.onCommandsChanged) {
throw new Error('Should not init this plugin without Ed onCommandsChanged option.')
}
const commands = makeCommands(state)
ed.onCommandsChanged(commands)
},
apply: function (transaction, value, prev, state) {
const {ed} = this.options.edStuff
const commands = makeCommands(state)
ed.onCommandsChanged(commands)
return transaction
},
},
}