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

Commit a49be6d

Browse files
committed
remove styles and links inside inline code block
1 parent d1d6fdf commit a49be6d

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

src/modifiers/__test__/changeCurrentInlineStyle-test.js

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,36 @@ describe("changeCurrentInlineStyle", () => {
4444
);
4545
expect(newEditorState).not.toEqual(editorState);
4646
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
47-
rawContentState(
48-
"foo bar baz",
49-
[
50-
{
51-
length: 3,
52-
offset: 4,
53-
style: "CODE",
54-
},
55-
],
56-
"CODE"
57-
)
47+
rawContentState("foo bar baz", [
48+
{
49+
length: 3,
50+
offset: 4,
51+
style: "CODE",
52+
},
53+
])
54+
);
55+
});
56+
it("removes inline styles when applying code style", () => {
57+
const text = "`some bold text`";
58+
const editorState = createEditorState(text, [
59+
{
60+
length: 4,
61+
offset: 6,
62+
style: "BOLD",
63+
},
64+
]);
65+
const matchArr = ["`some bold text`", "some bold text"];
66+
matchArr.index = 0;
67+
matchArr.input = text;
68+
let newEditorState = changeCurrentInlineStyle(
69+
editorState,
70+
matchArr,
71+
"CODE"
72+
);
73+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
74+
rawContentState("some bold text", [
75+
{ length: 14, offset: 0, style: "CODE" },
76+
])
5877
);
5978
});
6079
});

src/modifiers/changeCurrentInlineStyle.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
import { OrderedSet } from "immutable";
22
import { EditorState, SelectionState, Modifier } from "draft-js";
3+
import removeInlineStyles from "./removeInlineStyles";
34

45
const changeCurrentInlineStyle = (editorState, matchArr, style) => {
5-
const currentContent = editorState.getCurrentContent();
66
const selection = editorState.getSelection();
77
const key = selection.getStartKey();
88
const { index } = matchArr;
9-
const blockMap = currentContent.getBlockMap();
10-
const block = blockMap.get(key);
11-
const currentInlineStyle = block.getInlineStyleAt(index).merge();
12-
const newStyle = currentInlineStyle.merge([style]);
139
const focusOffset = index + matchArr[0].length;
1410

1511
const wordSelection = SelectionState.createEmpty(key).merge({
1612
anchorOffset: index,
1713
focusOffset,
1814
});
1915

20-
const inlineStyles = [];
16+
let newEditorState = editorState;
17+
if (style === "CODE") {
18+
newEditorState = removeInlineStyles(newEditorState, wordSelection);
19+
}
20+
const currentContent = newEditorState.getCurrentContent();
21+
2122
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
2223

2324
let newContentState = currentContent;
@@ -55,7 +56,7 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
5556
style
5657
);
5758

58-
const newEditorState = EditorState.push(
59+
newEditorState = EditorState.push(
5960
editorState,
6061
newContentState,
6162
"change-inline-style"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { EditorState, RichUtils, Modifier } from "draft-js";
2+
3+
export default (editorState, selection = editorState.getSelection()) => {
4+
const styles = ["BOLD", "ITALIC", "STRIKETHROUGH", "CODE"];
5+
6+
let newEditorState = EditorState.push(
7+
editorState,
8+
styles.reduce(
9+
(newContentState, style) =>
10+
Modifier.removeInlineStyle(newContentState, selection, style),
11+
editorState.getCurrentContent()
12+
),
13+
"change-inline-style"
14+
);
15+
16+
return RichUtils.toggleLink(newEditorState, selection, null);
17+
};

0 commit comments

Comments
 (0)