@@ -3,7 +3,7 @@ import { closeHistory, redo, undo } from "@tiptap/pm/history";
33import { splitBlock } from "@tiptap/pm/commands" ;
44import { TextSelection } from "@tiptap/pm/state" ;
55import StarterKit from "@tiptap/starter-kit" ;
6- import { describe , expect , it , onTestFinished } from "vite-plus/test" ;
6+ import { describe , expect , it , onTestFinished , vi } from "vite-plus/test" ;
77
88import {
99 buildDocJson ,
@@ -62,6 +62,14 @@ function createEditor(value: string) {
6262 return editor ;
6363}
6464
65+ function expectViewMatchesMarkdown ( editor : Editor ) {
66+ const markdown = serializeEditorDoc ( editor . state . doc ) . value ;
67+ const rebuilt = editor . schema . nodeFromJSON (
68+ buildDocJson ( markdown , ( name ) => ( { label : name , description : null } ) ) ,
69+ ) ;
70+ expect ( editor . state . doc . toString ( ) ) . toBe ( rebuilt . toString ( ) ) ;
71+ }
72+
6573function press ( value : string , spec : string ) {
6674 const editor = createEditor ( value ) ;
6775 const event = chord ( spec ) ;
@@ -115,27 +123,32 @@ describe("composer list normalization", () => {
115123 expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "bulletList" ) ;
116124 expect ( editor . state . doc . firstChild ?. firstChild ?. textContent ) . toBe ( "get md" ) ;
117125 expect ( serializeEditorDoc ( editor . state . doc ) . value ) . toBe ( "- get md\n" ) ;
118- expect (
119- editor . state . doc . eq (
120- editor . schema . nodeFromJSON (
121- buildDocJson ( serializeEditorDoc ( editor . state . doc ) . value , ( name ) => ( {
122- label : name ,
123- description : null ,
124- } ) ) ,
125- ) ,
126- ) ,
127- ) . toBe ( true ) ;
126+ expectViewMatchesMarkdown ( editor ) ;
128127 } ) ;
129128
130- it ( "converts a dash and space typed before existing text" , ( ) => {
131- const editor = createEditor ( "existing text" ) ;
132- editor . view . dispatch ( editor . state . tr . setSelection ( TextSelection . atStart ( editor . state . doc ) ) ) ;
133- editor . view . dispatch ( editor . state . tr . insertText ( "-" ) ) ;
134- expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "paragraph" ) ;
135- editor . view . dispatch ( editor . state . tr . insertText ( " " ) ) ;
136- expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "bulletList" ) ;
137- expect ( editor . state . selection . $from . parentOffset ) . toBe ( 0 ) ;
138- expect ( serializeEditorDoc ( editor . state . doc ) . value ) . toBe ( "- existing text" ) ;
129+ it . each ( [
130+ [ "- a\nb\n- c" , "- " ] ,
131+ [ "* a\nb" , "* " ] ,
132+ [ "1. a\nb" , "2. " ] ,
133+ [ "- [ ] a\nb" , "- [ ] " ] ,
134+ ] ) ( "joins the converted line into the neighbouring list in %j" , ( value , marker ) => {
135+ const editor = createEditor ( value ) ;
136+ let paragraph = 0 ;
137+ editor . state . doc . forEach ( ( node , pos ) => {
138+ if ( node . type . name === "paragraph" ) paragraph = pos + 1 ;
139+ } ) ;
140+ editor . view . dispatch ( editor . state . tr . insertText ( marker , paragraph ) ) ;
141+ expect ( editor . state . doc . childCount ) . toBe ( 1 ) ;
142+ expectViewMatchesMarkdown ( editor ) ;
143+ } ) ;
144+
145+ it ( "keeps a converted line apart from a list with another marker" , ( ) => {
146+ const editor = createEditor ( "* a\nb" ) ;
147+ editor . view . dispatch (
148+ editor . state . tr . insertText ( "- " , editor . state . doc . firstChild ! . nodeSize + 1 ) ,
149+ ) ;
150+ expect ( editor . state . doc . childCount ) . toBe ( 2 ) ;
151+ expectViewMatchesMarkdown ( editor ) ;
139152 } ) ;
140153
141154 it . each ( [ "[link](url)" , "[ordinary text]" , "[x]text" ] ) (
@@ -192,12 +205,6 @@ describe("composer list normalization", () => {
192205 expect ( editor . state . doc . firstChild ?. firstChild ?. attrs . checked ) . toBe ( true ) ;
193206 } ) ;
194207
195- it ( "normalizes pasted markers without waiting for another keystroke" , ( ) => {
196- const editor = createEditor ( "existing text" ) ;
197- editor . view . dispatch ( editor . state . tr . insertText ( "- " , 1 ) . setMeta ( "uiEvent" , "paste" ) ) ;
198- expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "bulletList" ) ;
199- } ) ;
200-
201208 it . each ( [ "```\ncode\n```" , "- outer\n - inner" , "> quoted" ] ) (
202209 "does not convert paragraphs inside %j" ,
203210 ( value ) => {
@@ -228,7 +235,18 @@ describe("composer list normalization", () => {
228235 expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "paragraph" ) ;
229236 editor . view . dispatch ( editor . state . tr . insertText ( "確定" , 3 , 6 ) . setMeta ( "composition" , 1 ) ) ;
230237 expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "paragraph" ) ;
231- editor . view . dispatch ( editor . state . tr . setSelection ( TextSelection . atEnd ( editor . state . doc ) ) ) ;
238+ vi . useFakeTimers ( ) ;
239+ onTestFinished ( ( ) => {
240+ vi . useRealTimers ( ) ;
241+ } ) ;
242+ for ( const plugin of editor . state . plugins ) {
243+ plugin . props . handleDOMEvents ?. compositionend ?. call (
244+ plugin ,
245+ editor . view ,
246+ new Event ( "compositionend" ) as CompositionEvent ,
247+ ) ;
248+ }
249+ vi . runAllTimers ( ) ;
232250 expect ( editor . state . doc . firstChild ?. type . name ) . toBe ( "bulletList" ) ;
233251 expect ( serializeEditorDoc ( editor . state . doc ) . value ) . toBe ( "- 確定" ) ;
234252 expect ( undo ( editor . state , editor . view . dispatch ) ) . toBe ( true ) ;
0 commit comments