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

Commit 034db23

Browse files
Merge branch 'master' into disable-inline-styles-in-link
2 parents 8df2a8d + 5b52fde commit 034db23

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

src/constants.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export const CODE_BLOCK_REGEX = /^```([\w-]+)?\s*$/;
22

33
export const inlineMatchers = {
4-
BOLD: [/\*\*([^(?**)]+)\*\*/g, /__([^(?:__)]+)__/g],
5-
ITALIC: [/\*([^*]+)\*/g, /_([^_]+)_/g],
6-
CODE: [/`([^`]+)`/g],
7-
STRIKETHROUGH: [/~~([^(?:~~)]+)~~/g],
4+
BOLD: [/\*\*([^(?**)]+)\*\*$/g, /__([^(?:__)]+)__$/g],
5+
ITALIC: [/\*([^*]+)\*$/g, /_([^_]+)_$/g],
6+
CODE: [/`([^`]+)`$/g],
7+
STRIKETHROUGH: [/~~([^(?:~~)]+)~~$/g],
88
};

src/modifiers/__test__/handleInlineStyle-test.js

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ describe("handleInlineStyle", () => {
7979
},
8080
selection: new SelectionState({
8181
anchorKey: "item1",
82-
anchorOffset: 14,
82+
anchorOffset: 16,
8383
focusKey: "item1",
84-
focusOffset: 14,
84+
focusOffset: 16,
8585
isBackward: false,
8686
hasFocus: true,
8787
}),
@@ -123,9 +123,9 @@ describe("handleInlineStyle", () => {
123123
},
124124
selection: new SelectionState({
125125
anchorKey: "item1",
126-
anchorOffset: 14,
126+
anchorOffset: 16,
127127
focusKey: "item1",
128-
focusOffset: 14,
128+
focusOffset: 16,
129129
isBackward: false,
130130
hasFocus: true,
131131
}),
@@ -167,9 +167,9 @@ describe("handleInlineStyle", () => {
167167
},
168168
selection: new SelectionState({
169169
anchorKey: "item1",
170-
anchorOffset: 14,
170+
anchorOffset: 16,
171171
focusKey: "item1",
172-
focusOffset: 14,
172+
focusOffset: 16,
173173
isBackward: false,
174174
hasFocus: true,
175175
}),
@@ -310,9 +310,9 @@ describe("handleInlineStyle", () => {
310310
},
311311
selection: new SelectionState({
312312
anchorKey: "item1",
313-
anchorOffset: 14,
313+
anchorOffset: 16,
314314
focusKey: "item1",
315-
focusOffset: 14,
315+
focusOffset: 16,
316316
isBackward: false,
317317
hasFocus: true,
318318
}),
@@ -398,9 +398,9 @@ describe("handleInlineStyle", () => {
398398
},
399399
selection: new SelectionState({
400400
anchorKey: "item1",
401-
anchorOffset: 14,
401+
anchorOffset: 16,
402402
focusKey: "item1",
403-
focusOffset: 14,
403+
focusOffset: 16,
404404
isBackward: false,
405405
hasFocus: true,
406406
}),
@@ -456,9 +456,9 @@ describe("handleInlineStyle", () => {
456456
},
457457
selection: new SelectionState({
458458
anchorKey: "item1",
459-
anchorOffset: 14,
459+
anchorOffset: 16,
460460
focusKey: "item1",
461-
focusOffset: 14,
461+
focusOffset: 16,
462462
isBackward: false,
463463
hasFocus: true,
464464
}),
@@ -529,7 +529,25 @@ describe("handleInlineStyle", () => {
529529
EditorState.createWithContent(contentState),
530530
selection
531531
);
532-
it("converts block type", () => {
532+
533+
const wrongSelectionState = selection.merge({
534+
anchorOffset: 0,
535+
focusOffset: 0,
536+
});
537+
const sameEditorState = EditorState.forceSelection(
538+
editorState,
539+
wrongSelectionState
540+
);
541+
542+
it("does not convert markdown to style or block type if selection is at the wrong place", () => {
543+
const newEditorState = handleInlineStyle(sameEditorState, character);
544+
expect(newEditorState).toEqual(sameEditorState);
545+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
546+
before
547+
);
548+
});
549+
550+
it("converts markdown to style or block type", () => {
533551
const newEditorState = handleInlineStyle(editorState, character);
534552
expect(newEditorState).not.toEqual(editorState);
535553
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(

src/modifiers/handleInlineStyle.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import changeCurrentInlineStyle from "./changeCurrentInlineStyle";
22
import { inlineMatchers } from "../constants";
33

44
const handleInlineStyle = (editorState, character) => {
5+
const selection = editorState.getSelection();
56
const key = editorState.getSelection().getStartKey();
67
const text = editorState
78
.getCurrentContent()
89
.getBlockForKey(key)
9-
.getText();
10+
.getText()
11+
.slice(0, selection.getFocusOffset());
1012

11-
const line = `${text}${character}`;
13+
const line = `${text}`;
1214
let newEditorState = editorState;
1315

16+
var i = 0;
17+
1418
Object.keys(inlineMatchers).some(k => {
1519
inlineMatchers[k].some(re => {
1620
let matchArr;
@@ -24,6 +28,7 @@ const handleInlineStyle = (editorState, character) => {
2428
character
2529
);
2630
}
31+
i++;
2732
} while (matchArr);
2833
return newEditorState !== editorState;
2934
});

0 commit comments

Comments
 (0)