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"); }); }