@tiptap/extension-list-keymap 3.0.0-next.4 → 3.0.0-next.6
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/LICENSE.md +1 -1
- package/dist/index.cjs +7 -281
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -63
- package/dist/index.d.ts +2 -63
- package/dist/index.js +5 -284
- package/dist/index.js.map +1 -1
- package/package.json +3 -5
- package/src/index.ts +2 -3
- package/src/list-keymap.ts +0 -106
- package/src/listHelpers/findListItemPos.ts +0 -30
- package/src/listHelpers/getNextListDepth.ts +0 -16
- package/src/listHelpers/handleBackspace.ts +0 -84
- package/src/listHelpers/handleDelete.ts +0 -43
- package/src/listHelpers/hasListBefore.ts +0 -15
- package/src/listHelpers/hasListItemAfter.ts +0 -17
- package/src/listHelpers/hasListItemBefore.ts +0 -17
- package/src/listHelpers/index.ts +0 -10
- package/src/listHelpers/listItemHasSubList.ts +0 -21
- package/src/listHelpers/nextListIsDeeper.ts +0 -19
- package/src/listHelpers/nextListIsHigher.ts +0 -19
package/LICENSE.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -20,293 +20,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
-
ListKeymap: () => ListKeymap,
|
|
23
|
+
ListKeymap: () => import_extension_list2.ListKeymap,
|
|
24
|
+
ListKeymapOptions: () => import_extension_list2.ListKeymapOptions,
|
|
24
25
|
default: () => index_default,
|
|
25
|
-
listHelpers: () =>
|
|
26
|
+
listHelpers: () => import_extension_list2.listHelpers
|
|
26
27
|
});
|
|
27
28
|
module.exports = __toCommonJS(index_exports);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
var
|
|
31
|
-
|
|
32
|
-
// src/listHelpers/index.ts
|
|
33
|
-
var listHelpers_exports = {};
|
|
34
|
-
__export(listHelpers_exports, {
|
|
35
|
-
findListItemPos: () => findListItemPos,
|
|
36
|
-
getNextListDepth: () => getNextListDepth,
|
|
37
|
-
handleBackspace: () => handleBackspace,
|
|
38
|
-
handleDelete: () => handleDelete,
|
|
39
|
-
hasListBefore: () => hasListBefore,
|
|
40
|
-
hasListItemAfter: () => hasListItemAfter,
|
|
41
|
-
hasListItemBefore: () => hasListItemBefore,
|
|
42
|
-
listItemHasSubList: () => listItemHasSubList,
|
|
43
|
-
nextListIsDeeper: () => nextListIsDeeper,
|
|
44
|
-
nextListIsHigher: () => nextListIsHigher
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// src/listHelpers/findListItemPos.ts
|
|
48
|
-
var import_core = require("@tiptap/core");
|
|
49
|
-
var findListItemPos = (typeOrName, state) => {
|
|
50
|
-
const { $from } = state.selection;
|
|
51
|
-
const nodeType = (0, import_core.getNodeType)(typeOrName, state.schema);
|
|
52
|
-
let currentNode = null;
|
|
53
|
-
let currentDepth = $from.depth;
|
|
54
|
-
let currentPos = $from.pos;
|
|
55
|
-
let targetDepth = null;
|
|
56
|
-
while (currentDepth > 0 && targetDepth === null) {
|
|
57
|
-
currentNode = $from.node(currentDepth);
|
|
58
|
-
if (currentNode.type === nodeType) {
|
|
59
|
-
targetDepth = currentDepth;
|
|
60
|
-
} else {
|
|
61
|
-
currentDepth -= 1;
|
|
62
|
-
currentPos -= 1;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
if (targetDepth === null) {
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
return { $pos: state.doc.resolve(currentPos), depth: targetDepth };
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
// src/listHelpers/getNextListDepth.ts
|
|
72
|
-
var import_core2 = require("@tiptap/core");
|
|
73
|
-
var getNextListDepth = (typeOrName, state) => {
|
|
74
|
-
const listItemPos = findListItemPos(typeOrName, state);
|
|
75
|
-
if (!listItemPos) {
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
const [, depth] = (0, import_core2.getNodeAtPosition)(state, typeOrName, listItemPos.$pos.pos + 4);
|
|
79
|
-
return depth;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
// src/listHelpers/handleBackspace.ts
|
|
83
|
-
var import_core4 = require("@tiptap/core");
|
|
84
|
-
|
|
85
|
-
// src/listHelpers/hasListBefore.ts
|
|
86
|
-
var hasListBefore = (editorState, name, parentListTypes) => {
|
|
87
|
-
const { $anchor } = editorState.selection;
|
|
88
|
-
const previousNodePos = Math.max(0, $anchor.pos - 2);
|
|
89
|
-
const previousNode = editorState.doc.resolve(previousNodePos).node();
|
|
90
|
-
if (!previousNode || !parentListTypes.includes(previousNode.type.name)) {
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
return true;
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
// src/listHelpers/hasListItemBefore.ts
|
|
97
|
-
var hasListItemBefore = (typeOrName, state) => {
|
|
98
|
-
var _a;
|
|
99
|
-
const { $anchor } = state.selection;
|
|
100
|
-
const $targetPos = state.doc.resolve($anchor.pos - 2);
|
|
101
|
-
if ($targetPos.index() === 0) {
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
if (((_a = $targetPos.nodeBefore) == null ? void 0 : _a.type.name) !== typeOrName) {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
return true;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
// src/listHelpers/listItemHasSubList.ts
|
|
111
|
-
var import_core3 = require("@tiptap/core");
|
|
112
|
-
var listItemHasSubList = (typeOrName, state, node) => {
|
|
113
|
-
if (!node) {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
const nodeType = (0, import_core3.getNodeType)(typeOrName, state.schema);
|
|
117
|
-
let hasSubList = false;
|
|
118
|
-
node.descendants((child) => {
|
|
119
|
-
if (child.type === nodeType) {
|
|
120
|
-
hasSubList = true;
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return hasSubList;
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
// src/listHelpers/handleBackspace.ts
|
|
127
|
-
var handleBackspace = (editor, name, parentListTypes) => {
|
|
128
|
-
if (editor.commands.undoInputRule()) {
|
|
129
|
-
return true;
|
|
130
|
-
}
|
|
131
|
-
if (editor.state.selection.from !== editor.state.selection.to) {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
if (!(0, import_core4.isNodeActive)(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {
|
|
135
|
-
const { $anchor } = editor.state.selection;
|
|
136
|
-
const $listPos = editor.state.doc.resolve($anchor.before() - 1);
|
|
137
|
-
const listDescendants = [];
|
|
138
|
-
$listPos.node().descendants((node, pos) => {
|
|
139
|
-
if (node.type.name === name) {
|
|
140
|
-
listDescendants.push({ node, pos });
|
|
141
|
-
}
|
|
142
|
-
});
|
|
143
|
-
const lastItem = listDescendants.at(-1);
|
|
144
|
-
if (!lastItem) {
|
|
145
|
-
return false;
|
|
146
|
-
}
|
|
147
|
-
const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1);
|
|
148
|
-
return editor.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run();
|
|
149
|
-
}
|
|
150
|
-
if (!(0, import_core4.isNodeActive)(editor.state, name)) {
|
|
151
|
-
return false;
|
|
152
|
-
}
|
|
153
|
-
if (!(0, import_core4.isAtStartOfNode)(editor.state)) {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
const listItemPos = findListItemPos(name, editor.state);
|
|
157
|
-
if (!listItemPos) {
|
|
158
|
-
return false;
|
|
159
|
-
}
|
|
160
|
-
const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2);
|
|
161
|
-
const prevNode = $prev.node(listItemPos.depth);
|
|
162
|
-
const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode);
|
|
163
|
-
if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
|
|
164
|
-
return editor.commands.joinItemBackward();
|
|
165
|
-
}
|
|
166
|
-
return editor.chain().liftListItem(name).run();
|
|
167
|
-
};
|
|
168
|
-
|
|
169
|
-
// src/listHelpers/handleDelete.ts
|
|
170
|
-
var import_core5 = require("@tiptap/core");
|
|
171
|
-
|
|
172
|
-
// src/listHelpers/nextListIsDeeper.ts
|
|
173
|
-
var nextListIsDeeper = (typeOrName, state) => {
|
|
174
|
-
const listDepth = getNextListDepth(typeOrName, state);
|
|
175
|
-
const listItemPos = findListItemPos(typeOrName, state);
|
|
176
|
-
if (!listItemPos || !listDepth) {
|
|
177
|
-
return false;
|
|
178
|
-
}
|
|
179
|
-
if (listDepth > listItemPos.depth) {
|
|
180
|
-
return true;
|
|
181
|
-
}
|
|
182
|
-
return false;
|
|
183
|
-
};
|
|
184
|
-
|
|
185
|
-
// src/listHelpers/nextListIsHigher.ts
|
|
186
|
-
var nextListIsHigher = (typeOrName, state) => {
|
|
187
|
-
const listDepth = getNextListDepth(typeOrName, state);
|
|
188
|
-
const listItemPos = findListItemPos(typeOrName, state);
|
|
189
|
-
if (!listItemPos || !listDepth) {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
if (listDepth < listItemPos.depth) {
|
|
193
|
-
return true;
|
|
194
|
-
}
|
|
195
|
-
return false;
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
// src/listHelpers/handleDelete.ts
|
|
199
|
-
var handleDelete = (editor, name) => {
|
|
200
|
-
if (!(0, import_core5.isNodeActive)(editor.state, name)) {
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
if (!(0, import_core5.isAtEndOfNode)(editor.state, name)) {
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
const { selection } = editor.state;
|
|
207
|
-
const { $from, $to } = selection;
|
|
208
|
-
if (!selection.empty && $from.sameParent($to)) {
|
|
209
|
-
return false;
|
|
210
|
-
}
|
|
211
|
-
if (nextListIsDeeper(name, editor.state)) {
|
|
212
|
-
return editor.chain().focus(editor.state.selection.from + 4).lift(name).joinBackward().run();
|
|
213
|
-
}
|
|
214
|
-
if (nextListIsHigher(name, editor.state)) {
|
|
215
|
-
return editor.chain().joinForward().joinBackward().run();
|
|
216
|
-
}
|
|
217
|
-
return editor.commands.joinItemForward();
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
// src/listHelpers/hasListItemAfter.ts
|
|
221
|
-
var hasListItemAfter = (typeOrName, state) => {
|
|
222
|
-
var _a;
|
|
223
|
-
const { $anchor } = state.selection;
|
|
224
|
-
const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2);
|
|
225
|
-
if ($targetPos.index() === $targetPos.parent.childCount - 1) {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
if (((_a = $targetPos.nodeAfter) == null ? void 0 : _a.type.name) !== typeOrName) {
|
|
229
|
-
return false;
|
|
230
|
-
}
|
|
231
|
-
return true;
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
// src/list-keymap.ts
|
|
235
|
-
var ListKeymap = import_core6.Extension.create({
|
|
236
|
-
name: "listKeymap",
|
|
237
|
-
addOptions() {
|
|
238
|
-
return {
|
|
239
|
-
listTypes: [
|
|
240
|
-
{
|
|
241
|
-
itemName: "listItem",
|
|
242
|
-
wrapperNames: ["bulletList", "orderedList"]
|
|
243
|
-
},
|
|
244
|
-
{
|
|
245
|
-
itemName: "taskItem",
|
|
246
|
-
wrapperNames: ["taskList"]
|
|
247
|
-
}
|
|
248
|
-
]
|
|
249
|
-
};
|
|
250
|
-
},
|
|
251
|
-
addKeyboardShortcuts() {
|
|
252
|
-
return {
|
|
253
|
-
Delete: ({ editor }) => {
|
|
254
|
-
let handled = false;
|
|
255
|
-
this.options.listTypes.forEach(({ itemName }) => {
|
|
256
|
-
if (editor.state.schema.nodes[itemName] === void 0) {
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
if (handleDelete(editor, itemName)) {
|
|
260
|
-
handled = true;
|
|
261
|
-
}
|
|
262
|
-
});
|
|
263
|
-
return handled;
|
|
264
|
-
},
|
|
265
|
-
"Mod-Delete": ({ editor }) => {
|
|
266
|
-
let handled = false;
|
|
267
|
-
this.options.listTypes.forEach(({ itemName }) => {
|
|
268
|
-
if (editor.state.schema.nodes[itemName] === void 0) {
|
|
269
|
-
return;
|
|
270
|
-
}
|
|
271
|
-
if (handleDelete(editor, itemName)) {
|
|
272
|
-
handled = true;
|
|
273
|
-
}
|
|
274
|
-
});
|
|
275
|
-
return handled;
|
|
276
|
-
},
|
|
277
|
-
Backspace: ({ editor }) => {
|
|
278
|
-
let handled = false;
|
|
279
|
-
this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
|
|
280
|
-
if (editor.state.schema.nodes[itemName] === void 0) {
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
if (handleBackspace(editor, itemName, wrapperNames)) {
|
|
284
|
-
handled = true;
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
return handled;
|
|
288
|
-
},
|
|
289
|
-
"Mod-Backspace": ({ editor }) => {
|
|
290
|
-
let handled = false;
|
|
291
|
-
this.options.listTypes.forEach(({ itemName, wrapperNames }) => {
|
|
292
|
-
if (editor.state.schema.nodes[itemName] === void 0) {
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
if (handleBackspace(editor, itemName, wrapperNames)) {
|
|
296
|
-
handled = true;
|
|
297
|
-
}
|
|
298
|
-
});
|
|
299
|
-
return handled;
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
});
|
|
304
|
-
|
|
305
|
-
// src/index.ts
|
|
306
|
-
var index_default = ListKeymap;
|
|
29
|
+
var import_extension_list = require("@tiptap/extension-list");
|
|
30
|
+
var import_extension_list2 = require("@tiptap/extension-list");
|
|
31
|
+
var index_default = import_extension_list.ListKeymap;
|
|
307
32
|
// Annotate the CommonJS export names for ESM import in node:
|
|
308
33
|
0 && (module.exports = {
|
|
309
34
|
ListKeymap,
|
|
35
|
+
ListKeymapOptions,
|
|
310
36
|
listHelpers
|
|
311
37
|
});
|
|
312
38
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../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"],"sourcesContent":["import { ListKeymap } from './list-keymap.js'\n\nexport * from './list-keymap.js'\nexport * as listHelpers from './listHelpers/index.js'\n\nexport default ListKeymap\n","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\n .chain()\n .cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end())\n .joinForward()\n .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().joinForward().joinBackward().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"],"mappings":";;;;;;;;;;;;;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AE,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;;;ADpGD,IAAO,gBAAQ;","names":["import_core","import_core","import_core","import_core","import_core"]}
|
|
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;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA2B;AAE3B,IAAAA,yBAA2D;AAE3D,IAAO,gBAAQ;","names":["import_extension_list"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,63 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { NodeType, Node } from '@tiptap/pm/model';
|
|
4
|
-
import { EditorState } from '@tiptap/pm/state';
|
|
5
|
-
|
|
6
|
-
type ListKeymapOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* An array of list types. This is used for item and wrapper list matching.
|
|
9
|
-
* @default []
|
|
10
|
-
* @example [{ itemName: 'listItem', wrapperNames: ['bulletList', 'orderedList'] }]
|
|
11
|
-
*/
|
|
12
|
-
listTypes: Array<{
|
|
13
|
-
itemName: string;
|
|
14
|
-
wrapperNames: string[];
|
|
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
|
-
declare const ListKeymap: Extension<ListKeymapOptions, any>;
|
|
25
|
-
|
|
26
|
-
declare const findListItemPos: (typeOrName: string | NodeType, state: EditorState) => {
|
|
27
|
-
$pos: _tiptap_pm_model.ResolvedPos;
|
|
28
|
-
depth: number;
|
|
29
|
-
} | null;
|
|
30
|
-
|
|
31
|
-
declare const getNextListDepth: (typeOrName: string, state: EditorState) => number | false;
|
|
32
|
-
|
|
33
|
-
declare const handleBackspace: (editor: Editor, name: string, parentListTypes: string[]) => boolean;
|
|
34
|
-
|
|
35
|
-
declare const handleDelete: (editor: Editor, name: string) => boolean;
|
|
36
|
-
|
|
37
|
-
declare const hasListBefore: (editorState: EditorState, name: string, parentListTypes: string[]) => boolean;
|
|
38
|
-
|
|
39
|
-
declare const hasListItemAfter: (typeOrName: string, state: EditorState) => boolean;
|
|
40
|
-
|
|
41
|
-
declare const hasListItemBefore: (typeOrName: string, state: EditorState) => boolean;
|
|
42
|
-
|
|
43
|
-
declare const listItemHasSubList: (typeOrName: string, state: EditorState, node?: Node) => boolean;
|
|
44
|
-
|
|
45
|
-
declare const nextListIsDeeper: (typeOrName: string, state: EditorState) => boolean;
|
|
46
|
-
|
|
47
|
-
declare const nextListIsHigher: (typeOrName: string, state: EditorState) => boolean;
|
|
48
|
-
|
|
49
|
-
declare const index_findListItemPos: typeof findListItemPos;
|
|
50
|
-
declare const index_getNextListDepth: typeof getNextListDepth;
|
|
51
|
-
declare const index_handleBackspace: typeof handleBackspace;
|
|
52
|
-
declare const index_handleDelete: typeof handleDelete;
|
|
53
|
-
declare const index_hasListBefore: typeof hasListBefore;
|
|
54
|
-
declare const index_hasListItemAfter: typeof hasListItemAfter;
|
|
55
|
-
declare const index_hasListItemBefore: typeof hasListItemBefore;
|
|
56
|
-
declare const index_listItemHasSubList: typeof listItemHasSubList;
|
|
57
|
-
declare const index_nextListIsDeeper: typeof nextListIsDeeper;
|
|
58
|
-
declare const index_nextListIsHigher: typeof nextListIsHigher;
|
|
59
|
-
declare namespace index {
|
|
60
|
-
export { index_findListItemPos as findListItemPos, index_getNextListDepth as getNextListDepth, index_handleBackspace as handleBackspace, index_handleDelete as handleDelete, index_hasListBefore as hasListBefore, index_hasListItemAfter as hasListItemAfter, index_hasListItemBefore as hasListItemBefore, index_listItemHasSubList as listItemHasSubList, index_nextListIsDeeper as nextListIsDeeper, index_nextListIsHigher as nextListIsHigher };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export { ListKeymap, type ListKeymapOptions, ListKeymap as default, index as listHelpers };
|
|
1
|
+
import { ListKeymap } from '@tiptap/extension-list';
|
|
2
|
+
export { ListKeymap, ListKeymapOptions, ListKeymap as default, listHelpers } from '@tiptap/extension-list';
|
package/dist/index.d.ts
CHANGED
|
@@ -1,63 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import { NodeType, Node } from '@tiptap/pm/model';
|
|
4
|
-
import { EditorState } from '@tiptap/pm/state';
|
|
5
|
-
|
|
6
|
-
type ListKeymapOptions = {
|
|
7
|
-
/**
|
|
8
|
-
* An array of list types. This is used for item and wrapper list matching.
|
|
9
|
-
* @default []
|
|
10
|
-
* @example [{ itemName: 'listItem', wrapperNames: ['bulletList', 'orderedList'] }]
|
|
11
|
-
*/
|
|
12
|
-
listTypes: Array<{
|
|
13
|
-
itemName: string;
|
|
14
|
-
wrapperNames: string[];
|
|
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
|
-
declare const ListKeymap: Extension<ListKeymapOptions, any>;
|
|
25
|
-
|
|
26
|
-
declare const findListItemPos: (typeOrName: string | NodeType, state: EditorState) => {
|
|
27
|
-
$pos: _tiptap_pm_model.ResolvedPos;
|
|
28
|
-
depth: number;
|
|
29
|
-
} | null;
|
|
30
|
-
|
|
31
|
-
declare const getNextListDepth: (typeOrName: string, state: EditorState) => number | false;
|
|
32
|
-
|
|
33
|
-
declare const handleBackspace: (editor: Editor, name: string, parentListTypes: string[]) => boolean;
|
|
34
|
-
|
|
35
|
-
declare const handleDelete: (editor: Editor, name: string) => boolean;
|
|
36
|
-
|
|
37
|
-
declare const hasListBefore: (editorState: EditorState, name: string, parentListTypes: string[]) => boolean;
|
|
38
|
-
|
|
39
|
-
declare const hasListItemAfter: (typeOrName: string, state: EditorState) => boolean;
|
|
40
|
-
|
|
41
|
-
declare const hasListItemBefore: (typeOrName: string, state: EditorState) => boolean;
|
|
42
|
-
|
|
43
|
-
declare const listItemHasSubList: (typeOrName: string, state: EditorState, node?: Node) => boolean;
|
|
44
|
-
|
|
45
|
-
declare const nextListIsDeeper: (typeOrName: string, state: EditorState) => boolean;
|
|
46
|
-
|
|
47
|
-
declare const nextListIsHigher: (typeOrName: string, state: EditorState) => boolean;
|
|
48
|
-
|
|
49
|
-
declare const index_findListItemPos: typeof findListItemPos;
|
|
50
|
-
declare const index_getNextListDepth: typeof getNextListDepth;
|
|
51
|
-
declare const index_handleBackspace: typeof handleBackspace;
|
|
52
|
-
declare const index_handleDelete: typeof handleDelete;
|
|
53
|
-
declare const index_hasListBefore: typeof hasListBefore;
|
|
54
|
-
declare const index_hasListItemAfter: typeof hasListItemAfter;
|
|
55
|
-
declare const index_hasListItemBefore: typeof hasListItemBefore;
|
|
56
|
-
declare const index_listItemHasSubList: typeof listItemHasSubList;
|
|
57
|
-
declare const index_nextListIsDeeper: typeof nextListIsDeeper;
|
|
58
|
-
declare const index_nextListIsHigher: typeof nextListIsHigher;
|
|
59
|
-
declare namespace index {
|
|
60
|
-
export { index_findListItemPos as findListItemPos, index_getNextListDepth as getNextListDepth, index_handleBackspace as handleBackspace, index_handleDelete as handleDelete, index_hasListBefore as hasListBefore, index_hasListItemAfter as hasListItemAfter, index_hasListItemBefore as hasListItemBefore, index_listItemHasSubList as listItemHasSubList, index_nextListIsDeeper as nextListIsDeeper, index_nextListIsHigher as nextListIsHigher };
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export { ListKeymap, type ListKeymapOptions, ListKeymap as default, index as listHelpers };
|
|
1
|
+
import { ListKeymap } from '@tiptap/extension-list';
|
|
2
|
+
export { ListKeymap, ListKeymapOptions, ListKeymap as default, listHelpers } from '@tiptap/extension-list';
|
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
|
|
2
|
+
import { ListKeymap } from "@tiptap/extension-list";
|
|
3
|
+
import { listHelpers, ListKeymap as ListKeymap2, ListKeymapOptions } from "@tiptap/extension-list";
|
|
284
4
|
var index_default = ListKeymap;
|
|
285
5
|
export {
|
|
286
|
-
ListKeymap,
|
|
6
|
+
ListKeymap2 as ListKeymap,
|
|
7
|
+
ListKeymapOptions,
|
|
287
8
|
index_default as default,
|
|
288
|
-
|
|
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\n .chain()\n .cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end())\n .joinForward()\n .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().joinForward().joinBackward().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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A,cAAc,CAAC,EAAE,OAAO,MAAM;AAC5B,YAAI,UAAU;AAEd,aAAK,QAAQ,UAAU,QAAQ,CAAC,EAAE,SAAS,MAAM;AAC/C,cAAI,OAAO,MAAM,OAAO,MAAM,QAAQ,MAAM,QAAW;AACrD;AAAA,UACF;AAEA,cAAI,aAAa,QAAQ,QAAQ,GAAG;AAClC,sBAAU;AAAA,UACZ;AAAA,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,gBAAQ;","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.
|
|
4
|
+
"version": "3.0.0-next.6",
|
|
5
5
|
"homepage": "https://tiptap.dev",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"tiptap",
|
|
@@ -31,12 +31,10 @@
|
|
|
31
31
|
"dist"
|
|
32
32
|
],
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@tiptap/
|
|
35
|
-
"@tiptap/pm": "^3.0.0-next.4"
|
|
34
|
+
"@tiptap/extension-list": "^3.0.0-next.6"
|
|
36
35
|
},
|
|
37
36
|
"peerDependencies": {
|
|
38
|
-
"@tiptap/
|
|
39
|
-
"@tiptap/pm": "^3.0.0-next.3"
|
|
37
|
+
"@tiptap/extension-list": "^3.0.0-next.4"
|
|
40
38
|
},
|
|
41
39
|
"repository": {
|
|
42
40
|
"type": "git",
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { ListKeymap } from '
|
|
1
|
+
import { ListKeymap } from '@tiptap/extension-list'
|
|
2
2
|
|
|
3
|
-
export
|
|
4
|
-
export * as listHelpers from './listHelpers/index.js'
|
|
3
|
+
export { listHelpers, ListKeymap, ListKeymapOptions } from '@tiptap/extension-list'
|
|
5
4
|
|
|
6
5
|
export default ListKeymap
|
package/src/list-keymap.ts
DELETED
|
@@ -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,84 +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
|
|
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
|
-
}
|
|
@@ -1,43 +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().joinForward().joinBackward().run()
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return editor.commands.joinItemForward()
|
|
43
|
-
}
|
|
@@ -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
|
-
}
|
package/src/listHelpers/index.ts
DELETED
|
@@ -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
|
-
}
|