7.6 KiB
StyleManager
With Style Manager you basically build categories (called sectors) of CSS properties which could be used to custom components and classes. You can init the editor with all sectors and properties via configuration
var editor = grapesjs.init({
...
styleManager: {...} // Check below for the possible properties
...
});
Before using methods you should get first the module from the editor instance, in this way:
var styleManager = editor.StyleManager;
Parameters
configObject Configurations
Examples
...
styleManager: {
sectors: [{
id: 'dim',
name: 'Dimension',
properties: [{
name: 'Width',
property: 'width',
type: 'integer',
units: ['px', '%'],
defaults: 'auto',
min: 0,
}],
}],
}
...
init
Initialize module. Automatically called with a new instance of the editor
Parameters
configObject Configurations
addSector
Add new sector to the collection. If the sector with the same id already exists, that one will be returned
Parameters
Examples
var sector = styleManager.addSector('mySector',{
name: 'My sector',
open: true,
properties: [{ name: 'My property'}]
});
Returns Sector Added Sector
getSector
Get sector by id
Parameters
idstring Sector id
Examples
var sector = styleManager.getSector('mySector');
Returns (Sector | null)
removeSector
Remove a sector by id
Parameters
idstring Sector id
Examples
const removed = styleManager.removeSector('mySector');
Returns Sector Removed sector
getSectors
Get all sectors
Returns Sectors Collection of sectors
addProperty
Add property to the sector identified by id
Parameters
sectorIdstring Sector idpropertyObject Property objectproperty.namestring Name of the property (optional, default'')property.propertystring CSS property, eg.min-height(optional, default'')property.typestring Type of the property: integer | radio | select | color | file | composite | stack (optional, default'')property.unitsArray<string> Unit of measure available, eg. ['px','%','em']. Only for integer type (optional, default[])property.unitstring Default selected unit fromunits. Only for integer type (optional, default'')property.minnumber Min possible value. Only for integer type (optional, defaultnull)property.maxnumber Max possible value. Only for integer type (optional, defaultnull)property.defaultsstring Default value (optional, default'')property.infostring Some description (optional, default'')property.iconstring Class name. If exists no text will be displayed (optional, default'')property.previewBoolean Show layers preview. Only for stack type (optional, defaultfalse)property.functionNamestring Indicates if value need to be wrapped in some function, for istancetransform: rotate(90deg)(optional, default'')property.propertiesArray<Object> Nested properties for composite and stack type (optional, default[])property.layersArray<Object> Layers for stack properties (optional, default[])property.listArray<Object> List of possible options for radio and select types (optional, default[])
Examples
var property = styleManager.addProperty('mySector',{
name: 'Minimum height',
property: 'min-height',
type: 'select',
defaults: '100px',
list: [{
value: '100px',
name: '100',
},{
value: '200px',
name: '200',
}],
});
Returns (Property | null) Added Property or null in case 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
modelModel
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 addType('my-type', { model: {}, view: {}, isType: (value) => { if (value && value.type == 'my-type') { return value; } }, })
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
render
Render sectors and properties
Returns HTMLElement