Skip to content

Custom Code unable to be used if user clicks save without entering any text #33

Open

Description

@Wayne-Mather

If a user drags the custom code block onto the canvas, and then clicks save without entering any text - the component can no longer be edited - The only choice is to delete and start again (assuming the user understands the layers).

Would it be possible in the onceclose() function to not exit if the content is currently empty?

Activity

  1. Baki995 commented on Nov 26, 2024

    @Baki995

    I fixed it on this way

    const editor = grapesjs.init({

    `plugins: [
        'grapesjs-custom-code'
    ],
    pluginsOpts: {
        'grapesjs-custom-code': {
                propsCustomCode: {
                    components: {},
                },
                commandCustomCode: {
                    handleSave() {
                        const keyCustomCode = 'custom-code-plugin__code';
                        const { target, em } = this;
                        const code = this.getCodeViewer().getContent();
                        target?.set(keyCustomCode, code);
                        // eslint-disable-next-line
                        editor.Modal.close();
                        em.getSelected().view.render(); // this is important part
                    },
                }
        }
    }
    

    });

    And created the new type with the same id to rewrite logic

    editor.Components.addType('custom-code', {

        `view: {
            onRender({ el }) {
                setTimeout(() => {
                    if (el.offsetHeight === 0) { // no content or without height. It should be clickable 
                        const defaultEl = document.createElement('p');
                        defaultEl.classList.add('p-4', 'border', 'border-dark');
                        defaultEl.textContent = 'Default embed code placeholder';
                        el.appendChild(defaultEl);
                    }
                }, 500); // If we have some script with src, then we should wait for load
            },
        },
    });`
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions