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

Commit 1efbea8

Browse files
committed
- remove .eslintignore and .eslintrc since we have prettier
- add inline style matchers to constants - refactor changeCurrentInlineStyle logic - correct handleInlineStyle tests
1 parent e40ff6a commit 1efbea8

File tree

6 files changed

+64
-98
lines changed

6 files changed

+64
-98
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: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint-disable no-unused-vars */
22

3-
import { expect } from "chai";
3+
//import { expect } from "chai";
44
import Draft, { EditorState, SelectionState } from "draft-js";
55
import handleInlineStyle from "../handleInlineStyle";
66

7-
describe("handleInlineStyle", () => {
7+
describe.only("handleInlineStyle", () => {
88
describe("no markup", () => {
99
const rawContentState = {
1010
entityMap: {},
@@ -35,10 +35,10 @@ describe("handleInlineStyle", () => {
3535
);
3636
it("does not convert block type", () => {
3737
const newEditorState = handleInlineStyle(editorState, " ");
38-
expect(newEditorState).to.equal(editorState);
39-
expect(
40-
Draft.convertToRaw(newEditorState.getCurrentContent())
41-
).to.deep.equal(rawContentState);
38+
expect(newEditorState).toEqual(editorState);
39+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
40+
rawContentState
41+
);
4242
});
4343
});
4444

@@ -231,7 +231,7 @@ describe("handleInlineStyle", () => {
231231
inlineStyleRanges: [
232232
{
233233
length: 3,
234-
offset: 5,
234+
offset: 2,
235235
style: "ITALIC",
236236
},
237237
],
@@ -250,8 +250,8 @@ describe("handleInlineStyle", () => {
250250
depth: 0,
251251
inlineStyleRanges: [
252252
{
253-
length: 7, // FIXME
254-
offset: 5,
253+
length: 3,
254+
offset: 2,
255255
style: "ITALIC",
256256
},
257257
{
@@ -376,7 +376,7 @@ describe("handleInlineStyle", () => {
376376
depth: 0,
377377
inlineStyleRanges: [
378378
{
379-
length: 3,
379+
length: 4,
380380
offset: 5,
381381
style: "ITALIC",
382382
},
@@ -396,7 +396,7 @@ describe("handleInlineStyle", () => {
396396
depth: 0,
397397
inlineStyleRanges: [
398398
{
399-
length: 7, // FIXME
399+
length: 2,
400400
offset: 5,
401401
style: "ITALIC",
402402
},
@@ -427,12 +427,12 @@ describe("handleInlineStyle", () => {
427427
blocks: [
428428
{
429429
key: "item1",
430-
text: "hello __inline__ style",
430+
text: "hello _inline_ style",
431431
type: "unstyled",
432432
depth: 0,
433433
inlineStyleRanges: [
434434
{
435-
length: 3,
435+
length: 5,
436436
offset: 5,
437437
style: "BOLD",
438438
},
@@ -452,14 +452,15 @@ describe("handleInlineStyle", () => {
452452
depth: 0,
453453
inlineStyleRanges: [
454454
{
455-
length: 7, // FIXME
455+
length: 4,
456456
offset: 5,
457457
style: "BOLD",
458-
} /* { FIXME
459-
length: 6,
460-
offset: 6,
461-
style: 'ITALIC'
462-
} */,
458+
},
459+
{
460+
length: 6,
461+
offset: 6,
462+
style: "ITALIC",
463+
},
463464
],
464465
entityRanges: [],
465466
data: {},
@@ -487,10 +488,12 @@ describe("handleInlineStyle", () => {
487488
);
488489
it("converts block type", () => {
489490
const newEditorState = handleInlineStyle(editorState, character);
490-
expect(newEditorState).not.to.equal(editorState);
491-
expect(
492-
Draft.convertToRaw(newEditorState.getCurrentContent())
493-
).to.deep.equal(after);
491+
expect(newEditorState).not.toEqual(editorState);
492+
// console.log('actual', JSON.stringify(Draft.convertToRaw(newEditorState.getCurrentContent()), null, 2))
493+
// console.log('expected', JSON.stringify(after, null, 2))
494+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
495+
after
496+
);
494497
});
495498
});
496499
});
Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { EditorState, SelectionState, Modifier } from "draft-js";
22

3-
const STYLES = ["BOLD", "ITALIC", "CODE", "STRIKETHROUGH"];
4-
53
const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
64
const currentContent = editorState.getCurrentContent();
75
const selection = editorState.getSelection();
@@ -19,48 +17,43 @@ const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
1917
});
2018

2119
const inlineStyles = [];
20+
const markdownCharacterLength = (matchArr[0].length - matchArr[1].length) / 2;
2221

23-
STYLES.forEach(_style =>
24-
block.findStyleRanges(
25-
styles => styles.getStyle().includes(_style),
26-
(start, end) => {
27-
if (focusOffset > start && index <= end) {
28-
inlineStyles.push({
29-
style: _style,
30-
start: start >= index ? start : index,
31-
end: end > focusOffset ? end : focusOffset,
32-
});
33-
}
34-
}
35-
)
36-
);
37-
38-
console.log("yo inline styles", inlineStyles);
22+
let newContentState = currentContent;
3923

40-
let newContentState = Modifier.replaceText(
41-
currentContent,
42-
wordSelection,
43-
matchArr[1],
44-
newStyle
24+
// remove markdown delimiter at end
25+
newContentState = Modifier.replaceText(
26+
newContentState,
27+
wordSelection.merge({
28+
anchorOffset: wordSelection.getFocusOffset() - markdownCharacterLength,
29+
}),
30+
character || " "
4531
);
4632

47-
const afterSelection = newContentState.getSelectionAfter();
33+
let afterSelection = newContentState.getSelectionAfter();
4834

49-
// re-apply previous styles
50-
newContentState = inlineStyles.reduce(
51-
(content, { style: _style, start, end }) =>
52-
Modifier.applyInlineStyle(
53-
content,
54-
wordSelection.merge({ anchorOffset: start, focusOffset: end }),
55-
_style
56-
),
57-
newContentState
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+
""
5847
);
5948

60-
newContentState = Modifier.insertText(
49+
// apply style
50+
newContentState = Modifier.applyInlineStyle(
6151
newContentState,
62-
afterSelection,
63-
character || " "
52+
wordSelection.merge({
53+
anchorOffset: index,
54+
focusOffset: focusOffset - markdownCharacterLength * 2,
55+
}),
56+
style
6457
);
6558

6659
const newEditorState = EditorState.push(
@@ -69,10 +62,7 @@ const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
6962
"change-inline-style"
7063
);
7164

72-
return EditorState.forceSelection(
73-
newEditorState,
74-
newContentState.getSelectionAfter()
75-
);
65+
return EditorState.forceSelection(newEditorState, afterSelection);
7666
};
7767

7868
export default changeCurrentInlineStyle;

src/modifiers/handleInlineStyle.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
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();

0 commit comments

Comments
 (0)