From 4ba1ec19893040c20fd43a8b2a22155a47b29598 Mon Sep 17 00:00:00 2001 From: adarshsm <24850536+adarshsm@users.noreply.github.com> Date: Sat, 17 Oct 2026 10:10:15 +0530 Subject: [PATCH] fix(core): separate blocks with newlines in getSelectedText `getSelectedText` called `doc.textBetween(from, to)` without a block separator, so a selection across blocks returned the block texts glued together ("FirstSecond" for two paragraphs "First" and "Second"). Pass "\n" as the block separator so text from each block is on its own line. Selections inside a single block are unchanged. Closes #710 --- .../reference/editor/manipulating-content.mdx | 2 +- packages/core/src/editor/BlockNoteEditor.ts | 3 +- .../src/editor/managers/StyleManager.test.ts | 32 +++++++++++++++++++ .../core/src/editor/managers/StyleManager.ts | 5 +-- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/docs/content/docs/reference/editor/manipulating-content.mdx b/docs/content/docs/reference/editor/manipulating-content.mdx index 3bb3091225..f112288aa9 100644 --- a/docs/content/docs/reference/editor/manipulating-content.mdx +++ b/docs/content/docs/reference/editor/manipulating-content.mdx @@ -384,7 +384,7 @@ editor.insertInlineContent([ getSelectedText(): string ``` -Retrieves the currently selected text as a plain string. +Retrieves the currently selected text as a plain string. When the selection spans multiple blocks, the text of each block is separated by a newline. ```typescript const selectedText = editor.getSelectedText(); diff --git a/packages/core/src/editor/BlockNoteEditor.ts b/packages/core/src/editor/BlockNoteEditor.ts index ddda8786f3..699dcbbc5b 100644 --- a/packages/core/src/editor/BlockNoteEditor.ts +++ b/packages/core/src/editor/BlockNoteEditor.ts @@ -1252,7 +1252,8 @@ export class BlockNoteEditor< } /** - * Gets the currently selected text. + * Gets the currently selected text. Text from different blocks is separated + * by a newline. */ public getSelectedText() { return this._styleManager.getSelectedText(); diff --git a/packages/core/src/editor/managers/StyleManager.test.ts b/packages/core/src/editor/managers/StyleManager.test.ts index 7090c7ddf8..aaea44526a 100644 --- a/packages/core/src/editor/managers/StyleManager.test.ts +++ b/packages/core/src/editor/managers/StyleManager.test.ts @@ -96,3 +96,35 @@ describe("getSelectedLinkUrl", () => { expect(editor.getLinkMarkAtPos(from - 2)).toBeUndefined(); }); }); + +describe("getSelectedText", () => { + it("separates text from different blocks with a newline", () => { + const editor = BlockNoteEditor.create({ + initialContent: [ + { type: "paragraph", content: "First" }, + { + type: "paragraph", + content: "Second", + children: [{ type: "paragraph", content: "Nested" }], + }, + { type: "heading", content: "Third" }, + ], + }); + editor.transact((tr) => + tr.setSelection(TextSelection.create(tr.doc, 1, tr.doc.content.size - 1)), + ); + expect(editor.getSelectedText()).toBe("First\nSecond\nNested\nThird"); + }); + + it("returns the text of a selection within one block unchanged", () => { + const editor = BlockNoteEditor.create({ + initialContent: [{ type: "paragraph", content: "Hello world" }], + }); + editor.setTextCursorPosition(editor.document[0], "start"); + const start = editor.prosemirrorState.selection.from; + editor.transact((tr) => + tr.setSelection(TextSelection.create(tr.doc, start, start + 5)), + ); + expect(editor.getSelectedText()).toBe("Hello"); + }); +}); diff --git a/packages/core/src/editor/managers/StyleManager.ts b/packages/core/src/editor/managers/StyleManager.ts index 062a5bac07..4ecad7a6d6 100644 --- a/packages/core/src/editor/managers/StyleManager.ts +++ b/packages/core/src/editor/managers/StyleManager.ts @@ -141,11 +141,12 @@ export class StyleManager< } /** - * Gets the currently selected text. + * Gets the currently selected text. Text from different blocks is separated + * by a newline. */ public getSelectedText() { return this.editor.transact((tr) => { - return tr.doc.textBetween(tr.selection.from, tr.selection.to); + return tr.doc.textBetween(tr.selection.from, tr.selection.to, "\n"); }); }