-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathTaskItem.ts
More file actions
139 lines (124 loc) Β· 3.27 KB
/
Copy pathTaskItem.ts
File metadata and controls
139 lines (124 loc) Β· 3.27 KB
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
/**
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { Node } from '@tiptap/pm/model'
import type { MarkdownSerializerState } from 'prosemirror-markdown'
import { mergeAttributes, wrappingInputRule } from '@tiptap/core'
import { TaskItem as TipTapTaskItem } from '@tiptap/extension-list'
import { Plugin } from '@tiptap/pm/state'
import { findParentNodeClosestToPos } from './../helpers/prosemirrorUtils.js'
const TaskItem = TipTapTaskItem.extend({
addOptions() {
return {
nested: true,
HTMLAttributes: {},
taskListTypeName: 'taskList',
}
},
draggable: false,
content: 'paragraph block*',
addAttributes() {
const adjust = { ...this.parent?.() }
adjust.checked.parseHTML = (el) => {
return (el.querySelector('input[type=checkbox]') as HTMLInputElement)
?.checked
}
return adjust
},
parseHTML() {
return [
{
priority: 101,
tag: 'li',
getAttrs: (el) => {
const checkbox = el.querySelector('input[type=checkbox]')
return checkbox
},
context: 'taskList/',
},
]
},
renderHTML({ node, HTMLAttributes }) {
const listAttributes = {
class: `task-list-item checkbox-item${node.attrs.checked ? ' checked' : ''}`,
} as const
const checkboxAttributes = {
type: 'checkbox',
class: '',
contenteditable: false,
...(node.attrs.checked ? { checked: true } : {}),
} as const
return [
'li',
mergeAttributes(HTMLAttributes, listAttributes),
['input', checkboxAttributes],
['div', { class: 'task-item-content' }, 0],
]
},
// overwrite the parent node view so renderHTML gets used
addNodeView: () => null,
// @ts-expect-error - toMarkdown is a custom field not part of the official Tiptap API
toMarkdown: (state: MarkdownSerializerState, node: Node) => {
state.write(`[${node.attrs.checked ? 'x' : ' '}] `)
state.renderContent(node)
},
addInputRules() {
return [
...(this.parent?.() || []),
wrappingInputRule({
find: /^\s*([-+*])\s(\[(x|X|\s)?\])\s$/,
type: this.type,
getAttributes: (match) => ({
checked: 'xX'.includes(match[match.length - 1]),
}),
}),
]
},
addProseMirrorPlugins() {
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,
})
if (!coordinates) {
return
}
const position = state.doc.resolve(coordinates.pos)
const parentList = findParentNodeClosestToPos(
position,
function (node: Node) {
return (
node.type === schema.nodes.taskItem
|| node.type === schema.nodes.listItem
)
},
)
const isListClicked =
event.target instanceof Element
&& event.target.tagName.toLowerCase() === 'li'
if (
!isListClicked
|| !parentList
|| parentList.node.type !== schema.nodes.taskItem
|| !view.editable
) {
return
}
const tr = state.tr
tr.setNodeMarkup(parentList.pos, schema.nodes.taskItem, {
checked: !parentList.node.attrs.checked,
})
view.dispatch(tr)
},
},
}),
]
},
})
export default TaskItem