Skip to content

Commit

Permalink
Merge pull request #726 from contember/fix/editor-blocks
Browse files Browse the repository at this point in the history
Fix editor blocks
  • Loading branch information
matej21 authored Jun 17, 2024
2 parents 99364ea + d334b15 commit 89dfa0c
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 20 deletions.
6 changes: 3 additions & 3 deletions build/api/react-slate-editor-legacy.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { EntityAccessor } from '@contember/react-binding';
import { EntityAccessor as EntityAccessor_2 } from '@contember/binding';
import { EntityId } from '@contember/react-binding';
import { EntityId as EntityId_2 } from '@contember/binding';
import type { Environment } from '@contember/react-binding';
import { Environment } from '@contember/react-binding';
import { FieldAccessor } from '@contember/react-binding';
import { FieldValue } from '@contember/react-binding';
import { FunctionComponent } from 'react';
Expand Down Expand Up @@ -339,7 +339,7 @@ export interface UseBlockEditorSlateNodesOptions {
}

// @public (undocumented)
export const useBlockProps: (children: ReactNode) => BlockProps[];
export const useBlockProps: (children: ReactNode, env: Environment) => BlockProps[];

// @public (undocumented)
export const useDiscriminatedData: <Datum extends DiscriminatedDatum = DiscriminatedDatum>(source: Iterable<Datum>) => NormalizedDiscriminatedData<Datum>;
Expand All @@ -348,7 +348,7 @@ export const useDiscriminatedData: <Datum extends DiscriminatedDatum = Discrimin
export const useEditorReferenceBlocks: () => EditorReferenceBlocks;

// @public (undocumented)
export const useNormalizedBlocks: (children: ReactNode) => NormalizedBlocks;
export const useNormalizedBlocks: (children: ReactNode, env: Environment) => NormalizedBlocks;

// @public (undocumented)
export interface WithBlockElements {
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/tailwind.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

/** @type {import('tailwindcss').Config} */
module.exports = {
content: ['./**/*.{html,js,tsx,ts}'],
content: ['./**/*.{html,js,tsx,ts}', '../react-ui-lib/src/**/*.{html,js,tsx,ts}'],
darkMode: ['class'],
theme: {
container: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SugaredField,
SugaredFieldProps,
SugaredRelativeEntityList,
TreeNodeEnvironmentFactory,
useDesugaredRelativeSingleField,
useEntity,
useEnvironment,
Expand Down Expand Up @@ -81,7 +82,17 @@ const BlockEditorComponent: FunctionComponent<BlockEditorProps> = Component(
const desugaredEmbedContentDiscriminationField = useDesugaredRelativeSingleField(embedContentDiscriminationField)


const editorReferenceBlocks = useEditorReferenceBlocks(children)

let refEnv = environment
if (!props.monolithicReferencesMode) {
refEnv = TreeNodeEnvironmentFactory.createEnvironmentForEntityList(refEnv, {
field: props.field,
})
}
refEnv = referencesField ? TreeNodeEnvironmentFactory.createEnvironmentForEntityList(refEnv, typeof referencesField === 'string' ? {
field: referencesField,
} : referencesField) : refEnv
const editorReferenceBlocks = useEditorReferenceBlocks(children, refEnv)

//

Expand All @@ -96,6 +107,7 @@ const BlockEditorComponent: FunctionComponent<BlockEditorProps> = Component(
embedReferenceDiscriminant !== undefined
? getDiscriminatedBlock(editorReferenceBlocks, embedReferenceDiscriminant)?.datum.children
: undefined, // TODO this may crash
refEnv,
)

const entity = useEntity()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEnvironment } from '@contember/react-binding'
import { Environment, useEnvironment } from '@contember/react-binding'
import { useConstantValueInvariant } from '@contember/react-utils'
import { ReactNode, useMemo } from 'react'
import { BlockProps, useBlockProps } from '../../blocks'
Expand All @@ -11,11 +11,10 @@ export interface EditorReferenceBlock extends BlockProps {

export type EditorReferenceBlocks = NormalizedDiscriminatedData<EditorReferenceBlock>

export const useEditorReferenceBlocks = (children: ReactNode): EditorReferenceBlocks => {
export const useEditorReferenceBlocks = (children: ReactNode, env: Environment): EditorReferenceBlocks => {
useConstantValueInvariant(children, `BlockEditor: cannot change the set of Blocks between renders!`)
const env = useEnvironment()

const propList = useBlockProps(children)
const propList = useBlockProps(children, env)
const propsWithTemplates = useMemo(() => {
return propList.map(
(props): EditorReferenceBlock => ({
Expand Down
10 changes: 4 additions & 6 deletions packages/react-slate-editor-legacy/src/blocks/useBlockProps.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { useEnvironment } from '@contember/react-binding'
import { Environment, useEnvironment } from '@contember/react-binding'
import { ReactNode, useMemo } from 'react'
import type { BlockProps } from './Block'
import { blockAnalyzer } from './blockAnalyzer'

export const useBlockProps = (children: ReactNode): BlockProps[] => {
const environment = useEnvironment()

export const useBlockProps = (children: ReactNode, env: Environment): BlockProps[] => {
return useMemo(() => {
if (children === undefined) {
return []
}
return blockAnalyzer.processChildren(children, environment)
}, [children, environment])
return blockAnalyzer.processChildren(children, env)
}, [children, env])
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import type { ReactNode } from 'react'
import { NormalizedDiscriminatedData, useDiscriminatedData } from '../discrimination'
import type { BlockProps } from './Block'
import { useBlockProps } from './useBlockProps'
import { Environment } from '@contember/react-binding'

export type NormalizedBlocks = NormalizedDiscriminatedData<BlockProps>

export const useNormalizedBlocks = (children: ReactNode): NormalizedBlocks => {
const propList = useBlockProps(children)
export const useNormalizedBlocks = (children: ReactNode, env: Environment): NormalizedBlocks => {
const propList = useBlockProps(children, env)

return useDiscriminatedData<BlockProps>(propList)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EditorPlugin, EditorPluginWrapperProps } from '@contember/react-slate-editor-base'
import { useMemo } from 'react'
import { SugaredRelativeEntityList, SugaredRelativeSingleField } from '@contember/binding'
import { SugaredRelativeEntityList, SugaredRelativeSingleField, TreeNodeEnvironmentFactory } from '@contember/binding'
import { isElementWithReference } from './elements'
import { referenceOverrides } from './referenceOverrides'
import { ErrorBoundary } from 'react-error-boundary'
Expand All @@ -22,7 +22,9 @@ export interface ReferencesPluginArgs {
export const withReferences = (args: ReferencesPluginArgs): EditorPlugin => {
return {
extendEditor: ({ editor, children, environment, entity }) => {
const blocks = getEditorReferenceBlocks(children, environment)
const blocks = getEditorReferenceBlocks(children, TreeNodeEnvironmentFactory.createEnvironmentForEntityList(environment, {
field: args.field,
}))
for (const block of Object.values(blocks)) {
editor.registerElement({
type: block.name,
Expand Down Expand Up @@ -58,7 +60,9 @@ export const withReferences = (args: ReferencesPluginArgs): EditorPlugin => {
const env = useEnvironment()

const editorReferenceBlocks = useMemo(() => {
return getEditorReferenceBlocks(children, env)
return getEditorReferenceBlocks(children, TreeNodeEnvironmentFactory.createEnvironmentForEntityList(env, {
field: args.field,
}))
}, [children, env])
useCleanupReferences({ field: args.field, editor })

Expand Down

0 comments on commit 89dfa0c

Please sign in to comment.