actions.js
2 years ago
constants.js
2 years ago
dispatchers.js
2 years ago
getPreparedBlocks.js
2 years ago
index.js
2 years ago
reducer.js
2 years ago
selectors.js
2 years ago
selectors.js
171 lines
| 1 | const selectors = { |
| 2 | getBlocks( state ) { |
| 3 | return state.blocks; |
| 4 | }, |
| 5 | getBlockMap( state ) { |
| 6 | return state.blockMap; |
| 7 | }, |
| 8 | getPropsToSave( state ) { |
| 9 | return state.propsToSave; |
| 10 | }, |
| 11 | getFields( |
| 12 | state, |
| 13 | { |
| 14 | withInner = true, |
| 15 | currentId = false, |
| 16 | }, |
| 17 | ) { |
| 18 | const fields = []; |
| 19 | |
| 20 | const iterateFields = blocks => { |
| 21 | for ( const block of blocks ) { |
| 22 | if ( block.fields?.length && block.clientId !== currentId ) { |
| 23 | fields.push( ...block.fields ); |
| 24 | } |
| 25 | |
| 26 | if ( !withInner || !block.innerBlocks?.length ) { |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | iterateFields( block.innerBlocks ); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | iterateFields( state.blocks ); |
| 35 | |
| 36 | return fields; |
| 37 | }, |
| 38 | isExecuted( state ) { |
| 39 | return state.executed; |
| 40 | }, |
| 41 | isRecentlyAdded( state, clientId ) { |
| 42 | return -1 !== state.recentlyAdded.indexOf( clientId ); |
| 43 | }, |
| 44 | getUniqueNames( state, clientId ) { |
| 45 | const currentBlock = state.blockMap[ clientId ] ?? false; |
| 46 | |
| 47 | if ( !currentBlock ) { |
| 48 | return { |
| 49 | hasChanged: false, |
| 50 | }; |
| 51 | } |
| 52 | |
| 53 | let hasChanged = false; |
| 54 | |
| 55 | let names = currentBlock.fields.map( ( { value } ) => value ); |
| 56 | const scope = currentBlock.hasOwnProperty( 'parentBlock' ) |
| 57 | ? currentBlock.parentBlock.innerBlocks |
| 58 | : state.blocks; |
| 59 | |
| 60 | const walkerFields = ( fields ) => { |
| 61 | for ( const field of fields ) { |
| 62 | const nameIndex = names.indexOf( field.value ); |
| 63 | |
| 64 | if ( -1 === nameIndex ) { |
| 65 | continue; |
| 66 | } |
| 67 | |
| 68 | if ( 'field_name' === field.value ) { |
| 69 | hasChanged = true; |
| 70 | continue; |
| 71 | } |
| 72 | |
| 73 | names[ nameIndex ] = `${ names[ nameIndex ] }_copy`; |
| 74 | hasChanged = true; |
| 75 | walkerFields( fields ); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | for ( const block of scope ) { |
| 80 | if ( clientId === block.clientId ) { |
| 81 | continue; |
| 82 | } |
| 83 | walkerFields( block.fields ); |
| 84 | } |
| 85 | |
| 86 | return { |
| 87 | hasChanged: hasChanged, |
| 88 | names: names.join( '|' ), |
| 89 | }; |
| 90 | }, |
| 91 | getSanitizedAttributes( state, attrs, { name: type } = {} ) { |
| 92 | for ( const attrsKey in attrs ) { |
| 93 | if ( !attrs.hasOwnProperty( attrsKey ) ) { |
| 94 | continue; |
| 95 | } |
| 96 | |
| 97 | const sanitizers = ( |
| 98 | state.sanitizers?.[ type ]?.[ attrsKey ] ?? |
| 99 | state.sanitizers?.[ attrsKey ] ?? |
| 100 | false |
| 101 | ); |
| 102 | |
| 103 | if ( !sanitizers?.length ) { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | for ( const sanitizer of sanitizers ) { |
| 108 | if ( 'function' !== typeof sanitizer ) { |
| 109 | continue; |
| 110 | } |
| 111 | attrs[ attrsKey ] = sanitizer( attrs[ attrsKey ] ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return attrs; |
| 116 | }, |
| 117 | isUniqueName( state, clientId ) { |
| 118 | const { hasChanged } = selectors.getUniqueNames( state, clientId ); |
| 119 | |
| 120 | return !hasChanged; |
| 121 | }, |
| 122 | /** |
| 123 | * @since 3.1.0 |
| 124 | * |
| 125 | * @param state |
| 126 | * @param blockId {String} Block clientId or name |
| 127 | * @returns {*} |
| 128 | */ |
| 129 | getBlock( state, blockId ) { |
| 130 | return state.blocks.find( ( { name, clientId } ) => ( |
| 131 | [ name, clientId ].includes( blockId ) |
| 132 | ) ); |
| 133 | }, |
| 134 | getBlockByName( state, fieldName ) { |
| 135 | if ( !fieldName ) { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | const iterateFields = blocks => { |
| 140 | for ( const block of blocks ) { |
| 141 | if ( block.fields.some( |
| 142 | ( { value } ) => value === fieldName, |
| 143 | ) ) { |
| 144 | return block; |
| 145 | } |
| 146 | |
| 147 | if ( !block.innerBlocks?.length ) { |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | iterateFields( block.innerBlocks ); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | iterateFields( state.blocks ); |
| 156 | |
| 157 | return false; |
| 158 | }, |
| 159 | getBlockNameByName( state, fieldName ) { |
| 160 | const block = selectors.getBlockByName( state, fieldName ); |
| 161 | |
| 162 | return block?.name ?? ''; |
| 163 | }, |
| 164 | getBlockById( state, clientId ) { |
| 165 | return state.blockMap[ clientId ] ?? false; |
| 166 | }, |
| 167 | }; |
| 168 | |
| 169 | export default { |
| 170 | ...selectors, |
| 171 | }; |