@tiptap/extension-list-keymap 3.0.0-next.3 → 3.0.0-next.5

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.
package/dist/index.js CHANGED
@@ -1,290 +1,11 @@
1
- var __defProp = Object.defineProperty;
2
- var __export = (target, all) => {
3
- for (var name in all)
4
- __defProp(target, name, { get: all[name], enumerable: true });
5
- };
6
-
7
- // src/list-keymap.ts
8
- import { Extension } from "@tiptap/core";
9
-
10
- // src/listHelpers/index.ts
11
- var listHelpers_exports = {};
12
- __export(listHelpers_exports, {
13
- findListItemPos: () => findListItemPos,
14
- getNextListDepth: () => getNextListDepth,
15
- handleBackspace: () => handleBackspace,
16
- handleDelete: () => handleDelete,
17
- hasListBefore: () => hasListBefore,
18
- hasListItemAfter: () => hasListItemAfter,
19
- hasListItemBefore: () => hasListItemBefore,
20
- listItemHasSubList: () => listItemHasSubList,
21
- nextListIsDeeper: () => nextListIsDeeper,
22
- nextListIsHigher: () => nextListIsHigher
23
- });
24
-
25
- // src/listHelpers/findListItemPos.ts
26
- import { getNodeType } from "@tiptap/core";
27
- var findListItemPos = (typeOrName, state) => {
28
- const { $from } = state.selection;
29
- const nodeType = getNodeType(typeOrName, state.schema);
30
- let currentNode = null;
31
- let currentDepth = $from.depth;
32
- let currentPos = $from.pos;
33
- let targetDepth = null;
34
- while (currentDepth > 0 && targetDepth === null) {
35
- currentNode = $from.node(currentDepth);
36
- if (currentNode.type === nodeType) {
37
- targetDepth = currentDepth;
38
- } else {
39
- currentDepth -= 1;
40
- currentPos -= 1;
41
- }
42
- }
43
- if (targetDepth === null) {
44
- return null;
45
- }
46
- return { $pos: state.doc.resolve(currentPos), depth: targetDepth };
47
- };
48
-
49
- // src/listHelpers/getNextListDepth.ts
50
- import { getNodeAtPosition } from "@tiptap/core";
51
- var getNextListDepth = (typeOrName, state) => {
52
- const listItemPos = findListItemPos(typeOrName, state);
53
- if (!listItemPos) {
54
- return false;
55
- }
56
- const [, depth] = getNodeAtPosition(state, typeOrName, listItemPos.$pos.pos + 4);
57
- return depth;
58
- };
59
-
60
- // src/listHelpers/handleBackspace.ts
61
- import { isAtStartOfNode, isNodeActive } from "@tiptap/core";
62
-
63
- // src/listHelpers/hasListBefore.ts
64
- var hasListBefore = (editorState, name, parentListTypes) => {
65
- const { $anchor } = editorState.selection;
66
- const previousNodePos = Math.max(0, $anchor.pos - 2);
67
- const previousNode = editorState.doc.resolve(previousNodePos).node();
68
- if (!previousNode || !parentListTypes.includes(previousNode.type.name)) {
69
- return false;
70
- }
71
- return true;
72
- };
73
-
74
- // src/listHelpers/hasListItemBefore.ts
75
- var hasListItemBefore = (typeOrName, state) => {
76
- var _a;
77
- const { $anchor } = state.selection;
78
- const $targetPos = state.doc.resolve($anchor.pos - 2);
79
- if ($targetPos.index() === 0) {
80
- return false;
81
- }
82
- if (((_a = $targetPos.nodeBefore) == null ? void 0 : _a.type.name) !== typeOrName) {
83
- return false;
84
- }
85
- return true;
86
- };
87
-
88
- // src/listHelpers/listItemHasSubList.ts
89
- import { getNodeType as getNodeType2 } from "@tiptap/core";
90
- var listItemHasSubList = (typeOrName, state, node) => {
91
- if (!node) {
92
- return false;
93
- }
94
- const nodeType = getNodeType2(typeOrName, state.schema);
95
- let hasSubList = false;
96
- node.descendants((child) => {
97
- if (child.type === nodeType) {
98
- hasSubList = true;
99
- }
100
- });
101
- return hasSubList;
102
- };
103
-
104
- // src/listHelpers/handleBackspace.ts
105
- var handleBackspace = (editor, name, parentListTypes) => {
106
- if (editor.commands.undoInputRule()) {
107
- return true;
108
- }
109
- if (editor.state.selection.from !== editor.state.selection.to) {
110
- return false;
111
- }
112
- if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {
113
- const { $anchor } = editor.state.selection;
114
- const $listPos = editor.state.doc.resolve($anchor.before() - 1);
115
- const listDescendants = [];
116
- $listPos.node().descendants((node, pos) => {
117
- if (node.type.name === name) {
118
- listDescendants.push({ node, pos });
119
- }
120
- });
121
- const lastItem = listDescendants.at(-1);
122
- if (!lastItem) {
123
- return false;
124
- }
125
- const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1);
126
- return editor.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run();
127
- }
128
- if (!isNodeActive(editor.state, name)) {
129
- return false;
130
- }
131
- if (!isAtStartOfNode(editor.state)) {
132
- return false;
133
- }
134
- const listItemPos = findListItemPos(name, editor.state);
135
- if (!listItemPos) {
136
- return false;
137
- }
138
- const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2);
139
- const prevNode = $prev.node(listItemPos.depth);
140
- const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode);
141
- if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
142
- return editor.commands.joinItemBackward();
143
- }
144
- return editor.chain().liftListItem(name).run();
145
- };
146
-
147
- // src/listHelpers/handleDelete.ts
148
- import { isAtEndOfNode, isNodeActive as isNodeActive2 } from "@tiptap/core";
149
-
150
- // src/listHelpers/nextListIsDeeper.ts
151
- var nextListIsDeeper = (typeOrName, state) => {
152
- const listDepth = getNextListDepth(typeOrName, state);
153
- const listItemPos = findListItemPos(typeOrName, state);
154
- if (!listItemPos || !listDepth) {
155
- return false;
156
- }
157
- if (listDepth > listItemPos.depth) {
158
- return true;
159
- }
160
- return false;
161
- };
162
-
163
- // src/listHelpers/nextListIsHigher.ts
164
- var nextListIsHigher = (typeOrName, state) => {
165
- const listDepth = getNextListDepth(typeOrName, state);
166
- const listItemPos = findListItemPos(typeOrName, state);
167
- if (!listItemPos || !listDepth) {
168
- return false;
169
- }
170
- if (listDepth < listItemPos.depth) {
171
- return true;
172
- }
173
- return false;
174
- };
175
-
176
- // src/listHelpers/handleDelete.ts
177
- var handleDelete = (editor, name) => {
178
- if (!isNodeActive2(editor.state, name)) {
179
- return false;
180
- }
181
- if (!isAtEndOfNode(editor.state, name)) {
182
- return false;
183
- }
184
- const { selection } = editor.state;
185
- const { $from, $to } = selection;
186
- if (!selection.empty && $from.sameParent($to)) {
187
- return false;
188
- }
189
- if (nextListIsDeeper(name, editor.state)) {
190
- return editor.chain().focus(editor.state.selection.from + 4).lift(name).joinBackward().run();
191
- }
192
- if (nextListIsHigher(name, editor.state)) {
193
- return editor.chain().joinForward().joinBackward().run();
194
- }
195
- return editor.commands.joinItemForward();
196
- };
197
-
198
- // src/listHelpers/hasListItemAfter.ts
199
- var hasListItemAfter = (typeOrName, state) => {
200
- var _a;
201
- const { $anchor } = state.selection;
202
- const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2);
203
- if ($targetPos.index() === $targetPos.parent.childCount - 1) {
204
- return false;
205
- }
206
- if (((_a = $targetPos.nodeAfter) == null ? void 0 : _a.type.name) !== typeOrName) {
207
- return false;
208
- }
209
- return true;
210
- };
211
-
212
- // src/list-keymap.ts
213
- var ListKeymap = Extension.create({
214
- name: "listKeymap",
215
- addOptions() {
216
- return {
217
- listTypes: [
218
- {
219
- itemName: "listItem",
220
- wrapperNames: ["bulletList", "orderedList"]
221
- },
222
- {
223
- itemName: "taskItem",
224
- wrapperNames: ["taskList"]
225
- }
226
- ]
227
- };
228
- },
229
- addKeyboardShortcuts() {
230
- return {
231
- Delete: ({ editor }) => {
232
- let handled = false;
233
- this.options.listTypes.forEach(({ itemName }) => {
234
- if (editor.state.schema.nodes[itemName] === void 0) {
235
- return;
236
- }
237
- if (handleDelete(editor, itemName)) {
238
- handled = true;
239
- }
240
- });
241
- return handled;
242
- },
243
- "Mod-Delete": ({ editor }) => {
244
- let handled = false;
245
- this.options.listTypes.forEach(({ itemName }) => {
246
- if (editor.state.schema.nodes[itemName] === void 0) {
247
- return;
248
- }
249
- if (handleDelete(editor, itemName)) {
250
- handled = true;
251
- }
252
- });
253
- return handled;
254
- },
255
- Backspace: ({ editor }) => {
256
- let handled = false;
257
- this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
258
- if (editor.state.schema.nodes[itemName] === void 0) {
259
- return;
260
- }
261
- if (handleBackspace(editor, itemName, wrapperNames)) {
262
- handled = true;
263
- }
264
- });
265
- return handled;
266
- },
267
- "Mod-Backspace": ({ editor }) => {
268
- let handled = false;
269
- this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
270
- if (editor.state.schema.nodes[itemName] === void 0) {
271
- return;
272
- }
273
- if (handleBackspace(editor, itemName, wrapperNames)) {
274
- handled = true;
275
- }
276
- });
277
- return handled;
278
- }
279
- };
280
- }
281
- });
282
-
283
1
  // src/index.ts
284
- var src_default = ListKeymap;
2
+ import { ListKeymap } from "@tiptap/extension-list";
3
+ import { listHelpers, ListKeymap as ListKeymap2, ListKeymapOptions } from "@tiptap/extension-list";
4
+ var index_default = ListKeymap;
285
5
  export {
286
- ListKeymap,
287
- src_default as default,
288
- listHelpers_exports as listHelpers
6
+ ListKeymap2 as ListKeymap,
7
+ ListKeymapOptions,
8
+ index_default as default,
9
+ listHelpers
289
10
  };
290
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/list-keymap.ts","../src/listHelpers/index.ts","../src/listHelpers/findListItemPos.ts","../src/listHelpers/getNextListDepth.ts","../src/listHelpers/handleBackspace.ts","../src/listHelpers/hasListBefore.ts","../src/listHelpers/hasListItemBefore.ts","../src/listHelpers/listItemHasSubList.ts","../src/listHelpers/handleDelete.ts","../src/listHelpers/nextListIsDeeper.ts","../src/listHelpers/nextListIsHigher.ts","../src/listHelpers/hasListItemAfter.ts","../src/index.ts"],"sourcesContent":["import { Extension } from '@tiptap/core'\n\nimport { handleBackspace, handleDelete } from './listHelpers/index.js'\n\nexport type ListKeymapOptions = {\n /**\n * An array of list types. This is used for item and wrapper list matching.\n * @default []\n * @example [{ itemName: 'listItem', wrapperNames: ['bulletList', 'orderedList'] }]\n */\n listTypes: Array<{\n itemName: string,\n wrapperNames: string[],\n }>\n}\n\n/**\n * This extension registers custom keymaps to change the behaviour of the backspace and delete keys.\n * By default Prosemirror keyhandling will always lift or sink items so paragraphs are joined into\n * the adjacent or previous list item. This extension will prevent this behaviour and instead will\n * try to join paragraphs from two list items into a single list item.\n * @see https://www.tiptap.dev/api/extensions/list-keymap\n */\nexport const ListKeymap = Extension.create<ListKeymapOptions>({\n name: 'listKeymap',\n\n addOptions() {\n return {\n listTypes: [\n {\n itemName: 'listItem',\n wrapperNames: ['bulletList', 'orderedList'],\n },\n {\n itemName: 'taskItem',\n wrapperNames: ['taskList'],\n },\n ],\n }\n },\n\n addKeyboardShortcuts() {\n return {\n Delete: ({ editor }) => {\n let handled = false\n\n this.options.listTypes.forEach(({ itemName }) => {\n if (editor.state.schema.nodes[itemName] === undefined) {\n return\n }\n\n if (handleDelete(editor, itemName)) {\n handled = true\n }\n })\n\n return handled\n },\n 'Mod-Delete': ({ editor }) => {\n let handled = false\n\n this.options.listTypes.forEach(({ itemName }) => {\n if (editor.state.schema.nodes[itemName] === undefined) {\n return\n }\n\n if (handleDelete(editor, itemName)) {\n handled = true\n }\n })\n\n return handled\n },\n Backspace: ({ editor }) => {\n let handled = false\n\n this.options.listTypes.forEach(({ itemName, wrapperNames }) => {\n if (editor.state.schema.nodes[itemName] === undefined) {\n return\n }\n\n if (handleBackspace(editor, itemName, wrapperNames)) {\n handled = true\n }\n })\n\n return handled\n },\n 'Mod-Backspace': ({ editor }) => {\n let handled = false\n\n this.options.listTypes.forEach(({ itemName, wrapperNames }) => {\n if (editor.state.schema.nodes[itemName] === undefined) {\n return\n }\n\n if (handleBackspace(editor, itemName, wrapperNames)) {\n handled = true\n }\n })\n\n return handled\n },\n }\n },\n})\n","export * from './findListItemPos.js'\nexport * from './getNextListDepth.js'\nexport * from './handleBackspace.js'\nexport * from './handleDelete.js'\nexport * from './hasListBefore.js'\nexport * from './hasListItemAfter.js'\nexport * from './hasListItemBefore.js'\nexport * from './listItemHasSubList.js'\nexport * from './nextListIsDeeper.js'\nexport * from './nextListIsHigher.js'\n","import { getNodeType } from '@tiptap/core'\nimport { NodeType } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nexport const findListItemPos = (typeOrName: string | NodeType, state: EditorState) => {\n const { $from } = state.selection\n const nodeType = getNodeType(typeOrName, state.schema)\n\n let currentNode = null\n let currentDepth = $from.depth\n let currentPos = $from.pos\n let targetDepth: number | null = null\n\n while (currentDepth > 0 && targetDepth === null) {\n currentNode = $from.node(currentDepth)\n\n if (currentNode.type === nodeType) {\n targetDepth = currentDepth\n } else {\n currentDepth -= 1\n currentPos -= 1\n }\n }\n\n if (targetDepth === null) {\n return null\n }\n\n return { $pos: state.doc.resolve(currentPos), depth: targetDepth }\n}\n","import { getNodeAtPosition } from '@tiptap/core'\nimport { EditorState } from '@tiptap/pm/state'\n\nimport { findListItemPos } from './findListItemPos.js'\n\nexport const getNextListDepth = (typeOrName: string, state: EditorState) => {\n const listItemPos = findListItemPos(typeOrName, state)\n\n if (!listItemPos) {\n return false\n }\n\n const [, depth] = getNodeAtPosition(state, typeOrName, listItemPos.$pos.pos + 4)\n\n return depth\n}\n","import { Editor, isAtStartOfNode, isNodeActive } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\n\nimport { findListItemPos } from './findListItemPos.js'\nimport { hasListBefore } from './hasListBefore.js'\nimport { hasListItemBefore } from './hasListItemBefore.js'\nimport { listItemHasSubList } from './listItemHasSubList.js'\n\nexport const handleBackspace = (editor: Editor, name: string, parentListTypes: string[]) => {\n // this is required to still handle the undo handling\n if (editor.commands.undoInputRule()) {\n return true\n }\n\n // if the selection is not collapsed\n // we can rely on the default backspace behavior\n if (editor.state.selection.from !== editor.state.selection.to) {\n return false\n }\n\n // if the current item is NOT inside a list item &\n // the previous item is a list (orderedList or bulletList)\n // move the cursor into the list and delete the current item\n if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {\n const { $anchor } = editor.state.selection\n\n const $listPos = editor.state.doc.resolve($anchor.before() - 1)\n\n const listDescendants: Array<{ node: Node, pos: number }> = []\n\n $listPos.node().descendants((node, pos) => {\n if (node.type.name === name) {\n listDescendants.push({ node, pos })\n }\n })\n\n const lastItem = listDescendants.at(-1)\n\n if (!lastItem) {\n return false\n }\n\n const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1)\n\n return editor.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run()\n }\n\n // if the cursor is not inside the current node type\n // do nothing and proceed\n if (!isNodeActive(editor.state, name)) {\n return false\n }\n\n // if the cursor is not at the start of a node\n // do nothing and proceed\n if (!isAtStartOfNode(editor.state)) {\n return false\n }\n\n const listItemPos = findListItemPos(name, editor.state)\n\n if (!listItemPos) {\n return false\n }\n\n const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2)\n const prevNode = $prev.node(listItemPos.depth)\n\n const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode)\n\n // if the previous item is a list item and doesn't have a sublist, join the list items\n if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {\n return editor.commands.joinItemBackward()\n }\n\n // otherwise in the end, a backspace should\n // always just lift the list item if\n // joining / merging is not possible\n return editor.chain().liftListItem(name).run()\n}\n","import { EditorState } from '@tiptap/pm/state'\n\nexport const hasListBefore = (editorState: EditorState, name: string, parentListTypes: string[]) => {\n const { $anchor } = editorState.selection\n\n const previousNodePos = Math.max(0, $anchor.pos - 2)\n\n const previousNode = editorState.doc.resolve(previousNodePos).node()\n\n if (!previousNode || !parentListTypes.includes(previousNode.type.name)) {\n return false\n }\n\n return true\n}\n","import { EditorState } from '@tiptap/pm/state'\n\nexport const hasListItemBefore = (typeOrName: string, state: EditorState): boolean => {\n const { $anchor } = state.selection\n\n const $targetPos = state.doc.resolve($anchor.pos - 2)\n\n if ($targetPos.index() === 0) {\n return false\n }\n\n if ($targetPos.nodeBefore?.type.name !== typeOrName) {\n return false\n }\n\n return true\n}\n","import { getNodeType } from '@tiptap/core'\nimport { Node } from '@tiptap/pm/model'\nimport { EditorState } from '@tiptap/pm/state'\n\nexport const listItemHasSubList = (typeOrName: string, state: EditorState, node?: Node) => {\n if (!node) {\n return false\n }\n\n const nodeType = getNodeType(typeOrName, state.schema)\n\n let hasSubList = false\n\n node.descendants(child => {\n if (child.type === nodeType) {\n hasSubList = true\n }\n })\n\n return hasSubList\n}\n","import { Editor, isAtEndOfNode, isNodeActive } from '@tiptap/core'\n\nimport { nextListIsDeeper } from './nextListIsDeeper.js'\nimport { nextListIsHigher } from './nextListIsHigher.js'\n\nexport const handleDelete = (editor: Editor, name: string) => {\n // if the cursor is not inside the current node type\n // do nothing and proceed\n if (!isNodeActive(editor.state, name)) {\n return false\n }\n\n // if the cursor is not at the end of a node\n // do nothing and proceed\n if (!isAtEndOfNode(editor.state, name)) {\n return false\n }\n\n // if the selection is not collapsed, or not within a single node\n // do nothing and proceed\n const { selection } = editor.state\n const { $from, $to } = selection\n\n if (!selection.empty && $from.sameParent($to)) {\n return false\n }\n\n // check if the next node is a list with a deeper depth\n if (nextListIsDeeper(name, editor.state)) {\n return editor\n .chain()\n .focus(editor.state.selection.from + 4)\n .lift(name)\n .joinBackward()\n .run()\n }\n\n if (nextListIsHigher(name, editor.state)) {\n return editor.chain()\n .joinForward()\n .joinBackward()\n .run()\n }\n\n return editor.commands.joinItemForward()\n}\n","import { EditorState } from '@tiptap/pm/state'\n\nimport { findListItemPos } from './findListItemPos.js'\nimport { getNextListDepth } from './getNextListDepth.js'\n\nexport const nextListIsDeeper = (typeOrName: string, state: EditorState) => {\n const listDepth = getNextListDepth(typeOrName, state)\n const listItemPos = findListItemPos(typeOrName, state)\n\n if (!listItemPos || !listDepth) {\n return false\n }\n\n if (listDepth > listItemPos.depth) {\n return true\n }\n\n return false\n}\n","import { EditorState } from '@tiptap/pm/state'\n\nimport { findListItemPos } from './findListItemPos.js'\nimport { getNextListDepth } from './getNextListDepth.js'\n\nexport const nextListIsHigher = (typeOrName: string, state: EditorState) => {\n const listDepth = getNextListDepth(typeOrName, state)\n const listItemPos = findListItemPos(typeOrName, state)\n\n if (!listItemPos || !listDepth) {\n return false\n }\n\n if (listDepth < listItemPos.depth) {\n return true\n }\n\n return false\n}\n","import { EditorState } from '@tiptap/pm/state'\n\nexport const hasListItemAfter = (typeOrName: string, state: EditorState): boolean => {\n const { $anchor } = state.selection\n\n const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2)\n\n if ($targetPos.index() === $targetPos.parent.childCount - 1) {\n return false\n }\n\n if ($targetPos.nodeAfter?.type.name !== typeOrName) {\n return false\n }\n\n return true\n}\n","import { ListKeymap } from './list-keymap.js'\n\nexport * from './list-keymap.js'\nexport * as listHelpers from './listHelpers/index.js'\n\nexport default ListKeymap\n"],"mappings":";;;;;;;AAAA,SAAS,iBAAiB;;;ACA1B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,SAAS,mBAAmB;AAIrB,IAAM,kBAAkB,CAAC,YAA+B,UAAuB;AACpF,QAAM,EAAE,MAAM,IAAI,MAAM;AACxB,QAAM,WAAW,YAAY,YAAY,MAAM,MAAM;AAErD,MAAI,cAAc;AAClB,MAAI,eAAe,MAAM;AACzB,MAAI,aAAa,MAAM;AACvB,MAAI,cAA6B;AAEjC,SAAO,eAAe,KAAK,gBAAgB,MAAM;AAC/C,kBAAc,MAAM,KAAK,YAAY;AAErC,QAAI,YAAY,SAAS,UAAU;AACjC,oBAAc;AAAA,IAChB,OAAO;AACL,sBAAgB;AAChB,oBAAc;AAAA,IAChB;AAAA,EACF;AAEA,MAAI,gBAAgB,MAAM;AACxB,WAAO;AAAA,EACT;AAEA,SAAO,EAAE,MAAM,MAAM,IAAI,QAAQ,UAAU,GAAG,OAAO,YAAY;AACnE;;;AC7BA,SAAS,yBAAyB;AAK3B,IAAM,mBAAmB,CAAC,YAAoB,UAAuB;AAC1E,QAAM,cAAc,gBAAgB,YAAY,KAAK;AAErD,MAAI,CAAC,aAAa;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AA,QACF,CAAC;AAED,eAAO;AAAA,MACT;AAAA,MACA,WAAW,CAAC,EAAE,OAAO,MAAM;AACzB,YAAI,UAAU;AAEd,aAAK,QAAQ,UAAU,QAAQ,CAAC,EAAE,UAAU,aAAa,MAAM;AAC7D,cAAI,OAAO,MAAM,OAAO,MAAM,QAAQ,MAAM,QAAW;AACrD;AAAA,UACF;AAEA,cAAI,gBAAgB,QAAQ,UAAU,YAAY,GAAG;AACnD,sBAAU;AAAA,UACZ;AAAA,QACF,CAAC;AAED,eAAO;AAAA,MACT;AAAA,MACA,iBAAiB,CAAC,EAAE,OAAO,MAAM;AAC/B,YAAI,UAAU;AAEd,aAAK,QAAQ,UAAU,QAAQ,CAAC,EAAE,UAAU,aAAa,MAAM;AAC7D,cAAI,OAAO,MAAM,OAAO,MAAM,QAAQ,MAAM,QAAW;AACrD;AAAA,UACF;AAEA,cAAI,gBAAgB,QAAQ,UAAU,YAAY,GAAG;AACnD,sBAAU;AAAA,UACZ;AAAA,QACF,CAAC;AAED,eAAO;AAAA,MACT;AAAA,IACF;AAAA,EACF;AACF,CAAC;;;AYpGD,IAAO,cAAQ;","names":["getNodeType","isNodeActive","isNodeActive"]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { ListKeymap } from '@tiptap/extension-list'\n\nexport { listHelpers, ListKeymap, ListKeymapOptions } from '@tiptap/extension-list'\n\nexport default ListKeymap\n"],"mappings":";AAAA,SAAS,kBAAkB;AAE3B,SAAS,aAAa,cAAAA,aAAY,yBAAyB;AAE3D,IAAO,gBAAQ;","names":["ListKeymap"]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiptap/extension-list-keymap",
3
3
  "description": "list keymap extension for tiptap",
4
- "version": "3.0.0-next.3",
4
+ "version": "3.0.0-next.5",
5
5
  "homepage": "https://tiptap.dev",
6
6
  "keywords": [
7
7
  "tiptap",
@@ -15,7 +15,10 @@
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/index.d.ts",
18
+ "types": {
19
+ "import": "./dist/index.d.ts",
20
+ "require": "./dist/index.d.cts"
21
+ },
19
22
  "import": "./dist/index.js",
20
23
  "require": "./dist/index.cjs"
21
24
  }
@@ -28,10 +31,10 @@
28
31
  "dist"
29
32
  ],
30
33
  "devDependencies": {
31
- "@tiptap/core": "^3.0.0-next.3"
34
+ "@tiptap/extension-list": "^3.0.0-next.5"
32
35
  },
33
36
  "peerDependencies": {
34
- "@tiptap/core": "^3.0.0-next.1"
37
+ "@tiptap/extension-list": "^3.0.0-next.4"
35
38
  },
36
39
  "repository": {
37
40
  "type": "git",
@@ -39,6 +42,7 @@
39
42
  "directory": "packages/extension-list-keymap"
40
43
  },
41
44
  "scripts": {
42
- "build": "tsup"
45
+ "build": "tsup",
46
+ "lint": "prettier ./src/ --check && eslint --cache --quiet --no-error-on-unmatched-pattern ./src/"
43
47
  }
44
- }
48
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,5 @@
1
- import { ListKeymap } from './list-keymap.js'
1
+ import { ListKeymap } from '@tiptap/extension-list'
2
2
 
3
- export * from './list-keymap.js'
4
- export * as listHelpers from './listHelpers/index.js'
3
+ export { listHelpers, ListKeymap, ListKeymapOptions } from '@tiptap/extension-list'
5
4
 
6
5
  export default ListKeymap
@@ -1,106 +0,0 @@
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
- })
@@ -1,30 +0,0 @@
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
- }
@@ -1,16 +0,0 @@
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
- }
@@ -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
- }