working lists somewhat

This commit is contained in:
Palanikannan1437 2024-03-28 12:14:57 +05:30
parent 35fe2987ff
commit 94e83ab946
5 changed files with 113 additions and 7 deletions

View File

@ -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;
};

View File

@ -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

View File

@ -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;
}

View File

@ -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;
};

View File

@ -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,