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

Commit

Permalink
Add handling of whitespaces after the inline style change
Browse files Browse the repository at this point in the history
  • Loading branch information
Axel Wahlen committed Jun 25, 2018
1 parent 2df3609 commit 2350fb0
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/modifiers/changeCurrentInlineStyle.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,40 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
focusOffset,
});

const inlineStyles = [];
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
// check if match contains a terminator group at the end
let matchTerminatorLength = 0;
if (matchArr.length == 3) {
matchTerminatorLength = matchArr[2].length;
}

const markdownCharacterLength =
(matchArr[0].length - matchArr[1].length - matchTerminatorLength) / 2;

const inlineStyles = [];
let newContentState = currentContent;

// remove markdown delimiter at end
newContentState = Modifier.removeRange(
newContentState,
wordSelection.merge({
anchorOffset: wordSelection.getFocusOffset() - markdownCharacterLength,
anchorOffset:
wordSelection.getFocusOffset() -
markdownCharacterLength -
matchTerminatorLength,
})
);

let afterSelection = newContentState.getSelectionAfter();

afterSelection = afterSelection.merge({
anchorOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
focusOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
anchorOffset:
afterSelection.getFocusOffset() -
markdownCharacterLength -
matchTerminatorLength,
focusOffset:
afterSelection.getFocusOffset() -
markdownCharacterLength -
matchTerminatorLength,
});

// remove markdown delimiter at start
Expand All @@ -50,11 +66,22 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
newContentState,
wordSelection.merge({
anchorOffset: index,
focusOffset: focusOffset - markdownCharacterLength * 2,
focusOffset:
focusOffset - markdownCharacterLength * 2 - matchTerminatorLength,
}),
style
);

// Check if a terminator exists and re-add it after the styled text
if (matchTerminatorLength > 0) {
newContentState = Modifier.insertText(
newContentState,
afterSelection,
matchTerminatorLength
);
afterSelection = newContentState.getSelectionAfter();
}

const newEditorState = EditorState.push(
editorState,
newContentState,
Expand Down

0 comments on commit 2350fb0

Please sign in to comment.