11import { useBlockElementCache } from './useBlockElementCache'
22import { Editor } from 'slate'
3- import { SugaredFieldProps , SugaredRelativeEntityList , useEntityList , useSortedEntities } from '@contember/binding'
3+ import {
4+ EntityId , SugaredFieldProps , SugaredRelativeEntityList ,
5+ sortEntities , useDesugaredRelativeSingleField , useEntityList ,
6+ } from '@contember/binding'
47import { useBlockElementPathRefs } from './useBlockElementPathRefs'
58import { useBlockEditorOnChange } from './useBlockEditorOnChange'
6- import { useRef } from 'react'
9+ import { useEffect , useMemo , useRef } from 'react'
710import { useBlockEditorSlateNodes } from '../useBlockEditorSlateNodes'
811import { useRefreshBlocks } from './useRefreshBlocks'
912
@@ -15,13 +18,23 @@ export const useBlockEditorState = ({ editor, blockList, sortableBy, contentFiel
1518 referencesField ?: SugaredRelativeEntityList | string
1619 monolithicReferencesMode ?: boolean
1720} ) => {
18- const { entities : sortedBlocks } = useSortedEntities ( useEntityList ( blockList ) , sortableBy )
21+ const entityList = useEntityList ( blockList )
22+ const desugaredSortableByField = useDesugaredRelativeSingleField ( sortableBy )
23+ const trashFakeBlockId = useRef < EntityId > ( )
24+ const sortedBlocks = useMemo ( ( ) => {
25+ return sortEntities (
26+ Array . from ( entityList ) . filter ( it => it . id !== trashFakeBlockId . current ) ,
27+ desugaredSortableByField ,
28+ )
29+ } , [ desugaredSortableByField , entityList ] )
1930 const sortedBlocksRef = useRef ( sortedBlocks )
20- sortedBlocksRef . current = sortedBlocks
31+ useEffect ( ( ) => {
32+ sortedBlocksRef . current = sortedBlocks
33+ } , [ sortedBlocks ] )
2134 const blockElementCache = useBlockElementCache ( { editor, blockList, sortableBy, contentField } )
2235 const blockElementPathRefs = useBlockElementPathRefs ( { editor, blockList } )
2336
24- const refreshBlocks = useRefreshBlocks ( { editor, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, sortedBlocksRef } )
37+ const refreshBlocks = useRefreshBlocks ( { editor, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, sortedBlocksRef, trashFakeBlockId } )
2538 const onChange = useBlockEditorOnChange ( { editor, blockList, contentField, blockElementCache, sortedBlocksRef, refreshBlocks } )
2639 const nodes = useBlockEditorSlateNodes ( { editor, blockElementCache, blockElementPathRefs, blockContentField : contentField , topLevelBlocks : sortedBlocks } )
2740
0 commit comments