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

Commit 190ec81

Browse files
committed
Disallow users from typing two spaces
@uberbryn I couldn't make it so we don't automatically type the space, so I've just prevented users from double typing spaces in general.
1 parent ed0a4c6 commit 190ec81

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

src/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import insertText from "./modifiers/insertText";
2929
import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
3030
import createLinkDecorator from "./decorators/link";
3131
import createImageDecorator from "./decorators/image";
32-
import { replaceText } from "./utils";
32+
import { replaceText, getCurrentLine } from "./utils";
3333
import {
3434
CODE_BLOCK_REGEX,
3535
CODE_BLOCK_TYPE,
@@ -57,7 +57,7 @@ const defaultLanguages = {
5757
swift: "Swift",
5858
};
5959

60-
const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];
60+
const INLINE_STYLE_CHARACTERS = ["*", "_", "`", "~"];
6161

6262
const defaultRenderSelect = ({ options, onChange, selectedValue }) => (
6363
<select value={selectedValue} onChange={onChange}>
@@ -347,6 +347,10 @@ const createMarkdownPlugin = (_config = {}) => {
347347
// If we're in a link - don't transform markdown
348348
if (inLink(editorState)) return "not-handled";
349349

350+
// Don't let users type two spaces after another
351+
if (character === " " && getCurrentLine(editorState).slice(-1) === " ")
352+
return "handled";
353+
350354
const newEditorState = checkCharacterForState(
351355
config,
352356
editorState,

src/modifiers/handleInlineStyle.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import changeCurrentInlineStyle from "./changeCurrentInlineStyle";
22
import { EditorState, Modifier } from "draft-js";
33
import { inlineMatchers } from "../constants";
44
import insertText from "./insertText";
5+
import { getCurrentLine as getLine } from "../utils";
56

67
const handleChange = (editorState, line, whitelist) => {
78
let newEditorState = editorState;
@@ -27,32 +28,21 @@ const handleChange = (editorState, line, whitelist) => {
2728
return newEditorState;
2829
};
2930

30-
const getLine = (editorState, anchorOffset) => {
31-
const selection = editorState.getSelection().merge({ anchorOffset });
32-
const key = editorState.getSelection().getStartKey();
33-
34-
return editorState
35-
.getCurrentContent()
36-
.getBlockForKey(key)
37-
.getText()
38-
.slice(0, selection.getFocusOffset());
39-
};
40-
4131
const handleInlineStyle = (
4232
whitelist,
4333
editorStateWithoutCharacter,
4434
character
4535
) => {
4636
const editorState = insertText(editorStateWithoutCharacter, character);
4737
let selection = editorState.getSelection();
48-
let line = getLine(editorState, selection.getAnchorOffset());
38+
let line = getLine(editorState);
4939
let newEditorState = handleChange(editorState, line, whitelist);
5040
let lastEditorState = editorState;
5141

5242
// Recursively resolve markdown, e.g. _*text*_ should turn into both italic and bold
5343
while (newEditorState !== lastEditorState) {
5444
lastEditorState = newEditorState;
55-
line = getLine(newEditorState, selection.getAnchorOffset());
45+
line = getLine(newEditorState);
5646
newEditorState = handleChange(newEditorState, line, whitelist);
5747
}
5848

src/modifiers/insertText.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EditorState, Modifier } from "draft-js";
33
const insertText = (editorState, text) => {
44
const selection = editorState.getSelection();
55
const content = editorState.getCurrentContent();
6+
if (!selection.isCollapsed()) return editorState;
67
const newContentState = Modifier.insertText(
78
content,
89
selection,

src/utils.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { Modifier, EditorState } from "draft-js";
22

3+
export const getCurrentLine = editorState => {
4+
const { anchorOffset } = editorState.getSelection();
5+
const selection = editorState.getSelection().merge({ anchorOffset });
6+
const key = editorState.getSelection().getStartKey();
7+
8+
return editorState
9+
.getCurrentContent()
10+
.getBlockForKey(key)
11+
.getText()
12+
.slice(0, selection.getFocusOffset());
13+
};
14+
315
export function addText(editorState, bufferText) {
416
const contentState = Modifier.insertText(
517
editorState.getCurrentContent(),

0 commit comments

Comments
 (0)