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

Commit

Permalink
Merge branch 'master' into fix-first-block
Browse files Browse the repository at this point in the history
  • Loading branch information
mxstbr authored Sep 22, 2017
2 parents a4ef4ec + 016ee0c commit 4c15dc1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ demo/public
.deploy
test-results.xml

*.swp
1 change: 1 addition & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const CODE_BLOCK_REGEX = /^```([\w-]+)?\s*$/;
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import changeCurrentBlockType from "./modifiers/changeCurrentBlockType";
import createLinkDecorator from "./decorators/link";
import createImageDecorator from "./decorators/image";
import { replaceText } from "./utils";
import { CODE_BLOCK_REGEX } from "./constants";

const INLINE_STYLE_CHARACTERS = [" ", "*", "_"];

Expand Down Expand Up @@ -63,7 +64,7 @@ function checkReturnForState(editorState, ev) {
if (
newEditorState === editorState &&
type !== "code-block" &&
/^```([\w-]+)?$/.test(text)
CODE_BLOCK_REGEX.test(text)
) {
newEditorState = handleNewCodeBlock(editorState);
}
Expand Down Expand Up @@ -139,6 +140,7 @@ const createMarkdownPlugin = (config = {}) => {
return "not-handled";
},
handleReturn(ev, editorState, { setEditorState }) {
console.log("HANDLE RETURN");
const newEditorState = checkReturnForState(editorState, ev);
if (editorState !== newEditorState) {
setEditorState(newEditorState);
Expand Down
9 changes: 7 additions & 2 deletions src/modifiers/handleNewCodeBlock.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import changeCurrentBlockType from "./changeCurrentBlockType";
import insertEmptyBlock from "./insertEmptyBlock";
import { CODE_BLOCK_REGEX } from "../constants";

const handleNewCodeBlock = editorState => {
const contentState = editorState.getCurrentContent();
const selection = editorState.getSelection();
const key = selection.getStartKey();
const currentBlock = contentState.getBlockForKey(key);
const matchData = /^```([\w-]+)?$/.exec(currentBlock.getText());
const isLast = selection.getEndOffset() === currentBlock.getLength();
const matchData = CODE_BLOCK_REGEX.exec(currentBlock.getText());
const currentText = currentBlock.getText();
const endOffset = selection.getEndOffset();
// We .trim the text here to make sure pressing enter after "``` " works even if the cursor is before the space
const isLast =
endOffset === currentText.length || endOffset === currentText.trim().length;
if (matchData && isLast) {
const data = {};
const language = matchData[1];
Expand Down

0 comments on commit 4c15dc1

Please sign in to comment.