| 1 |
import './editor.scss'; |
| 2 |
import { addFilter } from '@wordpress/hooks'; |
| 3 |
import { withStylesControls } from './hoc-wrappers/with-styles-controls'; |
| 4 |
import { withBlockUniqueClass } from './hoc-wrappers/with-block-class'; |
| 5 |
|
| 6 |
class CrocoBlockStyleEditor { |
| 7 |
|
| 8 |
init() { |
| 9 |
|
| 10 |
this.blocks = window?.crocoStyleEditorData?.blocks_supports || {}; |
| 11 |
this.supportName = window?.crocoStyleEditorData?.support_name; |
| 12 |
this.defaults = window?.crocoStyleEditorData?.defaults || {}; |
| 13 |
this.usedClasses = {}; |
| 14 |
|
| 15 |
addFilter( |
| 16 |
'blocks.registerBlockType', |
| 17 |
'crocoblock-style-manager/enable-block-support', |
| 18 |
this.registerBlock.bind( this ) |
| 19 |
); |
| 20 |
|
| 21 |
addFilter( |
| 22 |
'editor.BlockEdit', |
| 23 |
'crocoblock-style-manager/enable-block-support', |
| 24 |
withStylesControls |
| 25 |
); |
| 26 |
|
| 27 |
addFilter( |
| 28 |
'editor.BlockListBlock', |
| 29 |
'crocoblock-style-manager/with-crocoblock-editor-class', |
| 30 |
withBlockUniqueClass, |
| 31 |
0 |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
classIsUsed( className, clientId ) { |
| 36 |
|
| 37 |
if ( ! className || ! clientId ) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
if ( this.usedClasses[ className ] ) { |
| 42 |
if ( this.usedClasses[ className ] === clientId ) { |
| 43 |
// It's the same clientId, so we can use this class |
| 44 |
return false; |
| 45 |
} else { |
| 46 |
// The class is already used by another block |
| 47 |
return true; |
| 48 |
} |
| 49 |
} else { |
| 50 |
// The class is not used yet, so we can use it |
| 51 |
this.usedClasses[ className ] = clientId; |
| 52 |
return false; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
getBlockControls( blockName ) { |
| 57 |
return this.blocks[ blockName ] || false; |
| 58 |
} |
| 59 |
|
| 60 |
registerBlock( settings, name ) { |
| 61 |
|
| 62 |
if ( ! this.getBlockControls( name ) ) { |
| 63 |
return settings; |
| 64 |
} |
| 65 |
|
| 66 |
const supports = settings.supports || {}; |
| 67 |
supports[ this.supportName ] = true; |
| 68 |
|
| 69 |
const attributes = settings.attributes || {}; |
| 70 |
|
| 71 |
if ( ! attributes[ this.supportName ] ) { |
| 72 |
|
| 73 |
let defaults = { |
| 74 |
_uniqueClassName: '', |
| 75 |
}; |
| 76 |
|
| 77 |
if ( this.defaults[ name ] ) { |
| 78 |
defaults = { |
| 79 |
...defaults, |
| 80 |
...this.defaults[ name ], |
| 81 |
}; |
| 82 |
} |
| 83 |
|
| 84 |
attributes[ this.supportName ] = { |
| 85 |
type: 'object', |
| 86 |
default: defaults, |
| 87 |
style: true, |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
return { |
| 92 |
...settings, |
| 93 |
supports: supports, |
| 94 |
attributes: attributes, |
| 95 |
}; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
window.crocoBlockStyleEditor = new CrocoBlockStyleEditor(); |
| 100 |
window.crocoBlockStyleEditor.init(); |