@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 @@
1
+ export * from './bullet-list.js'
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ export * from './bullet-list/index.js'
2
+ export * from './item/index.js'
3
+ export * from './keymap/index.js'
4
+ export * from './kit/index.js'
5
+ export * from './ordered-list/index.js'
6
+ export * from './task-item/index.js'
7
+ export * from './task-list/index.js'
@@ -0,0 +1 @@
1
+ export * from './list-item.js'
@@ -0,0 +1,64 @@
1
+ import { mergeAttributes, Node } from '@tiptap/core'
2
+
3
+ export interface ListItemOptions {
4
+ /**
5
+ * The HTML attributes for a list item node.
6
+ * @default {}
7
+ * @example { class: 'foo' }
8
+ */
9
+ HTMLAttributes: Record<string, any>
10
+
11
+ /**
12
+ * The node type for bulletList nodes
13
+ * @default 'bulletList'
14
+ * @example 'myCustomBulletList'
15
+ */
16
+ bulletListTypeName: string
17
+
18
+ /**
19
+ * The node type for orderedList nodes
20
+ * @default 'orderedList'
21
+ * @example 'myCustomOrderedList'
22
+ */
23
+ orderedListTypeName: string
24
+ }
25
+
26
+ /**
27
+ * This extension allows you to create list items.
28
+ * @see https://www.tiptap.dev/api/nodes/list-item
29
+ */
30
+ export const ListItem = Node.create<ListItemOptions>({
31
+ name: 'listItem',
32
+
33
+ addOptions() {
34
+ return {
35
+ HTMLAttributes: {},
36
+ bulletListTypeName: 'bulletList',
37
+ orderedListTypeName: 'orderedList',
38
+ }
39
+ },
40
+
41
+ content: 'paragraph block*',
42
+
43
+ defining: true,
44
+
45
+ parseHTML() {
46
+ return [
47
+ {
48
+ tag: 'li',
49
+ },
50
+ ]
51
+ },
52
+
53
+ renderHTML({ HTMLAttributes }) {
54
+ return ['li', mergeAttributes(this.options.HTMLAttributes, HTMLAttributes), 0]
55
+ },
56
+
57
+ addKeyboardShortcuts() {
58
+ return {
59
+ Enter: () => this.editor.commands.splitListItem(this.name),
60
+ Tab: () => this.editor.commands.sinkListItem(this.name),
61
+ 'Shift-Tab': () => this.editor.commands.liftListItem(this.name),
62
+ }
63
+ },
64
+ })
@@ -0,0 +1,2 @@
1
+ export * from './list-keymap.js'
2
+ export * as listHelpers from './listHelpers/index.js'
@@ -0,0 +1,106 @@
1
+ import { Extension } from '@tiptap/core'
2
+
3
+ import { handleBackspace, handleDelete } from './listHelpers/index.js'
4
+
5
+ export type ListKeymapOptions = {
6
+ /**
7
+ * An array of list types. This is used for item and wrapper list matching.
8
+ * @default []
9
+ * @example [{ itemName: 'listItem', wrapperNames: ['bulletList', 'orderedList'] }]
10
+ */
11
+ listTypes: Array<{
12
+ itemName: string
13
+ wrapperNames: string[]
14
+ }>
15
+ }
16
+
17
+ /**
18
+ * This extension registers custom keymaps to change the behaviour of the backspace and delete keys.
19
+ * By default Prosemirror keyhandling will always lift or sink items so paragraphs are joined into
20
+ * the adjacent or previous list item. This extension will prevent this behaviour and instead will
21
+ * try to join paragraphs from two list items into a single list item.
22
+ * @see https://www.tiptap.dev/api/extensions/list-keymap
23
+ */
24
+ export const ListKeymap = Extension.create<ListKeymapOptions>({
25
+ name: 'listKeymap',
26
+
27
+ addOptions() {
28
+ return {
29
+ listTypes: [
30
+ {
31
+ itemName: 'listItem',
32
+ wrapperNames: ['bulletList', 'orderedList'],
33
+ },
34
+ {
35
+ itemName: 'taskItem',
36
+ wrapperNames: ['taskList'],
37
+ },
38
+ ],
39
+ }
40
+ },
41
+
42
+ addKeyboardShortcuts() {
43
+ return {
44
+ Delete: ({ editor }) => {
45
+ let handled = false
46
+
47
+ this.options.listTypes.forEach(({ itemName }) => {
48
+ if (editor.state.schema.nodes[itemName] === undefined) {
49
+ return
50
+ }
51
+
52
+ if (handleDelete(editor, itemName)) {
53
+ handled = true
54
+ }
55
+ })
56
+
57
+ return handled
58
+ },
59
+ 'Mod-Delete': ({ editor }) => {
60
+ let handled = false
61
+
62
+ this.options.listTypes.forEach(({ itemName }) => {
63
+ if (editor.state.schema.nodes[itemName] === undefined) {
64
+ return
65
+ }
66
+
67
+ if (handleDelete(editor, itemName)) {
68
+ handled = true
69
+ }
70
+ })
71
+
72
+ return handled
73
+ },
74
+ Backspace: ({ editor }) => {
75
+ let handled = false
76
+
77
+ this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
78
+ if (editor.state.schema.nodes[itemName] === undefined) {
79
+ return
80
+ }
81
+
82
+ if (handleBackspace(editor, itemName, wrapperNames)) {
83
+ handled = true
84
+ }
85
+ })
86
+
87
+ return handled
88
+ },
89
+ 'Mod-Backspace': ({ editor }) => {
90
+ let handled = false
91
+
92
+ this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
93
+ if (editor.state.schema.nodes[itemName] === undefined) {
94
+ return
95
+ }
96
+
97
+ if (handleBackspace(editor, itemName, wrapperNames)) {
98
+ handled = true
99
+ }
100
+ })
101
+
102
+ return handled
103
+ },
104
+ }
105
+ },
106
+ })
@@ -0,0 +1,30 @@
1
+ import { getNodeType } from '@tiptap/core'
2
+ import { NodeType } from '@tiptap/pm/model'
3
+ import { EditorState } from '@tiptap/pm/state'
4
+
5
+ export const findListItemPos = (typeOrName: string | NodeType, state: EditorState) => {
6
+ const { $from } = state.selection
7
+ const nodeType = getNodeType(typeOrName, state.schema)
8
+
9
+ let currentNode = null
10
+ let currentDepth = $from.depth
11
+ let currentPos = $from.pos
12
+ let targetDepth: number | null = null
13
+
14
+ while (currentDepth > 0 && targetDepth === null) {
15
+ currentNode = $from.node(currentDepth)
16
+
17
+ if (currentNode.type === nodeType) {
18
+ targetDepth = currentDepth
19
+ } else {
20
+ currentDepth -= 1
21
+ currentPos -= 1
22
+ }
23
+ }
24
+
25
+ if (targetDepth === null) {
26
+ return null
27
+ }
28
+
29
+ return { $pos: state.doc.resolve(currentPos), depth: targetDepth }
30
+ }
@@ -0,0 +1,16 @@
1
+ import { getNodeAtPosition } from '@tiptap/core'
2
+ import { EditorState } from '@tiptap/pm/state'
3
+
4
+ import { findListItemPos } from './findListItemPos.js'
5
+
6
+ export const getNextListDepth = (typeOrName: string, state: EditorState) => {
7
+ const listItemPos = findListItemPos(typeOrName, state)
8
+
9
+ if (!listItemPos) {
10
+ return false
11
+ }
12
+
13
+ const [, depth] = getNodeAtPosition(state, typeOrName, listItemPos.$pos.pos + 4)
14
+
15
+ return depth
16
+ }
@@ -0,0 +1,84 @@
1
+ import { Editor, isAtStartOfNode, isNodeActive } from '@tiptap/core'
2
+ import { Node } from '@tiptap/pm/model'
3
+
4
+ import { findListItemPos } from './findListItemPos.js'
5
+ import { hasListBefore } from './hasListBefore.js'
6
+ import { hasListItemBefore } from './hasListItemBefore.js'
7
+ import { listItemHasSubList } from './listItemHasSubList.js'
8
+
9
+ export const handleBackspace = (editor: Editor, name: string, parentListTypes: string[]) => {
10
+ // this is required to still handle the undo handling
11
+ if (editor.commands.undoInputRule()) {
12
+ return true
13
+ }
14
+
15
+ // if the selection is not collapsed
16
+ // we can rely on the default backspace behavior
17
+ if (editor.state.selection.from !== editor.state.selection.to) {
18
+ return false
19
+ }
20
+
21
+ // if the current item is NOT inside a list item &
22
+ // the previous item is a list (orderedList or bulletList)
23
+ // move the cursor into the list and delete the current item
24
+ if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {
25
+ const { $anchor } = editor.state.selection
26
+
27
+ const $listPos = editor.state.doc.resolve($anchor.before() - 1)
28
+
29
+ const listDescendants: Array<{ node: Node; pos: number }> = []
30
+
31
+ $listPos.node().descendants((node, pos) => {
32
+ if (node.type.name === name) {
33
+ listDescendants.push({ node, pos })
34
+ }
35
+ })
36
+
37
+ const lastItem = listDescendants.at(-1)
38
+
39
+ if (!lastItem) {
40
+ return false
41
+ }
42
+
43
+ const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1)
44
+
45
+ return editor
46
+ .chain()
47
+ .cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end())
48
+ .joinForward()
49
+ .run()
50
+ }
51
+
52
+ // if the cursor is not inside the current node type
53
+ // do nothing and proceed
54
+ if (!isNodeActive(editor.state, name)) {
55
+ return false
56
+ }
57
+
58
+ // if the cursor is not at the start of a node
59
+ // do nothing and proceed
60
+ if (!isAtStartOfNode(editor.state)) {
61
+ return false
62
+ }
63
+
64
+ const listItemPos = findListItemPos(name, editor.state)
65
+
66
+ if (!listItemPos) {
67
+ return false
68
+ }
69
+
70
+ const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2)
71
+ const prevNode = $prev.node(listItemPos.depth)
72
+
73
+ const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode)
74
+
75
+ // if the previous item is a list item and doesn't have a sublist, join the list items
76
+ if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
77
+ return editor.commands.joinItemBackward()
78
+ }
79
+
80
+ // otherwise in the end, a backspace should
81
+ // always just lift the list item if
82
+ // joining / merging is not possible
83
+ return editor.chain().liftListItem(name).run()
84
+ }
@@ -0,0 +1,43 @@
1
+ import { Editor, isAtEndOfNode, isNodeActive } from '@tiptap/core'
2
+
3
+ import { nextListIsDeeper } from './nextListIsDeeper.js'
4
+ import { nextListIsHigher } from './nextListIsHigher.js'
5
+
6
+ export const handleDelete = (editor: Editor, name: string) => {
7
+ // if the cursor is not inside the current node type
8
+ // do nothing and proceed
9
+ if (!isNodeActive(editor.state, name)) {
10
+ return false
11
+ }
12
+
13
+ // if the cursor is not at the end of a node
14
+ // do nothing and proceed
15
+ if (!isAtEndOfNode(editor.state, name)) {
16
+ return false
17
+ }
18
+
19
+ // if the selection is not collapsed, or not within a single node
20
+ // do nothing and proceed
21
+ const { selection } = editor.state
22
+ const { $from, $to } = selection
23
+
24
+ if (!selection.empty && $from.sameParent($to)) {
25
+ return false
26
+ }
27
+
28
+ // check if the next node is a list with a deeper depth
29
+ if (nextListIsDeeper(name, editor.state)) {
30
+ return editor
31
+ .chain()
32
+ .focus(editor.state.selection.from + 4)
33
+ .lift(name)
34
+ .joinBackward()
35
+ .run()
36
+ }
37
+
38
+ if (nextListIsHigher(name, editor.state)) {
39
+ return editor.chain().joinForward().joinBackward().run()
40
+ }
41
+
42
+ return editor.commands.joinItemForward()
43
+ }
@@ -0,0 +1,15 @@
1
+ import { EditorState } from '@tiptap/pm/state'
2
+
3
+ export const hasListBefore = (editorState: EditorState, name: string, parentListTypes: string[]) => {
4
+ const { $anchor } = editorState.selection
5
+
6
+ const previousNodePos = Math.max(0, $anchor.pos - 2)
7
+
8
+ const previousNode = editorState.doc.resolve(previousNodePos).node()
9
+
10
+ if (!previousNode || !parentListTypes.includes(previousNode.type.name)) {
11
+ return false
12
+ }
13
+
14
+ return true
15
+ }
@@ -0,0 +1,17 @@
1
+ import { EditorState } from '@tiptap/pm/state'
2
+
3
+ export const hasListItemAfter = (typeOrName: string, state: EditorState): boolean => {
4
+ const { $anchor } = state.selection
5
+
6
+ const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2)
7
+
8
+ if ($targetPos.index() === $targetPos.parent.childCount - 1) {
9
+ return false
10
+ }
11
+
12
+ if ($targetPos.nodeAfter?.type.name !== typeOrName) {
13
+ return false
14
+ }
15
+
16
+ return true
17
+ }
@@ -0,0 +1,17 @@
1
+ import { EditorState } from '@tiptap/pm/state'
2
+
3
+ export const hasListItemBefore = (typeOrName: string, state: EditorState): boolean => {
4
+ const { $anchor } = state.selection
5
+
6
+ const $targetPos = state.doc.resolve($anchor.pos - 2)
7
+
8
+ if ($targetPos.index() === 0) {
9
+ return false
10
+ }
11
+
12
+ if ($targetPos.nodeBefore?.type.name !== typeOrName) {
13
+ return false
14
+ }
15
+
16
+ return true
17
+ }
@@ -0,0 +1,10 @@
1
+ export * from './findListItemPos.js'
2
+ export * from './getNextListDepth.js'
3
+ export * from './handleBackspace.js'
4
+ export * from './handleDelete.js'
5
+ export * from './hasListBefore.js'
6
+ export * from './hasListItemAfter.js'
7
+ export * from './hasListItemBefore.js'
8
+ export * from './listItemHasSubList.js'
9
+ export * from './nextListIsDeeper.js'
10
+ export * from './nextListIsHigher.js'
@@ -0,0 +1,21 @@
1
+ import { getNodeType } from '@tiptap/core'
2
+ import { Node } from '@tiptap/pm/model'
3
+ import { EditorState } from '@tiptap/pm/state'
4
+
5
+ export const listItemHasSubList = (typeOrName: string, state: EditorState, node?: Node) => {
6
+ if (!node) {
7
+ return false
8
+ }
9
+
10
+ const nodeType = getNodeType(typeOrName, state.schema)
11
+
12
+ let hasSubList = false
13
+
14
+ node.descendants(child => {
15
+ if (child.type === nodeType) {
16
+ hasSubList = true
17
+ }
18
+ })
19
+
20
+ return hasSubList
21
+ }
@@ -0,0 +1,19 @@
1
+ import { EditorState } from '@tiptap/pm/state'
2
+
3
+ import { findListItemPos } from './findListItemPos.js'
4
+ import { getNextListDepth } from './getNextListDepth.js'
5
+
6
+ export const nextListIsDeeper = (typeOrName: string, state: EditorState) => {
7
+ const listDepth = getNextListDepth(typeOrName, state)
8
+ const listItemPos = findListItemPos(typeOrName, state)
9
+
10
+ if (!listItemPos || !listDepth) {
11
+ return false
12
+ }
13
+
14
+ if (listDepth > listItemPos.depth) {
15
+ return true
16
+ }
17
+
18
+ return false
19
+ }
@@ -0,0 +1,19 @@
1
+ import { EditorState } from '@tiptap/pm/state'
2
+
3
+ import { findListItemPos } from './findListItemPos.js'
4
+ import { getNextListDepth } from './getNextListDepth.js'
5
+
6
+ export const nextListIsHigher = (typeOrName: string, state: EditorState) => {
7
+ const listDepth = getNextListDepth(typeOrName, state)
8
+ const listItemPos = findListItemPos(typeOrName, state)
9
+
10
+ if (!listItemPos || !listDepth) {
11
+ return false
12
+ }
13
+
14
+ if (listDepth < listItemPos.depth) {
15
+ return true
16
+ }
17
+
18
+ return false
19
+ }
@@ -0,0 +1,75 @@
1
+ import { Extension } from '@tiptap/core'
2
+
3
+ import { BulletList, BulletListOptions } from '../bullet-list/index.js'
4
+ import { ListItem, ListItemOptions } from '../item/index.js'
5
+ import { ListKeymap, ListKeymapOptions } from '../keymap/index.js'
6
+ import { OrderedList, OrderedListOptions } from '../ordered-list/index.js'
7
+ import { TaskItem, TaskItemOptions } from '../task-item/index.js'
8
+ import { TaskList, TaskListOptions } from '../task-list/index.js'
9
+
10
+ export interface ListKitOptions {
11
+ /**
12
+ * If set to false, the bulletList extension will not be registered
13
+ * @example table: false
14
+ */
15
+ bulletList: Partial<BulletListOptions> | false
16
+ /**
17
+ * If set to false, the listItem extension will not be registered
18
+ */
19
+ listItem: Partial<ListItemOptions> | false
20
+ /**
21
+ * If set to false, the listKeymap extension will not be registered
22
+ */
23
+ listKeymap: Partial<ListKeymapOptions> | false
24
+ /**
25
+ * If set to false, the orderedList extension will not be registered
26
+ */
27
+ orderedList: Partial<OrderedListOptions> | false
28
+ /**
29
+ * If set to false, the taskItem extension will not be registered
30
+ */
31
+ taskItem: Partial<TaskItemOptions> | false
32
+ /**
33
+ * If set to false, the taskList extension will not be registered
34
+ */
35
+ taskList: Partial<TaskListOptions> | false
36
+ }
37
+
38
+ /**
39
+ * The table kit is a collection of table editor extensions.
40
+ *
41
+ * It’s a good starting point for building your own table in Tiptap.
42
+ */
43
+ export const ListKit = Extension.create<ListKitOptions>({
44
+ name: 'listKit',
45
+
46
+ addExtensions() {
47
+ const extensions = []
48
+
49
+ if (this.options.bulletList !== false) {
50
+ extensions.push(BulletList.configure(this.options.bulletList))
51
+ }
52
+
53
+ if (this.options.listItem !== false) {
54
+ extensions.push(ListItem.configure(this.options.listItem))
55
+ }
56
+
57
+ if (this.options.listKeymap !== false) {
58
+ extensions.push(ListKeymap.configure(this.options.listKeymap))
59
+ }
60
+
61
+ if (this.options.orderedList !== false) {
62
+ extensions.push(OrderedList.configure(this.options.orderedList))
63
+ }
64
+
65
+ if (this.options.taskItem !== false) {
66
+ extensions.push(TaskItem.configure(this.options.taskItem))
67
+ }
68
+
69
+ if (this.options.taskList !== false) {
70
+ extensions.push(TaskList.configure(this.options.taskList))
71
+ }
72
+
73
+ return extensions
74
+ },
75
+ })
@@ -0,0 +1 @@
1
+ export * from './ordered-list.js'