-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathstate.js
33 lines (32 loc) · 968 Bytes
/
state.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
export const removeBlockFromState = ( state, index ) => {
return Object.assign( {}, state, {
blocks: [
...state.blocks.slice( 0, index ),
...state.blocks.slice( index + 1 ),
]
} );
};
export const mergeInlineTextBlocks = ( state, index ) => {
const getLeaves = html => {
const div = document.createElement( 'div' );
div.innerHTML = html;
if ( div.childNodes.length === 1 && div.firstChild.nodeName === 'P' ) {
return getLeaves( div.firstChild.innerHTML );
}
return html;
};
const currentBlock = state.blocks[ index ];
const blockToMerge = state.blocks[ index + 1 ];
const newBlock = Object.assign( {}, currentBlock, {
content: getLeaves( currentBlock.content ) + getLeaves( blockToMerge.content ),
} );
const newBlocks = [
...state.blocks.slice( 0, index ),
newBlock,
...state.blocks.slice( index + 2 )
];
return Object.assign( {}, state, {
blocks: newBlocks,
focus: { uid: newBlock.uid, config: { end: true } }
} );
};