@tiptap/core 2.1.3 → 2.1.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/dist/index.cjs CHANGED
@@ -2015,261 +2015,6 @@ function getMarkAttributes(state, typeOrName) {
2015
2015
  return { ...mark.attrs };
2016
2016
  }
2017
2017
 
2018
- const findListItemPos = (typeOrName, state) => {
2019
- const { $from } = state.selection;
2020
- const nodeType = getNodeType(typeOrName, state.schema);
2021
- let currentNode = null;
2022
- let currentDepth = $from.depth;
2023
- let currentPos = $from.pos;
2024
- let targetDepth = null;
2025
- while (currentDepth > 0 && targetDepth === null) {
2026
- currentNode = $from.node(currentDepth);
2027
- if (currentNode.type === nodeType) {
2028
- targetDepth = currentDepth;
2029
- }
2030
- else {
2031
- currentDepth -= 1;
2032
- currentPos -= 1;
2033
- }
2034
- }
2035
- if (targetDepth === null) {
2036
- return null;
2037
- }
2038
- return { $pos: state.doc.resolve(currentPos), depth: targetDepth };
2039
- };
2040
-
2041
- /**
2042
- * Finds the first node of a given type or name in the current selection.
2043
- * @param state The editor state.
2044
- * @param typeOrName The node type or name.
2045
- * @param pos The position to start searching from.
2046
- * @param maxDepth The maximum depth to search.
2047
- * @returns The node and the depth as an array.
2048
- */
2049
- const getNodeAtPosition = (state, typeOrName, pos, maxDepth = 20) => {
2050
- const $pos = state.doc.resolve(pos);
2051
- let currentDepth = maxDepth;
2052
- let node = null;
2053
- while (currentDepth > 0 && node === null) {
2054
- const currentNode = $pos.node(currentDepth);
2055
- if ((currentNode === null || currentNode === void 0 ? void 0 : currentNode.type.name) === typeOrName) {
2056
- node = currentNode;
2057
- }
2058
- else {
2059
- currentDepth -= 1;
2060
- }
2061
- }
2062
- return [node, currentDepth];
2063
- };
2064
-
2065
- const getNextListDepth = (typeOrName, state) => {
2066
- const listItemPos = findListItemPos(typeOrName, state);
2067
- if (!listItemPos) {
2068
- return false;
2069
- }
2070
- const [, depth] = getNodeAtPosition(state, typeOrName, listItemPos.$pos.pos + 4);
2071
- return depth;
2072
- };
2073
-
2074
- const isAtStartOfNode = (state) => {
2075
- const { $from, $to } = state.selection;
2076
- if ($from.parentOffset > 0 || $from.pos !== $to.pos) {
2077
- return false;
2078
- }
2079
- return true;
2080
- };
2081
-
2082
- const hasListBefore = (editorState, name, parentListTypes) => {
2083
- const { $anchor } = editorState.selection;
2084
- const previousNodePos = Math.max(0, $anchor.pos - 2);
2085
- const previousNode = editorState.doc.resolve(previousNodePos).node();
2086
- if (!previousNode || !parentListTypes.includes(previousNode.type.name)) {
2087
- return false;
2088
- }
2089
- return true;
2090
- };
2091
-
2092
- const hasListItemBefore = (typeOrName, state) => {
2093
- var _a;
2094
- const { $anchor } = state.selection;
2095
- const $targetPos = state.doc.resolve($anchor.pos - 2);
2096
- if ($targetPos.index() === 0) {
2097
- return false;
2098
- }
2099
- if (((_a = $targetPos.nodeBefore) === null || _a === void 0 ? void 0 : _a.type.name) !== typeOrName) {
2100
- return false;
2101
- }
2102
- return true;
2103
- };
2104
-
2105
- const listItemHasSubList = (typeOrName, state, node) => {
2106
- if (!node) {
2107
- return false;
2108
- }
2109
- const nodeType = getNodeType(typeOrName, state.schema);
2110
- let hasSubList = false;
2111
- node.descendants(child => {
2112
- if (child.type === nodeType) {
2113
- hasSubList = true;
2114
- }
2115
- });
2116
- return hasSubList;
2117
- };
2118
-
2119
- const handleBackspace = (editor, name, parentListTypes) => {
2120
- // this is required to still handle the undo handling
2121
- if (editor.commands.undoInputRule()) {
2122
- return true;
2123
- }
2124
- // if the current item is NOT inside a list item &
2125
- // the previous item is a list (orderedList or bulletList)
2126
- // move the cursor into the list and delete the current item
2127
- if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) {
2128
- const { $anchor } = editor.state.selection;
2129
- const $listPos = editor.state.doc.resolve($anchor.before() - 1);
2130
- const listDescendants = [];
2131
- $listPos.node().descendants((node, pos) => {
2132
- if (node.type.name === name) {
2133
- listDescendants.push({ node, pos });
2134
- }
2135
- });
2136
- const lastItem = listDescendants.at(-1);
2137
- if (!lastItem) {
2138
- return false;
2139
- }
2140
- const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1);
2141
- return editor.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run();
2142
- }
2143
- // if the cursor is not inside the current node type
2144
- // do nothing and proceed
2145
- if (!isNodeActive(editor.state, name)) {
2146
- return false;
2147
- }
2148
- // if the cursor is not at the start of a node
2149
- // do nothing and proceed
2150
- if (!isAtStartOfNode(editor.state)) {
2151
- return false;
2152
- }
2153
- const listItemPos = findListItemPos(name, editor.state);
2154
- if (!listItemPos) {
2155
- return false;
2156
- }
2157
- const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2);
2158
- const prevNode = $prev.node(listItemPos.depth);
2159
- const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode);
2160
- // if the previous item is a list item and doesn't have a sublist, join the list items
2161
- if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) {
2162
- return editor.commands.joinItemBackward();
2163
- }
2164
- // otherwise in the end, a backspace should
2165
- // always just lift the list item if
2166
- // joining / merging is not possible
2167
- return editor.chain().liftListItem(name).run();
2168
- };
2169
-
2170
- function findParentNodeClosestToPos($pos, predicate) {
2171
- for (let i = $pos.depth; i > 0; i -= 1) {
2172
- const node = $pos.node(i);
2173
- if (predicate(node)) {
2174
- return {
2175
- pos: i > 0 ? $pos.before(i) : 0,
2176
- start: $pos.start(i),
2177
- depth: i,
2178
- node,
2179
- };
2180
- }
2181
- }
2182
- }
2183
-
2184
- function findParentNode(predicate) {
2185
- return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2186
- }
2187
-
2188
- const isAtEndOfNode = (state, nodeType) => {
2189
- const { $from, $to, $anchor } = state.selection;
2190
- if (nodeType) {
2191
- const parentNode = findParentNode(node => node.type.name === nodeType)(state.selection);
2192
- if (!parentNode) {
2193
- return false;
2194
- }
2195
- const $parentPos = state.doc.resolve(parentNode.pos + 1);
2196
- if ($anchor.pos + 1 === $parentPos.end()) {
2197
- return true;
2198
- }
2199
- return false;
2200
- }
2201
- if ($to.parentOffset < $to.parent.nodeSize - 2 || $from.pos !== $to.pos) {
2202
- return false;
2203
- }
2204
- return true;
2205
- };
2206
-
2207
- const nextListIsDeeper = (typeOrName, state) => {
2208
- const listDepth = getNextListDepth(typeOrName, state);
2209
- const listItemPos = findListItemPos(typeOrName, state);
2210
- if (!listItemPos || !listDepth) {
2211
- return false;
2212
- }
2213
- if (listDepth > listItemPos.depth) {
2214
- return true;
2215
- }
2216
- return false;
2217
- };
2218
-
2219
- const nextListIsHigher = (typeOrName, state) => {
2220
- const listDepth = getNextListDepth(typeOrName, state);
2221
- const listItemPos = findListItemPos(typeOrName, state);
2222
- if (!listItemPos || !listDepth) {
2223
- return false;
2224
- }
2225
- if (listDepth < listItemPos.depth) {
2226
- return true;
2227
- }
2228
- return false;
2229
- };
2230
-
2231
- const handleDelete = (editor, name) => {
2232
- // if the cursor is not inside the current node type
2233
- // do nothing and proceed
2234
- if (!isNodeActive(editor.state, name)) {
2235
- return false;
2236
- }
2237
- // if the cursor is not at the end of a node
2238
- // do nothing and proceed
2239
- if (!isAtEndOfNode(editor.state, name)) {
2240
- return false;
2241
- }
2242
- // check if the next node is a list with a deeper depth
2243
- if (nextListIsDeeper(name, editor.state)) {
2244
- return editor
2245
- .chain()
2246
- .focus(editor.state.selection.from + 4)
2247
- .lift(name)
2248
- .joinBackward()
2249
- .run();
2250
- }
2251
- if (nextListIsHigher(name, editor.state)) {
2252
- return editor.chain()
2253
- .joinForward()
2254
- .joinBackward()
2255
- .run();
2256
- }
2257
- return editor.commands.joinItemForward();
2258
- };
2259
-
2260
- const hasListItemAfter = (typeOrName, state) => {
2261
- var _a;
2262
- const { $anchor } = state.selection;
2263
- const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2);
2264
- if ($targetPos.index() === $targetPos.parent.childCount - 1) {
2265
- return false;
2266
- }
2267
- if (((_a = $targetPos.nodeAfter) === null || _a === void 0 ? void 0 : _a.type.name) !== typeOrName) {
2268
- return false;
2269
- }
2270
- return true;
2271
- };
2272
-
2273
2018
  /**
2274
2019
  * Returns a new `Transform` based on all steps of the passed transactions.
2275
2020
  */
@@ -2331,6 +2076,24 @@ function findChildrenInRange(node, range, predicate) {
2331
2076
  return nodesWithPos;
2332
2077
  }
2333
2078
 
2079
+ function findParentNodeClosestToPos($pos, predicate) {
2080
+ for (let i = $pos.depth; i > 0; i -= 1) {
2081
+ const node = $pos.node(i);
2082
+ if (predicate(node)) {
2083
+ return {
2084
+ pos: i > 0 ? $pos.before(i) : 0,
2085
+ start: $pos.start(i),
2086
+ depth: i,
2087
+ node,
2088
+ };
2089
+ }
2090
+ }
2091
+ }
2092
+
2093
+ function findParentNode(predicate) {
2094
+ return (selection) => findParentNodeClosestToPos(selection.$from, predicate);
2095
+ }
2096
+
2334
2097
  function getHTMLFromFragment(fragment, schema) {
2335
2098
  const documentFragment = model.DOMSerializer.fromSchema(schema).serializeFragment(fragment);
2336
2099
  const temporaryDocument = document.implementation.createHTMLDocument();
@@ -2548,6 +2311,30 @@ function getMarksBetween(from, to, doc) {
2548
2311
  return marks;
2549
2312
  }
2550
2313
 
2314
+ /**
2315
+ * Finds the first node of a given type or name in the current selection.
2316
+ * @param state The editor state.
2317
+ * @param typeOrName The node type or name.
2318
+ * @param pos The position to start searching from.
2319
+ * @param maxDepth The maximum depth to search.
2320
+ * @returns The node and the depth as an array.
2321
+ */
2322
+ const getNodeAtPosition = (state, typeOrName, pos, maxDepth = 20) => {
2323
+ const $pos = state.doc.resolve(pos);
2324
+ let currentDepth = maxDepth;
2325
+ let node = null;
2326
+ while (currentDepth > 0 && node === null) {
2327
+ const currentNode = $pos.node(currentDepth);
2328
+ if ((currentNode === null || currentNode === void 0 ? void 0 : currentNode.type.name) === typeOrName) {
2329
+ node = currentNode;
2330
+ }
2331
+ else {
2332
+ currentDepth -= 1;
2333
+ }
2334
+ }
2335
+ return [node, currentDepth];
2336
+ };
2337
+
2551
2338
  function getSplittedAttributes(extensionAttributes, typeName, attributes) {
2552
2339
  return Object.fromEntries(Object
2553
2340
  .entries(attributes)
@@ -2638,6 +2425,33 @@ function isActive(state, name, attributes = {}) {
2638
2425
  return false;
2639
2426
  }
2640
2427
 
2428
+ const isAtEndOfNode = (state, nodeType) => {
2429
+ const { $from, $to, $anchor } = state.selection;
2430
+ if (nodeType) {
2431
+ const parentNode = findParentNode(node => node.type.name === nodeType)(state.selection);
2432
+ if (!parentNode) {
2433
+ return false;
2434
+ }
2435
+ const $parentPos = state.doc.resolve(parentNode.pos + 1);
2436
+ if ($anchor.pos + 1 === $parentPos.end()) {
2437
+ return true;
2438
+ }
2439
+ return false;
2440
+ }
2441
+ if ($to.parentOffset < $to.parent.nodeSize - 2 || $from.pos !== $to.pos) {
2442
+ return false;
2443
+ }
2444
+ return true;
2445
+ };
2446
+
2447
+ const isAtStartOfNode = (state) => {
2448
+ const { $from, $to } = state.selection;
2449
+ if ($from.parentOffset > 0 || $from.pos !== $to.pos) {
2450
+ return false;
2451
+ }
2452
+ return true;
2453
+ };
2454
+
2641
2455
  function isList(name, extensions) {
2642
2456
  const { nodeExtensions } = splitExtensions(extensions);
2643
2457
  const extension = nodeExtensions.find(item => item.name === name);
@@ -3986,14 +3800,12 @@ function markInputRule(config) {
3986
3800
  function nodeInputRule(config) {
3987
3801
  return new InputRule({
3988
3802
  find: config.find,
3989
- handler: ({ state: state$1, range, match }) => {
3990
- var _a, _b;
3803
+ handler: ({ state, range, match }) => {
3991
3804
  const attributes = callOrReturn(config.getAttributes, undefined, match) || {};
3992
- const { tr } = state$1;
3993
- const start = config.blockReplace ? range.from - 1 : range.from;
3805
+ const { tr } = state;
3806
+ const start = range.from;
3994
3807
  let end = range.to;
3995
3808
  const newNode = config.type.create(attributes);
3996
- const { $to } = tr.selection;
3997
3809
  if (match[1]) {
3998
3810
  const offset = match[0].lastIndexOf(match[1]);
3999
3811
  let matchStart = start + offset;
@@ -4010,31 +3822,9 @@ function nodeInputRule(config) {
4010
3822
  tr.replaceWith(matchStart, end, newNode);
4011
3823
  }
4012
3824
  else if (match[0]) {
4013
- tr.replaceWith(start, end, newNode);
4014
- }
4015
- if (config.blockReplace && config.addExtraNewline) {
4016
- if ($to.nodeAfter) {
4017
- if ($to.nodeAfter.isTextblock) {
4018
- tr.setSelection(state.TextSelection.create(tr.doc, $to.pos + 1));
4019
- }
4020
- else if ($to.nodeAfter.isBlock) {
4021
- tr.setSelection(state.NodeSelection.create(tr.doc, $to.pos));
4022
- }
4023
- else {
4024
- tr.setSelection(state.TextSelection.create(tr.doc, $to.pos - 1));
4025
- }
4026
- }
4027
- else {
4028
- // add node after horizontal rule if it’s the end of the document
4029
- const defaultNode = ((_a = $to.parent.type.contentMatch.defaultType) === null || _a === void 0 ? void 0 : _a.create()) || ((_b = state$1.doc.type.contentMatch.defaultType) === null || _b === void 0 ? void 0 : _b.create());
4030
- if (defaultNode) {
4031
- const newPos = start + newNode.nodeSize;
4032
- tr.insert(newPos, defaultNode);
4033
- tr.setSelection(state.TextSelection.create(tr.doc, newPos));
4034
- }
4035
- }
4036
- tr.scrollIntoView();
3825
+ tr.insert(start - 1, config.type.create(attributes)).delete(tr.mapping.map(start), tr.mapping.map(end));
4037
3826
  }
3827
+ tr.scrollIntoView();
4038
3828
  },
4039
3829
  });
4040
3830
  }
@@ -4623,7 +4413,6 @@ exports.extensions = extensions;
4623
4413
  exports.findChildren = findChildren;
4624
4414
  exports.findChildrenInRange = findChildrenInRange;
4625
4415
  exports.findDuplicates = findDuplicates;
4626
- exports.findListItemPos = findListItemPos;
4627
4416
  exports.findParentNode = findParentNode;
4628
4417
  exports.findParentNodeClosestToPos = findParentNodeClosestToPos;
4629
4418
  exports.fromString = fromString;
@@ -4640,7 +4429,6 @@ exports.getMarkAttributes = getMarkAttributes;
4640
4429
  exports.getMarkRange = getMarkRange;
4641
4430
  exports.getMarkType = getMarkType;
4642
4431
  exports.getMarksBetween = getMarksBetween;
4643
- exports.getNextListDepth = getNextListDepth;
4644
4432
  exports.getNodeAtPosition = getNodeAtPosition;
4645
4433
  exports.getNodeAttributes = getNodeAttributes;
4646
4434
  exports.getNodeType = getNodeType;
@@ -4654,11 +4442,6 @@ exports.getText = getText;
4654
4442
  exports.getTextBetween = getTextBetween;
4655
4443
  exports.getTextContentFromNodes = getTextContentFromNodes;
4656
4444
  exports.getTextSerializersFromSchema = getTextSerializersFromSchema;
4657
- exports.handleBackspace = handleBackspace;
4658
- exports.handleDelete = handleDelete;
4659
- exports.hasListBefore = hasListBefore;
4660
- exports.hasListItemAfter = hasListItemAfter;
4661
- exports.hasListItemBefore = hasListItemBefore;
4662
4445
  exports.injectExtensionAttributesToParseRule = injectExtensionAttributesToParseRule;
4663
4446
  exports.inputRulesPlugin = inputRulesPlugin;
4664
4447
  exports.isActive = isActive;
@@ -4679,14 +4462,11 @@ exports.isRegExp = isRegExp;
4679
4462
  exports.isString = isString;
4680
4463
  exports.isTextSelection = isTextSelection;
4681
4464
  exports.isiOS = isiOS;
4682
- exports.listItemHasSubList = listItemHasSubList;
4683
4465
  exports.markInputRule = markInputRule;
4684
4466
  exports.markPasteRule = markPasteRule;
4685
4467
  exports.mergeAttributes = mergeAttributes;
4686
4468
  exports.mergeDeep = mergeDeep;
4687
4469
  exports.minMax = minMax;
4688
- exports.nextListIsDeeper = nextListIsDeeper;
4689
- exports.nextListIsHigher = nextListIsHigher;
4690
4470
  exports.nodeInputRule = nodeInputRule;
4691
4471
  exports.nodePasteRule = nodePasteRule;
4692
4472
  exports.objectIncludes = objectIncludes;