File size: 1,209 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import { Ancestor, Editor } from 'slate'
import { Key } from 'slate-dom'
import { ChunkTree } from './types'
import { ReconcileOptions, reconcileChildren } from './reconcile-children'
import { ReactEditor } from '../plugin/react-editor'
export const KEY_TO_CHUNK_TREE = new WeakMap<Key, ChunkTree>()
/**
* Get or create the chunk tree for a Slate node
*
* If the reconcile option is provided, the chunk tree will be updated to
* match the current children of the node. The children are chunked
* automatically using the given chunk size.
*/
export const getChunkTreeForNode = (
editor: Editor,
node: Ancestor,
// istanbul ignore next
options: {
reconcile?: Omit<ReconcileOptions, 'chunkTree' | 'children'> | false
} = {}
) => {
const key = ReactEditor.findKey(editor, node)
let chunkTree = KEY_TO_CHUNK_TREE.get(key)
if (!chunkTree) {
chunkTree = {
type: 'root',
movedNodeKeys: new Set(),
modifiedChunks: new Set(),
children: [],
}
KEY_TO_CHUNK_TREE.set(key, chunkTree)
}
if (options.reconcile) {
reconcileChildren(editor, {
chunkTree,
children: node.children,
...options.reconcile,
})
}
return chunkTree
}
|