Skip to content

Commit

Permalink
Merge pull request #44 from perimetre/3.0.8
Browse files Browse the repository at this point in the history
3.0.8
  • Loading branch information
adarleyjrr authored Jun 23, 2021
2 parents 3050969 + 2359872 commit 6b32691
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

## [3.0.8] - 2021-06-23

### Fixed

- Fixed bug that would cause `WYSIWYGInput` component to send `onChange` events with stale data while initializing its editor state

## [3.0.7] - 2021-06-21

### Changes
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@perimetre/ui",
"description": "A component library made by @perimetre",
"version": "3.0.7",
"version": "3.0.8",
"repository": {
"type": "git",
"url": "git+https://github.com/perimetre/ui.git"
Expand Down
19 changes: 13 additions & 6 deletions src/components/WYSIWYGInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ export const WYSIWYGInput = forwardRef<WYSIWYGInputRef, WYSIWYGInputProps>(
defaultDecorators
)
);
const [isEditorStateInitialized, setIsEditorStateInitialized] = useState(false);
const editor = useRef<Editor | null>(null);

/**
Expand All @@ -173,23 +174,23 @@ export const WYSIWYGInput = forwardRef<WYSIWYGInputRef, WYSIWYGInputProps>(
};

useEffect(() => {
// If the onChange prop is provided
if (onChangeProps) {
// If the onChange prop is provided and we are finished initializing the component state
if (onChangeProps && isEditorStateInitialized) {
// Notify the change
onChangeProps(editorState);
}
// If the editor state changes
}, [editorState, onChangeProps]);
}, [editorState, onChangeProps, isEditorStateInitialized]);

useEffect(() => {
// If the onChange prop is provided
if (onHtmlChangeSlow) {
// If the onChange prop is provided and we have initialized the component state
if (onHtmlChangeSlow && isEditorStateInitialized) {
// Ref(HTML part at the end): https://jpuri.github.io/react-draft-wysiwyg/#/docs
const htmlData = draftToHtml(convertToRaw(editorState.getCurrentContent()));
onHtmlChangeSlow(DOMPurify.sanitize(htmlData));
}
// If the editor state changes
}, [editorState, onHtmlChangeSlow]);
}, [editorState, onHtmlChangeSlow, isEditorStateInitialized]);

useEffect(() => {
/**
Expand All @@ -208,12 +209,18 @@ export const WYSIWYGInput = forwardRef<WYSIWYGInputRef, WYSIWYGInputProps>(
defaultDecorators
)
);

// make sure to set component as initialized so that it will start sending onChange events
setIsEditorStateInitialized(true);
};

setEditorState(EditorState.set(editorState, { decorator: defaultDecorators }));

if (defaultHtmlValue) {
updateFromHtml();
} else {
// make sure to set component as initialized so that it will start sending onChange events
setIsEditorStateInitialized(true);
}
// Disable because we only want this to update on the first render
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down

0 comments on commit 6b32691

Please sign in to comment.