|
| 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 | +); |
0 commit comments