This repository was archived by the owner on Oct 11, 2022. It is now read-only.
File tree 3 files changed +88
-2
lines changed
3 files changed +88
-2
lines changed Original file line number Diff line number Diff line change @@ -45,3 +45,35 @@ Object {
45
45
" entityMap" : Object {},
46
46
}
47
47
` ;
48
+
49
+ exports [` markdown should not have sticky inline styles after the line ending with styles 1` ] = `
50
+ Object {
51
+ " blocks" : Array [
52
+ Object {
53
+ " data" : Object {},
54
+ " depth" : 0 ,
55
+ " entityRanges" : Array [],
56
+ " inlineStyleRanges" : Array [
57
+ Object {
58
+ " length" : 4 ,
59
+ " offset" : 5 ,
60
+ " style" : " BOLD" ,
61
+ },
62
+ ],
63
+ " key" : " item1" ,
64
+ " text" : " Some text" ,
65
+ " type" : " unstyled" ,
66
+ },
67
+ Object {
68
+ " data" : Object {},
69
+ " depth" : 0 ,
70
+ " entityRanges" : Array [],
71
+ " inlineStyleRanges" : Array [],
72
+ " key" : " item2" ,
73
+ " text" : " a" ,
74
+ " type" : " unstyled" ,
75
+ },
76
+ ],
77
+ " entityMap" : Object {},
78
+ }
79
+ `
Original file line number Diff line number Diff line change @@ -111,4 +111,49 @@ describe("markdown", () => {
111
111
expect ( raw . blocks [ 0 ] . inlineStyleRanges [ 0 ] ) . toEqual ( boldInlineStyleRange ) ;
112
112
expect ( raw ) . toMatchSnapshot ( ) ;
113
113
} ) ;
114
+
115
+ it ( "should not have sticky inline styles after the line ending with styles" , ( ) => {
116
+ const { handleBeforeInput } = createMarkdownPlugin ( ) ;
117
+ const setEditorState = jest . fn ( ) ;
118
+ const boldInlineStyleRange = {
119
+ length : 4 ,
120
+ offset : 5 ,
121
+ style : "BOLD" ,
122
+ } ;
123
+ const before = EditorState . moveSelectionToEnd (
124
+ EditorState . createWithContent (
125
+ Draft . convertFromRaw ( {
126
+ entityMap : { } ,
127
+ blocks : [
128
+ {
129
+ key : "item1" ,
130
+ text : "Some text" ,
131
+ type : "unstyled" ,
132
+ depth : 0 ,
133
+ inlineStyleRanges : [ boldInlineStyleRange ] ,
134
+ entityRanges : [ ] ,
135
+ data : { } ,
136
+ } ,
137
+ {
138
+ key : "item2" ,
139
+ text : "" ,
140
+ type : "unstyled" ,
141
+ depth : 0 ,
142
+ inlineStyleRanges : [ ] ,
143
+ entityRanges : [ ] ,
144
+ data : { } ,
145
+ } ,
146
+ ] ,
147
+ } )
148
+ )
149
+ ) ;
150
+ expect ( handleBeforeInput ( "a" , before , { setEditorState } ) ) . toEqual (
151
+ "handled"
152
+ ) ;
153
+ const raw = convertToRaw (
154
+ setEditorState . mock . calls [ 0 ] [ 0 ] . getCurrentContent ( )
155
+ ) ;
156
+ expect ( raw . blocks [ 0 ] . inlineStyleRanges [ 0 ] ) . toEqual ( boldInlineStyleRange ) ;
157
+ expect ( raw ) . toMatchSnapshot ( ) ;
158
+ } ) ;
114
159
} ) ;
Original file line number Diff line number Diff line change @@ -209,13 +209,22 @@ const unstickyInlineStyles = (character, editorState) => {
209
209
const startOffset = selection . getStartOffset ( ) ;
210
210
const content = editorState . getCurrentContent ( ) ;
211
211
const block = content . getBlockForKey ( selection . getStartKey ( ) ) ;
212
+ const previousBlock = content . getBlockBefore ( block . getKey ( ) ) ;
212
213
const entity = block . getEntityAt ( startOffset - 1 ) ;
213
214
if ( entity !== null ) return editorState ;
214
215
215
216
// If we're currently in a style, but the next character doesn't have a style (or doesn't exist)
216
217
// we insert the characters manually without the inline style to "unsticky" them
217
- const style = block . getInlineStyleAt ( startOffset - 1 ) ;
218
- if ( style . size === 0 ) return editorState ;
218
+ if ( ! startOffset && previousBlock ) {
219
+ // If we're in the beginning of the line we have to check styles of the previous block
220
+ const previousBlockStyle = previousBlock . getInlineStyleAt (
221
+ previousBlock . getText ( ) . length - 1
222
+ ) ;
223
+ if ( previousBlockStyle . size === 0 ) return editorState ;
224
+ } else {
225
+ const style = block . getInlineStyleAt ( startOffset - 1 ) ;
226
+ if ( style . size === 0 ) return editorState ;
227
+ }
219
228
const nextStyle = block . getInlineStyleAt ( startOffset ) ;
220
229
if ( nextStyle . size !== 0 ) return editorState ;
221
230
You can’t perform that action at this time.
0 commit comments