55} from "@blocknote/core" ;
66import { flushSync } from "react-dom" ;
77import { createRoot } from "react-dom/client" ;
8+ import { TextSelection } from "@tiptap/pm/state" ;
89import { expect , it } from "vite-plus/test" ;
910import { userEvent } from "vite-plus/test/browser" ;
1011
@@ -19,6 +20,119 @@ const schema = BlockNoteSchema.create({
1920 styleSpecs : { ...defaultStyleSpecs , highlight } ,
2021} ) ;
2122
23+ it . each ( [
24+ "cursor" ,
25+ "forward selection" ,
26+ "backward selection" ,
27+ "mixed selection" ,
28+ ] as const ) (
29+ "preserves the native selection when a React style mounts (%s)" ,
30+ async ( selectionMode ) => {
31+ const editor = BlockNoteEditor . create ( {
32+ schema,
33+ initialContent : [
34+ {
35+ type : "paragraph" ,
36+ content :
37+ selectionMode === "mixed selection"
38+ ? [
39+ { type : "text" , text : "al" , styles : { } } ,
40+ { type : "text" , text : "p" , styles : { bold : true } } ,
41+ { type : "text" , text : "ha" , styles : { } } ,
42+ ]
43+ : "alpha" ,
44+ } ,
45+ ] ,
46+ } ) ;
47+ const host = document . createElement ( "div" ) ;
48+ document . body . append ( host ) ;
49+ const root = createRoot ( host ) ;
50+ try {
51+ flushSync ( ( ) => root . render ( < BlockNoteViewRaw editor = { editor } /> ) ) ;
52+ const view = editor . prosemirrorView ;
53+ const [ anchor , head ] =
54+ selectionMode === "cursor"
55+ ? [ 5 , 5 ]
56+ : selectionMode !== "backward selection"
57+ ? [ 4 , 7 ]
58+ : [ 7 , 4 ] ;
59+ view . dispatch (
60+ view . state . tr . setSelection (
61+ TextSelection . create ( view . state . doc , anchor , head ) ,
62+ ) ,
63+ ) ;
64+ view . focus ( ) ;
65+ expect ( document . getSelection ( ) ?. toString ( ) ) . toBe (
66+ selectionMode === "cursor" ? "" : "lph" ,
67+ ) ;
68+ editor . addStyles ( { highlight : true } ) ;
69+
70+ // Mounting a mark must preserve both endpoints, including a backwards
71+ // selection, rather than merely retaining the selected text.
72+ await expect
73+ . poll ( ( ) => {
74+ const selection = document . getSelection ( ) ;
75+ if ( ! selection ?. anchorNode || ! selection . focusNode ) {
76+ return undefined ;
77+ }
78+ return [
79+ view . posAtDOM ( selection . anchorNode , selection . anchorOffset ) ,
80+ view . posAtDOM ( selection . focusNode , selection . focusOffset ) ,
81+ ] ;
82+ } )
83+ . toEqual ( [ anchor , head ] ) ;
84+
85+ // The first character creates a mark view for a collapsed selection.
86+ // React may commit it after ProseMirror has already placed the caret.
87+ await userEvent . keyboard ( "X" ) ;
88+ await userEvent . keyboard ( "Y" ) ;
89+
90+ expect ( editor . prosemirrorState . doc . textContent ) . toBe (
91+ selectionMode === "cursor" ? "alXYpha" : "aXYa" ,
92+ ) ;
93+ expect ( editor . getActiveStyles ( ) ) . toMatchObject ( { highlight : true } ) ;
94+ } finally {
95+ root . unmount ( ) ;
96+ editor . _tiptapEditor . destroy ( ) ;
97+ host . remove ( ) ;
98+ }
99+ } ,
100+ ) ;
101+
102+ it ( "does not refocus the editor after a React style mounts" , async ( ) => {
103+ const editor = BlockNoteEditor . create ( {
104+ schema,
105+ initialContent : [ { type : "paragraph" , content : "alpha" } ] ,
106+ } ) ;
107+ const host = document . createElement ( "div" ) ;
108+ const input = document . createElement ( "input" ) ;
109+ document . body . append ( host , input ) ;
110+ const root = createRoot ( host ) ;
111+ try {
112+ flushSync ( ( ) => root . render ( < BlockNoteViewRaw editor = { editor } /> ) ) ;
113+ editor . prosemirrorView . focus ( ) ;
114+ flushSync ( ( ) => {
115+ editor . updateBlock ( editor . document [ 0 ] , {
116+ content : [
117+ { type : "text" , text : "styled" , styles : { highlight : true } } ,
118+ ] ,
119+ } ) ;
120+ } ) ;
121+ // Focus can change between React's ref callback and reconciliation.
122+ input . focus ( ) ;
123+ await userEvent . keyboard ( "X" ) ;
124+
125+ expect ( document . activeElement ) . toBe ( input ) ;
126+ expect ( input . value ) . toBe ( "X" ) ;
127+ expect ( editor . prosemirrorState . doc . textContent ) . toBe ( "styled" ) ;
128+ } finally {
129+ root . unmount ( ) ;
130+ editor . _tiptapEditor . destroy ( ) ;
131+ host . remove ( ) ;
132+ input . remove ( ) ;
133+ }
134+ } ) ;
135+
22136it . each ( [ "initial mount" , "remount" , "API remount" ] ) (
23137 "keeps typing after text inserted with a React style (%s)" ,
24138 async ( mountMode ) => {
0 commit comments