@@ -31,6 +31,7 @@ import {
31
31
$getSelection ,
32
32
$getSiblingCaret ,
33
33
$getTextPointCaret ,
34
+ $isParagraphNode ,
34
35
$isSiblingCaret ,
35
36
$isTextNode ,
36
37
$isTextPointCaret ,
@@ -41,6 +42,7 @@ import {
41
42
$setPointFromCaret ,
42
43
$setSelection ,
43
44
$setSelectionFromCaretRange ,
45
+ $splitAtPointCaretNext ,
44
46
ChildCaret ,
45
47
ElementNode ,
46
48
LexicalNode ,
@@ -1947,3 +1949,65 @@ describe('LexicalSelectionHelpers', () => {
1947
1949
} ) ;
1948
1950
} ) ;
1949
1951
} ) ;
1952
+
1953
+ describe ( '$splitAtPointCaretNext' , ( ) => {
1954
+ initializeUnitTest ( ( testEnv ) => {
1955
+ test ( 'Does not split a TextNode at the beginning' , ( ) => {
1956
+ testEnv . editor . update (
1957
+ ( ) => {
1958
+ const textNode = $createTextNode ( 'test' ) ;
1959
+ const paragraphNode = $createParagraphNode ( ) ;
1960
+ $getRoot ( ) . clear ( ) . append ( paragraphNode . append ( textNode ) ) ;
1961
+ const caret = $getTextPointCaret ( textNode , 'next' , 0 ) ;
1962
+ const after = $splitAtPointCaretNext ( caret ) ;
1963
+ expect ( textNode . getTextContent ( ) ) . toEqual ( 'test' ) ;
1964
+ expect (
1965
+ $getChildCaret ( paragraphNode , 'next' ) . isSamePointCaret ( after ) ,
1966
+ ) . toBe ( true ) ;
1967
+ } ,
1968
+ { discrete : true } ,
1969
+ ) ;
1970
+ } ) ;
1971
+ test ( 'Splits a TextNode in the middle' , ( ) => {
1972
+ testEnv . editor . update (
1973
+ ( ) => {
1974
+ const textNode = $createTextNode ( 'test' ) ;
1975
+ const paragraphNode = $createParagraphNode ( ) ;
1976
+ $getRoot ( ) . clear ( ) . append ( paragraphNode . append ( textNode ) ) ;
1977
+ const caret = $getTextPointCaret ( textNode , 'next' , 2 ) ;
1978
+ const after = $splitAtPointCaretNext ( caret ) ;
1979
+ expect ( textNode . getTextContent ( ) ) . toEqual ( 'te' ) ;
1980
+ const nextCaret = $getSiblingCaret ( textNode , 'next' ) ;
1981
+ expect ( nextCaret . isSamePointCaret ( after ) ) . toBe ( true ) ;
1982
+ const splitNode = nextCaret . getNodeAtCaret ( ) ;
1983
+ expect (
1984
+ $isTextNode ( splitNode ) ? splitNode . getTextContent ( ) : null ,
1985
+ ) . toEqual ( 'st' ) ;
1986
+ } ,
1987
+ { discrete : true } ,
1988
+ ) ;
1989
+ } ) ;
1990
+ test ( 'Splits a ParagraphNode' , ( ) => {
1991
+ testEnv . editor . update (
1992
+ ( ) => {
1993
+ const beforeTextNode = $createTextNode ( 'before' ) ;
1994
+ const afterTextNode = $createTextNode ( 'after' ) ;
1995
+ const paragraphNode = $createParagraphNode ( ) ;
1996
+ $getRoot ( )
1997
+ . clear ( )
1998
+ . append ( paragraphNode . append ( beforeTextNode , afterTextNode ) ) ;
1999
+ const caret = $getSiblingCaret ( beforeTextNode , 'next' ) ;
2000
+ const after = $splitAtPointCaretNext ( caret ) ;
2001
+ expect ( paragraphNode . getAllTextNodes ( ) ) . toEqual ( [ beforeTextNode ] ) ;
2002
+ const nextCaret = $getSiblingCaret ( paragraphNode , 'next' ) ;
2003
+ expect ( nextCaret . isSamePointCaret ( after ) ) . toBe ( true ) ;
2004
+ const splitNode = nextCaret . getNodeAtCaret ( ) ;
2005
+ expect (
2006
+ $isParagraphNode ( splitNode ) ? splitNode . getAllTextNodes ( ) : null ,
2007
+ ) . toEqual ( [ afterTextNode ] ) ;
2008
+ } ,
2009
+ { discrete : true } ,
2010
+ ) ;
2011
+ } ) ;
2012
+ } ) ;
2013
+ } ) ;
0 commit comments