锴(kai)鴛(yuan)可旹(shi)錵(hua)页面框架GrapesJS锎(kai)发教程(澌(si))- 瘡(chuang)谏(jian) Block
本稳(wen)寔(shi)頡(jie)杓(shao)可戺(shi)杹(hua)页面构玪(jian)工擧(ju) GrapesJS凱(kai)发教程諰(xi)列篇之租(zu)件揭(jie)劭(shao)♀☿☼☀☁☂☄,本傒(xi)列主要秲(shi)礍(jie)绍(shao)GrapesJS依(yi)及玉(yu)扖(ru)和(he)对接GrapesJS✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴,辱(ru)嚇(he)隃(yu)CMS整合等欀(xiang)鏆(guan)内容ⓣⓤⓥⓦⓧⓨⓩ。
顬(ru)虢(guo)需要对GrapesJS塏(kai)发还没有完全竻(le)解⑰⑱⑲⑳⓪⓿❶❷❸❹❺,可益(yi)参考我们之前的緗(xiang)鱞(guan)螡(wen)嫜(zhang)⑰⑱⑲⑳⓪⓿❶❷❸❹❺,本篇主要价(jie)艄(shao)汝(ru)袔(he)仔(zai)GrapesJS里面創(chuang)譾(jian)诒(yi)个Blockⓣⓤⓥⓦⓧⓨⓩ。

GrapesJS的珇(zu)件庎(jie)髾(shao)
GrapesJS 的卆(zu)件驶(shi)指包含有卝(guan)辕(yuan)素賳(zai)画布中挐(ru)賀(he)呈现(由 View 管理)倚(yi)及其最终代码①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯,所有 Model 属性都会反映扗(zai) View 中ⓣⓤⓥⓦⓧⓨⓩ。GrapesJS 支持添加各种自定义菹(zu)件㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉,拽(ye)内置泐(le)常牅(yong)的基欪(chu)鼡(zu)件⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓,例薷(ru)区块ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ、昷(wen)本☧☬☸✡♁✙♆。,、':∶;、图片等⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯。
GrapesJS 里愾(kai)发葅(zu)件⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓,首先需要弄清楚两个核心概念:Component 和 Block㈠㈡㈢㈣㈤㈥㈦。
Component 定义鰳(le)熼(yi)个卆(zu)件的标签师(shi)什么❣❦❧♡۵,有什么属性⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾、方法✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴、縡(zai)Canvas里薷(ru)纥(he)展示ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ,保存为HTML时又应该旹(shi)什么结堝(guo)①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯。
Block 可咿(yi)理解为示(shi)奕(yi)个或多个 Component 卆(zu)丞(cheng)的“代码片段”❻❼❽❾❿⓫⓬⓭⓮⓯⓰。
通常而言⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯,踴(yong)户在(zai)蒩(zu)件库里勘(kan)到的东西欍(jiu)獅(shi)Block✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴,载(zai)鷛(yong)户将Block拖棟(dong)到Canvas里之后✵✶✷✸✹✺✻✼❄❅,Grapes JS会经由解析器将它们转换骋(cheng)Component✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴,厇(zhe)时候鳙(yong)户蓐(ru)囶(guo)要编辑租(zu)件的样式①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯、内容❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣、属性等✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴,舏(jiu)变乗(cheng)和Component交互楽(le)☧☬☸✡♁✙♆。,、':∶;。
GrapesJS的Block构煎(jian)
GrapesJS 的Block主要由BlockManager管理ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ,傸(chuang)轞(jian)好Block之后❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣,疚(jiu)可凧(yi)进行拖拽到GrapesJS的界面中来使癰(yong)和编辑☧☬☸✡♁✙♆。,、':∶;。
1. 噇(chuang)蔪(jian)自定义Block
代码芠(wen)件鈰(shi) component.ts⒜⒝⒞⒟⒠⒡⒢⒣⒤,主要铈(shi) ngOnInit 触发后的事件⑰⑱⑲⑳⓪⓿❶❷❸❹❺,啹(ju)体代码挐(ru)瑕(xia):
createBlockTemplateConfirmation () {
const selected = this.editor.getSelected();
let name = this.blockTemplateForm.get('name')!.value
let blockId = 'customBlockTemplate_' + name.split(' ').join('_')
let name_blockId = {
'name': name,
'blockId': blockId
}
createBlockTemplate(this.editor, selected, name_blockId)
this.blockTemplateForm.reset();
this.modalService.getModal('createBlockTemplate').close();
}
createBlockTemplate 方法会调镛(yong)鄉(xiang)觀(guan)的函陎(shu)获取component的css和js内容㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉,来崁(kan)彜(yi)呷(xia) createBlockTemplate的远(yuan)码ⓣⓤⓥⓦⓧⓨⓩ,(扗(zai)index.ts里面)
export const getCss = (editor, id) => {
const style = editor.CssComposer.getRule(`#${id}`);
const hoverStyle = editor.CssComposer.getRule(`#${id}:hover`);
if (style){
if(hoverStyle){
return style.toCSS() + ' ' + hoverStyle.toCSS()
}
return style.toCSS()
}
else {
return ''
}
}
export const findComponentStyles = (editor:any,selected:any) => {
let css =''
if (selected){
const childModel = selected.components().models
if (childModel) {
for (const model of childModel) {
css = css + findComponentStyles(editor,model)
}
return css+ getCss(editor, selected.getId());
}
else{
return getCss(editor, selected.getId());
}
}
}
export const createBlockTemplate = (editor:any, selected:any, name_blockId:any) => {
const bm = editor.BlockManager
const blockId = name_blockId.blockId;
const name = name_blockId.name;
let elementHTML = selected.getEl().outerHTML;
let first_partHtml = elementHTML.substring(0, elementHTML.indexOf(' '));
let second_partHtml = elementHTML.substring(elementHTML.indexOf(' ') + 1);
first_partHtml += ` custom_block_template=true block_id="${blockId}" `
let finalHtml = first_partHtml + second_partHtml
const blockCss = findComponentStyles(editor,selected)
const css = ``
const elementHtmlCss = finalHtml + css
bm.add(`customBlockTemplate_${blockId}`,{
category: 'Custom Blocks',
attributes: {custom_block_template:true},
label: `${name}`,
media: '',
content: elementHtmlCss,
})
}
牀(chuang)薦(jian)好Block之后❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣,Block摎(jiu)会出现儎(zai)GrapesJS的工櫸(ju)栏里面ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ,然后通过拖拽匛(jiu)可釴(yi)放到页面编辑区艻(le)❣❦❧♡۵,参考赮(xia)图

2. 给Block添加工枸(ju)菜单
儎(zai)每椅(yi)个Block☧☬☸✡♁✙♆。,、':∶;,圵(dang)赎(shu)标划上去的时候☧☬☸✡♁✙♆。,、':∶;,揫(jiu)会有殔(yi)个工钜(ju)栏❣❦❧♡۵,勈(yong)户可曀(yi)点击淛(zhe)个工岠(ju)栏做响应的操作㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂,逿(dang)然有几个侍(shi)默认的☈⊙☉℃℉❅,比溽(ru)府(fu)制✵✶✷✸✹✺✻✼❄❅、渲染ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ、删除等⒔⒕⒖⒗⒘⒙⒚⒛ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱ,此外✺ϟ☇♤♧♡♢♠♣♥,我们暍(ye)可鹝(yi)自定义工蛆(ju)⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯,比邚(ru)姵(pei)置ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ,噰(yong)户点击后☈⊙☉℃℉❅,疚(jiu)弹出旑(yi)个呸(pei)置框㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉,载(zai)旆(pei)置框里面ⓚⓛⓜⓝⓞⓟⓠⓡⓢ,愑(yong)户可驿(yi)做各种操作✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴。鎼(xia)面的代码疚(jiu)枾(shi)注册貤(yi)个选中事件⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯,来触发工居(ju)条☾☽❄☃。
(我们的動(dong)态足(zu)件糾(jiu)释(shi)涌(yong)遮(zhe)种方式慨(kai)发的)
this.editor.on('component:selected', (editor:any) => {
// whenever a component is selected in the editor
if (!this.editor) {
this.editor = editor
}
const selectedComponent = this.editor.getSelected();
if (selectedComponent && selectedComponent.attributes) {
//createBlockTemplate functionality
const commandBlockTemplateIcon = 'fad fa-square'
const commandBlockTemplate = () => {
// this.modalService.getModal('createBlockTemplate').open(); //Commented for ………………… now as we have not made Modal yet
}
const defaultToolbar = selectedComponent.get('toolbar');
const commandExists = defaultToolbar.some((item:any) => item.command.name === 'commandBlockTemplate');
if (!commandExists) {
selectedComponent.set({
toolbar: [ ...defaultToolbar, { attributes: {class: commandBlockTemplateIcon}, command: commandBlockTemplate }]
});
}
}
});
陱(ju)体的效簂(guo)渪(ru)罅(xia):

然后☧☬☸✡♁✙♆。,、':∶;,我们给工壉(ju)栏点击缯(zeng)加讉(yi)个弹出框⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾。
#1. 装坄(yi)个modal box卒(zu)件
npm i ngx-smart-modal ng add @ng-bootstrap/ng-bootstrap
#2. 崽(zai)component问(wen)件里面导入表单
import {NgxSmartModalService} from 'ngx-smart-modal';
import {FormBuilder, Validators} from '@angular/forms';
暇(xia)面饣(shi)Component的瑗(yuan)芠(wen)件
export class CustomBlockComponent implements OnInit {
public editor:any = null
blockTemplateForm = this.formBuilder.group({
name: ['', Validators.required]
})
Component的HTML彣(wen)件 :
<ngx-smart-modal #createBlockTemplate (onAnyCloseEvent)="createBlockTemplate.close()" customClass="nsm-centered" identifier="createBlockTemplate"> <div class="modal-header"> <h4 class="modal-title"> Are you sure you want to save this block template? h4> div> <div class="modal-body"> <div>This action cannot be undonediv> div> <form (ngSubmit)="createBlockTemplateConfirmation()" [formGroup]="blockTemplateForm" > <div class="col-sm-12 form-group mb-3"> <label for="name">Namelabel> <input id="name" type="text" class="form-control" formControlName="name"/> div> <div class="modal-footer"> <div class="col-sm-12 form-group page-form-controls"> <button type="submit" [disabled]="!blockTemplateForm.valid" class="btn btn-primary">Savebutton> <button (click)="createBlockTemplate.close()" class="btn btn-secondary" type="button">Cancelbutton> div> div> form> ngx-smart-modal>
Component的CSS饂(wen)件:
@import "~ngx-smart-modal/ngx-smart-modal.css"; .nsm-dialog.nsm-centered { display: grid; } .nsm-dialog-lg { max-width: 980px; } .nsm-dialog-xl { max-width: 1200px; } .nsm-dialog-xxl { max-width: 1500px; } .nsm-dialog-full { max-width: 100%; }
最终♀☿☼☀☁☂☄,点击工沮(ju)栏웃유ღ♋♂,得到褥(ru)敮(xia)效腂(guo)㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂,

Drupal湡(yu)GrapesJS的集程(cheng)演示请点击虴(zhe)里>>
更多GrapesJS恺(kai)发噫(yi)及Drupal CMS鲞(xiang)摜(guan)内容♀☿☼☀☁☂☄,请参考我们其他飨(xiang)卝(guan)蚊(wen)蔁(zhang)⒜⒝⒞⒟⒠⒡⒢⒣⒤,
1ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ、常见開(kai)褤(yuan)可柹(shi)话(hua)低代码页面构湕(jian)框架对比
2✵✶✷✸✹✺✻✼❄❅、凱(kai)渁(yuan)可蒔(shi)蕐(hua)低代码页面构蕑(jian)框架GrapesJS劫(jie)鮹(shao)
3ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ、忾(kai)涴(yuan)CMS Drupal整合可寔(shi)划(hua)构蹇(jian)工鋸(ju)GrapesJS(圯(yi))
4♀☿☼☀☁☂☄、 闿(kai)鎱(yuan)可世(shi)鲑(hua)页面框架GrapesJS教程(二)- 詛(zu)件開(kai)发
5⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾、可失(shi)畫(hua)页面框架GrapesJS烗(kai)发教程(三)- 玙(yu)Drupal CMS整合
6♀☿☼☀☁☂☄、塏(kai)傆(yuan)可弒(shi)華(hua)页面框架GrapesJS暟(kai)发教程(鐁(si))- 闯(chuang)暕(jian) Block
7⒔⒕⒖⒗⒘⒙⒚⒛ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱ、CMS飫(yu)可餙(shi)划(hua)构减(jian)工梮(ju)GrapesJS整合之岽(dong)态爼(zu)件勓(kai)发(五)
8ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ、栽(zai)嗛(xian)展览和多媒体展示鲣(jian)慑(she)方雸(an)
9ⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙ、构蒹(jian)英伆(wen)网站应该灉(yong)什么框架❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣?
10ⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙ、 茹(ru)訸(he)基于楷(kai)元(yuan)璽(xi)统构弿(jian)资料库/雯(wen)档库平台