@tiptap/extension-list 3.0.0-beta.0

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 (76) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +18 -0
  3. package/dist/bullet-list/index.cjs +93 -0
  4. package/dist/bullet-list/index.cjs.map +1 -0
  5. package/dist/bullet-list/index.d.cts +51 -0
  6. package/dist/bullet-list/index.d.ts +51 -0
  7. package/dist/bullet-list/index.js +65 -0
  8. package/dist/bullet-list/index.js.map +1 -0
  9. package/dist/index.cjs +714 -0
  10. package/dist/index.cjs.map +1 -0
  11. package/dist/index.d.cts +288 -0
  12. package/dist/index.d.ts +288 -0
  13. package/dist/index.js +683 -0
  14. package/dist/index.js.map +1 -0
  15. package/dist/item/index.cjs +62 -0
  16. package/dist/item/index.cjs.map +1 -0
  17. package/dist/item/index.d.cts +29 -0
  18. package/dist/item/index.d.ts +29 -0
  19. package/dist/item/index.js +35 -0
  20. package/dist/item/index.js.map +1 -0
  21. package/dist/keymap/index.cjs +308 -0
  22. package/dist/keymap/index.cjs.map +1 -0
  23. package/dist/keymap/index.d.cts +63 -0
  24. package/dist/keymap/index.d.ts +63 -0
  25. package/dist/keymap/index.js +286 -0
  26. package/dist/keymap/index.js.map +1 -0
  27. package/dist/kit/index.cjs +692 -0
  28. package/dist/kit/index.cjs.map +1 -0
  29. package/dist/kit/index.d.cts +200 -0
  30. package/dist/kit/index.d.ts +200 -0
  31. package/dist/kit/index.js +673 -0
  32. package/dist/kit/index.js.map +1 -0
  33. package/dist/ordered-list/index.cjs +113 -0
  34. package/dist/ordered-list/index.cjs.map +1 -0
  35. package/dist/ordered-list/index.d.cts +52 -0
  36. package/dist/ordered-list/index.d.ts +52 -0
  37. package/dist/ordered-list/index.js +85 -0
  38. package/dist/ordered-list/index.js.map +1 -0
  39. package/dist/task-item/index.cjs +177 -0
  40. package/dist/task-item/index.cjs.map +1 -0
  41. package/dist/task-item/index.d.cts +41 -0
  42. package/dist/task-item/index.d.ts +41 -0
  43. package/dist/task-item/index.js +149 -0
  44. package/dist/task-item/index.js.map +1 -0
  45. package/dist/task-list/index.cjs +69 -0
  46. package/dist/task-list/index.cjs.map +1 -0
  47. package/dist/task-list/index.d.cts +34 -0
  48. package/dist/task-list/index.d.ts +34 -0
  49. package/dist/task-list/index.js +42 -0
  50. package/dist/task-list/index.js.map +1 -0
  51. package/package.json +106 -0
  52. package/src/bullet-list/bullet-list.ts +126 -0
  53. package/src/bullet-list/index.ts +1 -0
  54. package/src/index.ts +7 -0
  55. package/src/item/index.ts +1 -0
  56. package/src/item/list-item.ts +64 -0
  57. package/src/keymap/index.ts +2 -0
  58. package/src/keymap/list-keymap.ts +106 -0
  59. package/src/keymap/listHelpers/findListItemPos.ts +30 -0
  60. package/src/keymap/listHelpers/getNextListDepth.ts +16 -0
  61. package/src/keymap/listHelpers/handleBackspace.ts +85 -0
  62. package/src/keymap/listHelpers/handleDelete.ts +44 -0
  63. package/src/keymap/listHelpers/hasListBefore.ts +15 -0
  64. package/src/keymap/listHelpers/hasListItemAfter.ts +17 -0
  65. package/src/keymap/listHelpers/hasListItemBefore.ts +17 -0
  66. package/src/keymap/listHelpers/index.ts +10 -0
  67. package/src/keymap/listHelpers/listItemHasSubList.ts +21 -0
  68. package/src/keymap/listHelpers/nextListIsDeeper.ts +19 -0
  69. package/src/keymap/listHelpers/nextListIsHigher.ts +19 -0
  70. package/src/kit/index.ts +81 -0
  71. package/src/ordered-list/index.ts +1 -0
  72. package/src/ordered-list/ordered-list.ts +151 -0
  73. package/src/task-item/index.ts +1 -0
  74. package/src/task-item/task-item.ts +220 -0
  75. package/src/task-list/index.ts +1 -0
  76. package/src/task-list/task-list.ts +79 -0
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 type { NodeType } from '@tiptap/pm/model'
3
+ import type { 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 type { 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,85 @@
1
+ import type { Editor } from '@tiptap/core'
2
+ import { isAtStartOfNode, isNodeActive } from '@tiptap/core'
3
+ import type { Node } from '@tiptap/pm/model'
4
+
5
+ import { findListItemPos } from './findListItemPos.js'
6
+ import { hasListBefore } from './hasListBefore.js'
7
+ import { hasListItemBefore } from './hasListItemBefore.js'
8
+ import { listItemHasSubList } from './listItemHasSubList.js'
9
+
10
+ export const handleBackspace = (editor: Editor, name: string, parentListTypes: string[]) => {
11
+ // this is required to still handle the undo handling
12
+ if (editor.commands.undoInputRule()) {
13
+ return true
14
+ }
15
+
16
+ // if the selection is not collapsed
17
+ // we can rely on the default backspace behavior
18
+ if (editor.state.selection.from !== editor.state.selection.to) {
19
+ return false
20
+ }
21
+
22
+ // if the current item is NOT inside a list item &
23
+ // the previous item is a list (orderedList or bulletList)
24
+ // move the cursor into the list and delete the current item
25
+ if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {
26
+ const { $anchor } = editor.state.selection
27
+
28
+ const $listPos = editor.state.doc.resolve($anchor.before() - 1)
29
+
30
+ const listDescendants: Array<{ node: Node; pos: number }> = []
31
+
32
+ $listPos.node().descendants((node, pos) => {
33
+ if (node.type.name === name) {
34
+ listDescendants.push({ node, pos })
35
+ }
36
+ })
37
+
38
+ const lastItem = listDescendants.at(-1)
39
+
40
+ if (!lastItem) {
41
+ return false
42
+ }
43
+
44
+ const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1)
45
+
46
+ return editor
47
+ .chain()
48
+ .cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end())
49
+ .joinForward()
50
+ .run()
51
+ }
52
+
53
+ // if the cursor is not inside the current node type
54
+ // do nothing and proceed
55
+ if (!isNodeActive(editor.state, name)) {
56
+ return false
57
+ }
58
+
59
+ // if the cursor is not at the start of a node
60
+ // do nothing and proceed
61
+ if (!isAtStartOfNode(editor.state)) {
62
+ return false
63
+ }
64
+
65
+ const listItemPos = findListItemPos(name, editor.state)
66
+
67
+ if (!listItemPos) {
68
+ return false
69
+ }
70
+
71
+ const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2)
72
+ const prevNode = $prev.node(listItemPos.depth)
73
+
74
+ const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode)
75
+
76
+ // if the previous item is a list item and doesn't have a sublist, join the list items
77
+ if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
78
+ return editor.commands.joinItemBackward()
79
+ }
80
+
81
+ // otherwise in the end, a backspace should
82
+ // always just lift the list item if
83
+ // joining / merging is not possible
84
+ return editor.chain().liftListItem(name).run()
85
+ }
@@ -0,0 +1,44 @@
1
+ import type { Editor } from '@tiptap/core'
2
+ import { isAtEndOfNode, isNodeActive } from '@tiptap/core'
3
+
4
+ import { nextListIsDeeper } from './nextListIsDeeper.js'
5
+ import { nextListIsHigher } from './nextListIsHigher.js'
6
+
7
+ export const handleDelete = (editor: Editor, name: string) => {
8
+ // if the cursor is not inside the current node type
9
+ // do nothing and proceed
10
+ if (!isNodeActive(editor.state, name)) {
11
+ return false
12
+ }
13
+
14
+ // if the cursor is not at the end of a node
15
+ // do nothing and proceed
16
+ if (!isAtEndOfNode(editor.state, name)) {
17
+ return false
18
+ }
19
+
20
+ // if the selection is not collapsed, or not within a single node
21
+ // do nothing and proceed
22
+ const { selection } = editor.state
23
+ const { $from, $to } = selection
24
+
25
+ if (!selection.empty && $from.sameParent($to)) {
26
+ return false
27
+ }
28
+
29
+ // check if the next node is a list with a deeper depth
30
+ if (nextListIsDeeper(name, editor.state)) {
31
+ return editor
32
+ .chain()
33
+ .focus(editor.state.selection.from + 4)
34
+ .lift(name)
35
+ .joinBackward()
36
+ .run()
37
+ }
38
+
39
+ if (nextListIsHigher(name, editor.state)) {
40
+ return editor.chain().joinForward().joinBackward().run()
41
+ }
42
+
43
+ return editor.commands.joinItemForward()
44
+ }
@@ -0,0 +1,15 @@
1
+ import type { 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 type { 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 type { 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 type { Node } from '@tiptap/pm/model'
3
+ import type { 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 type { 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 type { 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,81 @@
1
+ import { Extension } from '@tiptap/core'
2
+
3
+ import type { BulletListOptions } from '../bullet-list/index.js'
4
+ import { BulletList } from '../bullet-list/index.js'
5
+ import type { ListItemOptions } from '../item/index.js'
6
+ import { ListItem } from '../item/index.js'
7
+ import type { ListKeymapOptions } from '../keymap/index.js'
8
+ import { ListKeymap } from '../keymap/index.js'
9
+ import type { OrderedListOptions } from '../ordered-list/index.js'
10
+ import { OrderedList } from '../ordered-list/index.js'
11
+ import type { TaskItemOptions } from '../task-item/index.js'
12
+ import { TaskItem } from '../task-item/index.js'
13
+ import type { TaskListOptions } from '../task-list/index.js'
14
+ import { TaskList } from '../task-list/index.js'
15
+
16
+ export interface ListKitOptions {
17
+ /**
18
+ * If set to false, the bulletList extension will not be registered
19
+ * @example table: false
20
+ */
21
+ bulletList: Partial<BulletListOptions> | false
22
+ /**
23
+ * If set to false, the listItem extension will not be registered
24
+ */
25
+ listItem: Partial<ListItemOptions> | false
26
+ /**
27
+ * If set to false, the listKeymap extension will not be registered
28
+ */
29
+ listKeymap: Partial<ListKeymapOptions> | false
30
+ /**
31
+ * If set to false, the orderedList extension will not be registered
32
+ */
33
+ orderedList: Partial<OrderedListOptions> | false
34
+ /**
35
+ * If set to false, the taskItem extension will not be registered
36
+ */
37
+ taskItem: Partial<TaskItemOptions> | false
38
+ /**
39
+ * If set to false, the taskList extension will not be registered
40
+ */
41
+ taskList: Partial<TaskListOptions> | false
42
+ }
43
+
44
+ /**
45
+ * The table kit is a collection of table editor extensions.
46
+ *
47
+ * It’s a good starting point for building your own table in Tiptap.
48
+ */
49
+ export const ListKit = Extension.create<ListKitOptions>({
50
+ name: 'listKit',
51
+
52
+ addExtensions() {
53
+ const extensions = []
54
+
55
+ if (this.options.bulletList !== false) {
56
+ extensions.push(BulletList.configure(this.options.bulletList))
57
+ }
58
+
59
+ if (this.options.listItem !== false) {
60
+ extensions.push(ListItem.configure(this.options.listItem))
61
+ }
62
+
63
+ if (this.options.listKeymap !== false) {
64
+ extensions.push(ListKeymap.configure(this.options.listKeymap))
65
+ }
66
+
67
+ if (this.options.orderedList !== false) {
68
+ extensions.push(OrderedList.configure(this.options.orderedList))
69
+ }
70
+
71
+ if (this.options.taskItem !== false) {
72
+ extensions.push(TaskItem.configure(this.options.taskItem))
73
+ }
74
+
75
+ if (this.options.taskList !== false) {
76
+ extensions.push(TaskList.configure(this.options.taskList))
77
+ }
78
+
79
+ return extensions
80
+ },
81
+ })
@@ -0,0 +1 @@
1
+ export * from './ordered-list.js'