From c0d7c093107dd82e41502195f4154a61d104a0d0 Mon Sep 17 00:00:00 2001 From: sugarshin Date: Thu, 12 Jul 2018 11:56:26 +0900 Subject: [PATCH] Add `insertEmptyBlockOnReturnWithModifierKey` config --- src/__test__/plugin-test.js | 35 +++++++++++++++++++++-------------- src/index.js | 9 +++++---- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/__test__/plugin-test.js b/src/__test__/plugin-test.js index 3f88cc1..8731e91 100755 --- a/src/__test__/plugin-test.js +++ b/src/__test__/plugin-test.js @@ -45,7 +45,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => { [ [], - [{}], + [{ insertEmptyBlockOnReturnWithModifierKey: false }] ].forEach((args) => { beforeEach(() => { modifierSpy = sinon.spy(() => newEditorState); @@ -85,7 +85,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => { subject = null; }); - describe(args.length === 0 ? 'without config' : 'with config', () => { + describe(args.length === 0 ? 'without config' : 'with `insertEmptyBlockOnReturnWithModifierKey: false` config', () => { beforeEach(() => { plugin = createMarkdownShortcutsPlugin(...args); }); @@ -98,6 +98,16 @@ describe('draft-js-markdown-shortcuts-plugin', () => { expect(plugin.store).to.deep.equal(store); }); describe('handleReturn', () => { + const expectsHandled = () => { + expect(subject()).to.equal('handled'); + expect(modifierSpy).to.have.been.calledOnce(); + expect(store.setEditorState).to.have.been.calledWith(newEditorState); + }; + const expectsNotHandled = () => { + expect(subject()).to.equal('not-handled'); + expect(modifierSpy).not.to.have.been.calledOnce(); + expect(store.setEditorState).not.to.have.been.called(); + }; beforeEach(() => { subject = () => plugin.handleReturn(event, store.getEditorState(), store); }); @@ -114,9 +124,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => { data: {} }] }; - expect(subject()).to.equal('not-handled'); - expect(modifierSpy).not.to.have.been.calledOnce(); - expect(store.setEditorState).not.to.have.been.called(); + expectsNotHandled(); }); it('leaves from list', () => { createMarkdownShortcutsPlugin.__Rewire__('leaveList', modifierSpy); // eslint-disable-line no-underscore-dangle @@ -132,11 +140,9 @@ describe('draft-js-markdown-shortcuts-plugin', () => { data: {} }] }; - expect(subject()).to.equal('handled'); - expect(modifierSpy).to.have.been.calledOnce(); - expect(store.setEditorState).to.have.been.calledWith(newEditorState); + expectsHandled(); }); - const testInsertNewBlock = (type) => () => { + const testInsertNewBlock = (type, expects) => () => { createMarkdownShortcutsPlugin.__Rewire__('insertEmptyBlock', modifierSpy); // eslint-disable-line no-underscore-dangle currentRawContentState = { entityMap: {}, @@ -158,13 +164,14 @@ describe('draft-js-markdown-shortcuts-plugin', () => { isBackward: false, hasFocus: true }); - expect(subject()).to.equal('handled'); - expect(modifierSpy).to.have.been.calledOnce(); - expect(store.setEditorState).to.have.been.calledWith(newEditorState); + expects(); }; + const expects = args[0] && args[0].insertEmptyBlockOnReturnWithModifierKey === false + ? expectsNotHandled + : expectsHandled; ['one', 'two', 'three', 'four', 'five', 'six'].forEach((level) => { describe(`on header-${level}`, () => { - it('inserts new empty block on end of header return', testInsertNewBlock(`header-${level}`)); + it('inserts new empty block on end of header return', testInsertNewBlock(`header-${level}`, expects)); }); }); ['ctrlKey', 'shiftKey', 'metaKey', 'altKey'].forEach((key) => { @@ -174,7 +181,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => { props[key] = true; event = new window.KeyboardEvent('keydown', props); }); - it('inserts new empty block', testInsertNewBlock('blockquote')); + it('inserts new empty block', testInsertNewBlock('blockquote', expects)); }); }); it('handles new code block', () => { diff --git a/src/index.js b/src/index.js index 7bfeeda..4d02e05 100755 --- a/src/index.js +++ b/src/index.js @@ -38,7 +38,7 @@ function checkCharacterForState(editorState, character) { return newEditorState; } -function checkReturnForState(editorState, ev) { +function checkReturnForState(editorState, ev, { insertEmptyBlockOnReturnWithModifierKey }) { let newEditorState = editorState; const contentState = editorState.getCurrentContent(); const selection = editorState.getSelection(); @@ -50,6 +50,7 @@ function checkReturnForState(editorState, ev) { newEditorState = leaveList(editorState); } if (newEditorState === editorState + && insertEmptyBlockOnReturnWithModifierKey && (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey || (/^header-/.test(type) && selection.isCollapsed() && selection.getEndOffset() === text.length))) { newEditorState = insertEmptyBlock(editorState); @@ -71,7 +72,7 @@ function checkReturnForState(editorState, ev) { return newEditorState; } -const createMarkdownShortcutsPlugin = (config = {}) => { +const createMarkdownShortcutsPlugin = (config = { insertEmptyBlockOnReturnWithModifierKey: true }) => { const store = {}; return { store, @@ -126,7 +127,7 @@ const createMarkdownShortcutsPlugin = (config = {}) => { return 'not-handled'; }, handleReturn(ev, editorState, { setEditorState }) { - const newEditorState = checkReturnForState(editorState, ev); + const newEditorState = checkReturnForState(editorState, ev, config); if (editorState !== newEditorState) { setEditorState(newEditorState); return 'handled'; @@ -157,7 +158,7 @@ const createMarkdownShortcutsPlugin = (config = {}) => { buffer = []; } else if (text[i].charCodeAt(0) === 10) { newEditorState = replaceText(newEditorState, buffer.join('')); - const tmpEditorState = checkReturnForState(newEditorState, {}); + const tmpEditorState = checkReturnForState(newEditorState, {}, config); if (newEditorState === tmpEditorState) { newEditorState = insertEmptyBlock(tmpEditorState); } else {