8.0 KiB
StyleManager
With Style Manager you build categories (called sectors) of CSS properties which could be used to customize the style of components. You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object
const editor = grapesjs.init({
styleManager: {
// options
}
})
Once the editor is instantiated you can use its API. Before using these methods you should get the module from the instance
const styleManager = editor.StyleManager;
- getConfig
- addSector
- getSector
- removeSector
- getSectors
- addProperty
- getProperty
- removeProperty
- getProperties
- getModelToStyle
- addType
- getType
- getTypes
- createType
getConfig
Get configuration object
Returns Object
addSector
Add new sector. If the sector with the same id already exists, that one will be returned.
Parameters
-
idString Sector id -
sectorObject Sector definition. Check the available properties -
optionsObject Options (optional, default{})options.atNumber? Position index (by default, will be appended at the end).
Examples
const sector = styleManager.addSector('mySector',{
name: 'My sector',
open: true,
properties: [{ name: 'My property'}]
}, { at: 0 });
// With `at: 0` we place the new sector at the beginning of the list
Returns Sector Added Sector
getSector
Get sector by id.
Parameters
idString Sector idopts(optional, default{})
Examples
const sector = styleManager.getSector('mySector');
Returns (Sector | null)
removeSector
Remove sector by id.
Parameters
idString Sector id
Examples
const removed = styleManager.removeSector('mySector');
Returns Sector Removed sector
getSectors
Get all sectors.
Parameters
opts(optional, default{})
Examples
const sectors = styleManager.getSectors();
Returns Collection<Sector> Collection of sectors
addProperty
Add new property to the sector.
Parameters
-
sectorIdString Sector id. -
propertyObject Property definition. Check the base available properties + others based on thetypeof your property. -
opts(optional, default{}) -
optionsObject Options (optional, default{})options.atNumber? Position index (by default, will be appended at the end).
Examples
const property = styleManager.addProperty('mySector', {
label: 'Minimum height',
property: 'min-height',
type: 'select',
default: '100px',
options: [
{ id: '100px', label: '100' },
{ id: '200px', label: '200' },
],
}, { at: 0 });
Returns (Property | null) Added property or null in case the sector doesn't exist.
getProperty
Get property by its CSS name and sector id
Parameters
Examples
var property = styleManager.getProperty('mySector','min-height');
Returns (Property | null)
removeProperty
Remove a property from the sector
Parameters
Examples
const property = styleManager.removeProperty('mySector', 'min-height');
Returns Property Removed property
getProperties
Get properties of the sector
Parameters
sectorIdstring Sector id
Examples
var properties = styleManager.getProperties('mySector');
Returns Properties Collection of properties
getModelToStyle
Get what to style inside Style Manager. If you select the component without classes the entity is the Component itself and all changes will go inside its 'style' property. Otherwise, if the selected component has one or more classes, the function will return the corresponding CSS Rule
Parameters
modelModeloptions(optional, default{})
Returns Model
addType
Add new property type
Parameters
idstring Type IDdefinitionObject Definition of the type. Each definition containsmodel(business logic),view(presentation logic) andisTypefunction which recognize the type of the passed entity
Examples
styleManager.addType('my-custom-prop', {
create({ props, change }) {
const el = document.createElement('div');
el.innerHTML = '<input type="range" class="my-input" min="10" max="50"/>';
const inputEl = el.querySelector('.my-input');
inputEl.addEventListener('change', event => change({ event })); // change will trigger the emit
inputEl.addEventListener('input', event => change({ event, complete: false }));
return el;
},
emit({ props, updateStyle }, { event, complete }) {
const { value } = event.target;
const valueRes = value + 'px';
// Pass a string value for the exact CSS property or an object containing multiple properties
// eg. updateStyle({ [props.property]: valueRes, color: 'red' });
updateStyle(valueRes, { complete });
},
update({ value, el }) {
el.querySelector('.my-input').value = parseInt(value, 10);
},
destroy() {
// In order to prevent memory leaks, use this method to clean, eventually, created instances, global event listeners, etc.
}
})
getType
Get type
Parameters
idstring Type ID
Returns Object Type definition
getTypes
Get all types
Returns Array
createType
Create new property from type
Parameters
Examples
const propView = styleManager.createType('integer', {
model: {units: ['px', 'rem']}
});
propView.render();
propView.model.on('change:value', ...);
someContainer.appendChild(propView.el);
Returns PropertyView
getBuiltIn
Return built-in property definition
Parameters
propString Property name.
Returns (Object | null) Property definition.
getBuiltInAll
Get all the available built-in property definitions.
Returns Object
addBuiltIn
Add built-in property definition. If the property exists already, it will extend it.
Parameters
Examples
const sector = styleManager.addBuiltIn('new-property', {
type: 'select',
default: 'value1',
options: [{ id: 'value1', label: 'Some label' }, ...],
})
Returns Object Added property definition.