Skip to content

Commit

Permalink
feat(react-client): allow read only blocks in richtext renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasnobile committed Sep 18, 2023
1 parent 856b76e commit 60594a6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export type RichTextBlock = Record<string, unknown>
export type RichTextBlock = Readonly<Record<string, unknown>>
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ import { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext'
import type { RootEditorNode } from './RootEditorNode'

export interface RichTextRendererFieldProps {
source: string | null
readonly source: string | null
}

export interface RichTextRendererBlockProps<
CustomElements extends RichTextElement = never,
CustomLeaves extends RichTextLeaf = never,
> {
blocks: RichTextBlock[]
referenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>
sourceField?: string
referencesField?: string
referenceDiscriminationField?: string
readonly blocks: RichTextBlock[]
readonly referenceRenderers?: Record<string, ReferenceRenderer<any, CustomElements, CustomLeaves>>
readonly sourceField?: string
readonly referencesField?: string
readonly referenceDiscriminationField?: string
}

export type RichTextRendererProps<
Expand Down Expand Up @@ -60,9 +60,9 @@ export const RichTextRenderer = memo(function RichTextRenderer<
content: sourceProps.source
? deserialize(sourceProps.source)
: {
formatVersion: 0,
children: [],
},
formatVersion: 0,
children: [],
},
id: undefined,
referencesField: undefined,
referenceDiscriminationField: undefined,
Expand All @@ -80,10 +80,10 @@ export const RichTextRenderer = memo(function RichTextRenderer<
if (!(sourceField in block)) {
throw new RichTextRendererError(
`Found a block without a '${sourceField}' field. ` +
(sourceProps.sourceField === undefined
? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`
: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +
`or the data supplied is invalid.`),
(sourceProps.sourceField === undefined
? `The 'sourceField' prop has not been supplied, and so '${sourceField}' was used as a default.`
: `That is what the 'sourceField' prop has been set to, and so either this is a typo, ` +
`or the data supplied is invalid.`),
)
}
const source = block[sourceField]
Expand All @@ -94,8 +94,8 @@ export const RichTextRenderer = memo(function RichTextRenderer<
if (sourceProps.referencesField !== undefined && !(referencesField in block)) {
throw new RichTextRendererError(
`The 'referencesField' prop is set to '${referencesField}' but a block without such field ` +
`has been encountered. Unless this is just a typo, ` +
`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,
`has been encountered. Unless this is just a typo, ` +
`if you do not wish to use references, avoid supplying the 'referencesField' prop.`,
)
}
const references = block[referencesField]
Expand All @@ -114,11 +114,11 @@ export const RichTextRenderer = memo(function RichTextRenderer<
if (!(referenceDiscriminationField in reference)) {
throw new RichTextRendererError(
`Found a reference without a '${referenceDiscriminationField}' field. ` +
(sourceProps.referenceDiscriminationField === undefined
? `The 'referenceDiscriminationField' prop has not been supplied, ` +
`and so '${referenceDiscriminationField}' was used as a default.`
: `That is what the 'referenceDiscriminationField' prop has been set to, ` +
`and so either this is a typo, or the data supplied is invalid.`),
(sourceProps.referenceDiscriminationField === undefined
? `The 'referenceDiscriminationField' prop has not been supplied, ` +
`and so '${referenceDiscriminationField}' was used as a default.`
: `That is what the 'referenceDiscriminationField' prop has been set to, ` +
`and so either this is a typo, or the data supplied is invalid.`),
)
}
normalizedReferences.set(reference.id, reference)
Expand Down Expand Up @@ -152,12 +152,12 @@ export const RichTextRenderer = memo(function RichTextRenderer<
>
{createElement(
renderBlock, {
block,
}, renderChildren(block.content.children, {
renderLeaf,
renderElement,
attributeNamePrefix,
}),
block,
}, renderChildren(block.content.children, {
renderLeaf,
renderElement,
attributeNamePrefix,
}),
)}
</RichTextRenderMetadataContext.Provider>
))}
Expand Down

0 comments on commit 60594a6

Please sign in to comment.