【问题标题】:How to add a custom button to the grapesjs toolbar?如何向grapesjs工具栏添加自定义按钮?
【发布时间】:2020-06-08 01:38:08
【问题描述】:

如何在grapesjs工具栏中添加自定义按钮?

我已按照this github issue 上的说明编写了下面的代码,但该按钮未按预期出现在工具栏中。

我错过了什么?

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' },
                    command: ed => ed.runCommand('core:component-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' } : '',
                    command: 'tlb-move'
                });
            }
            if (model.get('schedule')) {
                tb.push({
                    attributes: { class: 'fa fa-clock', },
                    command: 'tlb-settime'
                });
            }
            if (model.get('copyable')) {
                tb.push({
                    attributes: { class: 'fa fa-clone' },
                    command: 'tlb-clone'
                });
            }
            if (model.get('removable')) {
                tb.push({
                    attributes: { class: 'fa fa-trash-o' },
                    command: 'tlb-delete'
                });
            }
            model.set('toolbar', tb);
        }
    },

【问题讨论】:

    标签: javascript html grapesjs


    【解决方案1】:

    添加新工具栏图标的一种方法是在选择每个组件时添加按钮。

      // define this event handler after editor is defined
      // like in const editor = grapesjs.init({ ...config });
      editor.on('component:selected', () => {
    
        // whenever a component is selected in the editor
    
        // set your command and icon here
        const commandToAdd = 'tlb-settime';
        const commandIcon = 'fa fa-clock';
    
        // get the selected componnet and its default toolbar
        const selectedComponent = editor.getSelected();
        const defaultToolbar = selectedComponent.get('toolbar');
    
        // check if this command already exists on this component toolbar
        const commandExists = defaultToolbar.some(item => item.command === commandToAdd);
    
        // if it doesn't already exist, add it
        if (!commandExists) {
          selectedComponent.set({
            toolbar: [ ...defaultToolbar, {  attributes: {class: commandIcon}, command: commandToAdd }]
          });
        }
    
      });
    

    如果您认为只有具有“schedule”属性的组件才会显示此工具栏选项对您很重要,如您的示例所示,您可以从 selectedComponent 访问并检查它:

    const selectedComponent = editor.getSelected();
    const defaultToolbar = selectedComponent.get('toolbar');
    const commandExists = defaultToolbar.some(item => item.command === commandToAdd);
    
    // add this
    const hasScheduleAttribute = selectedComponent.attributes.schedule;
    
    if (!commandExists && hasScheduleAttribute) { // ...set toolbar code
    

    【讨论】:

    • 我尝试了很多设置工具栏,甚至在我按下图标时提醒我它可以工作,但没有任何效果。如果您能提供帮助,我们将不胜感激。
    • 亲爱的@EliasKhazzaka,如果我的回答成功回答了您的原始问题(如何在grapesjs 工具栏中添加按钮),请将我的回答标记为正确。如果您有一个涉及如何正确设置grapesjs 命令的后续问题,请创建另一个stackoverflow 问题,我很乐意看看。干杯。
    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多