How to select all the content of a node (not the node)

I have a schema with a content body foot for the top node.

Both the children contain block+ content and have isolating and defining true. And contenteditable is false for foot.

When one is edting in body, I want all it’s content to be selected with mod-a.

Now I come up with `tr.setSelection(TextSelection.create(doc, 1, doc.firstChild.nodeSize-1))`.

Everything works except:

  1. A waring in console: TextSelection endpoint not pointing into a node with inline content (body). I think it is explained in Why is the Ctrl+A selection different from selecting the whole document with the mouse?
  2. Cursor goes to foot(seems blured as it has contenteditable false) after hitting delete. While I want to keep it in body for pleasent editing.

I wonder what is recommended way to do it right.

Does the selection produced by TextSelection.between(doc.resolve(0), doc.resolve(doc.firstChild.nodeSize)) do what you are looking for? It’ll create a valid text selection from the first inline position in the doc to the last one in the body, which should still allow you to delete or overwrite the body’s content.

Yes. But it warns: TextSelection endpoint not pointing into a node with inline content (doc)

and the cursor position issue remain the same.

Can you share your editor content? I’m guessing you must have something like <blockquote><p></p></blockquote> in which case 0 would be the document, 1 would be in the blockquote but before the text content of the paragraph, and 2 would be the first text position. Normally 1 would be the first text position if the editor contained <p></p>

It does this for a selection created with TextSelection.between? Are you sure?

1 Like

Oops, my mistake, I was using `TextSelection.create`.

With `TextSelection.between`, it reports error:

index.js:295 Uncaught TypeError: Cannot read properties of undefined (reading 'inlineContent')
    at _TextSelection.between (index.js:295:27)

I am also wondering if say a codeblock is at the begining of body, the codeblock would remain there (although the content is cleared) after hitting delete with TextSelection.between, right?

You are right. I have a body and foot node under the top document and I am talking about editing in the body node.

Are you passing it resolved positions or just numbers?

Just numbers, as you suggested previously TextSelection.between(0, doc.firstChild.nodeSize).

Oh right, no, you have to pass resolved positions (via doc.resolve). Using TypeScript will help a lot with catching this kind of thing.

OK. The error goes away.

But a valid text selection in body is not “enough” or identical to the AllSelection. Say if a codeblock is at the begining of body, the codeblock would remain there (although the content is cleared) after hitting delete with TextSelection.between, while nothing left with AllSelection.

Besides, I have also defined some captioned-image/table node. If it is the last node, TextSelection.between would make the selection goes into the caption (instead of including the whole image). And I have some logic to prevent the partial deletion, which would surprise the use when one hits delete with no effect. To this regard, it seems tr.setSelection(TextSelection.create(doc, 1, doc.firstChild.nodeSize-1)) has the closest behavour to AllSelection.

I guess I find a bug. If the last cell of the table is empty, TextSelection.between would skip the whole table when it tries to find a valid text selection. And if I fill it with some text, then TextSelection.between would locate in the cell.