Skip to content

Commit

Permalink
fix(admin): do not render trash block in block editor
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Jun 24, 2024
1 parent 7ad00c4 commit 1cbad59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 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

0 comments on commit 1cbad59

Please sign in to comment.