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

Missing trailingBreaks when Widget decorations are used #1487

Closed
MathiasFavro opened this issue Sep 13, 2024 · 1 comment
Closed

Missing trailingBreaks when Widget decorations are used #1487

MathiasFavro opened this issue Sep 13, 2024 · 1 comment

Comments

@MathiasFavro
Copy link

I believe trailingBreaks are no longer added to all expected locations when a transaction that modifies widget decorations is dispatched. I bisected down the changed behaviour to prosemirror-view version 1.31.0 and the commits do mention decorations. Debugging the in the client also lead to updateChildren but that is how far I got.

My simplified repro is a widget decoration to show placeholder text and a transaction to replace the content of a document:

new Plugin({
	props: {
		decorations: function(state: ProseMirror.EditorState): ProseMirror.DecorationSet {
			let onlyEmptyParagraphs = true;

			state.doc.forEach(function(node: ProseMirror.ProsemirrorNode): void {
				if (node.type != schema.nodes.paragraph || node.content.size > 0)
					onlyEmptyParagraphs = false;
			});

			if (!onlyEmptyParagraphs)
				return DecorationSet.empty;

			let widget = Decoration.widget(0, function(): Element {
				let element = document.createElement("div");
				element.innerHTML = "Start writing your ideas here...";
				return element;
			});

			return DecorationSet.create(state.doc, [widget]);
		},
	},
});

To trigger the bug dispatch a transaction to replace the doc content with two nodes:

state.tr.replaceWith(0, state.doc.content.size, [
	schema.nodes.code_block.create(),
	schema.nodes.paragraph.create(),
]);

Testing in Chrome:

Document with a single empty paragraph, before transaction:
image

After transaction with prosemirror-view 1.30.2: Both blocks have trailing break
image

After transaction with prosemirror-view 1.31.0: Paragraph has no trailing break, leaving it collapsed.
image

The bug is not reproduced if the plugin is removed or modified to not branch, ie always return either the DecorationSet or empty, or if the transaction inserts two paragraphs. It also works as expected when the editor is loaded with the resulting document, so it appears limited to updating.

Thank you and have a good day.

@marijnh
Copy link
Member

marijnh commented Sep 16, 2024

Thanks for the reproduction! Attached patch should help.

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

2 participants