@tiptap/core 2.1.4 → 2.2.0-rc.3

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);
@@ -4599,7 +4413,6 @@ exports.extensions = extensions;
4599
4413
  exports.findChildren = findChildren;
4600
4414
  exports.findChildrenInRange = findChildrenInRange;
4601
4415
  exports.findDuplicates = findDuplicates;
4602
- exports.findListItemPos = findListItemPos;
4603
4416
  exports.findParentNode = findParentNode;
4604
4417
  exports.findParentNodeClosestToPos = findParentNodeClosestToPos;
4605
4418
  exports.fromString = fromString;
@@ -4616,7 +4429,6 @@ exports.getMarkAttributes = getMarkAttributes;
4616
4429
  exports.getMarkRange = getMarkRange;
4617
4430
  exports.getMarkType = getMarkType;
4618
4431
  exports.getMarksBetween = getMarksBetween;
4619
- exports.getNextListDepth = getNextListDepth;
4620
4432
  exports.getNodeAtPosition = getNodeAtPosition;
4621
4433
  exports.getNodeAttributes = getNodeAttributes;
4622
4434
  exports.getNodeType = getNodeType;
@@ -4630,11 +4442,6 @@ exports.getText = getText;
4630
4442
  exports.getTextBetween = getTextBetween;
4631
4443
  exports.getTextContentFromNodes = getTextContentFromNodes;
4632
4444
  exports.getTextSerializersFromSchema = getTextSerializersFromSchema;
4633
- exports.handleBackspace = handleBackspace;
4634
- exports.handleDelete = handleDelete;
4635
- exports.hasListBefore = hasListBefore;
4636
- exports.hasListItemAfter = hasListItemAfter;
4637
- exports.hasListItemBefore = hasListItemBefore;
4638
4445
  exports.injectExtensionAttributesToParseRule = injectExtensionAttributesToParseRule;
4639
4446
  exports.inputRulesPlugin = inputRulesPlugin;
4640
4447
  exports.isActive = isActive;
@@ -4655,14 +4462,11 @@ exports.isRegExp = isRegExp;
4655
4462
  exports.isString = isString;
4656
4463
  exports.isTextSelection = isTextSelection;
4657
4464
  exports.isiOS = isiOS;
4658
- exports.listItemHasSubList = listItemHasSubList;
4659
4465
  exports.markInputRule = markInputRule;
4660
4466
  exports.markPasteRule = markPasteRule;
4661
4467
  exports.mergeAttributes = mergeAttributes;
4662
4468
  exports.mergeDeep = mergeDeep;
4663
4469
  exports.minMax = minMax;
4664
- exports.nextListIsDeeper = nextListIsDeeper;
4665
- exports.nextListIsHigher = nextListIsHigher;
4666
4470
  exports.nodeInputRule = nodeInputRule;
4667
4471
  exports.nodePasteRule = nodePasteRule;
4668
4472
  exports.objectIncludes = objectIncludes;