@tiptap/extension-list 3.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/README.md +18 -0
  2. package/dist/bullet-list/index.cjs +93 -0
  3. package/dist/bullet-list/index.cjs.map +1 -0
  4. package/dist/bullet-list/index.d.cts +51 -0
  5. package/dist/bullet-list/index.d.ts +51 -0
  6. package/dist/bullet-list/index.js +65 -0
  7. package/dist/bullet-list/index.js.map +1 -0
  8. package/dist/index.cjs +714 -0
  9. package/dist/index.cjs.map +1 -0
  10. package/dist/index.d.cts +288 -0
  11. package/dist/index.d.ts +288 -0
  12. package/dist/index.js +683 -0
  13. package/dist/index.js.map +1 -0
  14. package/dist/item/index.cjs +62 -0
  15. package/dist/item/index.cjs.map +1 -0
  16. package/dist/item/index.d.cts +29 -0
  17. package/dist/item/index.d.ts +29 -0
  18. package/dist/item/index.js +35 -0
  19. package/dist/item/index.js.map +1 -0
  20. package/dist/keymap/index.cjs +308 -0
  21. package/dist/keymap/index.cjs.map +1 -0
  22. package/dist/keymap/index.d.cts +63 -0
  23. package/dist/keymap/index.d.ts +63 -0
  24. package/dist/keymap/index.js +286 -0
  25. package/dist/keymap/index.js.map +1 -0
  26. package/dist/kit/index.cjs +692 -0
  27. package/dist/kit/index.cjs.map +1 -0
  28. package/dist/kit/index.d.cts +200 -0
  29. package/dist/kit/index.d.ts +200 -0
  30. package/dist/kit/index.js +673 -0
  31. package/dist/kit/index.js.map +1 -0
  32. package/dist/ordered-list/index.cjs +113 -0
  33. package/dist/ordered-list/index.cjs.map +1 -0
  34. package/dist/ordered-list/index.d.cts +52 -0
  35. package/dist/ordered-list/index.d.ts +52 -0
  36. package/dist/ordered-list/index.js +85 -0
  37. package/dist/ordered-list/index.js.map +1 -0
  38. package/dist/task-item/index.cjs +177 -0
  39. package/dist/task-item/index.cjs.map +1 -0
  40. package/dist/task-item/index.d.cts +41 -0
  41. package/dist/task-item/index.d.ts +41 -0
  42. package/dist/task-item/index.js +149 -0
  43. package/dist/task-item/index.js.map +1 -0
  44. package/dist/task-list/index.cjs +69 -0
  45. package/dist/task-list/index.cjs.map +1 -0
  46. package/dist/task-list/index.d.cts +34 -0
  47. package/dist/task-list/index.d.ts +34 -0
  48. package/dist/task-list/index.js +42 -0
  49. package/dist/task-list/index.js.map +1 -0
  50. package/package.json +109 -0
  51. package/src/bullet-list/bullet-list.ts +126 -0
  52. package/src/bullet-list/index.ts +1 -0
  53. package/src/index.ts +7 -0
  54. package/src/item/index.ts +1 -0
  55. package/src/item/list-item.ts +64 -0
  56. package/src/keymap/index.ts +2 -0
  57. package/src/keymap/list-keymap.ts +106 -0
  58. package/src/keymap/listHelpers/findListItemPos.ts +30 -0
  59. package/src/keymap/listHelpers/getNextListDepth.ts +16 -0
  60. package/src/keymap/listHelpers/handleBackspace.ts +84 -0
  61. package/src/keymap/listHelpers/handleDelete.ts +43 -0
  62. package/src/keymap/listHelpers/hasListBefore.ts +15 -0
  63. package/src/keymap/listHelpers/hasListItemAfter.ts +17 -0
  64. package/src/keymap/listHelpers/hasListItemBefore.ts +17 -0
  65. package/src/keymap/listHelpers/index.ts +10 -0
  66. package/src/keymap/listHelpers/listItemHasSubList.ts +21 -0
  67. package/src/keymap/listHelpers/nextListIsDeeper.ts +19 -0
  68. package/src/keymap/listHelpers/nextListIsHigher.ts +19 -0
  69. package/src/kit/index.ts +75 -0
  70. package/src/ordered-list/index.ts +1 -0
  71. package/src/ordered-list/ordered-list.ts +151 -0
  72. package/src/task-item/index.ts +1 -0
  73. package/src/task-item/task-item.ts +219 -0
  74. package/src/task-list/index.ts +1 -0
  75. package/src/task-list/task-list.ts +79 -0
@@ -0,0 +1,151 @@
1
+ import { mergeAttributes, Node, wrappingInputRule } from '@tiptap/core'
2
+
3
+ const ListItemName = 'listItem'
4
+ const TextStyleName = 'textStyle'
5
+
6
+ export interface OrderedListOptions {
7
+ /**
8
+ * The node type name for list items.
9
+ * @default 'listItem'
10
+ * @example 'myListItem'
11
+ */
12
+ itemTypeName: string
13
+
14
+ /**
15
+ * The HTML attributes for an ordered list node.
16
+ * @default {}
17
+ * @example { class: 'foo' }
18
+ */
19
+ HTMLAttributes: Record<string, any>
20
+
21
+ /**
22
+ * Keep the marks when splitting a list item.
23
+ * @default false
24
+ * @example true
25
+ */
26
+ keepMarks: boolean
27
+
28
+ /**
29
+ * Keep the attributes when splitting a list item.
30
+ * @default false
31
+ * @example true
32
+ */
33
+ keepAttributes: boolean
34
+ }
35
+
36
+ declare module '@tiptap/core' {
37
+ interface Commands<ReturnType> {
38
+ orderedList: {
39
+ /**
40
+ * Toggle an ordered list
41
+ * @example editor.commands.toggleOrderedList()
42
+ */
43
+ toggleOrderedList: () => ReturnType
44
+ }
45
+ }
46
+ }
47
+
48
+ /**
49
+ * Matches an ordered list to a 1. on input (or any number followed by a dot).
50
+ */
51
+ export const orderedListInputRegex = /^(\d+)\.\s$/
52
+
53
+ /**
54
+ * This extension allows you to create ordered lists.
55
+ * This requires the ListItem extension
56
+ * @see https://www.tiptap.dev/api/nodes/ordered-list
57
+ * @see https://www.tiptap.dev/api/nodes/list-item
58
+ */
59
+ export const OrderedList = Node.create<OrderedListOptions>({
60
+ name: 'orderedList',
61
+
62
+ addOptions() {
63
+ return {
64
+ itemTypeName: 'listItem',
65
+ HTMLAttributes: {},
66
+ keepMarks: false,
67
+ keepAttributes: false,
68
+ }
69
+ },
70
+
71
+ group: 'block list',
72
+
73
+ content() {
74
+ return `${this.options.itemTypeName}+`
75
+ },
76
+
77
+ addAttributes() {
78
+ return {
79
+ start: {
80
+ default: 1,
81
+ parseHTML: element => {
82
+ return element.hasAttribute('start') ? parseInt(element.getAttribute('start') || '', 10) : 1
83
+ },
84
+ },
85
+ type: {
86
+ default: undefined,
87
+ parseHTML: element => element.getAttribute('type'),
88
+ },
89
+ }
90
+ },
91
+
92
+ parseHTML() {
93
+ return [
94
+ {
95
+ tag: 'ol',
96
+ },
97
+ ]
98
+ },
99
+
100
+ renderHTML({ HTMLAttributes }) {
101
+ const { start, ...attributesWithoutStart } = HTMLAttributes
102
+
103
+ return start === 1
104
+ ? ['ol', mergeAttributes(this.options.HTMLAttributes, attributesWithoutStart), 0]
105
+ : ['ol', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
106
+ },
107
+
108
+ addCommands() {
109
+ return {
110
+ toggleOrderedList:
111
+ () =>
112
+ ({ commands, chain }) => {
113
+ if (this.options.keepAttributes) {
114
+ return chain()
115
+ .toggleList(this.name, this.options.itemTypeName, this.options.keepMarks)
116
+ .updateAttributes(ListItemName, this.editor.getAttributes(TextStyleName))
117
+ .run()
118
+ }
119
+ return commands.toggleList(this.name, this.options.itemTypeName, this.options.keepMarks)
120
+ },
121
+ }
122
+ },
123
+
124
+ addKeyboardShortcuts() {
125
+ return {
126
+ 'Mod-Shift-7': () => this.editor.commands.toggleOrderedList(),
127
+ }
128
+ },
129
+
130
+ addInputRules() {
131
+ let inputRule = wrappingInputRule({
132
+ find: orderedListInputRegex,
133
+ type: this.type,
134
+ getAttributes: match => ({ start: +match[1] }),
135
+ joinPredicate: (match, node) => node.childCount + node.attrs.start === +match[1],
136
+ })
137
+
138
+ if (this.options.keepMarks || this.options.keepAttributes) {
139
+ inputRule = wrappingInputRule({
140
+ find: orderedListInputRegex,
141
+ type: this.type,
142
+ keepMarks: this.options.keepMarks,
143
+ keepAttributes: this.options.keepAttributes,
144
+ getAttributes: match => ({ start: +match[1], ...this.editor.getAttributes(TextStyleName) }),
145
+ joinPredicate: (match, node) => node.childCount + node.attrs.start === +match[1],
146
+ editor: this.editor,
147
+ })
148
+ }
149
+ return [inputRule]
150
+ },
151
+ })
@@ -0,0 +1 @@
1
+ export * from './task-item.js'
@@ -0,0 +1,219 @@
1
+ import { KeyboardShortcutCommand, mergeAttributes, Node, wrappingInputRule } from '@tiptap/core'
2
+ import { Node as ProseMirrorNode } from '@tiptap/pm/model'
3
+
4
+ export interface TaskItemOptions {
5
+ /**
6
+ * A callback function that is called when the checkbox is clicked while the editor is in readonly mode.
7
+ * @param node The prosemirror node of the task item
8
+ * @param checked The new checked state
9
+ * @returns boolean
10
+ */
11
+ onReadOnlyChecked?: (node: ProseMirrorNode, checked: boolean) => boolean
12
+
13
+ /**
14
+ * Controls whether the task items can be nested or not.
15
+ * @default false
16
+ * @example true
17
+ */
18
+ nested: boolean
19
+
20
+ /**
21
+ * HTML attributes to add to the task item element.
22
+ * @default {}
23
+ * @example { class: 'foo' }
24
+ */
25
+ HTMLAttributes: Record<string, any>
26
+
27
+ /**
28
+ * The node type for taskList nodes
29
+ * @default 'taskList'
30
+ * @example 'myCustomTaskList'
31
+ */
32
+ taskListTypeName: string
33
+ }
34
+
35
+ /**
36
+ * Matches a task item to a - [ ] on input.
37
+ */
38
+ export const inputRegex = /^\s*(\[([( |x])?\])\s$/
39
+
40
+ /**
41
+ * This extension allows you to create task items.
42
+ * @see https://www.tiptap.dev/api/nodes/task-item
43
+ */
44
+ export const TaskItem = Node.create<TaskItemOptions>({
45
+ name: 'taskItem',
46
+
47
+ addOptions() {
48
+ return {
49
+ nested: false,
50
+ HTMLAttributes: {},
51
+ taskListTypeName: 'taskList',
52
+ }
53
+ },
54
+
55
+ content() {
56
+ return this.options.nested ? 'paragraph block*' : 'paragraph+'
57
+ },
58
+
59
+ defining: true,
60
+
61
+ addAttributes() {
62
+ return {
63
+ checked: {
64
+ default: false,
65
+ keepOnSplit: false,
66
+ parseHTML: element => {
67
+ const dataChecked = element.getAttribute('data-checked')
68
+
69
+ return dataChecked === '' || dataChecked === 'true'
70
+ },
71
+ renderHTML: attributes => ({
72
+ 'data-checked': attributes.checked,
73
+ }),
74
+ },
75
+ }
76
+ },
77
+
78
+ parseHTML() {
79
+ return [
80
+ {
81
+ tag: `li[data-type="${this.name}"]`,
82
+ priority: 51,
83
+ },
84
+ ]
85
+ },
86
+
87
+ renderHTML({ node, HTMLAttributes }) {
88
+ return [
89
+ 'li',
90
+ mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, {
91
+ 'data-type': this.name,
92
+ }),
93
+ [
94
+ 'label',
95
+ [
96
+ 'input',
97
+ {
98
+ type: 'checkbox',
99
+ checked: node.attrs.checked ? 'checked' : null,
100
+ },
101
+ ],
102
+ ['span'],
103
+ ],
104
+ ['div', 0],
105
+ ]
106
+ },
107
+
108
+ addKeyboardShortcuts() {
109
+ const shortcuts: {
110
+ [key: string]: KeyboardShortcutCommand
111
+ } = {
112
+ Enter: () => this.editor.commands.splitListItem(this.name),
113
+ 'Shift-Tab': () => this.editor.commands.liftListItem(this.name),
114
+ }
115
+
116
+ if (!this.options.nested) {
117
+ return shortcuts
118
+ }
119
+
120
+ return {
121
+ ...shortcuts,
122
+ Tab: () => this.editor.commands.sinkListItem(this.name),
123
+ }
124
+ },
125
+
126
+ addNodeView() {
127
+ return ({ node, HTMLAttributes, getPos, editor }) => {
128
+ const listItem = document.createElement('li')
129
+ const checkboxWrapper = document.createElement('label')
130
+ const checkboxStyler = document.createElement('span')
131
+ const checkbox = document.createElement('input')
132
+ const content = document.createElement('div')
133
+
134
+ checkboxWrapper.contentEditable = 'false'
135
+ checkbox.type = 'checkbox'
136
+ checkbox.addEventListener('mousedown', event => event.preventDefault())
137
+ checkbox.addEventListener('change', event => {
138
+ // if the editor isn’t editable and we don't have a handler for
139
+ // readonly checks we have to undo the latest change
140
+ if (!editor.isEditable && !this.options.onReadOnlyChecked) {
141
+ checkbox.checked = !checkbox.checked
142
+
143
+ return
144
+ }
145
+
146
+ const { checked } = event.target as any
147
+
148
+ if (editor.isEditable && typeof getPos === 'function') {
149
+ editor
150
+ .chain()
151
+ .focus(undefined, { scrollIntoView: false })
152
+ .command(({ tr }) => {
153
+ const position = getPos()
154
+
155
+ if (typeof position !== 'number') {
156
+ return false
157
+ }
158
+ const currentNode = tr.doc.nodeAt(position)
159
+
160
+ tr.setNodeMarkup(position, undefined, {
161
+ ...currentNode?.attrs,
162
+ checked,
163
+ })
164
+
165
+ return true
166
+ })
167
+ .run()
168
+ }
169
+ if (!editor.isEditable && this.options.onReadOnlyChecked) {
170
+ // Reset state if onReadOnlyChecked returns false
171
+ if (!this.options.onReadOnlyChecked(node, checked)) {
172
+ checkbox.checked = !checkbox.checked
173
+ }
174
+ }
175
+ })
176
+
177
+ Object.entries(this.options.HTMLAttributes).forEach(([key, value]) => {
178
+ listItem.setAttribute(key, value)
179
+ })
180
+
181
+ listItem.dataset.checked = node.attrs.checked
182
+ checkbox.checked = node.attrs.checked
183
+
184
+ checkboxWrapper.append(checkbox, checkboxStyler)
185
+ listItem.append(checkboxWrapper, content)
186
+
187
+ Object.entries(HTMLAttributes).forEach(([key, value]) => {
188
+ listItem.setAttribute(key, value)
189
+ })
190
+
191
+ return {
192
+ dom: listItem,
193
+ contentDOM: content,
194
+ update: updatedNode => {
195
+ if (updatedNode.type !== this.type) {
196
+ return false
197
+ }
198
+
199
+ listItem.dataset.checked = updatedNode.attrs.checked
200
+ checkbox.checked = updatedNode.attrs.checked
201
+
202
+ return true
203
+ },
204
+ }
205
+ }
206
+ },
207
+
208
+ addInputRules() {
209
+ return [
210
+ wrappingInputRule({
211
+ find: inputRegex,
212
+ type: this.type,
213
+ getAttributes: match => ({
214
+ checked: match[match.length - 1] === 'x',
215
+ }),
216
+ }),
217
+ ]
218
+ },
219
+ })
@@ -0,0 +1 @@
1
+ export * from './task-list.js'
@@ -0,0 +1,79 @@
1
+ import { mergeAttributes, Node } from '@tiptap/core'
2
+
3
+ export interface TaskListOptions {
4
+ /**
5
+ * The node type name for a task item.
6
+ * @default 'taskItem'
7
+ * @example 'myCustomTaskItem'
8
+ */
9
+ itemTypeName: string
10
+
11
+ /**
12
+ * The HTML attributes for a task list node.
13
+ * @default {}
14
+ * @example { class: 'foo' }
15
+ */
16
+ HTMLAttributes: Record<string, any>
17
+ }
18
+
19
+ declare module '@tiptap/core' {
20
+ interface Commands<ReturnType> {
21
+ taskList: {
22
+ /**
23
+ * Toggle a task list
24
+ * @example editor.commands.toggleTaskList()
25
+ */
26
+ toggleTaskList: () => ReturnType
27
+ }
28
+ }
29
+ }
30
+
31
+ /**
32
+ * This extension allows you to create task lists.
33
+ * @see https://www.tiptap.dev/api/nodes/task-list
34
+ */
35
+ export const TaskList = Node.create<TaskListOptions>({
36
+ name: 'taskList',
37
+
38
+ addOptions() {
39
+ return {
40
+ itemTypeName: 'taskItem',
41
+ HTMLAttributes: {},
42
+ }
43
+ },
44
+
45
+ group: 'block list',
46
+
47
+ content() {
48
+ return `${this.options.itemTypeName}+`
49
+ },
50
+
51
+ parseHTML() {
52
+ return [
53
+ {
54
+ tag: `ul[data-type="${this.name}"]`,
55
+ priority: 51,
56
+ },
57
+ ]
58
+ },
59
+
60
+ renderHTML({ HTMLAttributes }) {
61
+ return ['ul', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes, { 'data-type': this.name }), 0]
62
+ },
63
+
64
+ addCommands() {
65
+ return {
66
+ toggleTaskList:
67
+ () =>
68
+ ({ commands }) => {
69
+ return commands.toggleList(this.name, this.options.itemTypeName)
70
+ },
71
+ }
72
+ },
73
+
74
+ addKeyboardShortcuts() {
75
+ return {
76
+ 'Mod-Shift-9': () => this.editor.commands.toggleTaskList(),
77
+ }
78
+ },
79
+ })