-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathToggleListCommand.js
85 lines (79 loc) · 2.33 KB
/
ToggleListCommand.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Command } from 'substance'
// TODO: move manipulation code into ArticleAPI
export default class ToggleListCommand extends Command {
isSwitchTypeCommand () { return true }
// TODO: do we want to generalize this to other list types?
getType () {
return 'list'
}
/*
Note: this implementation is still very coupled with the specific internal data model.
TODO: we could try to use API to generalize this
*/
getCommandState (params) {
let editorSession = params.editorSession
let doc = editorSession.getDocument()
let sel = editorSession.getSelection()
if (sel && sel.isPropertySelection()) {
let path = sel.path
let node = doc.get(path[0])
if (node) {
if (node.isListItem()) {
let level = node.getLevel()
let list = node.getParent()
let listType = list.getListType(level)
let active = listType === this.config.spec.listType
let action = active ? 'toggleList' : 'setListType'
let listId = list.id
return {
disabled: false,
active,
action,
listId,
level
}
} else if (node.isText()) {
return {
disabled: false,
action: 'switchTextType'
}
}
}
}
return { disabled: true }
}
/*
Note: this implementation is still very coupled with the specific internal data model.
TODO: we could try to use API to generalize this
*/
execute (params) {
let commandState = params.commandState
const { disabled, action } = commandState
if (disabled) return
let editorSession = params.editorSession
switch (action) {
case 'toggleList': {
editorSession.transaction((tx) => {
tx.toggleList()
}, { action: 'toggleList' })
break
}
case 'setListType': {
const { listId, level } = commandState
editorSession.transaction((tx) => {
let list = tx.get(listId)
list.setListType(level, this.config.spec.listType)
}, { action: 'setListType' })
break
}
case 'switchTextType': {
editorSession.transaction((tx) => {
tx.toggleList({ listType: this.config.spec.listType })
}, { action: 'toggleList' })
break
}
default:
//
}
}
}