-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding JSDoc, removing id from block config and including schemna in …
…reader/editor (#4) * Adding JSDoc * Updating tests to not include id * Including schema in reader/editor * Bumping version
- Loading branch information
Showing
11 changed files
with
71 additions
and
39 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,13 @@ | ||
import React from 'react'; | ||
import { z } from 'zod'; | ||
|
||
import { BaseZodDictionary, DocumentBlocksDictionary } from '../utils'; | ||
import { BaseZodDictionary, BlockConfiguration, DocumentBlocksDictionary } from '../utils'; | ||
|
||
/** | ||
* @param blocks Main DocumentBlocksDictionary | ||
* @returns React component that can render a BlockConfiguration that is compatible with blocks | ||
*/ | ||
export default function buildBlockComponent<T extends BaseZodDictionary>(blocks: DocumentBlocksDictionary<T>) { | ||
type BaseBlockComponentProps<TType extends keyof T> = { | ||
type: TType; | ||
data: z.infer<T[TType]>; | ||
}; | ||
|
||
return function BlockComponent({ type, data }: BaseBlockComponentProps<keyof T>): React.ReactNode { | ||
return function BlockComponent({ type, data }: BlockConfiguration<T>): React.ReactNode { | ||
return React.createElement(blocks[type].Component, data); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,22 @@ | ||
import { z } from 'zod'; | ||
|
||
import { BaseZodDictionary, DocumentBlocksDictionary } from '../utils'; | ||
import { BaseZodDictionary, BlockConfiguration, DocumentBlocksDictionary } from '../utils'; | ||
|
||
/** | ||
* | ||
* @param blocks Main DocumentBlocksDictionary | ||
* @returns zod schema that can parse arbitary objects into a single BlockConfiguration | ||
*/ | ||
export default function buildBlockConfigurationSchema<T extends BaseZodDictionary>( | ||
blocks: DocumentBlocksDictionary<T> | ||
) { | ||
type BaseBlockComponentProps<TType extends keyof T> = { | ||
id: string; | ||
type: TType; | ||
data: z.infer<T[TType]>; | ||
}; | ||
|
||
const blockObjects = Object.keys(blocks).map((type: keyof T) => | ||
z.object({ | ||
id: z.string(), | ||
type: z.literal(type), | ||
data: blocks[type].schema, | ||
}) | ||
); | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
return z.discriminatedUnion('type', blockObjects as any).transform((v) => v as BaseBlockComponentProps<keyof T>); | ||
return z.discriminatedUnion('type', blockObjects as any).transform((v) => v as BlockConfiguration<T>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters