admin管理员组文章数量:1332661
How do you add a custom button to the grapesjs toolbar?
I have followed the instructions on this github issue and written the code below, but the button doesn't appear in the toolbar as expected.
What am I missing?
initToolbar() {
const { em } = this;
const model = this;
const ppfx = (em && em.getConfig('stylePrefix')) || '';
if (!model.get('toolbar')) {
var tb = [];
if (model.collection) {
tb.push({
attributes: { class: 'fa fa-arrow-up' },
mand: ed => ed.runCommand('core:ponent-exit', { force: 1 })
});
}
if (model.get('draggable')) {
tb.push({
attributes: {
class: `fa fa-arrows ${ppfx}no-touch-actions`,
draggable: true
},
//events: hasDnd(this.em) ? { dragstart: 'execCommand' } : '',
mand: 'tlb-move'
});
}
if (model.get('schedule')) {
tb.push({
attributes: { class: 'fa fa-clock', },
mand: 'tlb-settime'
});
}
if (model.get('copyable')) {
tb.push({
attributes: { class: 'fa fa-clone' },
mand: 'tlb-clone'
});
}
if (model.get('removable')) {
tb.push({
attributes: { class: 'fa fa-trash-o' },
mand: 'tlb-delete'
});
}
model.set('toolbar', tb);
}
},
How do you add a custom button to the grapesjs toolbar?
I have followed the instructions on this github issue and written the code below, but the button doesn't appear in the toolbar as expected.
What am I missing?
initToolbar() {
const { em } = this;
const model = this;
const ppfx = (em && em.getConfig('stylePrefix')) || '';
if (!model.get('toolbar')) {
var tb = [];
if (model.collection) {
tb.push({
attributes: { class: 'fa fa-arrow-up' },
mand: ed => ed.runCommand('core:ponent-exit', { force: 1 })
});
}
if (model.get('draggable')) {
tb.push({
attributes: {
class: `fa fa-arrows ${ppfx}no-touch-actions`,
draggable: true
},
//events: hasDnd(this.em) ? { dragstart: 'execCommand' } : '',
mand: 'tlb-move'
});
}
if (model.get('schedule')) {
tb.push({
attributes: { class: 'fa fa-clock', },
mand: 'tlb-settime'
});
}
if (model.get('copyable')) {
tb.push({
attributes: { class: 'fa fa-clone' },
mand: 'tlb-clone'
});
}
if (model.get('removable')) {
tb.push({
attributes: { class: 'fa fa-trash-o' },
mand: 'tlb-delete'
});
}
model.set('toolbar', tb);
}
},
Share
Improve this question
edited Jun 7, 2020 at 17:38
GiorgiosJames
1,04010 silver badges14 bronze badges
asked Jun 2, 2020 at 7:26
Elias KhazzakaElias Khazzaka
1172 silver badges7 bronze badges
1 Answer
Reset to default 6One way to add new toolbar icons is to add the button as each ponent is selected.
// define this event handler after editor is defined
// like in const editor = grapesjs.init({ ...config });
editor.on('ponent:selected', () => {
// whenever a ponent is selected in the editor
// set your mand and icon here
const mandToAdd = 'tlb-settime';
const mandIcon = 'fa fa-clock';
// get the selected ponnet and its default toolbar
const selectedComponent = editor.getSelected();
const defaultToolbar = selectedComponent.get('toolbar');
// check if this mand already exists on this ponent toolbar
const mandExists = defaultToolbar.some(item => item.mand === mandToAdd);
// if it doesn't already exist, add it
if (!mandExists) {
selectedComponent.set({
toolbar: [ ...defaultToolbar, { attributes: {class: mandIcon}, mand: mandToAdd }]
});
}
});
If it's important to you that only ponents with the "schedule" attribute have this toolbar option show up, as in your example, you can access and check this from selectedComponent:
const selectedComponent = editor.getSelected();
const defaultToolbar = selectedComponent.get('toolbar');
const mandExists = defaultToolbar.some(item => item.mand === mandToAdd);
// add this
const hasScheduleAttribute = selectedComponent.attributes.schedule;
if (!mandExists && hasScheduleAttribute) { // ...set toolbar code
本文标签: javascriptHow to add a custom button to the grapesjs toolbarStack Overflow
版权声明:本文标题:javascript - How to add a custom button to the grapesjs toolbar? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742259279a2442207.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论