This repository was archived by the owner on Oct 11, 2022. It is now read-only.
forked from ngs/draft-js-markdown-shortcuts-plugin
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathhandleInlineStyle.js
73 lines (64 loc) · 2.13 KB
/
handleInlineStyle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import changeCurrentInlineStyle from "./changeCurrentInlineStyle";
import { EditorState, Modifier } from "draft-js";
import { inlineMatchers } from "../constants";
import insertText from "./insertText";
import { getCurrentLine as getLine } from "../utils";
const handleChange = (editorState, line, whitelist) => {
let newEditorState = editorState;
Object.keys(inlineMatchers)
.filter(matcher => whitelist.includes(matcher))
.some(k => {
inlineMatchers[k].some(re => {
let matchArr;
do {
matchArr = re.exec(line);
if (matchArr) {
if (matchArr[0][0].match(/^\s/)) {
matchArr[0] = matchArr[0].replace(/^\s/, "");
matchArr.index += 1;
}
newEditorState = changeCurrentInlineStyle(
newEditorState,
matchArr,
k
);
}
} while (matchArr);
return newEditorState !== editorState;
});
return newEditorState !== editorState;
});
return newEditorState;
};
const handleInlineStyle = (
whitelist,
editorStateWithoutCharacter,
character
) => {
const editorState = insertText(editorStateWithoutCharacter, character);
let selection = editorState.getSelection();
let line = getLine(editorState);
let newEditorState = handleChange(editorState, line, whitelist);
let lastEditorState = editorState;
// Recursively resolve markdown, e.g. _*text*_ should turn into both italic and bold
while (newEditorState !== lastEditorState) {
lastEditorState = newEditorState;
line = getLine(newEditorState);
newEditorState = handleChange(newEditorState, line, whitelist);
}
if (newEditorState !== editorState) {
let newContentState = newEditorState.getCurrentContent();
selection = newEditorState.getSelection();
if (character === "\n") {
newContentState = Modifier.splitBlock(newContentState, selection);
}
newEditorState = EditorState.push(
newEditorState,
newContentState,
"md-to-inline-style"
);
return newEditorState;
}
return editorStateWithoutCharacter;
};
export default handleInlineStyle;