@tiptap/extension-list-keymap
Advanced tools
Comparing version 3.0.0-next.4 to 3.0.0-next.5
@@ -1,63 +0,2 @@ | ||
import { Extension, Editor } from '@tiptap/core'; | ||
import * as _tiptap_pm_model from '@tiptap/pm/model'; | ||
import { NodeType, Node } from '@tiptap/pm/model'; | ||
import { EditorState } from '@tiptap/pm/state'; | ||
type ListKeymapOptions = { | ||
/** | ||
* An array of list types. This is used for item and wrapper list matching. | ||
* @default [] | ||
* @example [{ itemName: 'listItem', wrapperNames: ['bulletList', 'orderedList'] }] | ||
*/ | ||
listTypes: Array<{ | ||
itemName: string; | ||
wrapperNames: string[]; | ||
}>; | ||
}; | ||
/** | ||
* This extension registers custom keymaps to change the behaviour of the backspace and delete keys. | ||
* By default Prosemirror keyhandling will always lift or sink items so paragraphs are joined into | ||
* the adjacent or previous list item. This extension will prevent this behaviour and instead will | ||
* try to join paragraphs from two list items into a single list item. | ||
* @see https://www.tiptap.dev/api/extensions/list-keymap | ||
*/ | ||
declare const ListKeymap: Extension<ListKeymapOptions, any>; | ||
declare const findListItemPos: (typeOrName: string | NodeType, state: EditorState) => { | ||
$pos: _tiptap_pm_model.ResolvedPos; | ||
depth: number; | ||
} | null; | ||
declare const getNextListDepth: (typeOrName: string, state: EditorState) => number | false; | ||
declare const handleBackspace: (editor: Editor, name: string, parentListTypes: string[]) => boolean; | ||
declare const handleDelete: (editor: Editor, name: string) => boolean; | ||
declare const hasListBefore: (editorState: EditorState, name: string, parentListTypes: string[]) => boolean; | ||
declare const hasListItemAfter: (typeOrName: string, state: EditorState) => boolean; | ||
declare const hasListItemBefore: (typeOrName: string, state: EditorState) => boolean; | ||
declare const listItemHasSubList: (typeOrName: string, state: EditorState, node?: Node) => boolean; | ||
declare const nextListIsDeeper: (typeOrName: string, state: EditorState) => boolean; | ||
declare const nextListIsHigher: (typeOrName: string, state: EditorState) => boolean; | ||
declare const index_findListItemPos: typeof findListItemPos; | ||
declare const index_getNextListDepth: typeof getNextListDepth; | ||
declare const index_handleBackspace: typeof handleBackspace; | ||
declare const index_handleDelete: typeof handleDelete; | ||
declare const index_hasListBefore: typeof hasListBefore; | ||
declare const index_hasListItemAfter: typeof hasListItemAfter; | ||
declare const index_hasListItemBefore: typeof hasListItemBefore; | ||
declare const index_listItemHasSubList: typeof listItemHasSubList; | ||
declare const index_nextListIsDeeper: typeof nextListIsDeeper; | ||
declare const index_nextListIsHigher: typeof nextListIsHigher; | ||
declare namespace index { | ||
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 }; | ||
} | ||
export { ListKeymap, type ListKeymapOptions, ListKeymap as default, index as listHelpers }; | ||
import { ListKeymap } from '@tiptap/extension-list'; | ||
export { ListKeymap, ListKeymapOptions, ListKeymap as default, listHelpers } from '@tiptap/extension-list'; |
@@ -1,290 +0,11 @@ | ||
var __defProp = Object.defineProperty; | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
// src/list-keymap.ts | ||
import { Extension } from "@tiptap/core"; | ||
// src/listHelpers/index.ts | ||
var listHelpers_exports = {}; | ||
__export(listHelpers_exports, { | ||
findListItemPos: () => findListItemPos, | ||
getNextListDepth: () => getNextListDepth, | ||
handleBackspace: () => handleBackspace, | ||
handleDelete: () => handleDelete, | ||
hasListBefore: () => hasListBefore, | ||
hasListItemAfter: () => hasListItemAfter, | ||
hasListItemBefore: () => hasListItemBefore, | ||
listItemHasSubList: () => listItemHasSubList, | ||
nextListIsDeeper: () => nextListIsDeeper, | ||
nextListIsHigher: () => nextListIsHigher | ||
}); | ||
// src/listHelpers/findListItemPos.ts | ||
import { getNodeType } from "@tiptap/core"; | ||
var findListItemPos = (typeOrName, state) => { | ||
const { $from } = state.selection; | ||
const nodeType = getNodeType(typeOrName, state.schema); | ||
let currentNode = null; | ||
let currentDepth = $from.depth; | ||
let currentPos = $from.pos; | ||
let targetDepth = null; | ||
while (currentDepth > 0 && targetDepth === null) { | ||
currentNode = $from.node(currentDepth); | ||
if (currentNode.type === nodeType) { | ||
targetDepth = currentDepth; | ||
} else { | ||
currentDepth -= 1; | ||
currentPos -= 1; | ||
} | ||
} | ||
if (targetDepth === null) { | ||
return null; | ||
} | ||
return { $pos: state.doc.resolve(currentPos), depth: targetDepth }; | ||
}; | ||
// src/listHelpers/getNextListDepth.ts | ||
import { getNodeAtPosition } from "@tiptap/core"; | ||
var getNextListDepth = (typeOrName, state) => { | ||
const listItemPos = findListItemPos(typeOrName, state); | ||
if (!listItemPos) { | ||
return false; | ||
} | ||
const [, depth] = getNodeAtPosition(state, typeOrName, listItemPos.$pos.pos + 4); | ||
return depth; | ||
}; | ||
// src/listHelpers/handleBackspace.ts | ||
import { isAtStartOfNode, isNodeActive } from "@tiptap/core"; | ||
// src/listHelpers/hasListBefore.ts | ||
var hasListBefore = (editorState, name, parentListTypes) => { | ||
const { $anchor } = editorState.selection; | ||
const previousNodePos = Math.max(0, $anchor.pos - 2); | ||
const previousNode = editorState.doc.resolve(previousNodePos).node(); | ||
if (!previousNode || !parentListTypes.includes(previousNode.type.name)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
// src/listHelpers/hasListItemBefore.ts | ||
var hasListItemBefore = (typeOrName, state) => { | ||
var _a; | ||
const { $anchor } = state.selection; | ||
const $targetPos = state.doc.resolve($anchor.pos - 2); | ||
if ($targetPos.index() === 0) { | ||
return false; | ||
} | ||
if (((_a = $targetPos.nodeBefore) == null ? void 0 : _a.type.name) !== typeOrName) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
// src/listHelpers/listItemHasSubList.ts | ||
import { getNodeType as getNodeType2 } from "@tiptap/core"; | ||
var listItemHasSubList = (typeOrName, state, node) => { | ||
if (!node) { | ||
return false; | ||
} | ||
const nodeType = getNodeType2(typeOrName, state.schema); | ||
let hasSubList = false; | ||
node.descendants((child) => { | ||
if (child.type === nodeType) { | ||
hasSubList = true; | ||
} | ||
}); | ||
return hasSubList; | ||
}; | ||
// src/listHelpers/handleBackspace.ts | ||
var handleBackspace = (editor, name, parentListTypes) => { | ||
if (editor.commands.undoInputRule()) { | ||
return true; | ||
} | ||
if (editor.state.selection.from !== editor.state.selection.to) { | ||
return false; | ||
} | ||
if (!isNodeActive(editor.state, name) && hasListBefore(editor.state, name, parentListTypes)) { | ||
const { $anchor } = editor.state.selection; | ||
const $listPos = editor.state.doc.resolve($anchor.before() - 1); | ||
const listDescendants = []; | ||
$listPos.node().descendants((node, pos) => { | ||
if (node.type.name === name) { | ||
listDescendants.push({ node, pos }); | ||
} | ||
}); | ||
const lastItem = listDescendants.at(-1); | ||
if (!lastItem) { | ||
return false; | ||
} | ||
const $lastItemPos = editor.state.doc.resolve($listPos.start() + lastItem.pos + 1); | ||
return editor.chain().cut({ from: $anchor.start() - 1, to: $anchor.end() + 1 }, $lastItemPos.end()).joinForward().run(); | ||
} | ||
if (!isNodeActive(editor.state, name)) { | ||
return false; | ||
} | ||
if (!isAtStartOfNode(editor.state)) { | ||
return false; | ||
} | ||
const listItemPos = findListItemPos(name, editor.state); | ||
if (!listItemPos) { | ||
return false; | ||
} | ||
const $prev = editor.state.doc.resolve(listItemPos.$pos.pos - 2); | ||
const prevNode = $prev.node(listItemPos.depth); | ||
const previousListItemHasSubList = listItemHasSubList(name, editor.state, prevNode); | ||
if (hasListItemBefore(name, editor.state) && !previousListItemHasSubList) { | ||
return editor.commands.joinItemBackward(); | ||
} | ||
return editor.chain().liftListItem(name).run(); | ||
}; | ||
// src/listHelpers/handleDelete.ts | ||
import { isAtEndOfNode, isNodeActive as isNodeActive2 } from "@tiptap/core"; | ||
// src/listHelpers/nextListIsDeeper.ts | ||
var nextListIsDeeper = (typeOrName, state) => { | ||
const listDepth = getNextListDepth(typeOrName, state); | ||
const listItemPos = findListItemPos(typeOrName, state); | ||
if (!listItemPos || !listDepth) { | ||
return false; | ||
} | ||
if (listDepth > listItemPos.depth) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
// src/listHelpers/nextListIsHigher.ts | ||
var nextListIsHigher = (typeOrName, state) => { | ||
const listDepth = getNextListDepth(typeOrName, state); | ||
const listItemPos = findListItemPos(typeOrName, state); | ||
if (!listItemPos || !listDepth) { | ||
return false; | ||
} | ||
if (listDepth < listItemPos.depth) { | ||
return true; | ||
} | ||
return false; | ||
}; | ||
// src/listHelpers/handleDelete.ts | ||
var handleDelete = (editor, name) => { | ||
if (!isNodeActive2(editor.state, name)) { | ||
return false; | ||
} | ||
if (!isAtEndOfNode(editor.state, name)) { | ||
return false; | ||
} | ||
const { selection } = editor.state; | ||
const { $from, $to } = selection; | ||
if (!selection.empty && $from.sameParent($to)) { | ||
return false; | ||
} | ||
if (nextListIsDeeper(name, editor.state)) { | ||
return editor.chain().focus(editor.state.selection.from + 4).lift(name).joinBackward().run(); | ||
} | ||
if (nextListIsHigher(name, editor.state)) { | ||
return editor.chain().joinForward().joinBackward().run(); | ||
} | ||
return editor.commands.joinItemForward(); | ||
}; | ||
// src/listHelpers/hasListItemAfter.ts | ||
var hasListItemAfter = (typeOrName, state) => { | ||
var _a; | ||
const { $anchor } = state.selection; | ||
const $targetPos = state.doc.resolve($anchor.pos - $anchor.parentOffset - 2); | ||
if ($targetPos.index() === $targetPos.parent.childCount - 1) { | ||
return false; | ||
} | ||
if (((_a = $targetPos.nodeAfter) == null ? void 0 : _a.type.name) !== typeOrName) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
// src/list-keymap.ts | ||
var ListKeymap = Extension.create({ | ||
name: "listKeymap", | ||
addOptions() { | ||
return { | ||
listTypes: [ | ||
{ | ||
itemName: "listItem", | ||
wrapperNames: ["bulletList", "orderedList"] | ||
}, | ||
{ | ||
itemName: "taskItem", | ||
wrapperNames: ["taskList"] | ||
} | ||
] | ||
}; | ||
}, | ||
addKeyboardShortcuts() { | ||
return { | ||
Delete: ({ editor }) => { | ||
let handled = false; | ||
this.options.listTypes.forEach(({ itemName }) => { | ||
if (editor.state.schema.nodes[itemName] === void 0) { | ||
return; | ||
} | ||
if (handleDelete(editor, itemName)) { | ||
handled = true; | ||
} | ||
}); | ||
return handled; | ||
}, | ||
"Mod-Delete": ({ editor }) => { | ||
let handled = false; | ||
this.options.listTypes.forEach(({ itemName }) => { | ||
if (editor.state.schema.nodes[itemName] === void 0) { | ||
return; | ||
} | ||
if (handleDelete(editor, itemName)) { | ||
handled = true; | ||
} | ||
}); | ||
return handled; | ||
}, | ||
Backspace: ({ editor }) => { | ||
let handled = false; | ||
this.options.listTypes.forEach(({ itemName, wrapperNames }) => { | ||
if (editor.state.schema.nodes[itemName] === void 0) { | ||
return; | ||
} | ||
if (handleBackspace(editor, itemName, wrapperNames)) { | ||
handled = true; | ||
} | ||
}); | ||
return handled; | ||
}, | ||
"Mod-Backspace": ({ editor }) => { | ||
let handled = false; | ||
this.options.listTypes.forEach(({ itemName, wrapperNames }) => { | ||
if (editor.state.schema.nodes[itemName] === void 0) { | ||
return; | ||
} | ||
if (handleBackspace(editor, itemName, wrapperNames)) { | ||
handled = true; | ||
} | ||
}); | ||
return handled; | ||
} | ||
}; | ||
} | ||
}); | ||
// src/index.ts | ||
import { ListKeymap } from "@tiptap/extension-list"; | ||
import { listHelpers, ListKeymap as ListKeymap2, ListKeymapOptions } from "@tiptap/extension-list"; | ||
var index_default = ListKeymap; | ||
export { | ||
ListKeymap, | ||
ListKeymap2 as ListKeymap, | ||
ListKeymapOptions, | ||
index_default as default, | ||
listHelpers_exports as listHelpers | ||
listHelpers | ||
}; | ||
//# sourceMappingURL=index.js.map |
MIT License | ||
Copyright (c) 2024, Tiptap GmbH | ||
Copyright (c) 2025, Tiptap GmbH | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "@tiptap/extension-list-keymap", | ||
"description": "list keymap extension for tiptap", | ||
"version": "3.0.0-next.4", | ||
"version": "3.0.0-next.5", | ||
"homepage": "https://tiptap.dev", | ||
@@ -34,8 +34,6 @@ "keywords": [ | ||
"devDependencies": { | ||
"@tiptap/core": "^3.0.0-next.4", | ||
"@tiptap/pm": "^3.0.0-next.4" | ||
"@tiptap/extension-list": "^3.0.0-next.5" | ||
}, | ||
"peerDependencies": { | ||
"@tiptap/core": "^3.0.0-next.3", | ||
"@tiptap/pm": "^3.0.0-next.3" | ||
"@tiptap/extension-list": "^3.0.0-next.4" | ||
}, | ||
@@ -42,0 +40,0 @@ "repository": { |
@@ -1,6 +0,5 @@ | ||
import { ListKeymap } from './list-keymap.js' | ||
import { ListKeymap } from '@tiptap/extension-list' | ||
export * from './list-keymap.js' | ||
export * as listHelpers from './listHelpers/index.js' | ||
export { listHelpers, ListKeymap, ListKeymapOptions } from '@tiptap/extension-list' | ||
export default ListKeymap |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
1
6317
10
51
1