Skip to content

Commit

Permalink
Merge pull request #730 from contember/fix/1.2/trash-block
Browse files Browse the repository at this point in the history
fix(admin): do not render trash block in block editor
  • Loading branch information
matej21 authored Jun 24, 2024
2 parents 7ad00c4 + 2e95712 commit cba4d8b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useBlockElementCache } from './useBlockElementCache'
import { Editor } from 'slate'
import { SugaredFieldProps, SugaredRelativeEntityList, useEntityList, useSortedEntities } from '@contember/binding'
import {
EntityId, SugaredFieldProps, SugaredRelativeEntityList,
sortEntities, useDesugaredRelativeSingleField, useEntityList,
} from '@contember/binding'
import { useBlockElementPathRefs } from './useBlockElementPathRefs'
import { useBlockEditorOnChange } from './useBlockEditorOnChange'
import { useRef } from 'react'
import { useEffect, useMemo, useRef } from 'react'
import { useBlockEditorSlateNodes } from '../useBlockEditorSlateNodes'
import { useRefreshBlocks } from './useRefreshBlocks'

Expand All @@ -15,13 +18,23 @@ export const useBlockEditorState = ({ editor, blockList, sortableBy, contentFiel
referencesField?: SugaredRelativeEntityList | string
monolithicReferencesMode?: boolean
}) => {
const { entities: sortedBlocks } = useSortedEntities(useEntityList(blockList), sortableBy)
const entityList = useEntityList(blockList)
const desugaredSortableByField = useDesugaredRelativeSingleField(sortableBy)
const trashFakeBlockId = useRef<EntityId>()
const sortedBlocks = useMemo(() => {
return sortEntities(
Array.from(entityList).filter(it => it.id !== trashFakeBlockId.current),
desugaredSortableByField,
)
}, [desugaredSortableByField, entityList])
const sortedBlocksRef = useRef(sortedBlocks)
sortedBlocksRef.current = sortedBlocks
useEffect(() => {
sortedBlocksRef.current = sortedBlocks
}, [sortedBlocks])
const blockElementCache = useBlockElementCache({ editor, blockList, sortableBy, contentField })
const blockElementPathRefs = useBlockElementPathRefs({ editor, blockList })

const refreshBlocks = useRefreshBlocks({ editor, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, sortedBlocksRef })
const refreshBlocks = useRefreshBlocks({ editor, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, sortedBlocksRef, trashFakeBlockId })
const onChange = useBlockEditorOnChange({ editor, blockList, contentField, blockElementCache, sortedBlocksRef, refreshBlocks })
const nodes = useBlockEditorSlateNodes({ editor, blockElementCache, blockElementPathRefs, blockContentField: contentField, topLevelBlocks: sortedBlocks })

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { MutableRefObject, useCallback, useRef } from 'react'
import { BlockElementCache } from './useBlockElementCache'
import { BlockElementPathRefs } from './useBlockElementPathRefs'

export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode }: {
export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentField, blockList, blockElementCache, blockElementPathRefs, referencesField, monolithicReferencesMode, trashFakeBlockId }: {
editor: Editor,
sortedBlocksRef: MutableRefObject<EntityAccessor[]>,
contentField: SugaredFieldProps['field'],
Expand All @@ -21,12 +21,12 @@ export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentF
blockElementPathRefs: BlockElementPathRefs
referencesField?: SugaredRelativeEntityList | string
monolithicReferencesMode?: boolean
trashFakeBlockId: MutableRefObject<EntityId | undefined>
}) => {
const desugaredBlockList = useDesugaredRelativeEntityList(blockList)
const desugaredBlockContentField = useDesugaredRelativeSingleField(contentField)
const desugaredSortableByField = useDesugaredRelativeSingleField(sortableBy)
const getParentEntityRef = useGetParentEntityRef()
const trashFakeBlockId = useRef<EntityId>()
useEntityBeforePersist(() => {
if (trashFakeBlockId.current) {
const block = getParentEntityRef.current().getRelativeEntityList(desugaredBlockList).getChildEntityById(trashFakeBlockId.current)
Expand Down Expand Up @@ -168,5 +168,5 @@ export const useRefreshBlocks = ({ editor, sortedBlocksRef, sortableBy, contentF
}
cleanupStack()
})
}, [blockElementCache, blockElementPathRefs, desugaredBlockContentField, desugaredBlockList, desugaredSortableByField, editor, getParentEntityRef, monolithicReferencesMode, referencesField, sortedBlocksRef])
}, [trashFakeBlockId, blockElementCache, blockElementPathRefs, desugaredBlockContentField, desugaredBlockList, desugaredSortableByField, editor, getParentEntityRef, monolithicReferencesMode, referencesField, sortedBlocksRef])
}

0 comments on commit cba4d8b

Please sign in to comment.