-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: skip set_selection onChange events (#1443)
- Loading branch information
Showing
10 changed files
with
122 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as React from 'react'; | ||
|
||
import * as Contentful from '@contentful/rich-text-types'; | ||
import { usePlateActions } from '@udecode/plate-core'; | ||
import equal from 'fast-deep-equal'; | ||
|
||
import { createOnChangeCallback } from './helpers/callbacks'; | ||
import { usePlateSelectors } from './internal/hooks'; | ||
import { setEditorValue } from './internal/transforms'; | ||
import { Value } from './internal/types'; | ||
|
||
/** | ||
* A hook responsible for keeping the editor state in sync with incoming | ||
* changes (aka. external updates | ||
*/ | ||
const useAcceptIncomingChanges = (incomingValue?: Value) => { | ||
const editor = usePlateSelectors().editor(); | ||
|
||
// Cache latest editor value to avoid unnecessary updates | ||
const lastIncomingValue = React.useRef(incomingValue); | ||
|
||
React.useEffect(() => { | ||
if (equal(lastIncomingValue.current, incomingValue)) { | ||
return; | ||
} | ||
|
||
lastIncomingValue.current = incomingValue; | ||
setEditorValue(editor, incomingValue); | ||
}, [editor, incomingValue]); | ||
}; | ||
|
||
/** | ||
* Attaches a custom onChange callback that | ||
*/ | ||
const useOnValueChanged = (onChange?: (doc: Contentful.Document) => unknown) => { | ||
const editor = usePlateSelectors().editor(); | ||
const setEditorOnChange = usePlateActions().onChange(); | ||
|
||
React.useEffect(() => { | ||
const cb = createOnChangeCallback(onChange); | ||
|
||
setEditorOnChange({ | ||
fn: (document) => { | ||
// Skip irrelevant events e.g. mouse selection | ||
const operations = editor?.operations.filter((op) => { | ||
return op.type !== 'set_selection'; | ||
}); | ||
|
||
if (operations.length === 0) { | ||
return; | ||
} | ||
|
||
cb(document); | ||
}, | ||
}); | ||
}, [editor, onChange, setEditorOnChange]); | ||
}; | ||
|
||
export type SyncEditorStateProps = { | ||
incomingValue?: Value; | ||
onChange?: (doc: Contentful.Document) => unknown; | ||
}; | ||
|
||
/** | ||
* A void component that's responsible for keeping the editor | ||
* state in sync with incoming changes (aka. external updates) and | ||
* triggering onChange events. | ||
* | ||
* This component is a hack to work around the limitation of Plate v17+ | ||
* where we can no longer access the editor instance outside the Plate | ||
* provider. | ||
*/ | ||
export const SyncEditorChanges = ({ incomingValue, onChange }: SyncEditorStateProps) => { | ||
useAcceptIncomingChanges(incomingValue); | ||
useOnValueChanged(onChange); | ||
|
||
return null; | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { toContentfulDocument } from '@contentful/contentful-slatejs-adapter'; | ||
import { Document } from '@contentful/rich-text-types'; | ||
import equal from 'fast-deep-equal'; | ||
import debounce from 'lodash/debounce'; | ||
|
||
import schema from '../constants/Schema'; | ||
import { removeInternalMarks } from './removeInternalMarks'; | ||
|
||
export const createOnChangeCallback = (handler?: (value: Document) => void) => { | ||
// Cache previous value to avoid firing the handler unnecessarily | ||
// | ||
// Note: We are not using lodash/memoize here to avoid memory leaks | ||
// due to having an infinite cache while we only care about the last | ||
// value. | ||
let cache: unknown = null; | ||
|
||
return debounce((document: unknown) => { | ||
if (equal(document, cache)) { | ||
return; | ||
} | ||
|
||
cache = document; | ||
export const createOnChangeCallback = (handler?: (value: Document) => void) => | ||
debounce((document: unknown) => { | ||
const doc = removeInternalMarks( | ||
toContentfulDocument({ | ||
// eslint-disable-next-line -- parameter type is not exported @typescript-eslint/no-explicit-any | ||
document: document as any, | ||
schema: schema, | ||
// eslint-disable-next-line -- parameter type is not exported @typescript-eslint/no-explicit-any | ||
}) as any | ||
); | ||
// eslint-disable-next-line -- correct parameter type is not defined @typescript-eslint/no-explicit-any | ||
|
||
const cleanedDocument = removeInternalMarks(doc as Record<string, any>); | ||
handler?.(cleanedDocument); | ||
}, 500); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters