@tiptap/extension-list-keymap 2.11.7 → 3.0.0-beta.1

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 (49) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +5 -1
  3. package/dist/index.cjs +34 -297
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.d.cts +2 -0
  6. package/dist/index.d.ts +2 -5
  7. package/dist/index.js +10 -293
  8. package/dist/index.js.map +1 -1
  9. package/package.json +10 -8
  10. package/src/index.ts +3 -3
  11. package/dist/index.d.ts.map +0 -1
  12. package/dist/index.umd.js +0 -303
  13. package/dist/index.umd.js.map +0 -1
  14. package/dist/list-keymap.d.ts +0 -21
  15. package/dist/list-keymap.d.ts.map +0 -1
  16. package/dist/listHelpers/findListItemPos.d.ts +0 -7
  17. package/dist/listHelpers/findListItemPos.d.ts.map +0 -1
  18. package/dist/listHelpers/getNextListDepth.d.ts +0 -3
  19. package/dist/listHelpers/getNextListDepth.d.ts.map +0 -1
  20. package/dist/listHelpers/handleBackspace.d.ts +0 -3
  21. package/dist/listHelpers/handleBackspace.d.ts.map +0 -1
  22. package/dist/listHelpers/handleDelete.d.ts +0 -3
  23. package/dist/listHelpers/handleDelete.d.ts.map +0 -1
  24. package/dist/listHelpers/hasListBefore.d.ts +0 -3
  25. package/dist/listHelpers/hasListBefore.d.ts.map +0 -1
  26. package/dist/listHelpers/hasListItemAfter.d.ts +0 -3
  27. package/dist/listHelpers/hasListItemAfter.d.ts.map +0 -1
  28. package/dist/listHelpers/hasListItemBefore.d.ts +0 -3
  29. package/dist/listHelpers/hasListItemBefore.d.ts.map +0 -1
  30. package/dist/listHelpers/index.d.ts +0 -11
  31. package/dist/listHelpers/index.d.ts.map +0 -1
  32. package/dist/listHelpers/listItemHasSubList.d.ts +0 -4
  33. package/dist/listHelpers/listItemHasSubList.d.ts.map +0 -1
  34. package/dist/listHelpers/nextListIsDeeper.d.ts +0 -3
  35. package/dist/listHelpers/nextListIsDeeper.d.ts.map +0 -1
  36. package/dist/listHelpers/nextListIsHigher.d.ts +0 -3
  37. package/dist/listHelpers/nextListIsHigher.d.ts.map +0 -1
  38. package/src/list-keymap.ts +0 -106
  39. package/src/listHelpers/findListItemPos.ts +0 -30
  40. package/src/listHelpers/getNextListDepth.ts +0 -16
  41. package/src/listHelpers/handleBackspace.ts +0 -80
  42. package/src/listHelpers/handleDelete.ts +0 -46
  43. package/src/listHelpers/hasListBefore.ts +0 -15
  44. package/src/listHelpers/hasListItemAfter.ts +0 -17
  45. package/src/listHelpers/hasListItemBefore.ts +0 -17
  46. package/src/listHelpers/index.ts +0 -10
  47. package/src/listHelpers/listItemHasSubList.ts +0 -21
  48. package/src/listHelpers/nextListIsDeeper.ts +0 -19
  49. package/src/listHelpers/nextListIsHigher.ts +0 -19
@@ -1,80 +0,0 @@
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.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run()
46
- }
47
-
48
- // if the cursor is not inside the current node type
49
- // do nothing and proceed
50
- if (!isNodeActive(editor.state, name)) {
51
- return false
52
- }
53
-
54
- // if the cursor is not at the start of a node
55
- // do nothing and proceed
56
- if (!isAtStartOfNode(editor.state)) {
57
- return false
58
- }
59
-
60
- const listItemPos = findListItemPos(name, editor.state)
61
-
62
- if (!listItemPos) {
63
- return false
64
- }
65
-
66
- const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2)
67
- const prevNode = $prev.node(listItemPos.depth)
68
-
69
- const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode)
70
-
71
- // if the previous item is a list item and doesn't have a sublist, join the list items
72
- if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
73
- return editor.commands.joinItemBackward()
74
- }
75
-
76
- // otherwise in the end, a backspace should
77
- // always just lift the list item if
78
- // joining / merging is not possible
79
- return editor.chain().liftListItem(name).run()
80
- }
@@ -1,46 +0,0 @@
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()
40
- .joinForward()
41
- .joinBackward()
42
- .run()
43
- }
44
-
45
- return editor.commands.joinItemForward()
46
- }
@@ -1,15 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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'
@@ -1,21 +0,0 @@
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
- }
@@ -1,19 +0,0 @@
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
- }
@@ -1,19 +0,0 @@
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
- }