GrapesJs and PHP - store and load data to show in editor and as HTML page as well

后端 未结 3 916
鱼传尺愫
鱼传尺愫 2020-12-21 05:54

I am using GrapesJS to build a simple webpage.

I included the script in the following way inside head part :



        
3条回答
  •  清歌不尽
    2020-12-21 06:17

    for Q3 you can use a "frontend option" like this:

    NOTE 1: i'm using angularJS

    NOTE 2: $scope.editor is my grapesJs instance

    Function to get HTML+CSS inline

    $scope.getHtmlCss = function(){
        var html = $scope.editor.runCommand('gjs-get-inlined-html');
        $log.log("-----> HTML & INLINED CSS: ", html);
        return html;
    }
    

    In your GrapesJs editor add an new option for "Donwload HTML file"

              $scope.editor.Panels.addButton('options', [ 
                { id: 'save', 
                  className: 'fa fa-file-code-o', 
                  command: function(editor1, sender) { 
    
                    if($scope.getHtmlCss()){
                      $scope.generateFile($scope.getHtmlCss());                      
                    }
    
                  }, 
                  attributes: { title: 'Download HMTL file' } 
                }, ]);
    

    function to generate HTML file:

    $scope.generateFile = function(code){
    
          $(function() 
            {
              console.log( "ready!" );
              // Check for the various File API support.
              if (window.File && window.FileReader && window.FileList && window.Blob) 
              {
                saveText(code, 'text/html;charset=utf-8', 'mailing.html');
              } 
              else 
              {
                alert('The File APIs are not fully supported in this browser.');
              }
            }
           );
    
          function saveText(text, mime, file)
          {
            var blob = new Blob([text], {type: mime});
            saveAs(blob, file);
    
            return false;
          }
    
          function handleFileSelect(code, mimeType) 
          {
    
            var reader = new FileReader();
            // Closure to capture the file information.
            reader.onload = (
              function(theFile) 
              {
                return function(e) 
                {
                  target.html( e.target.result );
                };
              }
            )(file);
    
            // Read in the image file as a data URL.
            reader.readAsText(file);
          }
    
    
    
    }
    

提交回复
热议问题