-
-
Notifications
You must be signed in to change notification settings - Fork 778
fix: Math/mermaid/etc block errors not selectable (BLO-1376) #3148
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
matthewlipski
wants to merge
1
commit into
main
Choose a base branch
from
source-preview-error-focus
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
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 |
|---|---|---|
|
|
@@ -183,8 +183,19 @@ export const SourceBlockWithPreviewExtension = createExtension( | |
| signal, | ||
| }); | ||
|
|
||
| const handleBlur = () => | ||
| function handleBlur(event: FocusEvent) { | ||
| // Error text has its own focus target so the browser can select and | ||
| // copy it. Keep the popup open while interacting with that text. | ||
| if ( | ||
| event.relatedTarget === dom || | ||
| (event.relatedTarget instanceof Element && | ||
| dom.contains(event.relatedTarget) && | ||
| event.relatedTarget.closest(".bn-code-block-source-error")) | ||
| ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder whether you can capture the events and stop them from propagating at the error message element, maybe it would allow you to not have to do focus capturing and blur handling? |
||
| return; | ||
| } | ||
| store.setState((state) => ({ ...state, popupOpen: undefined })); | ||
| } | ||
| dom.addEventListener("blur", handleBlur, { capture: true, signal }); | ||
| }, | ||
| }; | ||
|
|
||
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
138 changes: 138 additions & 0 deletions
138
tests/src/end-to-end/sourcepreview/errorSelection.test.tsx
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,138 @@ | ||
| import { BlockNoteSchema } from "@blocknote/core"; | ||
| import "@blocknote/core/fonts/inter.css"; | ||
| import { createReactDiagramBlockSpec } from "@blocknote/diagram-block"; | ||
| import { BlockNoteView } from "@blocknote/mantine"; | ||
| import "@blocknote/mantine/style.css"; | ||
| import { | ||
| createReactInlineMathSpec, | ||
| createReactMathBlockSpec, | ||
| } from "@blocknote/math-block"; | ||
| import { useCreateBlockNote } from "@blocknote/react"; | ||
| import { expect, test, vi } from "vite-plus/test"; | ||
| import { render } from "vitest-browser-react"; | ||
|
|
||
| import { browserName, MOD, userEvent } from "../../utils/context.js"; | ||
| import { mouseSequence } from "../../utils/mouse.js"; | ||
|
|
||
| const schema = BlockNoteSchema.create().extend({ | ||
| blockSpecs: { | ||
| mathBlock: createReactMathBlockSpec(), | ||
| diagram: createReactDiagramBlockSpec(), | ||
| }, | ||
| inlineContentSpecs: { math: createReactInlineMathSpec() }, | ||
| }); | ||
|
|
||
| type Kind = "mathBlock" | "diagram" | "math"; | ||
|
|
||
| function ErrorSelectionApp({ kind }: { kind: Kind }) { | ||
| const editor = useCreateBlockNote({ | ||
| schema, | ||
| initialContent: | ||
| kind === "math" | ||
| ? [ | ||
| { | ||
| type: "paragraph", | ||
| content: [{ type: "math", content: "\\badcommand" }], | ||
| }, | ||
| ] | ||
| : [ | ||
| { | ||
| type: kind, | ||
| content: kind === "diagram" ? "graph TD\nA -->" : "\\badcommand", | ||
| }, | ||
| ], | ||
| }); | ||
| return ( | ||
| <> | ||
| <BlockNoteView editor={editor} /> | ||
| <textarea aria-label="Paste error text" /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| test.each<Kind>(["mathBlock", "diagram", "math"])( | ||
| "selecting and copying %s error text preserves the popup and source editing", | ||
| async (kind) => { | ||
| await render(<ErrorSelectionApp kind={kind} />); | ||
| await userEvent.click( | ||
| document.querySelector<HTMLElement>(".bn-preview-container")!, | ||
| ); | ||
| const error = document.querySelector<HTMLElement>( | ||
| ".bn-code-block-source-error", | ||
| )!; | ||
| await vi.waitFor(() => { | ||
| expect(error.textContent?.length).toBeGreaterThan(0); | ||
| expect( | ||
| error | ||
| .closest(".bn-preview-with-source-popup") | ||
| ?.getAttribute("data-open"), | ||
| ).toBe("true"); | ||
| }); | ||
|
|
||
| // Drag across the first word using real mouse events. A DOM-only selection | ||
| // wouldn't reproduce Firefox resetting the selection after mouseup (#3138). | ||
| const text = error.firstChild!; | ||
| const word = (text.textContent ?? "").match(/\S+/)![0]; | ||
| const range = document.createRange(); | ||
| range.setStart(text, 0); | ||
| range.setEnd(text, word.length); | ||
| const rect = range.getBoundingClientRect(); | ||
| await mouseSequence([ | ||
| { type: "move", x: rect.left, y: rect.top + rect.height / 2 }, | ||
| { type: "down" }, | ||
| { type: "move", x: rect.right, y: rect.top + rect.height / 2, steps: 10 }, | ||
| { type: "up" }, | ||
| ]); | ||
| await vi.waitFor(() => { | ||
| expect(document.activeElement).toBe(error); | ||
| expect(window.getSelection()?.toString()).toBe(word); | ||
| expect( | ||
| error | ||
| .closest(".bn-preview-with-source-popup") | ||
| ?.getAttribute("data-open"), | ||
| ).toBe("true"); | ||
| }); | ||
| // Playwright WebKit does not support the native clipboard round trip. | ||
| // Still exercise selection, focus recovery, and dismissal in that browser. | ||
| if (browserName !== "webkit") { | ||
| await userEvent.keyboard(`{${MOD}>}c{/${MOD}}`); | ||
| } | ||
|
|
||
| // Focus leaving the error must close the popup. | ||
| const textarea = document.querySelector<HTMLTextAreaElement>("textarea")!; | ||
| await userEvent.click(textarea); | ||
| expect( | ||
| error.closest(".bn-preview-with-source-popup")?.getAttribute("data-open"), | ||
| ).toBe("false"); | ||
| await userEvent.click( | ||
| document.querySelector<HTMLElement>(".bn-preview-container")!, | ||
| ); | ||
| await userEvent.click(error); | ||
| expect(document.activeElement).toBe(error); | ||
|
|
||
| // Moving directly back from the error to the source must keep it editable. | ||
| const source = document.querySelector<HTMLElement>( | ||
| ".bn-source-block-popup code", | ||
| )!; | ||
| await userEvent.click(source); | ||
| const originalSource = source.textContent; | ||
| await userEvent.keyboard("{End}x"); | ||
| await vi.waitFor(() => | ||
| expect(source.textContent).toBe(originalSource + "x"), | ||
| ); | ||
| expect( | ||
| source | ||
| .closest(".bn-preview-with-source-popup") | ||
| ?.getAttribute("data-open"), | ||
| ).toBe("true"); | ||
|
|
||
| await userEvent.click(textarea); | ||
| if (browserName !== "webkit") { | ||
| await userEvent.keyboard(`{${MOD}>}v{/${MOD}}`); | ||
| expect(textarea.value).toBe(word); | ||
| } | ||
| expect( | ||
| error.closest(".bn-preview-with-source-popup")?.getAttribute("data-open"), | ||
| ).toBe("false"); | ||
| }, | ||
| ); |
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.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 6213
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 2546
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 2437
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 8153
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 41696
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 41762
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 42231
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 41901
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 32591
🏁 Script executed:
Repository: TypeCellOS/BlockNote
Length of output: 41714
Keep source focus from clearing the popup state.
The source
<code>is a descendant of the editor mountdom, notdomitself. When focus moves from.bn-code-block-source-errorto the source, both blur handlers miss their allowlist and clearpopupOpenorselected.No source focus handler reopens the popup. The block handler also prevents typing while
popupOpenis cleared, so returning to the source can leave it non-editable. The e2e test catches this persistent failure, but it would not catch a transient close followed by reopening.Allow the source code target in both blur handlers.
Suggested fix
Apply the same condition in
SourceInlineContentWithPreview.ts.📝 Committable suggestion
🤖 Prompt for AI Agents