Comments (2)
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.
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)
- how do i add custom component. HOT 1
- Layer Manager HOT 1
- SVG classes bugs HOT 1
- How to upload files using assetmanger to AWS S3 or digitalOcean Space HOT 1
- Module not found: Error: Can't resolve 'fs' HOT 3
- Implementation problem HOT 2
- nameof is not defined HOT 10
- onInit has no access to editor HOT 2
- Using GrapeJS on nextjs HOT 1
- Window is not defined (grapesjs-react + Next JS) HOT 4
- Export the editor data as HTML file HOT 1
- Can't import it on React and Nextjs HOT 1
- I18n support HOT 1
- Autoload not working with custom storage adapter HOT 1
- Add custom block in latest version HOT 1
- Can you create React components? HOT 3
- Property 'plugins' does not exist on type 'IntrinsicAttributes & GrapesjsReactProps & { children?: ReactNode; } HOT 5
- How to detect changes in the canvas?
- How to load HTML string in the Canvas?
- Empty block list HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grapesjs-react.