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

Commit fcd392f

Browse files
Merge pull request #46 from michaelgmcd/master
fix: only insert empty block for headers and blockquote when at end of line
2 parents 78cb8c2 + 27e027c commit fcd392f

File tree

2 files changed

+92
-33
lines changed

2 files changed

+92
-33
lines changed

src/__test__/plugin.test.js

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -254,32 +254,66 @@ describe("draft-js-markdown-plugin", () => {
254254
expect(newEditorState.getCurrentInlineStyle().size).toBe(0);
255255
});
256256

257-
const testInsertNewBlock = type => () => {
258-
createMarkdownPlugin.__Rewire__("insertEmptyBlock", modifierSpy); // eslint-disable-line no-underscore-dangle
259-
currentRawContentState = {
260-
entityMap: {},
261-
blocks: [
262-
{
263-
key: "item1",
264-
text: "Hello",
265-
type,
266-
depth: 0,
267-
inlineStyleRanges: [],
268-
entityRanges: [],
269-
data: {},
270-
},
271-
],
272-
};
273-
expect(subject()).toBe("handled");
274-
expect(modifierSpy).toHaveBeenCalledTimes(1);
275-
expect(store.setEditorState).toHaveBeenCalledWith(newEditorState);
276-
};
277-
["one", "two", "three", "four", "five", "six"].forEach(level => {
278-
describe(`on header-${level}`, () => {
279-
it(
280-
"inserts new empty block",
281-
testInsertNewBlock(`header-${level}`)
282-
);
257+
const emptyBlockTypes = [
258+
"blockquote",
259+
"header-one",
260+
"header-two",
261+
"header-three",
262+
"header-four",
263+
"header-five",
264+
"header-six",
265+
];
266+
267+
emptyBlockTypes.forEach(type => {
268+
describe(`on ${type}`, () => {
269+
const text = "Hello";
270+
beforeEach(() => {
271+
createMarkdownPlugin.__Rewire__("insertEmptyBlock", modifierSpy); // eslint-disable-line no-underscore-dangle
272+
currentRawContentState = {
273+
entityMap: {},
274+
blocks: [
275+
{
276+
key: "item1",
277+
text,
278+
type,
279+
depth: 0,
280+
inlineStyleRanges: [],
281+
entityRanges: [],
282+
data: {},
283+
},
284+
],
285+
};
286+
});
287+
288+
describe("at the end of line", () => {
289+
beforeEach(() => {
290+
currentSelectionState = currentEditorState
291+
.getSelection()
292+
.merge({
293+
focusOffset: text.length,
294+
anchorOffset: text.length,
295+
});
296+
297+
currentEditorState = createEditorState(
298+
currentRawContentState,
299+
currentSelectionState
300+
);
301+
});
302+
it("inserts new empty block", () => {
303+
expect(subject()).toBe("handled");
304+
expect(modifierSpy).toHaveBeenCalledTimes(1);
305+
expect(store.setEditorState).toHaveBeenCalledWith(
306+
newEditorState
307+
);
308+
});
309+
});
310+
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();
315+
});
316+
});
283317
});
284318
});
285319
["ctrlKey", "shiftKey", "metaKey", "altKey"].forEach(key => {
@@ -289,7 +323,27 @@ describe("draft-js-markdown-plugin", () => {
289323
props[key] = true;
290324
event = new window.KeyboardEvent("keydown", props);
291325
});
292-
it("inserts new empty block", testInsertNewBlock("blockquote"));
326+
it("inserts new empty block", () => {
327+
createMarkdownPlugin.__Rewire__("insertEmptyBlock", modifierSpy); // eslint-disable-line no-underscore-dangle
328+
const text = "Hello";
329+
currentRawContentState = {
330+
entityMap: {},
331+
blocks: [
332+
{
333+
key: "item1",
334+
text,
335+
type: "any type",
336+
depth: 0,
337+
inlineStyleRanges: [],
338+
entityRanges: [],
339+
data: {},
340+
},
341+
],
342+
};
343+
expect(subject()).toBe("handled");
344+
expect(modifierSpy).toHaveBeenCalledTimes(1);
345+
expect(store.setEditorState).toHaveBeenCalledWith(newEditorState);
346+
});
293347
});
294348
});
295349
it("handles new code block", () => {

src/index.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,23 +122,28 @@ function checkReturnForState(config, editorState, ev) {
122122
let newEditorState = editorState;
123123
const contentState = editorState.getCurrentContent();
124124
const selection = editorState.getSelection();
125+
const isCollapsed = selection.isCollapsed();
125126
const key = selection.getStartKey();
127+
const endOffset = selection.getEndOffset();
126128
const currentBlock = contentState.getBlockForKey(key);
129+
const blockLength = currentBlock.getLength();
127130
const type = currentBlock.getType();
128131
const text = currentBlock.getText();
129132

130133
if (/-list-item$/.test(type) && text === "") {
131134
newEditorState = leaveList(editorState);
132135
}
133136

137+
const modifierKeyPressed =
138+
ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey;
139+
const isAtEndOfLine = endOffset === blockLength;
140+
const atEndOfHeader = /^header-/.test(type) && isAtEndOfLine;
141+
const atEndOfBlockQuote = type === "blockquote" && isAtEndOfLine;
142+
134143
if (
135144
newEditorState === editorState &&
136-
(ev.ctrlKey ||
137-
ev.shiftKey ||
138-
ev.metaKey ||
139-
ev.altKey ||
140-
/^header-/.test(type) ||
141-
type === "blockquote")
145+
isCollapsed &&
146+
(modifierKeyPressed || atEndOfHeader || atEndOfBlockQuote)
142147
) {
143148
// transform markdown (if we aren't in a codeblock that is)
144149
if (!inCodeBlock(editorState)) {

0 commit comments

Comments
 (0)