@tiptap/extension-list 3.24.0 → 3.25.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/kit/index.js CHANGED
@@ -5,7 +5,7 @@ var __export = (target, all) => {
5
5
  };
6
6
 
7
7
  // src/kit/index.ts
8
- import { Extension as Extension2 } from "@tiptap/core";
8
+ import { Extension as Extension3 } from "@tiptap/core";
9
9
 
10
10
  // src/bullet-list/bullet-list.ts
11
11
  import { mergeAttributes, Node, wrappingInputRule } from "@tiptap/core";
@@ -89,6 +89,115 @@ var BulletList = Node.create({
89
89
 
90
90
  // src/item/list-item.ts
91
91
  import { mergeAttributes as mergeAttributes2, Node as Node2, renderNestedMarkdownContent } from "@tiptap/core";
92
+
93
+ // src/helpers/createBranchingListDeleteKeymap.ts
94
+ import { Extension } from "@tiptap/core";
95
+
96
+ // src/helpers/hoistBranchingNestedList.ts
97
+ import { Fragment } from "@tiptap/pm/model";
98
+
99
+ // src/helpers/getBranchingNestedListAtCursor.ts
100
+ var getBranchingNestedListAtCursor = (state, itemName, wrapperNames) => {
101
+ const { selection } = state;
102
+ if (!selection.empty) {
103
+ return null;
104
+ }
105
+ const { $from } = selection;
106
+ if (!$from.parent.isTextblock) {
107
+ return null;
108
+ }
109
+ if ($from.parentOffset !== $from.parent.content.size) {
110
+ return null;
111
+ }
112
+ let listItemDepth = -1;
113
+ for (let depth = $from.depth; depth > 0; depth -= 1) {
114
+ if ($from.node(depth).type.name === itemName) {
115
+ listItemDepth = depth;
116
+ break;
117
+ }
118
+ }
119
+ if (listItemDepth < 0) {
120
+ return null;
121
+ }
122
+ const listItem = $from.node(listItemDepth);
123
+ const indexInListItem = $from.index(listItemDepth);
124
+ if (indexInListItem + 1 >= listItem.childCount) {
125
+ return null;
126
+ }
127
+ const nextChild = listItem.child(indexInListItem + 1);
128
+ if (!wrapperNames.includes(nextChild.type.name)) {
129
+ return null;
130
+ }
131
+ const itemType = state.schema.nodes[itemName];
132
+ let hasBranching = false;
133
+ nextChild.forEach((child) => {
134
+ if (child.type === itemType && child.childCount > 1) {
135
+ hasBranching = true;
136
+ }
137
+ });
138
+ if (!hasBranching) {
139
+ return null;
140
+ }
141
+ const nodeAfter = state.doc.resolve($from.after()).nodeAfter;
142
+ if (!nodeAfter || !wrapperNames.includes(nodeAfter.type.name)) {
143
+ return null;
144
+ }
145
+ const items = [];
146
+ nodeAfter.forEach((child) => {
147
+ items.push(child);
148
+ });
149
+ if (items.length === 0) {
150
+ return null;
151
+ }
152
+ return {
153
+ listItemDepth,
154
+ nestedList: nodeAfter,
155
+ nestedListPos: $from.after(),
156
+ insertPos: $from.after(listItemDepth),
157
+ items
158
+ };
159
+ };
160
+
161
+ // src/helpers/hoistBranchingNestedList.ts
162
+ var hoistBranchingNestedList = (state, dispatch, itemName, wrapperNames) => {
163
+ const context = getBranchingNestedListAtCursor(state, itemName, wrapperNames);
164
+ if (!context) {
165
+ return false;
166
+ }
167
+ const { selection } = state;
168
+ const { nestedList, nestedListPos, insertPos, items } = context;
169
+ const tr = state.tr;
170
+ tr.delete(nestedListPos, nestedListPos + nestedList.nodeSize);
171
+ const mappedInsertPos = tr.mapping.map(insertPos);
172
+ tr.insert(mappedInsertPos, Fragment.from(items));
173
+ tr.setSelection(selection.map(tr.doc, tr.mapping));
174
+ if (dispatch) {
175
+ dispatch(tr);
176
+ }
177
+ return true;
178
+ };
179
+
180
+ // src/helpers/handleDeleteBranchingNestedList.ts
181
+ var handleDeleteBranchingNestedList = (editor, itemName, wrapperNames) => {
182
+ return hoistBranchingNestedList(editor.state, editor.view.dispatch, itemName, wrapperNames);
183
+ };
184
+
185
+ // src/helpers/createBranchingListDeleteKeymap.ts
186
+ var createBranchingListDeleteKeymap = (itemName, wrapperNames) => {
187
+ return Extension.create({
188
+ name: `${itemName}BranchingDeleteKeymap`,
189
+ priority: 101,
190
+ addKeyboardShortcuts() {
191
+ const handleDelete2 = () => handleDeleteBranchingNestedList(this.editor, itemName, wrapperNames);
192
+ return {
193
+ Delete: handleDelete2,
194
+ "Mod-Delete": handleDelete2
195
+ };
196
+ }
197
+ });
198
+ };
199
+
200
+ // src/item/list-item.ts
92
201
  function isSameLineOrderedListToken(token) {
93
202
  var _a, _b;
94
203
  const nestedToken = (_a = token.tokens) == null ? void 0 : _a[0];
@@ -203,6 +312,14 @@ var ListItem = Node2.create({
203
312
  ctx
204
313
  );
205
314
  },
315
+ addExtensions() {
316
+ return [
317
+ createBranchingListDeleteKeymap(this.name, [
318
+ this.options.bulletListTypeName,
319
+ this.options.orderedListTypeName
320
+ ])
321
+ ];
322
+ },
206
323
  addKeyboardShortcuts() {
207
324
  return {
208
325
  Enter: () => this.editor.commands.splitListItem(this.name),
@@ -213,7 +330,7 @@ var ListItem = Node2.create({
213
330
  });
214
331
 
215
332
  // src/keymap/list-keymap.ts
216
- import { Extension } from "@tiptap/core";
333
+ import { Extension as Extension2 } from "@tiptap/core";
217
334
 
218
335
  // src/keymap/listHelpers/index.ts
219
336
  var listHelpers_exports = {};
@@ -279,36 +396,6 @@ var hasListBefore = (editorState, name, parentListTypes) => {
279
396
  return true;
280
397
  };
281
398
 
282
- // src/keymap/listHelpers/hasListItemBefore.ts
283
- var hasListItemBefore = (typeOrName, state) => {
284
- var _a;
285
- const { $anchor } = state.selection;
286
- const $targetPos = state.doc.resolve($anchor.pos - 2);
287
- if ($targetPos.index() === 0) {
288
- return false;
289
- }
290
- if (((_a = $targetPos.nodeBefore) == null ? void 0 : _a.type.name) !== typeOrName) {
291
- return false;
292
- }
293
- return true;
294
- };
295
-
296
- // src/keymap/listHelpers/listItemHasSubList.ts
297
- import { getNodeType as getNodeType2 } from "@tiptap/core";
298
- var listItemHasSubList = (typeOrName, state, node) => {
299
- if (!node) {
300
- return false;
301
- }
302
- const nodeType = getNodeType2(typeOrName, state.schema);
303
- let hasSubList = false;
304
- node.descendants((child) => {
305
- if (child.type === nodeType) {
306
- hasSubList = true;
307
- }
308
- });
309
- return hasSubList;
310
- };
311
-
312
399
  // src/keymap/listHelpers/handleBackspace.ts
313
400
  var handleBackspace = (editor, name, parentListTypes) => {
314
401
  if (editor.commands.undoInputRule()) {
@@ -339,16 +426,6 @@ var handleBackspace = (editor, name, parentListTypes) => {
339
426
  if (!isAtStartOfNode(editor.state)) {
340
427
  return false;
341
428
  }
342
- const listItemPos = findListItemPos(name, editor.state);
343
- if (!listItemPos) {
344
- return false;
345
- }
346
- const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2);
347
- const prevNode = $prev.node(listItemPos.depth);
348
- const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode);
349
- if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
350
- return editor.commands.joinItemBackward();
351
- }
352
429
  return editor.chain().liftListItem(name).run();
353
430
  };
354
431
 
@@ -417,8 +494,38 @@ var hasListItemAfter = (typeOrName, state) => {
417
494
  return true;
418
495
  };
419
496
 
497
+ // src/keymap/listHelpers/hasListItemBefore.ts
498
+ var hasListItemBefore = (typeOrName, state) => {
499
+ var _a;
500
+ const { $anchor } = state.selection;
501
+ const $targetPos = state.doc.resolve($anchor.pos - 2);
502
+ if ($targetPos.index() === 0) {
503
+ return false;
504
+ }
505
+ if (((_a = $targetPos.nodeBefore) == null ? void 0 : _a.type.name) !== typeOrName) {
506
+ return false;
507
+ }
508
+ return true;
509
+ };
510
+
511
+ // src/keymap/listHelpers/listItemHasSubList.ts
512
+ import { getNodeType as getNodeType2 } from "@tiptap/core";
513
+ var listItemHasSubList = (typeOrName, state, node) => {
514
+ if (!node) {
515
+ return false;
516
+ }
517
+ const nodeType = getNodeType2(typeOrName, state.schema);
518
+ let hasSubList = false;
519
+ node.descendants((child) => {
520
+ if (child.type === nodeType) {
521
+ hasSubList = true;
522
+ }
523
+ });
524
+ return hasSubList;
525
+ };
526
+
420
527
  // src/keymap/list-keymap.ts
421
- var ListKeymap = Extension.create({
528
+ var ListKeymap = Extension2.create({
422
529
  name: "listKeymap",
423
530
  addOptions() {
424
531
  return {
@@ -888,6 +995,12 @@ var TaskItem = Node4.create({
888
995
  const prefix = `- [${checkedChar}] `;
889
996
  return renderNestedMarkdownContent2(node, h, prefix);
890
997
  },
998
+ addExtensions() {
999
+ if (!this.options.nested) {
1000
+ return [];
1001
+ }
1002
+ return [createBranchingListDeleteKeymap(this.name, [this.options.taskListTypeName])];
1003
+ },
891
1004
  addKeyboardShortcuts() {
892
1005
  const shortcuts = {
893
1006
  Enter: () => this.editor.commands.splitListItem(this.name),
@@ -1141,7 +1254,7 @@ var TaskList = Node5.create({
1141
1254
  });
1142
1255
 
1143
1256
  // src/kit/index.ts
1144
- var ListKit = Extension2.create({
1257
+ var ListKit = Extension3.create({
1145
1258
  name: "listKit",
1146
1259
  addExtensions() {
1147
1260
  const extensions = [];