Skip to content

Commit

Permalink
docs: add more details to typedoc support (#691)
Browse files Browse the repository at this point in the history
* wip: annotated typedocs progress

* docs: adjust reference generation

* Docs: add emoji example and update import (#630)

* docs: add emoji example

* docs(fix): fix coreBehaviors import

* docs: adjust reference generation

* docs: add example to defineBehavior
  • Loading branch information
markmichon authored Jan 16, 2025
1 parent 8a9666c commit cc1e0ee
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 9 deletions.
67 changes: 62 additions & 5 deletions apps/docs/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import starlight from '@astrojs/starlight'
import tailwind from '@astrojs/tailwind'
import {defineConfig} from 'astro/config'
import starlightLinksValidator from 'starlight-links-validator'
import starlightTypeDoc, {typeDocSidebarGroup} from 'starlight-typedoc'
import {createStarlightTypeDocPlugin} from 'starlight-typedoc'

const [editorTypeDoc, editorTypeDocSidebar] = createStarlightTypeDocPlugin()
const [behaviorTypeDoc, behaviorTypeDocSidebar] = createStarlightTypeDocPlugin()
const [selectorsTypeDoc, selectorsTypeDocSidebar] =
createStarlightTypeDocPlugin()
const tsconfig = '../../packages/editor/tsconfig.json'

// https://astro.build/config
export default defineConfig({
Expand Down Expand Up @@ -58,9 +64,31 @@ export default defineConfig({
},
{
label: 'Reference',
autogenerate: {directory: 'reference'},
collapsed: true,
items: [
{
label: 'Editor',
items: [
{label: 'Overview', slug: 'reference/editor'},
{...editorTypeDocSidebar, badge: 'Generated'},
],
},
{
label: 'Behaviors',
items: [
{label: 'Overview', slug: 'reference/behavior-api'},
{...behaviorTypeDocSidebar, badge: 'Generated'},
],
},
{
label: 'Selectors',
items: [
{label: 'Overview', slug: 'reference/selectors'},
{...selectorsTypeDocSidebar, badge: 'Generated'},
],
},
],
},
typeDocSidebarGroup,
{
label: 'Integrations',
autogenerate: {directory: 'integrations'},
Expand All @@ -71,9 +99,38 @@ export default defineConfig({
},
],
plugins: [
starlightTypeDoc({
editorTypeDoc({
entryPoints: ['../../packages/editor/src/index.ts'],
tsconfig: '../../packages/editor/tsconfig.json',
output: 'api/editor',
typeDoc: {
excludeReferences: true,
},
sidebar: {
collapsed: true,
},
tsconfig,
}),
behaviorTypeDoc({
entryPoints: ['../../packages/editor/src/behaviors/index.ts'],
output: 'api/behaviors',
typeDoc: {
excludeReferences: true,
},
sidebar: {
collapsed: true,
},
tsconfig,
}),
selectorsTypeDoc({
entryPoints: ['../../packages/editor/src/selectors/index.ts'],
output: 'api/selectors',
typeDoc: {
excludeReferences: true,
},
sidebar: {
collapsed: true,
},
tsconfig,
}),
...(process.env.CHECK_LINKS ? [starlightLinksValidator()] : []),
],
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/content/docs/reference/behavior-api.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Behavior API
title: Behavior API Overview
description: Reference documentation for the Behavior API.
---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
---
title: Hooks
description: Hooks reference
title: Editor API Overview
description: Editor API reference overview
---

The editor API provides access to the editor context and selectors for deriving state.

Use the following hooks to access the editor from within your components.

## `useEditor`

Access the editor context. Commonly used when building toolbars or passing context to selectors.
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/src/content/docs/reference/selectors.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: Selectors
title: Selectors API Overview
description: Selectors reference
prev: false
next: false
Expand Down
10 changes: 10 additions & 0 deletions packages/editor/package.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ export default defineConfig({
allowMultiple: true,
syntaxKind: 'block',
},
{
name: 'group',
allowMultiple: true,
syntaxKind: 'block',
},
{
name: 'groupDescription',
allowMultiple: true,
syntaxKind: 'block',
},
],
rules: {
// Disable rules for now
Expand Down
42 changes: 42 additions & 0 deletions packages/editor/src/behaviors/behavior.markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,48 @@ export type MarkdownBehaviorsConfig = {

/**
* @beta
* Create markdown behaviors for common markdown actions such as converting ### to headings, --- to HRs, and more.
*
* @example
* Configure the bundled markdown behaviors
* ```ts
* import {EditorProvider} from '@portabletext/editor'
* import {createMarkdownBehaviors, coreBehaviors} from '@portabletext/editor/behaviors'
*
* function App() {
* return (
* <EditorProvider
* initialConfig={{
* behaviors: [
* ...coreBehaviors,
* ...createMarkdownBehaviors({
* horizontalRuleObject: ({schema}) => {
* const name = schema.blockObjects.find(
* (object) => object.name === 'break',
* )?.name
* return name ? {name} : undefined
* },
* defaultStyle: ({schema}) => schema.styles[0].value,
* headingStyle: ({schema, level}) =>
* schema.styles.find((style) => style.value === `h${level}`)
* ?.value,
* blockquoteStyle: ({schema}) =>
* schema.styles.find((style) => style.value === 'blockquote')
* ?.value,
* unorderedListStyle: ({schema}) =>
* schema.lists.find((list) => list.value === 'bullet')?.value,
* orderedListStyle: ({schema}) =>
* schema.lists.find((list) => list.value === 'number')?.value,
* }),
* ]
* }}
* >
* {...}
* </EditorProvider>
* )
* }
* ```
*
*/
export function createMarkdownBehaviors(config: MarkdownBehaviorsConfig) {
const automaticBlockquoteOnSpace = defineBehavior({
Expand Down
15 changes: 15 additions & 0 deletions packages/editor/src/behaviors/behavior.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,21 @@ export type BehaviorActionIntendSet<TBehaviorEvent, TGuardResponse> = (

/**
* @beta
*
* @example
*
* ```tsx
* const noLowerCaseA = defineBehavior({
* on: 'insert.text',
* guard: ({event, context}) => event.text === 'a',
* actions: [({event, context}) => [{type: 'insert.text', text: 'A'}]],
* })
* ```
*
*
*
*
*
*/
export function defineBehavior<
TPayload extends Record<string, unknown>,
Expand Down
17 changes: 17 additions & 0 deletions packages/editor/src/editor/Editable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ export type PortableTextEditableProps = Omit<

/**
* @public
*
*
* The core component that renders the editor. Must be placed within the {@link EventProvider} component.
*
* @example
* ```tsx
* import { PortableTextEditable, EditorProvider } from '@portabletext/editor'
*
* function MyComponent() {
* return (
* <EditorProvider>
* <PortableTextEditable />
* </EditorProvider>
* )
* }
* ```
* @group Components
*/
export const PortableTextEditable = forwardRef<
Omit<HTMLDivElement, 'as' | 'onPaste' | 'onBeforeInput'>,
Expand Down
20 changes: 20 additions & 0 deletions packages/editor/src/editor/define-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ export type SchemaDefinition<

/**
* @public
* A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
* @example
* ```ts
* import { defineSchema } from '@portabletext/editor'
*
* const schemaDefinition = defineSchema({
* decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
* annotations: [{name: 'link'}],
* styles: [
* {name: 'normal'},
* {name: 'h1'},
* {name: 'h2'},
* {name: 'h3'},
* {name: 'blockquote'},
* ],
* lists: [],
* inlineObjects: [],
* blockObjects: [],
* }
* ```
*/
export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
definition: TSchemaDefinition,
Expand Down
45 changes: 45 additions & 0 deletions packages/editor/src/editor/editor-event-listener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,51 @@ import {useEditor} from './editor-provider'

/**
* @public
* Listen for events emitted by the editor. Must be used inside `EditorProvider`. Events available include:
* - 'blurred'
* - 'done loading'
* - 'editable'
* - 'error'
* - 'focused'
* - 'invalid value'
* - 'loading'
* - 'mutation'
* - 'patch'
* - 'read only'
* - 'ready'
* - 'selection'
* - 'value changed'
*
* @example
* Listen and log events.
* ```tsx
* import {EditorEventListener, EditorProvider} from '@portabletext/editor'
*
* function MyComponent() {
* return (
* <EditorProvider>
* <EditorEventListener
* on={(event) => {
* console.log(event)
* }
* } />
* { ... }
* </EditorProvider>
* )
* }
* ```
* @example
* Handle events when there is a mutation.
* ```tsx
* <EditorEventListener
* on={(event) => {
* if (event.type === 'mutation') {
* console.log('Value changed:', event.snapshot)
* }
* }}
* />
* ```
* @group Components
*/
export function EditorEventListener(props: {
on: (event: EditorEmittedEvent) => void
Expand Down
27 changes: 27 additions & 0 deletions packages/editor/src/editor/editor-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ export type EditorProviderProps = {

/**
* @public
* The EditorProvider component is used to set up the editor context and configure the Portable Text Editor.
* @example
* ```tsx
* import {EditorProvider} from '@portabletext/editor'
*
* function App() {
* return (
* <EditorProvider initialConfig={{ ... }} >
* ...
* </EditorProvider>
* )
* }
*
* ```
* @group Components
*/
export function EditorProvider(props: EditorProviderProps) {
const editor = useCreateEditor(props.initialConfig)
Expand Down Expand Up @@ -66,6 +81,18 @@ export function EditorProvider(props: EditorProviderProps) {

/**
* @public
* Get the current editor context from the `EditorProvider`.
* Must be used inside the `EditorProvider` component.
* @returns The current editor object.
* @example
* ```tsx
* import { useEditor } from '@portabletext/editor'
*
* function MyComponent() {
* const editor = useEditor()
* }
* ```
* @group Hooks
*/
export function useEditor() {
const editor = React.useContext(EditorContext)
Expand Down
21 changes: 21 additions & 0 deletions packages/editor/src/editor/editor-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ export type EditorSelector<TSelected> = (snapshot: EditorSnapshot) => TSelected

/**
* @public
* Hook to select a value from the editor state.
* @example
* Pass a selector as the second argument
* ```tsx
* import { useEditorSelector } from '@portabletext/editor'
*
* function MyComponent(editor) {
* const value = useEditorSelector(editor, selector)
* }
* ```
* @example
* Pass an inline selector as the second argument.
* In this case, use the editor context to obtain the schema.
* ```tsx
* import { useEditorSelector } from '@portabletext/editor'
*
* function MyComponent(editor) {
* const schema = useEditorSelector(editor, (snapshot) => snapshot.context.schema)
* }
* ```
* @group Hooks
*/
export function useEditorSelector<TSelected>(
editor: Editor,
Expand Down

0 comments on commit cc1e0ee

Please sign in to comment.