From 24e57009af2dc54702d2ca0bc858b6c3f0f3ec39 Mon Sep 17 00:00:00 2001 From: Vipin Chaudhary Date: Thu, 19 Jun 2025 17:17:56 +0530 Subject: [PATCH] [WIKI-465] fix : Add new node on click of doc end (#7063) * fix : handle last node * fix : handle unexpected node * remove logs * feat: handle focus --------- Co-authored-by: M. Palanikannan <73993394+Palanikannan1437@users.noreply.github.com> --- .../components/editors/editor-container.tsx | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/editor/src/core/components/editors/editor-container.tsx b/packages/editor/src/core/components/editors/editor-container.tsx index 6daa0719a..3553f07fd 100644 --- a/packages/editor/src/core/components/editors/editor-container.tsx +++ b/packages/editor/src/core/components/editors/editor-container.tsx @@ -53,17 +53,14 @@ export const EditorContainer: FC = (props) => { const lastNodePos = editor.state.doc.resolve(Math.max(0, docSize - 2)); const lastNode = lastNodePos.node(); - // Check if the last node is a not paragraph - if (lastNode && lastNode.type.name !== CORE_EXTENSIONS.PARAGRAPH) { - // If last node is not a paragraph, insert a new paragraph at the end - const endPosition = editor?.state.doc.content.size; - editor?.chain().insertContentAt(endPosition, { type: CORE_EXTENSIONS.PARAGRAPH }).run(); - - // Focus the newly added paragraph for immediate editing - editor - .chain() - .setTextSelection(endPosition + 1) - .run(); + // Check if its last node and add new node + if (lastNode) { + const isLastNodeEmptyParagraph = lastNode.type.name === CORE_EXTENSIONS.PARAGRAPH && lastNode.content.size === 0; + // Only insert a new paragraph if the last node is not an empty paragraph and not a doc node + if (!isLastNodeEmptyParagraph && lastNode.type.name !== "doc") { + const endPosition = editor?.state.doc.content.size; + editor?.chain().insertContentAt(endPosition, { type: "paragraph" }).focus("end").run(); + } } } catch (error) { console.error("An error occurred while handling container click to insert new empty node at bottom:", error);