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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
*
* @author Julius Härtl <jus@bitgrid.net>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { ListItem as TiptapListItem } from 'tiptap-extensions'
import { Plugin } from 'tiptap'
import { toggleList, wrappingInputRule } from 'tiptap-commands'
import { findParentNode, findParentNodeClosestToPos } from 'prosemirror-utils'
const TYPES = {
BULLET: 0,
CHECKBOX: 1,
}
const getParentList = (schema, selection) => {
return findParentNode(function(node) {
return node.type === schema.nodes.list_item
})(selection)
}
export default class ListItem extends TiptapListItem {
get defaultOptions() {
return {
nested: true,
}
}
get schema() {
return {
attrs: {
done: {
default: false,
},
type: {
default: TYPES.BULLET,
},
},
draggable: false,
content: 'paragraph block*',
toDOM: node => {
if (node.attrs.type === TYPES.BULLET) {
return ['li', 0]
}
const listAttributes = { class: 'checkbox-item' }
const checkboxAttributes = { type: 'checkbox', class: '', contenteditable: false }
if (node.attrs.done) {
checkboxAttributes.checked = true
listAttributes.class += ' checked'
}
return [
'li',
listAttributes,
[
'input',
checkboxAttributes,
],
[
'label',
0,
],
]
},
parseDOM: [
{
priority: 100,
tag: 'li',
getAttrs: el => {
const checkbox = el.querySelector('input[type=checkbox]')
return { done: checkbox && checkbox.checked, type: checkbox ? TYPES.CHECKBOX : TYPES.BULLET }
},
},
],
toMarkdown: (state, node) => {
if (node.attrs.type === TYPES.CHECKBOX) {
state.write(`[${node.attrs.done ? 'x' : ' '}] `)
}
state.renderContent(node)
},
}
}
commands({ type, schema }) {
return {
bullet_list_item: () => {
return (state, dispatch, view) => {
return toggleList(schema.nodes.bullet_list, type)(state, dispatch, view)
}
},
todo_item: () => {
return (state, dispatch, view) => {
const schema = state.schema
const selection = state.selection
const $from = selection.$from
const $to = selection.$to
const range = $from.blockRange($to)
let tr = state.tr
let parentList = getParentList(schema, selection)
if (typeof parentList === 'undefined') {
toggleList(schema.nodes.bullet_list, type)(state, (_transaction) => {
tr = _transaction
}, view)
parentList = getParentList(schema, tr.selection)
}
if (!range || typeof parentList === 'undefined') {
return false
}
tr.setNodeMarkup(parentList.pos, schema.nodes.list_item, { type: parentList.node.attrs.type === TYPES.CHECKBOX ? TYPES.BULLET : TYPES.CHECKBOX })
tr.scrollIntoView()
if (dispatch) {
dispatch(tr)
}
}
},
}
}
inputRules({ type }) {
return [
wrappingInputRule(/^\s*([-+*])\s(\[ ?\])\s$/, type, (match) => {
return {
type: TYPES.CHECKBOX,
}
}),
wrappingInputRule(/^\s*([-+*])\s(\[(x|X)\])\s$/, type, (match) => {
return {
type: TYPES.CHECKBOX,
done: true,
}
}),
wrappingInputRule(/^\s*([-+*])\s[^\s[]$/, type),
]
}
get plugins() {
return [
new Plugin({
props: {
handleClick: (view, pos, event) => {
const state = view.state
const schema = state.schema
const coordinates = view.posAtCoords({ left: event.clientX, top: event.clientY })
const position = state.doc.resolve(coordinates.pos)
const parentList = findParentNodeClosestToPos(position, function(node) {
return node.type === schema.nodes.list_item
})
const isListClicked = event.target.tagName.toLowerCase() === 'li'
if (typeof parentList === 'undefined' || parentList.node.attrs.type !== TYPES.CHECKBOX || !isListClicked) {
return
}
const tr = state.tr
tr.setNodeMarkup(parentList.pos, schema.nodes.list_item, { done: !parentList.node.attrs.done, type: TYPES.CHECKBOX })
view.dispatch(tr)
},
},
}),
]
}
}
|