Git Product home page Git Product logo

Comments (2)

bumpradar avatar bumpradar commented on June 8, 2024

Edits I made to be able to access the editor to export the files programmatically:

// @flow

import React from 'react';
import GrapesJS from 'grapesjs';
// Presets and Plugins
import gjsPresetWebpage from 'grapesjs-preset-webpage';
import gjsPresetNewsletter from 'grapesjs-preset-newsletter';
import gjsBasicBlocks from 'grapesjs-blocks-basic';

const {
  useEffect,
  useState,
} = React;


function GEditor(props: PropsType) {
  const {
    id = 'grapesjs-react-editor',
    storageManager,
    blockManager,
    webpage,
    newsletter,
    setGlobalEditor
  } = props;
  const [editor, setEditor] = useState(null);
  useEffect(
    () => {
      if (!editor) {
        let plugins = [
          gjsBasicBlocks,
          ...props.plugins,
        ];
        if (webpage) {
          plugins = [
            ...plugins,
            gjsPresetWebpage,
          ];
        }
        if (newsletter) {
          plugins = [
            ...plugins,
            gjsPresetNewsletter,
          ];
        }
        const editor = GrapesJS.init({
          fromElement: true,
          container: `#${id}`,
          plugins,
          storageManager: storageManager,
          blockManager: blockManager,
        });
        setGlobalEditor(editor);
        setEditor(editor);
      } else {
        if (document) {
          document.getElementById(id).append(editor.render());
        }
      }
      return function cleanup() {
        if (editor) {
          // Destroy current editor
          editor.destroy();
        }
        // Remove editor from global GrapesJS store
        GrapesJS.editors = GrapesJS.editors.filter((e) => e !== editor);
      };
    },
    [],
  );

  return (
    <div id={id}/>
  );
}

GEditor.defaultProps = {
  webpage: false,
  newsletter: false,
  plugins: [],
  components: [],
  storageManager: {},
  blockManager: {},
};

export default GEditor;

My editor component:


import React from 'react';
import {bindActionCreators} from 'redux';
import { connect } from 'react-redux';
import {
  withRouter
} from 'react-router-dom';
import * as actions from '../redux/actions';
import {
  PageHeader,
  Button,
} from 'antd';
import Editor from '../components/ui/Editor';
import 'grapesjs/dist/css/grapes.min.css';

//import LoadingSpinner from '../components/LoadingSpinner';
import {
  DashboardContainer,
} from './styles';


class CreatePopup extends React.Component {
  constructor(props) {
    super(props);
    this.editor = React.createRef();
    this.state = {
      pageTitle: 'Create Popup Link',
      pageButtonTitle: 'Save',
      personalizeVisible: false
    }
  }

  componentWillMount() {
  //  this.props.getPopup(this.props.match.params.id);
  }

  componentDidUpdate(prevProps){
    const {
      popup
    } = this.props.data;
    if (prevProps.data.popup !== popup) {

    }
  }

  savePopup() {
    console.log(this.editor);
    console.log(this.editor.getHtml());
  }



  render() {
    // const {
    //   creatingTemplate,
    //   isLoadingTemplate
    // } = this.props.data;
    const {
      pageTitle,
      pageButtonTitle
    } = this.state;
    // if(isLoadingTemplate) {
    //   return <LoadingSpinner />
    // }
    return (
      <DashboardContainer>
        <PageHeader
          title={pageTitle}
          onBack={() => window.history.back()}
          extra={[
            <Button
              type='primary'
              onClick={() => this.savePopup()}
            >
              {pageButtonTitle}
            </Button>
          ]}
          style={{ marginBottom: 10 }}
        />
        <Editor
          blocks={[]}
          webpage
          setGlobalEditor={(editor) => this.editor = editor}
        />

      </DashboardContainer>
    );
  }
}

function mapStateToProps(state, props) {
    return {
        auth: state.AuthReducer,
        data: state.DataReducer
    }
}

function mapDispatchToProps(dispatch) {
    return bindActionCreators(actions, dispatch);
}

export default withRouter(connect(mapStateToProps, mapDispatchToProps)(CreatePopup));

from grapesjs-react.

thanhtunguet avatar thanhtunguet commented on June 8, 2024

Sorry for replying late.
When I was developing this package, I thought of this use case and I found out that GrapesJS already have an array that contains all the editors in global variable window.grapesjs. That's why I did not create a method to access the editor. You can simply access the editor as follow:

const editor = grapesjs.editors[index];

with index is the index of the editor you want to access.
Then, you can access getHtml(), getCss, getJs by using the editor object:

editor.getHtml();
editor.getCss();
editor.getJs();

However, this means you have to determine what is the index of the editor you want to access.


By the way, I do not have time to develop this package, I am looking for support from the community. If you have ideas, find bugs, simply fork this repository and contribute your code by pull requests. Thank you.

from grapesjs-react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.