Search code examples
asp.net-mvcc#-3.0grapesjs

How to Save a page assembled using GrapeJS to a database using ASP.NET MVC


Well, I am developing my own Content Management System using ASP.NET MVC and for a page builder I've decided to use GrapeJS. Now, since this is new to me, I can't seem to find a way to save the pages I would have assembled using GrapeJS. I have used the gjs-preset-webpage GrapeJS plugin and the only JavaScript for the assembling area I have is as shown below:

<script type="text/javascript">
        var editor = grapesjs.init({
        height: '100%',
        showOffsets: 1,
        noticeOnUnload: 0,
        storageManager: { autoload: 0 },
        container: '#gjs',
        fromElement: true,

        plugins: ['gjs-preset-webpage'],
        pluginsOpts: {
            'gjs-preset-webpage': {}
        }
    });
   </script>

The HTML I've used is as follows as well:

<div id="gjs" style="height:0px; overflow:hidden">
    <div class="panel">
        <h1 class="welcome">Welcome to</h1>
        <div class="big-title">
            <svg class="logo" viewBox="0 0 100 100">
                <path d="M40 5l-12.9 7.4 -12.9 7.4c-1.4 0.8-2.7 2.3-3.7 3.9 -0.9 1.6-1.5 3.5-1.5 5.1v14.9 14.9c0 1.7 0.6 3.5 1.5 5.1 0.9 1.6 2.2 3.1 3.7 3.9l12.9 7.4 12.9 7.4c1.4 0.8 3.3 1.2 5.2 1.2 1.9 0 3.8-0.4 5.2-1.2l12.9-7.4 12.9-7.4c1.4-0.8 2.7-2.2 3.7-3.9 0.9-1.6 1.5-3.5 1.5-5.1v-14.9 -12.7c0-4.6-3.8-6-6.8-4.2l-28 16.2" />
            </svg>
            <span>GrapesJS</span>
        </div>
        <div class="description">
            This is a demo content from index.html. For the development, you shouldn't edit this file, instead you can
            copy and rename it to _index.html, on next server start the new file will be served, and it will be ignored by git.
        </div>
    </div>
    <style>
        .panel {
            width: 90%;
            max-width: 700px;
            border-radius: 3px;
            padding: 30px 20px;
            margin: 150px auto 0px;
            background-color: #d983a6;
            box-shadow: 0px 3px 10px 0px rgba(0,0,0,0.25);
            color: rgba(255,255,255,0.75);
            font: caption;
            font-weight: 100;
        }

        .welcome {
            text-align: center;
            font-weight: 100;
            margin: 0px;
        }

        .logo {
            width: 70px;
            height: 70px;
            vertical-align: middle;
        }

            .logo path {
                pointer-events: none;
                fill: none;
                stroke-linecap: round;
                stroke-width: 7;
                stroke: #fff
            }

        .big-title {
            text-align: center;
            font-size: 3.5rem;
            margin: 15px 0;
        }

        .description {
            text-align: justify;
            font-size: 1rem;
            line-height: 1.5rem;
        }
    </style>
</div> 

Now that I have embedded it into my project and can assembled pages using it, I can't seem to find how to save or where would the assembled page be to be saved. Can anyone help me?


Solution

  • You need a endpoint to allow grapes' storage manager to send your site current status information. I save the json and html just in case, but I think json is enough. Well then you setup your storage manager.

    const editor = grapesjs.init({
       storageManager:{
           type: 'remote',
           autosave: true, // Store data automatically
           urlStore: 'YOUR_ENDPOINT_URL',
       }
    });
    

    You will get a call to your endpoint every time something changes with the following parameterts:

    • gjs-assets: Assets array
    • gjs-components: Object with markup definition of your site
    • gjs-styles: Object with styles definition
    • gjs-html: your site HTML
    • gjs-css: your CSS

    Just be sure to inject this definitions when initialising grapes as well:

    const editor = grapesjs.init({
           components: "YOUR_STORED_COMPONENTS_HERE",
           style: "YOUR_STORED_CSS_HERE",
           storageManager:{
               type: 'remote',
               autosave: true, // Store data automatically
               urlStore: 'YOUR_ENDPOINT_URL',
           }
        });
    

    For further information: Docs