Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 2350fb0

Browse files
author
Axel Wahlen
committed
Add handling of whitespaces after the inline style change
1 parent 2df3609 commit 2350fb0

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

src/modifiers/changeCurrentInlineStyle.js

+33-6
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,40 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
1717
focusOffset,
1818
});
1919

20-
const inlineStyles = [];
21-
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
20+
// check if match contains a terminator group at the end
21+
let matchTerminatorLength = 0;
22+
if (matchArr.length == 3) {
23+
matchTerminatorLength = matchArr[2].length;
24+
}
25+
26+
const markdownCharacterLength =
27+
(matchArr[0].length - matchArr[1].length - matchTerminatorLength) / 2;
2228

29+
const inlineStyles = [];
2330
let newContentState = currentContent;
2431

2532
// remove markdown delimiter at end
2633
newContentState = Modifier.removeRange(
2734
newContentState,
2835
wordSelection.merge({
29-
anchorOffset: wordSelection.getFocusOffset() - markdownCharacterLength,
36+
anchorOffset:
37+
wordSelection.getFocusOffset() -
38+
markdownCharacterLength -
39+
matchTerminatorLength,
3040
})
3141
);
3242

3343
let afterSelection = newContentState.getSelectionAfter();
3444

3545
afterSelection = afterSelection.merge({
36-
anchorOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
37-
focusOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
46+
anchorOffset:
47+
afterSelection.getFocusOffset() -
48+
markdownCharacterLength -
49+
matchTerminatorLength,
50+
focusOffset:
51+
afterSelection.getFocusOffset() -
52+
markdownCharacterLength -
53+
matchTerminatorLength,
3854
});
3955

4056
// remove markdown delimiter at start
@@ -50,11 +66,22 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
5066
newContentState,
5167
wordSelection.merge({
5268
anchorOffset: index,
53-
focusOffset: focusOffset - markdownCharacterLength * 2,
69+
focusOffset:
70+
focusOffset - markdownCharacterLength * 2 - matchTerminatorLength,
5471
}),
5572
style
5673
);
5774

75+
// Check if a terminator exists and re-add it after the styled text
76+
if (matchTerminatorLength > 0) {
77+
newContentState = Modifier.insertText(
78+
newContentState,
79+
afterSelection,
80+
matchTerminatorLength
81+
);
82+
afterSelection = newContentState.getSelectionAfter();
83+
}
84+
5885
const newEditorState = EditorState.push(
5986
editorState,
6087
newContentState,

0 commit comments

Comments
 (0)