Skip to content

Commit

Permalink
Add onReady method (called after editor:ready event)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunolemos committed Sep 30, 2021
1 parent e8b3abe commit 50caa88
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,25 @@ const App = (props) => {
});
};

// called when the editor is created
const onLoad = () => {
// you can load your template here;
// const templateJson = {};
// emailEditorRef.current.editor.loadDesign(templateJson);
};

// called when the editor has finished loading all custom css/js/fonts
const onReady = () => {
console.log('onReady');
};

return (
<div>
<div>
<button onClick={exportHtml}>Export HTML</button>
</div>

<EmailEditor ref={emailEditorRef} onLoad={onLoad} />
<EmailEditor ref={emailEditorRef} onLoad={onLoad} onReady={onReady} />
</div>
);
};
Expand All @@ -78,7 +84,8 @@ See the [example source](https://github.com/unlayer/react-email-editor/blob/mast
- `editorId` `String` HTML div id of the container where the editor will be embedded (optional)
- `style` `Object` style object for the editor container (default {})
- `minHeight` `String` minimum height to initialize the editor with (default 500px)
- `onLoad` `Function` called when the editor has finished loading
- `onLoad` `Function` called when the editor is created
- `onReady` `Function` called when the editor has finished loading all custom css/js/fonts
- `options` `Object` options passed to the Unlayer editor instance (default {})
- `tools` `Object` configuration for the built-in and custom tools (default {})
- `appearance` `Object` configuration for appearance and theme (default {})
Expand Down
8 changes: 7 additions & 1 deletion demo/src/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const Example = (props) => {
console.log('onDesignLoad', data);
};

// called when the editor is created
const onLoad = () => {
emailEditorRef.current.editor.addEventListener(
'onDesignLoad',
Expand All @@ -69,6 +70,11 @@ const Example = (props) => {
emailEditorRef.current.editor.loadDesign(sample);
};

// called when the editor has finished loading all custom css/js/fonts
const onReady = () => {
console.log('onReady');
};

return (
<Container>
<Bar>
Expand All @@ -79,7 +85,7 @@ const Example = (props) => {
</Bar>

<React.StrictMode>
<EmailEditor ref={emailEditorRef} onLoad={onLoad} />
<EmailEditor ref={emailEditorRef} onLoad={onLoad} onReady={onReady} />
</React.StrictMode>
</Container>
);
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ export default class extends Component {

// All properties starting with on[Name] are registered as event listeners.
for (const [key, value] of Object.entries(this.props)) {
if (/^on/.test(key) && key != 'onLoad') {
if (/^on/.test(key) && key !== 'onLoad' && key !== 'onReady') {
this.addEventListener(key, value);
}
}

const { onLoad } = this.props;
const { onLoad, onReady } = this.props;

onLoad && onLoad();

if (onReady) this.editor.addEventListener('editor:ready', onReady);
};

registerCallback = (type, callback) => {
Expand Down

0 comments on commit 50caa88

Please sign in to comment.