Skip to content

Commit

Permalink
Add insertEmptyBlockOnReturnWithModifierKey config
Browse files Browse the repository at this point in the history
  • Loading branch information
sugarshin committed Jul 12, 2018
1 parent fe82a6d commit c0d7c09
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
35 changes: 21 additions & 14 deletions src/__test__/plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {

[
[],
[{}],
[{ insertEmptyBlockOnReturnWithModifierKey: false }]
].forEach((args) => {
beforeEach(() => {
modifierSpy = sinon.spy(() => newEditorState);
Expand Down Expand Up @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -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
Expand All @@ -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: {},
Expand All @@ -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) => {
Expand All @@ -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', () => {
Expand Down
9 changes: 5 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -71,7 +72,7 @@ function checkReturnForState(editorState, ev) {
return newEditorState;
}

const createMarkdownShortcutsPlugin = (config = {}) => {
const createMarkdownShortcutsPlugin = (config = { insertEmptyBlockOnReturnWithModifierKey: true }) => {
const store = {};
return {
store,
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit c0d7c09

Please sign in to comment.