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

Commit 871f3dd

Browse files
authored
Merge pull request #33 from juliankrispel/maintain-sub-styles
Handle inline style changes without removing existing ones
2 parents 3bb3ec8 + a978fa8 commit 871f3dd

File tree

6 files changed

+65
-60
lines changed

6 files changed

+65
-60
lines changed

.eslintignore

Lines changed: 0 additions & 4 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/constants.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11
export const CODE_BLOCK_REGEX = /^```([\w-]+)?\s*$/;
2+
3+
export const inlineMatchers = {
4+
BOLD: [/\*\*([^(?**)]+)\*\*/g, /__([^(?:__)]+)__/g],
5+
ITALIC: [/\*([^*]+)\*/g, /_([^_]+)_/g],
6+
CODE: [/`([^`]+)`/g],
7+
STRIKETHROUGH: [/~~([^(?:~~)]+)~~/g],
8+
};

src/modifiers/__test__/handleInlineStyle-test.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ describe("handleInlineStyle", () => {
274274
inlineStyleRanges: [
275275
{
276276
length: 3,
277-
offset: 5,
277+
offset: 2,
278278
style: "ITALIC",
279279
},
280280
],
@@ -293,8 +293,8 @@ describe("handleInlineStyle", () => {
293293
depth: 0,
294294
inlineStyleRanges: [
295295
{
296-
length: 7, // FIXME
297-
offset: 5,
296+
length: 3,
297+
offset: 2,
298298
style: "ITALIC",
299299
},
300300
{
@@ -419,7 +419,7 @@ describe("handleInlineStyle", () => {
419419
depth: 0,
420420
inlineStyleRanges: [
421421
{
422-
length: 3,
422+
length: 4,
423423
offset: 5,
424424
style: "ITALIC",
425425
},
@@ -439,7 +439,7 @@ describe("handleInlineStyle", () => {
439439
depth: 0,
440440
inlineStyleRanges: [
441441
{
442-
length: 7, // FIXME
442+
length: 2,
443443
offset: 5,
444444
style: "ITALIC",
445445
},
@@ -470,12 +470,12 @@ describe("handleInlineStyle", () => {
470470
blocks: [
471471
{
472472
key: "item1",
473-
text: "hello __inline__ style",
473+
text: "hello _inline_ style",
474474
type: "unstyled",
475475
depth: 0,
476476
inlineStyleRanges: [
477477
{
478-
length: 3,
478+
length: 5,
479479
offset: 5,
480480
style: "BOLD",
481481
},
@@ -495,14 +495,15 @@ describe("handleInlineStyle", () => {
495495
depth: 0,
496496
inlineStyleRanges: [
497497
{
498-
length: 7, // FIXME
498+
length: 4,
499499
offset: 5,
500500
style: "BOLD",
501-
} /* { FIXME
502-
length: 6,
503-
offset: 6,
504-
style: 'ITALIC'
505-
} */,
501+
},
502+
{
503+
length: 6,
504+
offset: 6,
505+
style: "ITALIC",
506+
},
506507
],
507508
entityRanges: [],
508509
data: {},

src/modifiers/changeCurrentInlineStyle.js

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,59 @@ const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
1010
const currentInlineStyle = block.getInlineStyleAt(index).merge();
1111
const newStyle = currentInlineStyle.merge([style]);
1212
const focusOffset = index + matchArr[0].length;
13+
1314
const wordSelection = SelectionState.createEmpty(key).merge({
1415
anchorOffset: index,
1516
focusOffset,
1617
});
17-
let newContentState = Modifier.replaceText(
18-
currentContent,
19-
wordSelection,
20-
matchArr[1],
21-
newStyle
22-
);
23-
newContentState = Modifier.insertText(
18+
19+
const inlineStyles = [];
20+
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
21+
22+
let newContentState = currentContent;
23+
24+
// remove markdown delimiter at end
25+
newContentState = Modifier.replaceText(
2426
newContentState,
25-
newContentState.getSelectionAfter(),
27+
wordSelection.merge({
28+
anchorOffset: wordSelection.getFocusOffset() - markdownCharacterLength,
29+
}),
2630
character || " "
2731
);
32+
33+
let afterSelection = newContentState.getSelectionAfter();
34+
35+
afterSelection = afterSelection.merge({
36+
anchorOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
37+
focusOffset: afterSelection.getFocusOffset() - markdownCharacterLength,
38+
});
39+
40+
// remove markdown delimiter at start
41+
newContentState = Modifier.replaceText(
42+
newContentState,
43+
wordSelection.merge({
44+
focusOffset: wordSelection.getAnchorOffset() + markdownCharacterLength,
45+
}),
46+
""
47+
);
48+
49+
// apply style
50+
newContentState = Modifier.applyInlineStyle(
51+
newContentState,
52+
wordSelection.merge({
53+
anchorOffset: index,
54+
focusOffset: focusOffset - markdownCharacterLength * 2,
55+
}),
56+
style
57+
);
58+
2859
const newEditorState = EditorState.push(
2960
editorState,
3061
newContentState,
3162
"change-inline-style"
3263
);
33-
return EditorState.forceSelection(
34-
newEditorState,
35-
newContentState.getSelectionAfter()
36-
);
64+
65+
return EditorState.forceSelection(newEditorState, afterSelection);
3766
};
3867

3968
export default changeCurrentInlineStyle;

src/modifiers/handleInlineStyle.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import changeCurrentInlineStyle from "./changeCurrentInlineStyle";
2-
3-
const inlineMatchers = {
4-
BOLD: [/\*\*([^(?**)]+)\*\*/g, /__([^(?:__)]+)__/g],
5-
ITALIC: [/\*([^*]+)\*/g, /_([^_]+)_/g],
6-
CODE: [/`([^`]+)`/g],
7-
STRIKETHROUGH: [/~~([^(?:~~)]+)~~/g],
8-
};
2+
import { inlineMatchers } from "../constants";
93

104
const handleInlineStyle = (editorState, character) => {
115
const key = editorState.getSelection().getStartKey();
126
const text = editorState
137
.getCurrentContent()
148
.getBlockForKey(key)
159
.getText();
10+
1611
const line = `${text}${character}`;
1712
let newEditorState = editorState;
13+
1814
Object.keys(inlineMatchers).some(k => {
1915
inlineMatchers[k].some(re => {
2016
let matchArr;

0 commit comments

Comments
 (0)