-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1ab7d7
commit b80d8b3
Showing
10 changed files
with
101 additions
and
6 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
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
64 changes: 64 additions & 0 deletions
64
packages/core/src/shared/plugins/createQueryCacheToState.ts
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,64 @@ | ||
import { | ||
type AncestorOf, | ||
type ENode, | ||
type TEditor, | ||
type TNodeEntry, | ||
type Value, | ||
getAboveNode, | ||
getNodeEntries, | ||
getNodeEntry, | ||
} from '@udecode/slate'; | ||
import { getBlockAbove } from '@udecode/slate-utils'; | ||
|
||
import type { PlateEditor } from '../types/PlateEditor'; | ||
|
||
import { createPluginFactory } from '../utils/createPluginFactory'; | ||
|
||
export type QueryCachToSateEditor<V extends Value> = { | ||
state: { | ||
aboveEntries?: Generator<TNodeEntry<ENode<V>>, void, undefined>; | ||
aboveNode?: TNodeEntry<AncestorOf<TEditor<V>>>; | ||
ancestorNode?: TNodeEntry<ENode<V>>; | ||
blockAbove?: TNodeEntry<AncestorOf<TEditor<V>>>; | ||
}; | ||
}; | ||
|
||
export const withQueryCachToState = < | ||
V extends Value = Value, | ||
E extends PlateEditor<V> = PlateEditor<V>, | ||
>( | ||
editor: E | ||
) => { | ||
if (typeof editor.state !== 'object') editor.state = {}; | ||
|
||
const { apply } = editor; | ||
|
||
editor.apply = (operation) => { | ||
apply(operation); | ||
|
||
const aboveNode = getAboveNode(editor); | ||
const aboveEntries = getNodeEntries(editor); | ||
const blockAbove = getBlockAbove(editor); | ||
|
||
editor.state.aboveNode = aboveNode; | ||
editor.state.blockAbove = blockAbove; | ||
editor.state.aboveEntries = aboveEntries; | ||
|
||
const { selection } = editor; | ||
|
||
if (selection?.focus?.path) { | ||
editor.state.ancestorNode = getNodeEntry(editor, [ | ||
selection.focus.path[0], | ||
]); | ||
} | ||
}; | ||
|
||
return editor; | ||
}; | ||
|
||
export const KEY_QUERY_CACHE_TO_STATE = 'queryCacheToSate'; | ||
|
||
export const createQueryCachToStatePlugin = createPluginFactory({ | ||
key: KEY_QUERY_CACHE_TO_STATE, | ||
withOverrides: withQueryCachToState, | ||
}); |
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