-
-
Notifications
You must be signed in to change notification settings - Fork 725
fix(core): trigger codeblock input rule on Enter and place cursor inside #2686
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
3
commits into
main
Choose a base branch
from
feat/fix-codeblock-input-rule
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.
+338
−25
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,258 @@ | ||
| import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest"; | ||
| import { BlockNoteEditor } from "../../editor/BlockNoteEditor.js"; | ||
| import type { PartialBlock } from "../defaultBlocks.js"; | ||
| import { getLanguageId, type CodeBlockOptions } from "./block.js"; | ||
|
|
||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
|
|
||
| /** | ||
| * Simulate typing text into the editor at the current cursor position. | ||
| * This triggers input rules by calling the view's handleTextInput prop, | ||
| * which is how ProseMirror processes keyboard text input. | ||
| */ | ||
| function simulateTextInput(editor: BlockNoteEditor, text: string) { | ||
| const view = editor.prosemirrorView; | ||
| const { from, to } = view.state.selection; | ||
| const deflt = () => view.state.tr.insertText(text, from, to); | ||
| const handled = view.someProp("handleTextInput", (f) => | ||
| f(view, from, to, text, deflt), | ||
| ); | ||
| if (!handled) { | ||
| view.dispatch(deflt()); | ||
| } | ||
| } | ||
|
|
||
| function typeString(editor: BlockNoteEditor, str: string) { | ||
| for (const char of str) { | ||
| simulateTextInput(editor, char); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Simulate a keyboard shortcut by invoking the view's handleKeyDown prop, | ||
| * which is how ProseMirror routes keymap-based handlers like Enter. | ||
| */ | ||
| function pressKey(editor: BlockNoteEditor, key: string) { | ||
| const view = editor.prosemirrorView; | ||
| const event = new KeyboardEvent("keydown", { key }); | ||
| view.someProp("handleKeyDown", (f) => f(view, event)); | ||
| } | ||
|
|
||
| describe("Code block input rule", () => { | ||
| let editor: BlockNoteEditor; | ||
| const div = document.createElement("div"); | ||
|
|
||
| beforeAll(() => { | ||
| editor = BlockNoteEditor.create(); | ||
| editor.mount(div); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| editor._tiptapEditor.destroy(); | ||
| editor = undefined as any; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| const testDoc: PartialBlock[] = [ | ||
| { | ||
| id: "test-paragraph", | ||
| type: "paragraph", | ||
| content: "", | ||
| }, | ||
| ]; | ||
| editor.replaceBlocks(editor.document, testDoc); | ||
| editor.setTextCursorPosition("test-paragraph", "start"); | ||
| }); | ||
|
|
||
| it("converts ```ts + space into a codeBlock", () => { | ||
| typeString(editor, "```ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| // Without supportedLanguages configured, the raw alias is used | ||
| expect((block.props as any).language).toBe("ts"); | ||
| }); | ||
|
|
||
| it("converts ``` + space into a codeBlock with empty language", () => { | ||
| typeString(editor, "``` "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect((block.props as any).language).toBe(""); | ||
| }); | ||
|
|
||
| it("converts ```javascript + space into a codeBlock", () => { | ||
| typeString(editor, "```javascript "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect((block.props as any).language).toBe("javascript"); | ||
| }); | ||
|
|
||
| it("does not trigger input rule without trailing space", () => { | ||
| typeString(editor, "```ts"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("paragraph"); | ||
| }); | ||
|
|
||
| it("does not trigger with only two backticks", () => { | ||
| typeString(editor, "``ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("paragraph"); | ||
| }); | ||
|
|
||
| it("does not trigger in non-empty paragraph with preceding text", () => { | ||
| typeString(editor, "some text ```ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| // The ^ anchor in the regex means it only triggers at the start of a block | ||
| expect(block.type).toBe("paragraph"); | ||
| }); | ||
|
|
||
| it("code block content is empty after conversion", () => { | ||
| typeString(editor, "```ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect(block.content).toEqual([]); | ||
| }); | ||
|
|
||
| it("converts ```ts + Enter into a codeBlock", () => { | ||
| typeString(editor, "```ts"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect((block.props as any).language).toBe("ts"); | ||
| expect(block.content).toEqual([]); | ||
| }); | ||
|
|
||
| it("converts ``` + Enter into a codeBlock with empty language", () => { | ||
| typeString(editor, "```"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect((block.props as any).language).toBe(""); | ||
| }); | ||
|
|
||
| it("converts ```javascript + Enter into a codeBlock", () => { | ||
| typeString(editor, "```javascript"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
| expect((block.props as any).language).toBe("javascript"); | ||
| }); | ||
|
|
||
| it("does not trigger Enter conversion in non-empty paragraph with preceding text", () => { | ||
| typeString(editor, "some text ```ts"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("paragraph"); | ||
| }); | ||
|
|
||
| it("does not trigger Enter conversion with only two backticks", () => { | ||
| typeString(editor, "``ts"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("paragraph"); | ||
| }); | ||
|
|
||
| it("places cursor inside the new code block after space conversion", () => { | ||
| typeString(editor, "```ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
|
|
||
| const { block: cursorBlock } = editor.getTextCursorPosition(); | ||
| expect(cursorBlock.id).toBe(block.id); | ||
|
|
||
| // Typing should now go into the code block, not after it. | ||
| typeString(editor, "hello"); | ||
| const after = editor.document[0]; | ||
| expect(after.type).toBe("codeBlock"); | ||
| expect(after.id).toBe(block.id); | ||
| expect((after.content as Array<{ type: string; text: string }>)[0].text).toBe( | ||
| "hello", | ||
| ); | ||
| }); | ||
|
|
||
| it("places cursor inside the new code block after Enter conversion", () => { | ||
| typeString(editor, "```ts"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
|
|
||
| const { block: cursorBlock } = editor.getTextCursorPosition(); | ||
| expect(cursorBlock.id).toBe(block.id); | ||
|
|
||
| typeString(editor, "world"); | ||
| const after = editor.document[0]; | ||
| expect(after.type).toBe("codeBlock"); | ||
| expect(after.id).toBe(block.id); | ||
| expect((after.content as Array<{ type: string; text: string }>)[0].text).toBe( | ||
| "world", | ||
| ); | ||
| }); | ||
|
|
||
| it("Enter inside an existing code block does not retrigger conversion", () => { | ||
| typeString(editor, "```ts "); | ||
|
|
||
| const block = editor.document[0]; | ||
| expect(block.type).toBe("codeBlock"); | ||
|
|
||
| typeString(editor, "```js"); | ||
| pressKey(editor, "Enter"); | ||
|
|
||
| // Enter inside a code block should insert a newline, not convert again. | ||
| const after = editor.document[0]; | ||
| expect(after.type).toBe("codeBlock"); | ||
| expect((after.props as any).language).toBe("ts"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("getLanguageId", () => { | ||
| const options: CodeBlockOptions = { | ||
| supportedLanguages: { | ||
| typescript: { | ||
| name: "TypeScript", | ||
| aliases: ["ts", "typescript"], | ||
| }, | ||
| javascript: { | ||
| name: "JavaScript", | ||
| aliases: ["js", "javascript"], | ||
| }, | ||
| python: { | ||
| name: "Python", | ||
| aliases: ["py", "python"], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| it("resolves alias to language id", () => { | ||
| expect(getLanguageId(options, "ts")).toBe("typescript"); | ||
| expect(getLanguageId(options, "js")).toBe("javascript"); | ||
| expect(getLanguageId(options, "py")).toBe("python"); | ||
| }); | ||
|
|
||
| it("resolves language id directly", () => { | ||
| expect(getLanguageId(options, "typescript")).toBe("typescript"); | ||
| expect(getLanguageId(options, "javascript")).toBe("javascript"); | ||
| }); | ||
|
|
||
| it("returns undefined for unknown language", () => { | ||
| expect(getLanguageId(options, "unknown")).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("returns undefined with no supportedLanguages", () => { | ||
| expect(getLanguageId({}, "ts")).toBeUndefined(); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.