diff --git a/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/get-prev-list-depth.ts b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/get-prev-list-depth.ts new file mode 100644 index 000000000..0a594ce64 --- /dev/null +++ b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/get-prev-list-depth.ts @@ -0,0 +1,30 @@ +import { EditorState } from "@tiptap/pm/state"; +import { findListItemPos } from "src/ui/extensions/custom-list-keymap/list-helpers/find-list-item-pos"; + +export const getPrevListDepth = (typeOrName: string, state: EditorState) => { + const listItemPos = findListItemPos(typeOrName, state); + + if (!listItemPos) { + return false; + } + + let depth = -2; + let pos = listItemPos.$pos; + + // Adjust the position to ensure we're within the list item, especially for edge cases + // This adjustment aims to more accurately reflect the document structure + let adjustedPos = pos; + // Adjusting the position by -3 to account for the off-by-three error + adjustedPos = state.doc.resolve(Math.max(adjustedPos.pos - 3, 0)); + + // Traverse up the document structure from the adjusted position + for (let d = adjustedPos.depth; d > 0; d--) { + const node = adjustedPos.node(d); + if (node.type.name === "bulletList" || node.type.name === "orderedList") { + depth++; + } + } + + console.log("depth", depth); + return depth; +}; diff --git a/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/handle-backspace.ts b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/handle-backspace.ts index 885169aab..ee0a60757 100644 --- a/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/handle-backspace.ts +++ b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/handle-backspace.ts @@ -6,6 +6,9 @@ import { hasListBefore } from "src/ui/extensions/custom-list-keymap/list-helpers import { hasListItemBefore } from "src/ui/extensions/custom-list-keymap/list-helpers/has-list-item-before"; import { listItemHasSubList } from "src/ui/extensions/custom-list-keymap/list-helpers/list-item-has-sub-list"; +import { isCursorInSubList } from "./is-sublist"; +import { nextListIsDeeper } from "./next-list-is-deeper"; +import { prevListIsHigher } from "./prev-list-is-deeper"; export const handleBackspace = (editor: Editor, name: string, parentListTypes: string[]) => { // this is required to still handle the undo handling @@ -61,18 +64,47 @@ export const handleBackspace = (editor: Editor, name: string, parentListTypes: s if (!listItemPos) { return false; } + const currentNode = listItemPos.$pos.node(listItemPos.depth); + const currentNodeSize = currentNode.nodeSize; + const currentListItemHasSubList = listItemHasSubList(name, editor.state, currentNode); + const isCurrentListItemSublist = prevListIsHigher(name, editor.state); + // __AUTO_GENERATED_PRINT_VAR_START__ + console.log( + "handleBackspace isCurrentListItemSublist: %s", + isCurrentListItemSublist, + hasListItemBefore(name, editor.state) + ); // __AUTO_GENERATED_PRINT_VAR_END__ + // if the cursor is not at the start of a node + // do nothing and proceed + if (!isAtStartOfNode(editor.state)) { + 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 the previous item is a list item and doesn't have a sublist, join the list items - if (hasListItemBefore(name, editor.state) && previousListItemHasSubList) { - return editor.chain().liftListItem(name).run(); - // return editor.commands.joinItemBackward(); + // if the previous item is a list item and has a sublist, join the list items (this scenario only occurs when a sublist's first item is lifted) + if ( + // hasListItemBefore(name, editor.state) && + // currentListItemHasSubList && + // currentNodeSize > 4 && + // !previousListItemHasSubList && + isCurrentListItemSublist + ) { + console.log("ran 0"); + editor.chain().liftListItem(name).run(); + return editor.commands.joinItemBackward(); + // return editor.chain().liftListItem(name).run(); } + // if the previous item is a list item and doesn't have a sublist, join the list items + if (hasListItemBefore(name, editor.state)) { + console.log("ran 1"); + return editor.chain().liftListItem(name).run(); + } + + console.log("ran 2"); // otherwise in the end, a backspace should // always just lift the list item if // joining / merging is not possible diff --git a/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/is-sublist.ts b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/is-sublist.ts new file mode 100644 index 000000000..cb95e6fc1 --- /dev/null +++ b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/is-sublist.ts @@ -0,0 +1,26 @@ +import { Editor } from "@tiptap/core"; + +export function isCursorInSubList(editor: Editor) { + const { selection } = editor.state; + const { $from } = selection; + + // Check if the current node is a list item + const listItem = editor.schema.nodes.listItem; + + // Traverse up the document tree from the current position + for (let depth = $from.depth; depth > 0; depth--) { + const node = $from.node(depth); + if (node.type === listItem) { + // If the parent of the list item is also a list, it's a sub-list + const parent = $from.node(depth - 1); + if ( + parent && + (parent.type === editor.schema.nodes.bulletList || parent.type === editor.schema.nodes.orderedList) + ) { + return true; + } + } + } + + return false; +} diff --git a/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/prev-list-is-deeper.ts b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/prev-list-is-deeper.ts new file mode 100644 index 000000000..51d790d20 --- /dev/null +++ b/packages/editor/core/src/ui/extensions/custom-list-keymap/list-helpers/prev-list-is-deeper.ts @@ -0,0 +1,20 @@ +import { EditorState } from "@tiptap/pm/state"; + +import { findListItemPos } from "src/ui/extensions/custom-list-keymap/list-helpers/find-list-item-pos"; +import { getPrevListDepth } from "./get-prev-list-depth"; + +export const prevListIsHigher = (typeOrName: string, state: EditorState) => { + const listDepth = getPrevListDepth(typeOrName, state); + const listItemPos = findListItemPos(typeOrName, state); + + if (!listItemPos || !listDepth) { + return false; + } + + console.log(listDepth, listItemPos.depth, listItemPos, listDepth < listItemPos.depth); + if (listDepth < listItemPos.depth) { + return true; + } + + return false; +}; diff --git a/packages/editor/core/src/ui/extensions/index.tsx b/packages/editor/core/src/ui/extensions/index.tsx index a26fa2e3d..e329f3881 100644 --- a/packages/editor/core/src/ui/extensions/index.tsx +++ b/packages/editor/core/src/ui/extensions/index.tsx @@ -91,7 +91,6 @@ export const CoreEditorExtensions = ( }), TiptapUnderline, TextStyle, - Color, TaskList.configure({ HTMLAttributes: { class: "not-prose pl-2", @@ -108,7 +107,6 @@ export const CoreEditorExtensions = ( CustomCodeInlineExtension, Markdown.configure({ html: true, - transformCopiedText: true, transformPastedText: true, }), Table,