-
Notifications
You must be signed in to change notification settings - Fork 118
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
- Loading branch information
Showing
4 changed files
with
36 additions
and
37 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
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); | ||
}; |