From 6565edcffb14a3695a6cbd61c8401c30ce7d2e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Nobile?= Date: Mon, 18 Sep 2023 16:31:41 +0200 Subject: [PATCH 1/2] feat(react-client): allow read only blocks in richtext renderer --- .../RichTextRenderer/RichTextBlock.ts | 2 +- .../RichTextRenderer/RichTextRenderer.tsx | 52 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/react-client/src/components/RichTextRenderer/RichTextBlock.ts b/packages/react-client/src/components/RichTextRenderer/RichTextBlock.ts index 6e269b3912..35a7276d99 100644 --- a/packages/react-client/src/components/RichTextRenderer/RichTextBlock.ts +++ b/packages/react-client/src/components/RichTextRenderer/RichTextBlock.ts @@ -1 +1 @@ -export type RichTextBlock = Record +export type RichTextBlock = Readonly> diff --git a/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx b/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx index e5b080803c..2c43acd1ab 100644 --- a/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx +++ b/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx @@ -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> - sourceField?: string - referencesField?: string - referenceDiscriminationField?: string + readonly blocks: RichTextBlock[] + readonly referenceRenderers?: Record> + readonly sourceField?: string + readonly referencesField?: string + readonly referenceDiscriminationField?: string } export type RichTextRendererProps< @@ -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, @@ -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] @@ -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] @@ -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) @@ -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, + }), )} ))} From cf2eadd02437f30d42277f39f178f3999f567b8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Nobile?= Date: Mon, 18 Sep 2023 16:32:19 +0200 Subject: [PATCH 2/2] build(api): generate new api --- build/api/react-client.api.md | 4 ++-- .../components/RichTextRenderer/RichTextRenderer.tsx | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/build/api/react-client.api.md b/build/api/react-client.api.md index e43b783dc9..c605407161 100644 --- a/build/api/react-client.api.md +++ b/build/api/react-client.api.md @@ -196,7 +196,7 @@ export interface RichTextAnchorElement; +export type RichTextBlock = Readonly>; // @public (undocumented) export interface RichTextBoldLeaf extends RichTextLeaf { @@ -312,7 +312,7 @@ export const RichTextRenderer: { // (undocumented) - blocks: RichTextBlock[]; + blocks: readonly RichTextBlock[]; // (undocumented) referenceDiscriminationField?: string; // (undocumented) diff --git a/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx b/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx index 2c43acd1ab..24020af175 100644 --- a/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx +++ b/packages/react-client/src/components/RichTextRenderer/RichTextRenderer.tsx @@ -14,18 +14,18 @@ import { RichTextRenderMetadataContext } from './RichTextRenderMetadataContext' import type { RootEditorNode } from './RootEditorNode' export interface RichTextRendererFieldProps { - readonly source: string | null + source: string | null } export interface RichTextRendererBlockProps< CustomElements extends RichTextElement = never, CustomLeaves extends RichTextLeaf = never, > { - readonly blocks: RichTextBlock[] - readonly referenceRenderers?: Record> - readonly sourceField?: string - readonly referencesField?: string - readonly referenceDiscriminationField?: string + blocks: readonly RichTextBlock[] + referenceRenderers?: Record> + sourceField?: string + referencesField?: string + referenceDiscriminationField?: string } export type RichTextRendererProps<