您可以在名为“components”的数组中推送组件,在我的示例中,有两个组件。
注意 1:我在我的示例中使用了 angularjs:
var components = [
{
'id' : '0001',
'data' : {
label: `<div>
<div class="my-label-block">Párrafo</div>
</div>`,
content: {
type: "text",
content: '<span style="margin:0; font-family: Arial, Helvetica, sans-serif; font-size: 16px; line-height: 24px;">Ejemplo.</span>'
},
attributes: {
class: "fa 0001",
id: '0001'
},
category: 'Basic element'
}
}
,
{
'id' : '0002',
'data' : {
label: `<div>
<div class="my-label-block">Botón</div>
</div>`,
content:`
<a style="font-family: Arial;">Button </a>
`,
editable: true,
draggable: true,
stylable: true,
category: 'Basic element',
selectable: true,
class : 'fa 0002',
attributes: {
class: "fa 0002",
id: '0002'
}
}
}
]
然后,动态插入你的grapesJS实例:
let bm = $scope.editor.BlockManager;
for (i=0;i<components.length;i++){
if(components[i].id && components[i].data){
bm.add(components[i].id, components[i].data)
}
}