3
3
Object . defineProperty ( exports , "__esModule" , {
4
4
value : true
5
5
} ) ;
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
-
9
6
exports . default = blockBreakoutPlugin ;
10
7
11
8
var _draftJs = require ( 'draft-js' ) ;
@@ -38,18 +35,16 @@ var defaults = {
38
35
* @return {Object } Object defining the draft-js API methods
39
36
*/
40
37
function blockBreakoutPlugin ( ) {
41
- var options = arguments . length <= 0 || arguments [ 0 ] === undefined ? { } : arguments [ 0 ] ;
38
+ var options = arguments . length > 0 && arguments [ 0 ] !== undefined ? arguments [ 0 ] : { } ;
42
39
43
40
var breakoutBlockType = options . breakoutBlockType || defaults . breakoutBlockType ;
44
41
var breakoutBlocks = options . breakoutBlocks || defaults . breakoutBlocks ;
45
42
var doubleBreakoutBlocks = options . doubleBreakoutBlocks || defaults . doubleBreakoutBlocks ;
46
43
47
44
return {
48
- handleReturn : function handleReturn ( e , _ref ) {
49
- var getEditorState = _ref . getEditorState ;
45
+ handleReturn : function handleReturn ( e , editorState , _ref ) {
50
46
var setEditorState = _ref . setEditorState ;
51
47
52
- var editorState = getEditorState ( ) ;
53
48
var currentBlockType = _draftJs . RichUtils . getCurrentBlockType ( editorState ) ;
54
49
var isSingleBreakoutBlock = breakoutBlocks . indexOf ( currentBlockType ) > - 1 ;
55
50
var isDoubleBreakoutBlock = doubleBreakoutBlocks . indexOf ( currentBlockType ) > - 1 ;
@@ -60,73 +55,67 @@ function blockBreakoutPlugin() {
60
55
61
56
// Check if the selection is collapsed
62
57
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 ] ] ;
103
92
} 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 ] ] ;
107
95
}
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 ( ) ;
126
101
}
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
+ }
130
119
}
131
120
}
132
121
return 'not-handled' ;
0 commit comments