Created
November 24, 2021 10:11
-
-
Save ronnyroeller/7841760f29b897d8417e728cdb30692c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { debounce } from 'remirror'; | |
import type { CommandFunctionProps, Transaction } from 'remirror'; | |
/** | |
* When we debounce positioner re-rendering, we need to trigger an | |
* update at the end of a burst of activity to actually update the | |
* view. We use this meta key to bail out of debounce logic, and | |
* force the positioners to re-render. | |
* @see isForcedDebouncedUpdate | |
*/ | |
const DEBOUNCED_POSITIONER_UPDATE = 'debouncedPositionerUpdate'; | |
export type GetCommandPropsCallback = () => CommandFunctionProps; | |
const forceUpdate = (cb: GetCommandPropsCallback): void => { | |
const { view, tr } = cb(); | |
view?.dispatch(tr.setMeta(DEBOUNCED_POSITIONER_UPDATE, {})); | |
}; | |
/** | |
* Create a transaction that will force positioners to re-render, | |
* but debounce it so that it will only be fired once at the end (or | |
* optionally beginning) of a burst of activity (i.e. multiple key | |
* presses while typing) | |
* @param ms | |
* @param atBegin - If true, re-render will be at beginning of | |
* burst, instead of end | |
*/ | |
export function createDebouncedUpdate(ms: number, atBegin = false) { | |
return debounce(ms, atBegin, forceUpdate); | |
} | |
/** | |
* Helps determine if this transaction is a forced positioner update | |
* @param tr - A ProseMirror transaction | |
*/ | |
export function isForcedDebouncedUpdate(tr?: Transaction): boolean { | |
return Boolean(tr?.getMeta(DEBOUNCED_POSITIONER_UPDATE)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment