-
Notifications
You must be signed in to change notification settings - Fork 2
Bug/74325 inserting hash inside text removes content after cursor #122
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
ihordubas99
wants to merge
3
commits into
dev
Choose a base branch
from
bug/74325-inserting-hash-inside-text-removes-content-after-cursor
base: dev
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.
+90
−137
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,114 +1,107 @@ | ||
| // @vitest-environment jsdom | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { BlockNoteEditor } from "@blocknote/core"; | ||
| import { | ||
| getSizeFromCurrentBlock, | ||
| insertWpChipIntoBlock, | ||
| clearTriggerText, | ||
| } from "../../../lib/components/HashMenu/editorUtils"; | ||
|
|
||
| type FakeContent = { type: string; text?: string; styles?: any; props?: any }[]; | ||
|
|
||
| function makeFakeEditor(content: FakeContent = []) { | ||
| let block = { id: "block-1", content }; | ||
| const inserted: FakeContent = []; | ||
|
|
||
| return { | ||
| block, | ||
| inserted, | ||
| getTextCursorPosition: () => ({ block }), | ||
| getBlock: (id: string) => (id === block.id ? block : null), | ||
| updateBlock: (id: string, update: { content: FakeContent }) => { | ||
| if (id === block.id) { | ||
| block.content = update.content; | ||
| inserted.push(...update.content); | ||
| } | ||
| }, | ||
| insertInlineContent: (content: FakeContent) => { | ||
| inserted.push(...content); | ||
| }, | ||
| focus: vi.fn(), | ||
| setTextCursorPosition: vi.fn(), | ||
| }; | ||
| function createTestEditor(text: string) { | ||
| const editor = BlockNoteEditor.create({ | ||
| initialContent: [{ type: "paragraph", content: text }], | ||
| }); | ||
|
|
||
| const block = editor.document[0]; | ||
| editor.setTextCursorPosition(block, "end"); | ||
|
|
||
| return editor; | ||
| } | ||
|
|
||
| describe("getSizeFromCurrentBlock", () => { | ||
| it("returns xxs for #", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "#foo" }]); | ||
| const editor = createTestEditor("#foo"); | ||
| expect(getSizeFromCurrentBlock(editor as any)).toBe("xxs"); | ||
| }); | ||
|
|
||
| it("returns xs for ##", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "##foo" }]); | ||
| const editor = createTestEditor("##foo"); | ||
| expect(getSizeFromCurrentBlock(editor as any)).toBe("xs"); | ||
| }); | ||
|
|
||
| it("returns s for ### or more", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "###foo" }]); | ||
| const editor = createTestEditor("###foo"); | ||
| expect(getSizeFromCurrentBlock(editor as any)).toBe("s"); | ||
|
|
||
| const editor2 = makeFakeEditor([{ type: "text", text: "####foo" }]); | ||
| const editor2 = createTestEditor("####foo"); | ||
| expect(getSizeFromCurrentBlock(editor2 as any)).toBe("s"); | ||
| }); | ||
|
|
||
| it("returns xxs if no hashes", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "foo" }]); | ||
| const editor = createTestEditor("foo"); | ||
| expect(getSizeFromCurrentBlock(editor as any)).toBe("xxs"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("clearTriggerText", () => { | ||
| it("removes # text and returns block id", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "#foo" }]); | ||
| const editor = createTestEditor("#foo"); | ||
| const blockId = clearTriggerText(editor as any); | ||
| expect(blockId).toBe("block-1"); | ||
| expect(editor.block.content).toEqual([]); | ||
|
|
||
| expect(blockId).toBe(editor.document[0].id); | ||
| const block = editor.getBlock(editor.document[0].id); | ||
| expect(block?.content).toEqual([]); | ||
| }); | ||
|
|
||
| it("does nothing if no block", () => { | ||
| const editor = { getTextCursorPosition: () => null, updateBlock: vi.fn() }; | ||
| const editor = { | ||
| _tiptapEditor: null, | ||
| getTextCursorPosition: () => null, | ||
| updateBlock: vi.fn(), | ||
| }; | ||
| expect(clearTriggerText(editor as any)).toBeNull(); | ||
| }); | ||
|
|
||
| it("keeps text before # and removes trigger", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "Hello #foo" }]); | ||
| const editor = createTestEditor("Hello #foo"); | ||
| const blockId = clearTriggerText(editor as any); | ||
| expect(blockId).toBe("block-1"); | ||
| expect(editor.block.content).toEqual([{ type: "text", text: "Hello " }]); | ||
|
|
||
| expect(blockId).toBe(editor.document[0].id); | ||
| const block = editor.getBlock(editor.document[0].id); | ||
| expect((block?.content as any)[0].text).toBe("Hello "); | ||
| }); | ||
|
|
||
| it("works with multiple # in the text", () => { | ||
| const editor = makeFakeEditor([{ type: "text", text: "Pre #one #two #three" }]); | ||
| const editor = createTestEditor("Pre #one #two #three"); | ||
| const blockId = clearTriggerText(editor as any); | ||
| expect(blockId).toBe("block-1"); | ||
| expect(editor.block.content).toEqual([{ type: "text", text: "Pre " }]); | ||
|
|
||
| expect(blockId).toBe(editor.document[0].id); | ||
| const block = editor.getBlock(editor.document[0].id); | ||
|
|
||
| expect((block?.content as any)[0].text).toBe("Pre #one #two "); | ||
| }); | ||
| }); | ||
|
|
||
| describe("insertWpChipIntoBlock", () => { | ||
| it("adds a chip to the block content", () => { | ||
| const editor = makeFakeEditor([]); | ||
| const editor = createTestEditor("test"); | ||
|
|
||
| const insertSpy = vi.spyOn(editor, "insertInlineContent").mockImplementation(() => {}); | ||
| vi.spyOn(editor, "focus").mockImplementation(() => {}); | ||
|
|
||
| insertWpChipIntoBlock( | ||
| editor as any, | ||
| "block-1", | ||
| editor.document[0].id, | ||
| { id: 1, subject: "Fix bug" } as any, | ||
| "xxs" | ||
| ); | ||
|
|
||
| const inserted = editor.inserted; | ||
| expect(inserted.length).toBe(2); | ||
|
|
||
| const chip = inserted.find( | ||
| (c): c is { type: string; props: { size: string } } => | ||
| c.type === "openProjectWorkPackageInline" && c.props?.size | ||
| ); | ||
| expect(chip).toBeDefined(); | ||
| expect(chip?.props.size).toBe("xxs"); | ||
| }); | ||
|
|
||
| it("does nothing if block not found", () => { | ||
| const editor = makeFakeEditor([]); | ||
| expect(() => | ||
| insertWpChipIntoBlock(editor as any, "wrong-id", { id: 1 } as any, "xxs") | ||
| ).not.toThrow(); | ||
| expect(insertSpy).toHaveBeenCalledWith([ | ||
| { | ||
| type: "openProjectWorkPackageInline", | ||
| props: { wpid: "1", instanceId: expect.any(String), size: "xxs" }, | ||
| }, | ||
| { type: "text", text: " ", styles: {} }, | ||
| ]); | ||
| }); | ||
| }); |
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.
For what reason do we need
pendingSizeRefandcurrentSizeand what's the difference?Same for
originalBlockIdRef,savedSelectionRef,currentBlockIdThere 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.
You're right, having both was confusing. I've completely removed the redundant useRef hooks (pendingSizeRef, originalBlockIdRef, savedSelectionRef).
The logic is now much simpler: we rely on the closure variables (currentSize, currentBlockId) from the render cycle. This makes the code cleaner and eliminates the need for manual state resets