-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[Bug]: Inserting new block using insertContent function at paragraph start will create empty paragraph above #5943
Comments
This is not a bug. When you place the mouse at the beginning of a paragraph, the cursor is actually positioned at pos=1, not pos=0. Therefore, when you insert a node, the paragraph gets split. Since there's no text before pos=1, the first part of the paragraph becomes an empty paragraph, followed by the node you insert, and then the second part of the original paragraph forms a new paragraph. I recorded a video to illustrate this. In your example, I changed the first node in the content to 2025-01-03.19.34.23.mov |
Agreed, closing |
Thank you for your explanation and the video illustration! I understand the technical reasoning behind this behavior and why it happens due to the positioning logic. This can be confusing for users, as it introduces unexpected whitespace into the content. While I understand this is how the editor's logic currently works, I believe it would be helpful to have more control over the What are your thoughts on providing this level of customization? I believe it could make the editor more flexible and user-friendly in scenarios like this. |
Your consideration also makes sense. I cannot confirm whether adding an optional parameter is worthwhile. Here are two solutions for your reference to address your current issue, although I can't confirm they are bug-free. Customize a import { GapCursor } from '@tiptap/pm/gapcursor';
const findSuitablePos = (tr) => {
tr.deleteSelection();
const selection = tr.selection;
const { $from } = selection;
if ($from.parent.isTextblock && !$from.nodeBefore) {
tr.setSelection(new GapCursor(tr.doc.resolve($from.before())));
}
return true;
};
const insertLock = () => {
editor.value.chain().command(({ tr }) => findSuitablePos(tr)).insertContentLock().run()
} Or redefine your addCommands() {
return {
insertContentLock:
() =>
({ commands, tr }) => {
const { $from, to } = tr.selection;
let from = $from.pos;
if ($from.parent.isTextblock && !$from.nodeBefore) {
from = $from.before();
}
return commands.insertContentAt(
{ from, to },
{
type: this.name,
attrs: {},
}
);
},
};
}, |
Affected Packages
core
Version(s)
2.10.3
Bug Description
Steps to reproduce
Browser Used
Chrome
Code Example URL
https://codesandbox.io/p/devbox/jovial-waterfall-z9nvpx
Expected Behavior
it should insert new node without creating additional empy paragraph
Additional Context (Optional)
when you now position at the end of paragraph and insert the block node using insertContent function it will do it correctly, it will just insert it after current paragraph, no empty paragraph will appear after, so it should do it same on paragraph start.
Dependency Updates
The text was updated successfully, but these errors were encountered: