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

Commit 313a97e

Browse files
authored
Merge pull request #50 from withspectrum/reset-block-style
Reset block style
2 parents fcd392f + 349cf0e commit 313a97e

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

src/__test__/plugin.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ describe("draft-js-markdown-plugin", () => {
1717
createMarkdownPlugin.__ResetDependency__("handleInlineStyle");
1818
createMarkdownPlugin.__ResetDependency__("handleNewCodeBlock");
1919
createMarkdownPlugin.__ResetDependency__("insertEmptyBlock");
20+
createMarkdownPlugin.__ResetDependency__("splitBlockAndChange");
2021
createMarkdownPlugin.__ResetDependency__("handleLink");
2122
createMarkdownPlugin.__ResetDependency__("handleImage");
2223
createMarkdownPlugin.__ResetDependency__("leaveList");
@@ -268,7 +269,10 @@ describe("draft-js-markdown-plugin", () => {
268269
describe(`on ${type}`, () => {
269270
const text = "Hello";
270271
beforeEach(() => {
271-
createMarkdownPlugin.__Rewire__("insertEmptyBlock", modifierSpy); // eslint-disable-line no-underscore-dangle
272+
createMarkdownPlugin.__Rewire__(
273+
"splitBlockAndChange",
274+
modifierSpy
275+
); // eslint-disable-line no-underscore-dangle
272276
currentRawContentState = {
273277
entityMap: {},
274278
blocks: [
@@ -287,6 +291,10 @@ describe("draft-js-markdown-plugin", () => {
287291

288292
describe("at the end of line", () => {
289293
beforeEach(() => {
294+
createMarkdownPlugin.__Rewire__(
295+
"insertEmptyBlock",
296+
modifierSpy
297+
); // eslint-disable-line no-underscore-dangle
290298
currentSelectionState = currentEditorState
291299
.getSelection()
292300
.merge({
@@ -308,14 +316,15 @@ describe("draft-js-markdown-plugin", () => {
308316
});
309317
});
310318
describe("when not at the end of the line", () => {
311-
it("does not handle", () => {
312-
expect(subject()).toBe("not-handled");
313-
expect(modifierSpy).not.toHaveBeenCalled();
314-
expect(store.setEditorState).not.toHaveBeenCalled();
319+
it("splits and resets block", () => {
320+
expect(subject()).toBe("handled");
321+
expect(modifierSpy).toHaveBeenCalled();
322+
expect(store.setEditorState).toHaveBeenCalled();
315323
});
316324
});
317325
});
318326
});
327+
319328
["ctrlKey", "shiftKey", "metaKey", "altKey"].forEach(key => {
320329
describe(`${key} is pressed`, () => {
321330
beforeEach(() => {

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
import adjustBlockDepth from "./modifiers/adjustBlockDepth";
1919
import handleBlockType from "./modifiers/handleBlockType";
2020
import handleInlineStyle from "./modifiers/handleInlineStyle";
21+
import splitBlockAndChange from "./modifiers/splitBlockAndChange";
2122
import handleNewCodeBlock from "./modifiers/handleNewCodeBlock";
2223
import resetInlineStyle from "./modifiers/resetInlineStyle";
2324
import insertEmptyBlock from "./modifiers/insertEmptyBlock";
@@ -134,11 +135,14 @@ function checkReturnForState(config, editorState, ev) {
134135
newEditorState = leaveList(editorState);
135136
}
136137

138+
const isHeader = /^header-/.test(type);
139+
const isBlockQuote = type === "blockquote";
140+
137141
const modifierKeyPressed =
138142
ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey;
139143
const isAtEndOfLine = endOffset === blockLength;
140-
const atEndOfHeader = /^header-/.test(type) && isAtEndOfLine;
141-
const atEndOfBlockQuote = type === "blockquote" && isAtEndOfLine;
144+
const atEndOfHeader = isHeader && isAtEndOfLine;
145+
const atEndOfBlockQuote = isBlockQuote && isAtEndOfLine;
142146

143147
if (
144148
newEditorState === editorState &&
@@ -154,6 +158,8 @@ function checkReturnForState(config, editorState, ev) {
154158
} else {
155159
newEditorState = RichUtils.toggleBlockType(newEditorState, type);
156160
}
161+
} else if (isCollapsed && (isHeader || isBlockQuote) && !isAtEndOfLine) {
162+
newEditorState = splitBlockAndChange(newEditorState);
157163
}
158164
if (
159165
newEditorState === editorState &&
@@ -296,7 +302,7 @@ const createMarkdownPlugin = (_config = {}) => {
296302

297303
newEditorState = resetInlineStyle(newEditorState);
298304

299-
if (!is(editorState.getImmutable(), newEditorState.getImmutable())) {
305+
if (editorState !== newEditorState) {
300306
setEditorState(
301307
EditorState.push(newEditorState, content, "split-block")
302308
);

src/modifiers/splitBlockAndChange.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { EditorState, Modifier } from "draft-js";
2+
3+
const splitBlockAndChange = (
4+
editorState,
5+
type = "unstyled",
6+
blockMetadata = {}
7+
) => {
8+
let currentContent = editorState.getCurrentContent();
9+
let selection = editorState.getSelection();
10+
currentContent = Modifier.splitBlock(currentContent, selection);
11+
selection = currentContent.getSelectionAfter();
12+
const key = selection.getStartKey();
13+
const blockMap = currentContent.getBlockMap();
14+
const block = blockMap.get(key);
15+
const data = block.getData().merge(blockMetadata);
16+
const newBlock = block.merge({ type, data });
17+
const newContentState = currentContent.merge({
18+
blockMap: blockMap.set(key, newBlock),
19+
selectionAfter: selection,
20+
});
21+
22+
return EditorState.push(editorState, newContentState, "split-block");
23+
};
24+
25+
export default splitBlockAndChange;

0 commit comments

Comments
 (0)