Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected behavior when hitting backspace on list item with children? #1467

Open
kasrak opened this issue May 28, 2024 · 6 comments
Open

Unexpected behavior when hitting backspace on list item with children? #1467

kasrak opened this issue May 28, 2024 · 6 comments

Comments

@kasrak
Copy link

kasrak commented May 28, 2024

I have the following doc:

hello
- first
  - second

The cursor is preceding the f. I'd like to hit backspace in order to bring the "xfirst" line up to the same line as "hello." Instead what happens is that the "hello" node gets selected, then deleted.

Here's a Loom showing the behavior in ProseMirror, Google Docs, and Notion:

https://www.loom.com/share/ad856a801d134478afc23de142c4c2e8

When the list only has a single level (i.e. no sublists), hitting backspace works as I'd expect.

@marijnh
Copy link
Member

marijnh commented May 28, 2024

The commands you have bound to backspace (probably some combination of joinBackward and selectNodeBackward) can't find any other operation that would leave the document in a valid state in this situation, so it fall back to selecting the node before the cursor. What would you expect to happen here?

@kasrak
Copy link
Author

kasrak commented May 28, 2024

Thank you for the fast response!

I think either the Google Docs & Notion behavior (in the linked Loom screen cap) would be reasonable:

  • Google Docs: 1st backspace converts - first into an indented paragraph (removes the bullet), 2nd backspace dedents the paragraph, 3rd backspace removes the preceding newline. It leaves the - second list item unchanged.
  • Notion: 1st backspace converts - first into an un-indented paragraph, 2nd backspace removes the preceding newline. It dedents the - second list item so it's now a top-level list item.

I was testing this in the "Basic example" playground (https://prosemirror.net/examples/basic/), so apologies if I missed how to configure the editor to change this behavior.

@marijnh
Copy link
Member

marijnh commented May 28, 2024

ProseMirror schemas tend to work quite differently from Google Docs/Notion documents. Does your schema have a way to represent the kind of documents this creates in those editors? Because typically, orphan indented list items just cannot be represented in ProseMirror schemas.

@kasrak
Copy link
Author

kasrak commented May 30, 2024

A way to keep the doc compatible with the schema is to de-dent the list item (and any of its descendants) -- I've got that working with the below key handler now.

But I noticed another related issue that I'm trying to solve: if I hit backspace at the beginning of a paragraph (screenshot 1) that's below a list, the paragraph gets added as a second list item (screenshot 2). I'd expect the paragraph text to be merged into the first list item (screenshot 3).

Backspace: ({ editor }) => {
  const { $from, empty } = editor.state.selection;
  if ($from.parentOffset !== 0 || !empty) {
	  return false;
  }
  
  // If the cursor is at the beginning of a list item, de-dent.
  const parentNode = $from.node(-1);
  if (parentNode?.type.name === "listItem") {
	  return editor.commands.liftListItem("listItem");
  }
  
  // TODO: If the cursor is at the beginning of a block that follows
  // a list, merge the block content into the last list item.
}

Screenshot 1: starting state
image

Screenshot 2: what happens after hitting backspace
image

Screenshot 3: what I'd like to happen
image

@julian-amaya
Copy link

Hi @kasrak did you ever find the solution? We ran into the same issue

@kasrak
Copy link
Author

kasrak commented Nov 22, 2024

No unfortunately

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants