Skip to content

Commit ace015f

Browse files
authored
Merge pull request #51 from ngs/add-option
Add `insertEmptyBlockOnReturnWithModifierKey` config
2 parents fe82a6d + c0d7c09 commit ace015f

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

src/__test__/plugin-test.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
4545

4646
[
4747
[],
48-
[{}],
48+
[{ insertEmptyBlockOnReturnWithModifierKey: false }]
4949
].forEach((args) => {
5050
beforeEach(() => {
5151
modifierSpy = sinon.spy(() => newEditorState);
@@ -85,7 +85,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
8585
subject = null;
8686
});
8787

88-
describe(args.length === 0 ? 'without config' : 'with config', () => {
88+
describe(args.length === 0 ? 'without config' : 'with `insertEmptyBlockOnReturnWithModifierKey: false` config', () => {
8989
beforeEach(() => {
9090
plugin = createMarkdownShortcutsPlugin(...args);
9191
});
@@ -98,6 +98,16 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
9898
expect(plugin.store).to.deep.equal(store);
9999
});
100100
describe('handleReturn', () => {
101+
const expectsHandled = () => {
102+
expect(subject()).to.equal('handled');
103+
expect(modifierSpy).to.have.been.calledOnce();
104+
expect(store.setEditorState).to.have.been.calledWith(newEditorState);
105+
};
106+
const expectsNotHandled = () => {
107+
expect(subject()).to.equal('not-handled');
108+
expect(modifierSpy).not.to.have.been.calledOnce();
109+
expect(store.setEditorState).not.to.have.been.called();
110+
};
101111
beforeEach(() => {
102112
subject = () => plugin.handleReturn(event, store.getEditorState(), store);
103113
});
@@ -114,9 +124,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
114124
data: {}
115125
}]
116126
};
117-
expect(subject()).to.equal('not-handled');
118-
expect(modifierSpy).not.to.have.been.calledOnce();
119-
expect(store.setEditorState).not.to.have.been.called();
127+
expectsNotHandled();
120128
});
121129
it('leaves from list', () => {
122130
createMarkdownShortcutsPlugin.__Rewire__('leaveList', modifierSpy); // eslint-disable-line no-underscore-dangle
@@ -132,11 +140,9 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
132140
data: {}
133141
}]
134142
};
135-
expect(subject()).to.equal('handled');
136-
expect(modifierSpy).to.have.been.calledOnce();
137-
expect(store.setEditorState).to.have.been.calledWith(newEditorState);
143+
expectsHandled();
138144
});
139-
const testInsertNewBlock = (type) => () => {
145+
const testInsertNewBlock = (type, expects) => () => {
140146
createMarkdownShortcutsPlugin.__Rewire__('insertEmptyBlock', modifierSpy); // eslint-disable-line no-underscore-dangle
141147
currentRawContentState = {
142148
entityMap: {},
@@ -158,13 +164,14 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
158164
isBackward: false,
159165
hasFocus: true
160166
});
161-
expect(subject()).to.equal('handled');
162-
expect(modifierSpy).to.have.been.calledOnce();
163-
expect(store.setEditorState).to.have.been.calledWith(newEditorState);
167+
expects();
164168
};
169+
const expects = args[0] && args[0].insertEmptyBlockOnReturnWithModifierKey === false
170+
? expectsNotHandled
171+
: expectsHandled;
165172
['one', 'two', 'three', 'four', 'five', 'six'].forEach((level) => {
166173
describe(`on header-${level}`, () => {
167-
it('inserts new empty block on end of header return', testInsertNewBlock(`header-${level}`));
174+
it('inserts new empty block on end of header return', testInsertNewBlock(`header-${level}`, expects));
168175
});
169176
});
170177
['ctrlKey', 'shiftKey', 'metaKey', 'altKey'].forEach((key) => {
@@ -174,7 +181,7 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
174181
props[key] = true;
175182
event = new window.KeyboardEvent('keydown', props);
176183
});
177-
it('inserts new empty block', testInsertNewBlock('blockquote'));
184+
it('inserts new empty block', testInsertNewBlock('blockquote', expects));
178185
});
179186
});
180187
it('handles new code block', () => {

src/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function checkCharacterForState(editorState, character) {
3838
return newEditorState;
3939
}
4040

41-
function checkReturnForState(editorState, ev) {
41+
function checkReturnForState(editorState, ev, { insertEmptyBlockOnReturnWithModifierKey }) {
4242
let newEditorState = editorState;
4343
const contentState = editorState.getCurrentContent();
4444
const selection = editorState.getSelection();
@@ -50,6 +50,7 @@ function checkReturnForState(editorState, ev) {
5050
newEditorState = leaveList(editorState);
5151
}
5252
if (newEditorState === editorState
53+
&& insertEmptyBlockOnReturnWithModifierKey
5354
&& (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey
5455
|| (/^header-/.test(type) && selection.isCollapsed() && selection.getEndOffset() === text.length))) {
5556
newEditorState = insertEmptyBlock(editorState);
@@ -71,7 +72,7 @@ function checkReturnForState(editorState, ev) {
7172
return newEditorState;
7273
}
7374

74-
const createMarkdownShortcutsPlugin = (config = {}) => {
75+
const createMarkdownShortcutsPlugin = (config = { insertEmptyBlockOnReturnWithModifierKey: true }) => {
7576
const store = {};
7677
return {
7778
store,
@@ -126,7 +127,7 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
126127
return 'not-handled';
127128
},
128129
handleReturn(ev, editorState, { setEditorState }) {
129-
const newEditorState = checkReturnForState(editorState, ev);
130+
const newEditorState = checkReturnForState(editorState, ev, config);
130131
if (editorState !== newEditorState) {
131132
setEditorState(newEditorState);
132133
return 'handled';
@@ -157,7 +158,7 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
157158
buffer = [];
158159
} else if (text[i].charCodeAt(0) === 10) {
159160
newEditorState = replaceText(newEditorState, buffer.join(''));
160-
const tmpEditorState = checkReturnForState(newEditorState, {});
161+
const tmpEditorState = checkReturnForState(newEditorState, {}, config);
161162
if (newEditorState === tmpEditorState) {
162163
newEditorState = insertEmptyBlock(tmpEditorState);
163164
} else {

0 commit comments

Comments
 (0)