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

Commit 77cafda

Browse files
authored
Merge pull request #97 from onebar/remove-styles-before-code
remove styles and links inside inline code block
2 parents d1d6fdf + aa7a23d commit 77cafda

File tree

4 files changed

+108
-39
lines changed

4 files changed

+108
-39
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/__test__/handleInlineStyle-test.js

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ describe("handleInlineStyle", () => {
4747
});
4848

4949
const testCases = {
50-
"converts a mix of code, bold and italic and strikethrough in one go": {
51-
character: "`",
50+
"converts a mix of bold and italic and strikethrough in one go": {
51+
character: "*",
5252
before: {
5353
entityMap: {},
5454
blocks: [
5555
{
5656
key: "item1",
57-
text: "`h~el*lo _inline~_* style",
57+
text: "*h~ello _inline~_ style",
5858
type: "unstyled",
5959
depth: 0,
6060
inlineStyleRanges: [],
@@ -72,32 +72,55 @@ describe("handleInlineStyle", () => {
7272
type: "unstyled",
7373
depth: 0,
7474
inlineStyleRanges: [
75-
{
76-
length: 12,
77-
offset: 0,
78-
style: "CODE",
79-
},
80-
{
81-
length: 11,
82-
offset: 1,
83-
style: "STRIKETHROUGH",
84-
},
85-
{
86-
length: 9,
87-
offset: 3,
88-
style: "BOLD",
89-
},
90-
{
91-
length: 6,
92-
offset: 6,
93-
style: "ITALIC",
94-
},
75+
{ length: 12, offset: 0, style: "BOLD" },
76+
{ length: 11, offset: 1, style: "STRIKETHROUGH" },
77+
{ length: 6, offset: 6, style: "ITALIC" },
9578
],
9679
entityRanges: [],
9780
data: {},
9881
},
9982
],
10083
},
84+
selection: new SelectionState({
85+
anchorKey: "item1",
86+
anchorOffset: 17,
87+
focusKey: "item1",
88+
focusOffset: 17,
89+
isBackward: false,
90+
hasFocus: true,
91+
}),
92+
},
93+
94+
"should not covert inside the code style": {
95+
character: "`",
96+
before: {
97+
entityMap: {},
98+
blocks: [
99+
{
100+
key: "item1",
101+
text: "`h~el*lo _inline~_* style",
102+
type: "unstyled",
103+
depth: 0,
104+
inlineStyleRanges: [],
105+
entityRanges: [],
106+
data: {},
107+
},
108+
],
109+
},
110+
after: {
111+
entityMap: {},
112+
blocks: [
113+
{
114+
key: "item1",
115+
text: "h~el*lo _inline~_* style",
116+
type: "unstyled",
117+
depth: 0,
118+
inlineStyleRanges: [{ length: 18, offset: 0, style: "CODE" }],
119+
entityRanges: [],
120+
data: {},
121+
},
122+
],
123+
},
101124
selection: new SelectionState({
102125
anchorKey: "item1",
103126
anchorOffset: 19,

src/modifiers/changeCurrentInlineStyle.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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) => {
56
const currentContent = editorState.getCurrentContent();
@@ -8,19 +9,28 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
89
const { index } = matchArr;
910
const blockMap = currentContent.getBlockMap();
1011
const block = blockMap.get(key);
11-
const currentInlineStyle = block.getInlineStyleAt(index).merge();
12-
const newStyle = currentInlineStyle.merge([style]);
12+
const currentInlineStyle = block.getInlineStyleAt(index);
13+
// do not modify the text if it is inside code style
14+
const hasCodeStyle = currentInlineStyle.find(style => style === "CODE");
15+
if (hasCodeStyle) {
16+
return editorState;
17+
}
1318
const focusOffset = index + matchArr[0].length;
1419

1520
const wordSelection = SelectionState.createEmpty(key).merge({
1621
anchorOffset: index,
1722
focusOffset,
1823
});
1924

20-
const inlineStyles = [];
25+
let newEditorState = editorState;
26+
// remove all styles if applying code style
27+
if (style === "CODE") {
28+
newEditorState = removeInlineStyles(newEditorState, wordSelection);
29+
}
30+
2131
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
2232

23-
let newContentState = currentContent;
33+
let newContentState = newEditorState.getCurrentContent();
2434

2535
// remove markdown delimiter at end
2636
newContentState = Modifier.removeRange(
@@ -55,7 +65,7 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
5565
style
5666
);
5767

58-
const newEditorState = EditorState.push(
68+
newEditorState = EditorState.push(
5969
editorState,
6070
newContentState,
6171
"change-inline-style"

src/modifiers/removeInlineStyles.js

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)