Skip to content

Commit

Permalink
fix: sync <Editor /> props with state options (#271)
Browse files Browse the repository at this point in the history
* Fixes #149
 - Updates Editor's options as a side-effect.

* Run the effect only when options change
  • Loading branch information
aroramayur committed Jul 26, 2021
1 parent 83fdc23 commit 3156043
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions packages/core/src/editor/Editor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ERROR_RESOLVER_NOT_AN_OBJECT } from '@craftjs/utils';
import React, { useEffect } from 'react';
import { pickBy } from 'lodash';
import React, { useEffect, useMemo } from 'react';
import invariant from 'tiny-invariant';

import { EditorContext } from './EditorContext';
Expand All @@ -13,23 +14,35 @@ import { Options } from '../interfaces';
*/
export const Editor: React.FC<Partial<Options>> = ({
children,
...options
onRender,
onNodesChange,
resolver,
enabled,
indicator,
}) => {
// we do not want to warn the user if no resolver was supplied
if (options.resolver !== undefined) {
if (resolver !== undefined) {
invariant(
typeof options.resolver === 'object' && !Array.isArray(options.resolver),
typeof resolver === 'object' && !Array.isArray(resolver),
ERROR_RESOLVER_NOT_AN_OBJECT
);
}

const options = useMemo(() => {
return pickBy(
{ onRender, onNodesChange, resolver, enabled, indicator },
(value) => value !== undefined
);
}, [enabled, indicator, onNodesChange, onRender, resolver]);

const context = useEditorStore(options);

useEffect(() => {
if (context && options)
if (context && options) {
context.actions.setOptions((editorOptions) => {
editorOptions = options;
Object.assign(editorOptions, options);
});
}
}, [context, options]);

useEffect(() => {
Expand Down

0 comments on commit 3156043

Please sign in to comment.