Skip to content

Commit a888e2d

Browse files
author
Rose Robertson
committed
Fix bug with handleReturn with latest version of draft-js-plugins
Looks like the arguments being passed got modified slightly.
1 parent 3b73f06 commit a888e2d

File tree

2 files changed

+61
-73
lines changed

2 files changed

+61
-73
lines changed

lib/index.js

+60-71
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6-
7-
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
8-
96
exports.default = blockBreakoutPlugin;
107

118
var _draftJs = require('draft-js');
@@ -38,18 +35,16 @@ var defaults = {
3835
* @return {Object} Object defining the draft-js API methods
3936
*/
4037
function blockBreakoutPlugin() {
41-
var options = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
38+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
4239

4340
var breakoutBlockType = options.breakoutBlockType || defaults.breakoutBlockType;
4441
var breakoutBlocks = options.breakoutBlocks || defaults.breakoutBlocks;
4542
var doubleBreakoutBlocks = options.doubleBreakoutBlocks || defaults.doubleBreakoutBlocks;
4643

4744
return {
48-
handleReturn: function handleReturn(e, _ref) {
49-
var getEditorState = _ref.getEditorState;
45+
handleReturn: function handleReturn(e, editorState, _ref) {
5046
var setEditorState = _ref.setEditorState;
5147

52-
var editorState = getEditorState();
5348
var currentBlockType = _draftJs.RichUtils.getCurrentBlockType(editorState);
5449
var isSingleBreakoutBlock = breakoutBlocks.indexOf(currentBlockType) > -1;
5550
var isDoubleBreakoutBlock = doubleBreakoutBlocks.indexOf(currentBlockType) > -1;
@@ -60,73 +55,67 @@ function blockBreakoutPlugin() {
6055

6156
// Check if the selection is collapsed
6257
if (selection.isCollapsed()) {
63-
var _ret = function () {
64-
var contentState = editorState.getCurrentContent();
65-
var currentBlock = contentState.getBlockForKey(selection.getEndKey());
66-
var endOffset = selection.getEndOffset();
67-
var atEndOfBlock = endOffset === currentBlock.getLength();
68-
var atStartOfBlock = endOffset === 0;
69-
70-
// Check we’re at the start/end of the current block
71-
if (atEndOfBlock && isSingleBreakoutBlock || atStartOfBlock && isSingleBreakoutBlock || atStartOfBlock && !currentBlock.getLength()) {
72-
var emptyBlockKey = (0, _draftJs.genKey)();
73-
var emptyBlock = new _draftJs.ContentBlock({
74-
key: emptyBlockKey,
75-
text: '',
76-
type: breakoutBlockType,
77-
characterList: (0, _immutable.List)(),
78-
depth: 0
79-
});
80-
var blockMap = contentState.getBlockMap();
81-
// Split the blocks
82-
var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
83-
return v === currentBlock;
84-
});
85-
86-
var blocksAfter = blockMap.toSeq().skipUntil(function (v) {
87-
return v === currentBlock;
88-
}).rest();
89-
90-
var augmentedBlocks = void 0;
91-
var focusKey = void 0;
92-
// Choose which order to apply the augmented blocks in depending
93-
// on whether we’re at the start or the end
94-
if (atEndOfBlock) {
95-
if (isDoubleBreakoutBlock) {
96-
// Discard Current as it was blank
97-
augmentedBlocks = [[emptyBlockKey, emptyBlock]];
98-
} else {
99-
// Current first, empty block afterwards
100-
augmentedBlocks = [[currentBlock.getKey(), currentBlock], [emptyBlockKey, emptyBlock]];
101-
}
102-
focusKey = emptyBlockKey;
58+
var contentState = editorState.getCurrentContent();
59+
var currentBlock = contentState.getBlockForKey(selection.getEndKey());
60+
var endOffset = selection.getEndOffset();
61+
var atEndOfBlock = endOffset === currentBlock.getLength();
62+
var atStartOfBlock = endOffset === 0;
63+
64+
// Check we’re at the start/end of the current block
65+
if (atEndOfBlock && isSingleBreakoutBlock || atStartOfBlock && isSingleBreakoutBlock || atStartOfBlock && !currentBlock.getLength()) {
66+
var emptyBlockKey = (0, _draftJs.genKey)();
67+
var emptyBlock = new _draftJs.ContentBlock({
68+
key: emptyBlockKey,
69+
text: '',
70+
type: breakoutBlockType,
71+
characterList: (0, _immutable.List)(),
72+
depth: 0
73+
});
74+
var blockMap = contentState.getBlockMap();
75+
// Split the blocks
76+
var blocksBefore = blockMap.toSeq().takeUntil(function (v) {
77+
return v === currentBlock;
78+
});
79+
80+
var blocksAfter = blockMap.toSeq().skipUntil(function (v) {
81+
return v === currentBlock;
82+
}).rest();
83+
84+
var augmentedBlocks = void 0;
85+
var focusKey = void 0;
86+
// Choose which order to apply the augmented blocks in depending
87+
// on whether we’re at the start or the end
88+
if (atEndOfBlock) {
89+
if (isDoubleBreakoutBlock) {
90+
// Discard Current as it was blank
91+
augmentedBlocks = [[emptyBlockKey, emptyBlock]];
10392
} else {
104-
// Empty first, current block afterwards
105-
augmentedBlocks = [[emptyBlockKey, emptyBlock], [currentBlock.getKey(), currentBlock]];
106-
focusKey = currentBlock.getKey();
93+
// Current first, empty block afterwards
94+
augmentedBlocks = [[currentBlock.getKey(), currentBlock], [emptyBlockKey, emptyBlock]];
10795
}
108-
// Join back together with the current + new block
109-
var newBlocks = blocksBefore.concat(augmentedBlocks, blocksAfter).toOrderedMap();
110-
var newContentState = contentState.merge({
111-
blockMap: newBlocks,
112-
selectionBefore: selection,
113-
selectionAfter: selection.merge({
114-
anchorKey: focusKey,
115-
anchorOffset: 0,
116-
focusKey: focusKey,
117-
focusOffset: 0,
118-
isBackward: false
119-
})
120-
});
121-
// Set the state
122-
setEditorState(_draftJs.EditorState.push(editorState, newContentState, 'split-block'));
123-
return {
124-
v: 'handled'
125-
};
96+
focusKey = emptyBlockKey;
97+
} else {
98+
// Empty first, current block afterwards
99+
augmentedBlocks = [[emptyBlockKey, emptyBlock], [currentBlock.getKey(), currentBlock]];
100+
focusKey = currentBlock.getKey();
126101
}
127-
}();
128-
129-
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
102+
// Join back together with the current + new block
103+
var newBlocks = blocksBefore.concat(augmentedBlocks, blocksAfter).toOrderedMap();
104+
var newContentState = contentState.merge({
105+
blockMap: newBlocks,
106+
selectionBefore: selection,
107+
selectionAfter: selection.merge({
108+
anchorKey: focusKey,
109+
anchorOffset: 0,
110+
focusKey: focusKey,
111+
focusOffset: 0,
112+
isBackward: false
113+
})
114+
});
115+
// Set the state
116+
setEditorState(_draftJs.EditorState.push(editorState, newContentState, 'split-block'));
117+
return 'handled';
118+
}
130119
}
131120
}
132121
return 'not-handled';

src/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ export default function blockBreakoutPlugin (options = {}) {
4949
const doubleBreakoutBlocks = options.doubleBreakoutBlocks || defaults.doubleBreakoutBlocks
5050

5151
return {
52-
handleReturn (e, { getEditorState, setEditorState }) {
53-
const editorState = getEditorState()
52+
handleReturn (e, editorState, { setEditorState }) {
5453
const currentBlockType = RichUtils.getCurrentBlockType(editorState)
5554
const isSingleBreakoutBlock = breakoutBlocks.indexOf(currentBlockType) > -1
5655
const isDoubleBreakoutBlock = doubleBreakoutBlocks.indexOf(currentBlockType) > -1

0 commit comments

Comments
 (0)