Repository navigation
fix(core): convert existing text via slash menu at block start #3276
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
Open
nperez0111
wants to merge
6
commits into
main
Choose a base branch
from
test/issue-2236-quote-paste
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
820f66a
fix(core): convert existing text via slash menu at block start
nperez0111 bd2527f
fix(core): convert inline blocks at any caret position
nperez0111 9aa04e7
fix(core): preserve caret during slash-menu inline conversion
nperez0111 8ab1ebc
fix(core): reset slash-menu caret only for content-type changes
nperez0111 686181d
fix(core): limit slash-menu text conversion to block start
nperez0111 488625e
test: wait for nesting suggestion decorations before screenshot
nperez0111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // @vitest-environment jsdom | ||
| import { afterEach, describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
| import { insertOrUpdateBlockForSlashMenu } from "../../extensions/SuggestionMenu/getDefaultSlashMenuItems.js"; | ||
|
|
||
| let editor: BlockNoteEditor | undefined; | ||
| let container: HTMLDivElement | undefined; | ||
|
|
||
| afterEach(() => { | ||
| editor?.unmount(); | ||
| container?.remove(); | ||
| }); | ||
|
|
||
| describe("pasting into an empty quote (#2236)", () => { | ||
| it.each(["text", "html"] as const)( | ||
| "preserves the quote when pasting %s", | ||
| (format) => { | ||
| container = document.createElement("div"); | ||
| document.body.append(container); | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [{ id: "source", type: "paragraph" }], | ||
| }); | ||
| editor.mount(container); | ||
| editor.setTextCursorPosition("source", "start"); | ||
| insertOrUpdateBlockForSlashMenu(editor, { type: "quote" }); | ||
|
|
||
| if (format === "text") { | ||
| editor.pasteText("Pasted paragraph"); | ||
| } else { | ||
| editor.pasteHTML("<p>Pasted paragraph</p>"); | ||
| } | ||
|
|
||
| expect(editor.document[0]).toMatchObject({ | ||
| type: "quote", | ||
| content: [{ type: "text", text: "Pasted paragraph", styles: {} }], | ||
| }); | ||
| expect( | ||
| editor.document.filter( | ||
| (block) => Array.isArray(block.content) && block.content.length > 0, | ||
| ), | ||
| ).toHaveLength(1); | ||
| }, | ||
| ); | ||
| }); |
136 changes: 136 additions & 0 deletions
136
packages/core/src/extensions/SuggestionMenu/getDefaultSlashMenuItems.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // @vitest-environment node | ||
| import { TextSelection } from "@tiptap/pm/state"; | ||
| import { afterEach, describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
| import { insertOrUpdateBlockForSlashMenu } from "./getDefaultSlashMenuItems.js"; | ||
|
|
||
| let editor: BlockNoteEditor | undefined; | ||
|
|
||
| afterEach(() => { | ||
| editor?._tiptapEditor.destroy(); | ||
| }); | ||
|
|
||
| describe("slash-menu block conversion (#2236)", () => { | ||
| it("converts the existing paragraph to a quote at its start", () => { | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "source", type: "paragraph", content: "Existing text" }, | ||
| ], | ||
| }); | ||
| // The suggestion menu removes its slash/query before invoking the item. | ||
| editor.setTextCursorPosition("source", "start"); | ||
|
|
||
| insertOrUpdateBlockForSlashMenu(editor, { type: "quote" }); | ||
|
|
||
| expect(editor.document).toHaveLength(1); | ||
| expect(editor.document[0]).toMatchObject({ | ||
| id: "source", | ||
| type: "quote", | ||
| content: [{ type: "text", text: "Existing text", styles: {} }], | ||
| }); | ||
| expect(editor.getTextCursorPosition().block.id).toBe("source"); | ||
| expect(editor.prosemirrorState.selection.empty).toBe(true); | ||
| expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(0); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["middle", 8], | ||
| ["end", 13], | ||
| ] as const)( | ||
| "inserts a new quote below when invoked at the paragraph's %s", | ||
| (_position, offset) => { | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "source", type: "paragraph", content: "Existing text" }, | ||
| ], | ||
| }); | ||
| // The suggestion menu removes its slash/query before invoking the item. | ||
| editor.setTextCursorPosition("source", "start"); | ||
| editor.transact((tr) => | ||
| tr.setSelection( | ||
| TextSelection.create(tr.doc, tr.selection.from + offset), | ||
| ), | ||
| ); | ||
|
|
||
| insertOrUpdateBlockForSlashMenu(editor, { type: "quote" }); | ||
|
|
||
| expect(editor.document).toHaveLength(2); | ||
| expect(editor.document[0]).toMatchObject({ | ||
| id: "source", | ||
| type: "paragraph", | ||
| content: [{ type: "text", text: "Existing text", styles: {} }], | ||
| }); | ||
| expect(editor.document[1]).toMatchObject({ type: "quote", content: [] }); | ||
| expect(editor.getTextCursorPosition().block.id).toBe( | ||
| editor.document[1].id, | ||
| ); | ||
| expect(editor.prosemirrorState.selection.empty).toBe(true); | ||
| expect(editor.prosemirrorState.selection.$from.parentOffset).toBe(0); | ||
| }, | ||
| ); | ||
|
|
||
| it("still inserts below when the item supplies replacement content", () => { | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "source", type: "paragraph", content: "Existing text" }, | ||
| ], | ||
| }); | ||
| editor.setTextCursorPosition("source", "start"); | ||
|
|
||
| insertOrUpdateBlockForSlashMenu(editor, { | ||
| type: "quote", | ||
| content: "New text", | ||
| }); | ||
|
|
||
| expect(editor.document.map((block) => block.type)).toEqual([ | ||
| "paragraph", | ||
| "quote", | ||
| ]); | ||
| expect(editor.document[0].content).toEqual([ | ||
| { type: "text", text: "Existing text", styles: {} }, | ||
| ]); | ||
| expect(editor.document[1].content).toEqual([ | ||
| { type: "text", text: "New text", styles: {} }, | ||
| ]); | ||
| }); | ||
|
|
||
| it("enters a table replacing an empty paragraph before other content", () => { | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "source", type: "paragraph" }, | ||
| { id: "following", type: "paragraph", content: "Following text" }, | ||
| ], | ||
| }); | ||
| editor.setTextCursorPosition("source", "start"); | ||
|
|
||
| insertOrUpdateBlockForSlashMenu(editor, { | ||
| type: "table", | ||
| content: { type: "tableContent", rows: [{ cells: ["Cell"] }] }, | ||
| }); | ||
|
|
||
| expect(editor.getTextCursorPosition().block).toMatchObject({ | ||
| id: "source", | ||
| type: "table", | ||
| }); | ||
| }); | ||
|
|
||
| it("still inserts non-inline blocks below existing text", () => { | ||
| editor = BlockNoteEditor.create({ | ||
| initialContent: [ | ||
| { id: "source", type: "paragraph", content: "Existing text" }, | ||
| ], | ||
| }); | ||
| editor.setTextCursorPosition("source", "start"); | ||
|
|
||
| insertOrUpdateBlockForSlashMenu(editor, { type: "divider" }); | ||
|
|
||
| expect(editor.document.slice(0, 2).map((block) => block.type)).toEqual([ | ||
| "paragraph", | ||
| "divider", | ||
| ]); | ||
| expect(editor.document[0].content).toEqual([ | ||
| { type: "text", text: "Existing text", styles: {} }, | ||
| ]); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 7964
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 41504
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 25850
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 25382
Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 8515
Convert existing text at every caret position without replacing children.
When the caret is in the middle or at the end of a paragraph,
selection.$from.parentOffset === 0prevents conversion. The helper inserts a new quote below the paragraph instead.Remove the start-only restriction. Also exclude items that supply
children; otherwise a custom inline item can update the current block and replace its existing children.Suggested fix
Update the middle/end tests to expect conversion of the existing block. Add coverage that an inline item with children is inserted below instead of replacing the current block’s children.