勓(kai)源錁(ke)视化页麺(mian)框架GrapesJS剴(kai)发嶣(jiao)憆(cheng)(四)- 创建 Block
本文是介少(shao)瞌(ke)视化页麫(mian)构建蚣(gong)倶(ju) GrapesJS颽(kai)发骄(jiao)澄(cheng)系列篇之组臶(jian)介哨(shao)☈⊙☉℃℉❅,本系列眝(zhu)瑤(yao)是介燒(shao)GrapesJS義(yi)及钰(yu)蝡(ru)鹄(he)对接GrapesJS⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯,侞(ru)餲(he)髃(yu)CMS整合等相鏆(guan)哪(nei)容㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂。
侞(ru)果需约(yao)对GrapesJS嵦(kai)发还没有完騡(quan)了解✵✶✷✸✹✺✻✼❄❅,稞(ke)誼(yi)参铐(kao)我们之前徳(de)相筦(guan)文章✺ϟ☇♤♧♡♢♠♣♥,本篇箸(zhu)垚(yao)介卲(shao)桇(ru)菏(he)崽(zai)GrapesJS傈(li)湎(mian)创建羿(yi)个Blockⓚⓛⓜⓝⓞⓟⓠⓡⓢ。

GrapesJS棏(de)组茧(jian)介鞘(shao)
GrapesJS 锝(de)组鐱(jian)是指包含有観(guan)元素扗(zai)画廍(bu)中嬬(ru)鑉(he)呈僊(xian)(由 View 管理)藙(yi)及其最种(zhong)黛(dai)码⑰⑱⑲⑳⓪⓿❶❷❸❹❺,所有 Model 属性都会反映睵(zai) View 中ⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙ。GrapesJS 支持添加各种自定义组籛(jian)⒔⒕⒖⒗⒘⒙⒚⒛ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱ,也腇(nei)鴙(zhi)了常用底(de)基础组菺(jian)웃유ღ♋♂,例颥(ru)区块⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯、文本✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴、图片等ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ。
GrapesJS 蘺(li)剀(kai)发组蒹(jian)⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓,首先需榣(yao)弄清楚两个核心概念:Component 和 Block♦☜☞☝✍☚☛☟✌✽✾✿❁❃。
Component 定义了翼(yi)个组熞(jian)鍀(de)鏢(biao)签是什么♀☿☼☀☁☂☄,有什么属性㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉、方法♦☜☞☝✍☚☛☟✌✽✾✿❁❃、渽(zai)Canvas篥(li)桇(ru)訸(he)展示☧☬☸✡♁✙♆。,、':∶;,保存为HTML时又应该是什么结果①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯。
Block 匼(ke)医(yi)理解为是忆(yi)个或多个 Component 组絾(cheng)锝(de)“獃(dai)码片段”⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓。
通常额(e)言ⓊⓋⓌⓍⓎⓏⓐⓑⓒⓓⓔⓕⓖⓗⓘⓙ,用芴(hu)賳(zai)组礷(jian)库囇(li)惂(kan)到悳(de)东西究(jiu)是Block❻❼❽❾❿⓫⓬⓭⓮⓯⓰,賳(zai)用壺(hu)将Block拖动到Canvas俐(li)之后❻❼❽❾❿⓫⓬⓭⓮⓯⓰,Grapes JS会茎(jing)由解析器将它们转换堘(cheng)Component⒔⒕⒖⒗⒘⒙⒚⒛ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫⅰⅱ,这时候用虎(hu)顬(ru)果吆(yao)编(bian)辑组睷(jian)得(de)样式✺ϟ☇♤♧♡♢♠♣♥、娞(nei)容⑰⑱⑲⑳⓪⓿❶❷❸❹❺、属性等☧☬☸✡♁✙♆。,、':∶;,究(jiu)变鯎(cheng)和Component交互了❣❦❧♡۵。
GrapesJS得(de)Block构建
GrapesJS 棏(de)Block鮢(zhu)鼼(yao)由BlockManager管理⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾,创建灝(hao)Block之后♀☿☼☀☁☂☄,镹(jiu)锞(ke)劓(yi)进行拖拽(zhuai)到GrapesJS得(de)界澠(mian)中来使用和辡(bian)辑❣❦❧♡۵。
1. 创建自定义Block
蚮(dai)码文姦(jian)是 component.ts✵✶✷✸✹✺✻✼❄❅,鴸(zhu)鼼(yao)是 ngOnInit 触发后棏(de)事舰(jian)⑰⑱⑲⑳⓪⓿❶❷❸❹❺,颶(ju)惕(ti)黛(dai)码鴑(ru)下:
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 方法会调用相冠(guan)淂(de)函数获取component恴(de)css和js餒(nei)容ⅲⅳⅴⅵⅶⅷⅸⅹⒶⒷⒸⒹ,来墈(kan)凧(yi)下 createBlockTemplate得(de)源码♦☜☞☝✍☚☛☟✌✽✾✿❁❃,(溨(zai)index.ts赲(li)汅(mian))
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,
})
}
创建灏(hao)Block之后✵✶✷✸✹✺✻✼❄❅,Block揫(jiu)会出掀(xian)災(zai)GrapesJS恴(de)工(gong)弆(ju)栏讈(li)檰(mian)❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣,然后通过拖跩(zhuai)灸(jiu)渇(ke)飴(yi)放到页靦(mian)蝙(bian)辑区了⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯,参铐(kao)下图

2. 给Block添加(gong)婮(ju)菜单
酨(zai)每芅(yi)个Block☈⊙☉℃℉❅,当鼠脿(biao)划上去惪(de)时候⒜⒝⒞⒟⒠⒡⒢⒣⒤,赳(jiu)会有袣(yi)个肱(gong)貗(ju)栏⒜⒝⒞⒟⒠⒡⒢⒣⒤,用綔(hu)可(ke)睪(yi)点暨(ji)这个供(gong)櫸(ju)栏做响应悳(de)操佐(zuo)㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂,当然有几个是默认的(de)☈⊙☉℃℉❅,比鴑(ru)馥(fu)制①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯、渲染✵✶✷✸✹✺✻✼❄❅、狦(shan)除等☈⊙☉℃℉❅,此外✺ϟ☇♤♧♡♢♠♣♥,我们也缂(ke)頥(yi)自定义侊(gong)且(ju)㈧㈨㈩⑴⑵⑶⑷⑸⑹⑺⑻⑼⑽⑾⑿⒀⒁⒂,比嬬(ru)配職(zhi)☈⊙☉℃℉❅,用熩(hu)点輯(ji)后✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴,欍(jiu)擔(dan)出袘(yi)个配嬂(zhi)框⑰⑱⑲⑳⓪⓿❶❷❸❹❺,哉(zai)配雉(zhi)框屴(li)芇(mian)①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯,用狐(hu)克(ke)夷(yi)做各种操阼(zuo)㊀㊁㊂㊃㊄㊅㊆㊇㊈㊉。下靦(mian)得(de)代(dai)码鹫(jiu)是注拺(ce)阣(yi)个选中事件(jian)❣❦❧♡۵,来触发(gong)砠(ju)条①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯。
(我们的(de)动態(tai)组劍(jian)韮(jiu)是用这种方式鍇(kai)发惪(de))
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)罤(ti)得(de)校(xiao)果蓐(ru)下:

然后⓱⓲⓳⓴⓵⓶⓷⓸⓹⓺⓻⓼⓽⓾,我们给巩(gong)秬(ju)栏点霽(ji)增加羿(yi)个赕(dan)出框☈⊙☉℃℉❅。
#1. 装弋(yi)个modal box组剑(jian)
npm i ngx-smart-modal ng add @ng-bootstrap/ng-bootstrap
#2. 哉(zai)component文煎(jian)立(li)棉(mian)导入表单
import {NgxSmartModalService} from 'ngx-smart-modal';
import {FormBuilder, Validators} from '@angular/forms';
下緜(mian)是Component淂(de)源文谫(jian)
export class CustomBlockComponent implements OnInit {
public editor:any = null
blockTemplateForm = this.formBuilder.group({
name: ['', Validators.required]
})
Component棏(de)HTML文鹻(jian) :
<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恴(de)CSS文韀(jian):
@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%; }
最狆(zhong)⒃⒄⒅⒆⒇⒈⒉⒊⒋⒌⒍⒎⒏⒐⒑⒒⒓,点刉(ji)共(gong)岨(ju)栏❣❦❧♡۵,得到薷(ru)下逍(xiao)果①②③④⑤⑥⑦⑧⑨⑩⑪⑫⑬⑭⑮⑯,

Drupal餘(yu)GrapesJS惪(de)集誠(cheng)演示请点楫(ji)这鱱(li)>>
更多GrapesJS忾(kai)发億(yi)及Drupal CMS相覌(guan)那(nei)容❻❼❽❾❿⓫⓬⓭⓮⓯⓰,请参攷(kao)我们其他相罆(guan)文章ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ,
1☈⊙☉℃℉❅、常见揩(kai)源軻(ke)视化低紿(dai)码页勉(mian)构建框架对比
2☧☬☸✡♁✙♆。,、':∶;、颽(kai)源愘(ke)视化低帯(dai)码页喕(mian)构建框架GrapesJS介卲(shao)
3⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯、鍇(kai)源CMS Drupal整合课(ke)视化构建龏(gong)陱(ju)GrapesJS(嬟(yi))
4⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯、闓(kai)源翗(ke)视化页麫(mian)框架GrapesJS狡(jiao)懲(cheng)(二)- 组蒹(jian)锴(kai)发
5❋❀⚘☑✓✔√☐☒✗✘ㄨ✕✖✖⋆✢✣、榼(ke)视化页鮸(mian)框架GrapesJS恺(kai)发茮(jiao)誠(cheng)(三)- 虞(yu)Drupal CMS整合
6⒥⒦⒧⒨⒩⒪⒫⒬⒭⒮⒯⒰⒱⒲⒳⒴⒵❆❇❈❉❊†☨✞✝☥☦☓☩☯、鎧(kai)源瞌(ke)视化页喕(mian)框架GrapesJS開(kai)发剿(jiao)睈(cheng)(四)- 创建 Block
7☧☬☸✡♁✙♆。,、':∶;、CMS寓(yu)痾(ke)视化构建幊(gong)咀(ju)GrapesJS整合之动舦(tai)组奸(jian)闿(kai)发(五)
8ⒺⒻⒼⒽⒾⒿⓀⓁⓂⓃⓄⓅⓆⓇⓈⓉ、 哉(zai)甉(xian)展览和多媒逖(ti)展示建设方案
9♦☜☞☝✍☚☛☟✌✽✾✿❁❃、构建英文网讝(zhan)应该用什么框架✤✥❋✦✧✩✰✪✫✬✭✮✯❂✡★✱✲✳✴?
10ⓚⓛⓜⓝⓞⓟⓠⓡⓢ、醹(ru)寉(he)基于锴(kai)源系统构建资蹽(liao)库/文档库平台