Skip to content

Commit f6ae2ba

Browse files
committed
fix: native text input and deletion in an empty inline math source
1 parent 45d07d3 commit f6ae2ba

3 files changed

Lines changed: 124 additions & 3 deletions

File tree

‎packages/core/src/editor/Block.css‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,16 @@ NESTED BLOCKS
673673
width: 0;
674674
}
675675

676+
/* A block with a line box, so native inserts into an empty inline source land inside it, and native deletions that empty it don't remove it (#3234). */
677+
.bn-inline-content-section
678+
.bn-code-block-source-popup-body
679+
> pre
680+
> code
681+
> [data-node-view-content-react] {
682+
display: block;
683+
min-height: 1.5em;
684+
}
685+
676686
.bn-code-block-source-popup-input-empty::before {
677687
color: currentColor;
678688
content: attr(data-placeholder);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import "@blocknote/core/style.css";
2+
3+
import { BlockNoteEditor, BlockNoteSchema } from "@blocknote/core";
4+
import { BlockNoteViewRaw } from "@blocknote/react";
5+
import { flushSync } from "react-dom";
6+
import { createRoot } from "react-dom/client";
7+
import { expect, it, vi } from "vite-plus/test";
8+
import { userEvent } from "vite-plus/test/browser";
9+
10+
import { getMathSlashMenuItems } from "../getMathSlashMenuItems.js";
11+
import { en } from "../i18n/locales/en.js";
12+
import { createReactInlineMathSpec } from "./createReactMathInlineContentSpec.js";
13+
14+
const schema = BlockNoteSchema.create().extend({
15+
inlineContentSpecs: { math: createReactInlineMathSpec() },
16+
});
17+
18+
// Dead keys (`^` on AZERTY) and IME commits insert text natively, like this.
19+
function insertTextNatively(text: string) {
20+
document.execCommand("insertText", false, text);
21+
}
22+
23+
it.each([
24+
// Without the CSS line box, Chromium & WebKit insert outside the source.
25+
{ name: "an empty source", typedFirst: "", deleteNatively: false },
26+
// `MathInlinePreviewWithPopup` trims the source, so " " used to render the
27+
// React `<br>` next to non-empty DOM, which Firefox removed while inserting.
28+
{ name: "a whitespace-only source", typedFirst: " ", deleteNatively: false },
29+
// Like Option+Backspace. Chromium used to also remove the source's inline
30+
// ancestors, which tiptap ignores as outside the content DOM.
31+
{
32+
name: "a source emptied by a native deletion",
33+
typedFirst: "hello",
34+
deleteNatively: true,
35+
},
36+
])(
37+
"natively inserts text into $name (#3234)",
38+
async ({ typedFirst, deleteNatively }) => {
39+
const editor = BlockNoteEditor.create({
40+
schema,
41+
trailingBlock: false,
42+
initialContent: [{ id: "paragraph", type: "paragraph" }],
43+
});
44+
const host = document.createElement("div");
45+
document.body.append(host);
46+
const root = createRoot(host);
47+
48+
function getMathSource() {
49+
let source: string | undefined;
50+
editor.prosemirrorState.doc.descendants((node) => {
51+
if (node.type.name === "math") {
52+
source = node.textContent;
53+
}
54+
});
55+
return source;
56+
}
57+
58+
try {
59+
flushSync(() => {
60+
root.render(<BlockNoteViewRaw editor={editor} />);
61+
});
62+
editor.setTextCursorPosition("paragraph", "end");
63+
editor.focus();
64+
65+
// Inserts the equation the way the slash menu does, which also moves the
66+
// caret into its source.
67+
const item = getMathSlashMenuItems(editor).find(
68+
(item) => item.title === en.slash_menu.inline_math.title,
69+
);
70+
if (!item) {
71+
throw new Error("Inline math slash menu item not found");
72+
}
73+
item.onItemClick();
74+
// The item opens the popup in an animation frame, which headless WebKit
75+
// sometimes delays past `waitFor`'s default 1s.
76+
const pre = await vi.waitFor(
77+
() => {
78+
const pre = host.querySelector(
79+
".bn-preview-with-source-popup[data-open='true'] pre",
80+
);
81+
if (!pre) {
82+
throw new Error("The source popup isn't open yet");
83+
}
84+
return pre;
85+
},
86+
{ timeout: 5000 },
87+
);
88+
89+
if (typedFirst) {
90+
await userEvent.keyboard(typedFirst);
91+
await vi.waitFor(() => expect(getMathSource()).toBe(typedFirst));
92+
}
93+
94+
if (deleteNatively) {
95+
// Deletes the word natively (Option+Backspace on macOS; the suite runs
96+
// on Linux).
97+
await userEvent.keyboard("{Control>}{Backspace}{/Control}");
98+
await vi.waitFor(() => expect(getMathSource()).toBe(""));
99+
}
100+
101+
insertTextNatively("^");
102+
103+
const expected = (deleteNatively ? "" : typedFirst) + "^";
104+
await vi.waitFor(() =>
105+
expect(getMathSource(), pre.innerHTML).toBe(expected),
106+
);
107+
expect(editor.prosemirrorView?.dom.isConnected).toBe(true);
108+
} finally {
109+
root.unmount();
110+
editor._tiptapEditor.destroy();
111+
host.remove();
112+
}
113+
},
114+
);

‎packages/react/src/blocks/SourceWithPreview/SourceWithPreview.tsx‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,6 @@ export const SourceWithPreview = (
206206
aria-invalid={error ? true : undefined}
207207
aria-describedby={error ? errorId : undefined}
208208
/>
209-
{inline && source.length === 0 && (
210-
<br aria-hidden="true" contentEditable={false} />
211-
)}
212209
</pre>
213210
<div
214211
className="bn-code-block-source-popup-ok-button-wrapper"

0 commit comments

Comments
 (0)