Skip to content

Commit cba4d8b

Browse files
authored
Merge pull request #730 from contember/fix/1.2/trash-block
fix(admin): do not render trash block in block editor
2 parents 7ad00c4 + 2e95712 commit cba4d8b

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

packages/admin/src/components/bindingFacade/richText/blockEditor/state/useBlockEditorState.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import { useBlockElementCache } from './useBlockElementCache'
22
import { 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'
47
import { useBlockElementPathRefs } from './useBlockElementPathRefs'
58
import { useBlockEditorOnChange } from './useBlockEditorOnChange'
6-
import { useRef } from 'react'
9+
import { useEffect, useMemo, useRef } from 'react'
710
import { useBlockEditorSlateNodes } from '../useBlockEditorSlateNodes'
811
import { 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

packages/admin/src/components/bindingFacade/richText/blockEditor/state/useRefreshBlocks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { MutableRefObject, useCallback, useRef } from 'react'
1111
import { BlockElementCache } from './useBlockElementCache'
1212
import { BlockElementPathRefs } from './useBlockElementPathRefs'
1313

14-
export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode }: {
14+
export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, trashFakeBlockId }: {
1515
editor: Editor,
1616
sortedBlocksRef: MutableRefObject<EntityAccessor[]>,
1717
contentField: SugaredFieldProps['field'],
@@ -21,12 +21,12 @@ export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentF
2121
blockElementPathRefs: BlockElementPathRefs
2222
referencesField?: SugaredRelativeEntityList | string
2323
monolithicReferencesMode?: boolean
24+
trashFakeBlockId: MutableRefObject<EntityId | undefined>
2425
}) => {
2526
const desugaredBlockList = useDesugaredRelativeEntityList(blockList)
2627
const desugaredBlockContentField = useDesugaredRelativeSingleField(contentField)
2728
const desugaredSortableByField = useDesugaredRelativeSingleField(sortableBy)
2829
const getParentEntityRef = useGetParentEntityRef()
29-
const trashFakeBlockId = useRef<EntityId>()
3030
useEntityBeforePersist(() => {
3131
if (trashFakeBlockId.current) {
3232
const block = getParentEntityRef.current().getRelativeEntityList(desugaredBlockList).getChildEntityById(trashFakeBlockId.current)
@@ -168,5 +168,5 @@ export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentF
168168
}
169169
cleanupStack()
170170
})
171-
}, [blockElementCache, blockElementPathRefs, desugaredBlockContentField, desugaredBlockList, desugaredSortableByField, editor, getParentEntityRef, monolithicReferencesMode, referencesField, sortedBlocksRef])
171+
}, [trashFakeBlockId, blockElementCache, blockElementPathRefs, desugaredBlockContentField, desugaredBlockList, desugaredSortableByField, editor, getParentEntityRef, monolithicReferencesMode, referencesField, sortedBlocksRef])
172172
}

0 commit comments

Comments
 (0)