|
| 1 | +import { BlockNoteEditor, type PartialBlock } from "@blocknote/core"; |
| 2 | +import { SideMenuExtension } from "@blocknote/core/extensions"; |
| 3 | +import "@blocknote/core/fonts/inter.css"; |
| 4 | +import { BlockNoteView } from "@blocknote/mantine"; |
| 5 | +import "@blocknote/mantine/style.css"; |
| 6 | +import { useCreateBlockNote } from "@blocknote/react"; |
| 7 | +import { afterEach, describe, expect, test } from "vite-plus/test"; |
| 8 | +import { cleanup, render } from "vitest-browser-react"; |
| 9 | + |
| 10 | +import { DRAG_HANDLE_SELECTOR } from "../../utils/const.js"; |
| 11 | +import { sleep, waitForSelector } from "../../utils/editor.js"; |
| 12 | +import { getRect, mouseSequence } from "../../utils/mouse.js"; |
| 13 | + |
| 14 | +// Two editors stacked on the same grid cell (#2559), e.g. an editor in a |
| 15 | +// dialog over another one. The top editor is editable; the bottom one is |
| 16 | +// read-only or editable. The side menu and drops must go to the top editor. |
| 17 | + |
| 18 | +const editors: Record<string, BlockNoteEditor<any, any, any>> = {}; |
| 19 | + |
| 20 | +function Editor(props: { name: string; ids: string[]; editable: boolean }) { |
| 21 | + const editor = useCreateBlockNote({ |
| 22 | + initialContent: props.ids.map( |
| 23 | + (id) => |
| 24 | + ({ |
| 25 | + id, |
| 26 | + type: "paragraph", |
| 27 | + content: `${props.name} ${id}`, |
| 28 | + }) as PartialBlock, |
| 29 | + ), |
| 30 | + }); |
| 31 | + editors[props.name] = editor; |
| 32 | + return <BlockNoteView editor={editor} editable={props.editable} />; |
| 33 | +} |
| 34 | + |
| 35 | +function Overlapping(props: { bottomEditable: boolean }) { |
| 36 | + return ( |
| 37 | + <div style={{ display: "grid", width: 600 }}> |
| 38 | + <div style={{ gridArea: "1 / 1" }} data-editor-name="bottom"> |
| 39 | + <Editor |
| 40 | + name="bottom" |
| 41 | + ids={["u1", "u2", "u3"]} |
| 42 | + editable={props.bottomEditable} |
| 43 | + /> |
| 44 | + </div> |
| 45 | + <div |
| 46 | + style={{ gridArea: "1 / 1", zIndex: 1, background: "white" }} |
| 47 | + data-editor-name="top" |
| 48 | + > |
| 49 | + <Editor name="top" ids={["t1", "t2", "t3"]} editable={true} /> |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +afterEach(async () => { |
| 56 | + await mouseSequence([{ type: "move", x: 1, y: 1 }]); |
| 57 | + cleanup(); |
| 58 | +}); |
| 59 | + |
| 60 | +function menuBlock(name: string): string | undefined { |
| 61 | + const state = editors[name].getExtension(SideMenuExtension)?.store.state; |
| 62 | + return state?.show ? state.block.id : undefined; |
| 63 | +} |
| 64 | + |
| 65 | +function blockRect(name: string, id: string) { |
| 66 | + return document |
| 67 | + .querySelector( |
| 68 | + `[data-editor-name="${name}"] .bn-block[data-id="${id}"] .bn-block-content`, |
| 69 | + )! |
| 70 | + .getBoundingClientRect(); |
| 71 | +} |
| 72 | + |
| 73 | +for (const bottomEditable of [false, true]) { |
| 74 | + describe(`overlapping editors, bottom editor ${bottomEditable ? "editable" : "read-only"}`, () => { |
| 75 | + test("hover: the top editor shows its menu", async () => { |
| 76 | + await render(<Overlapping bottomEditable={bottomEditable} />); |
| 77 | + await waitForSelector(".bn-editor"); |
| 78 | + const r = blockRect("top", "t2"); |
| 79 | + await mouseSequence([ |
| 80 | + { type: "move", x: r.x + 30, y: r.y + r.height / 2, steps: 8 }, |
| 81 | + ]); |
| 82 | + await sleep(150); |
| 83 | + expect(menuBlock("top")).toBe("t2"); |
| 84 | + expect(menuBlock("bottom")).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + test("drag and drop: a block of the top editor moves within it", async () => { |
| 88 | + await render(<Overlapping bottomEditable={bottomEditable} />); |
| 89 | + await waitForSelector(".bn-editor"); |
| 90 | + const r3 = blockRect("top", "t3"); |
| 91 | + await mouseSequence([ |
| 92 | + { type: "move", x: r3.x + 30, y: r3.y + r3.height / 2, steps: 8 }, |
| 93 | + ]); |
| 94 | + await sleep(150); |
| 95 | + const handle = getRect(await waitForSelector(DRAG_HANDLE_SELECTOR)); |
| 96 | + const t1 = blockRect("top", "t1"); |
| 97 | + await mouseSequence([ |
| 98 | + { |
| 99 | + type: "move", |
| 100 | + x: handle.x + handle.width / 2, |
| 101 | + y: handle.y + handle.height / 2, |
| 102 | + steps: 5, |
| 103 | + }, |
| 104 | + ]); |
| 105 | + await mouseSequence([{ type: "down" }]); |
| 106 | + await sleep(100); |
| 107 | + await mouseSequence([ |
| 108 | + { type: "move", x: t1.x + 30, y: t1.y + 2, steps: 5 }, |
| 109 | + ]); |
| 110 | + await sleep(100); |
| 111 | + await mouseSequence([{ type: "up" }]); |
| 112 | + await sleep(150); |
| 113 | + expect(editors.top.document.map((b) => b.id)).toEqual(["t3", "t1", "t2"]); |
| 114 | + expect(editors.bottom.document.map((b) => b.id)).toEqual([ |
| 115 | + "u1", |
| 116 | + "u2", |
| 117 | + "u3", |
| 118 | + ]); |
| 119 | + }); |
| 120 | + }); |
| 121 | +} |
0 commit comments