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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPZYQAumujy5kXaOKurr6zgTP2krHrFVXi2kj8seRYrlvq2GssUjReh5MiVpQbLFQNy9HsmcasqpBNVsXytWTw0ZDjA=

Editor

import { Editor } from '@dxos/react-ui-editor';
import { createBasicExtensions, createThemeExtensions } from '@dxos/ui-editor';
Terminal window
npm install @dxos/react-ui-editor @dxos/ui-editor @effect/atom-react

Editor is the text editor Composer uses for documents, built on CodeMirror.

  • The React parts draw the editor and its toolbar.
  • The extensions from @dxos/ui-editor decide what the editor does: markdown, formatting, themes, and binding the text to an ECHO object.
Part What it is
Editor.Root The root, which holds the extensions and the editorโ€™s state. viewMode sets source, preview or read-only
Editor.Content The layout that fills its container, with the toolbar above the text
Editor.View The CodeMirror editor. Pass value and onChange when the text lives in your state
Editor.Toolbar The formatting actions, as an ActionToolbar
Editor.ToolbarMenu The same actions as a menu, for where a toolbar does not fit
Editor.Blocks The part that draws the embedded blocks the extensions contribute
Editor.BlockFrame The frame of one embedded block, which takes focus for it
Editor.Diagnostics A developer panel of live editor state

For plain text, the editor takes two sets of extensions:

  • createBasicExtensions gives the editor its editing behavior, with options such as a placeholder and line wrapping.
  • createThemeExtensions matches the appโ€™s light or dark theme. Read the mode with useThemeMode, and rebuild the extensions when it changes.

Rebuilding the extensions recreates the editor, so keep the editorโ€™s text in state and pass it as value with onChange.

Example (Editing plain text held in state)

import {
function useMemo<T>(factory: () => T, deps: React.DependencyList): T

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
,
function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)

Returns a stateful value, and a function to update it.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useState

useState
} from 'react';
import {
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
} from '@dxos/react-ui-editor';
import * as
import Hooks
Hooks
from '@dxos/react-ui/Hooks';
import {
const createBasicExtensions: (propsProp?: BasicExtensionsOptions) => Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
,
const createThemeExtensions: ({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions) => Extension
createThemeExtensions
} from '@dxos/ui-editor';
/** A plain-text editor: the basic extensions give it editing, and the theme extensions match the app's theme. */
export const
const NotesEditor: () => React.JSX.Element

A plain-text editor: the basic extensions give it editing, and the theme extensions match the app's theme.

NotesEditor
= () => {
const
const themeMode: ThemeMode
themeMode
=
import Hooks
Hooks
.
const useThemeMode: () => ThemeMode

The ThemeProvider's light or dark mode.

useThemeMode
();
// The text lives in state, so it survives the editor being rebuilt when the theme changes.
const [
const text: string
text
,
const setText: React.Dispatch<React.SetStateAction<string>>
setText
] =
useState<string>(initialState: string | (() => string)): [string, React.Dispatch<React.SetStateAction<string>>] (+1 overload)

Returns a stateful value, and a function to update it.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useState

useState
('');
const
const extensions: Extension[]
extensions
=
useMemo<Extension[]>(factory: () => Extension[], deps: React.DependencyList): Extension[]

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
(
() => [
function createBasicExtensions(propsProp?: BasicExtensionsOptions): Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
({
placeholder?: string | undefined
placeholder
: 'Write a noteโ€ฆ',
lineWrapping?: boolean | undefined

If false then do not set a max-width or side margin on the editor.

lineWrapping
: true,
}),
function createThemeExtensions({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions): Extension
createThemeExtensions
({
themeMode?: ThemeMode | undefined
themeMode
}),
],
[
const themeMode: ThemeMode
themeMode
],
);
return (
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
extensions?: Extension[] | undefined
extensions
={
const extensions: Extension[]
extensions
}>
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type View: {
({ extensions: providedExtensions, ...props }: EditorViewProps): React.JSX.Element;
displayName: string;
}
View
value?: string | undefined
value
={
const text: string
text
}
onChange?: ((value: string) => void) | undefined
onChange
={
const setText: React.Dispatch<React.SetStateAction<string>>
setText
} />
</
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
>
);
};

useMemo rebuilds the extensions only when themeMode changes, so typing does not recreate the editor.

createMarkdownExtensions parses markdown, and decorateMarkdown styles it as you type.

Editor.Toolbar is an ActionToolbar, so its actions live in an atom.

Wrap your app in RegistryProvider from @effect/atom-react. This is best practice, because your app then controls the registry that the toolbar reads. Without a provider, the toolbar reads the libraryโ€™s default registry.

Add the strings from @dxos/react-ui-menu/translations and @dxos/react-ui-editor/translations to the resourceExtensions of Theme.Provider.

The toolbar is dimmed until its surface has the userโ€™s attention. Outside Composer, you set that up with two components from @dxos/react-ui-attention:

  • Put a RootAttentionProvider around the app.
  • Put an AttendableContainer around the toolbar and the editor.

Then pass the containerโ€™s id to the toolbar as attendableId.

Example (Editing markdown with a formatting toolbar)

import {
function useMemo<T>(factory: () => T, deps: React.DependencyList): T

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
,
function useState<S>(initialState: S | (() => S)): [S, React.Dispatch<React.SetStateAction<S>>] (+1 overload)

Returns a stateful value, and a function to update it.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useState

useState
} from 'react';
import {
const AttendableContainer: React.ForwardRefExoticComponent<Omit<AttendableContainerProps, "ref"> & React.RefAttributes<HTMLDivElement>>

Note that DeckPlugin and StackPlugin both handle attention on their own, and when rendering content in those cases it is not necessary to also render an AttendableContainer. This component is primarily for Storybook stories and other testing scenarios, or the rare cases where an attendable entity is rendered outside of either of those plugins.

AttendableContainer
,
const RootAttentionProvider: ({ children, attention: propsAttention, onChange }: RootAttentionProviderProps) => React.JSX.Element
RootAttentionProvider
} from '@dxos/react-ui-attention';
import {
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
} from '@dxos/react-ui-editor';
import * as
import Hooks
Hooks
from '@dxos/react-ui/Hooks';
import {
const createBasicExtensions: (propsProp?: BasicExtensionsOptions) => Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
,
const createMarkdownExtensions: (options?: MarkdownBundleOptions) => Extension[]

Creates markdown extensions. To be used in conjunction with createBasicExtensions.

Refs: https://codemirror.net/docs/community https://codemirror.net/docs/ref/#codemirror.basicSetup

createMarkdownExtensions
,
const createThemeExtensions: ({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions) => Extension
createThemeExtensions
,
const decorateMarkdown: (options?: DecorateOptions) => import("@codemirror/state").Extension[]

WYSIWYG markdown decorator (headings, lists, tasks, quotes, code, links, horizontal rules); bundles image, table, adjustChanges, and formattingStyles.

decorateMarkdown
} from '@dxos/ui-editor';
const
const NOTES: "# Notes\n\nSome **bold** text and a [link](https://dxos.org).\n"
NOTES
= '# Notes\n\nSome **bold** text and a [link](https://dxos.org).\n';
/**
* A markdown editor with a formatting toolbar, filling its container. The toolbar is active while the container it
* shares with the editor has the user's attention.
*/
export const
const MarkdownEditor: () => React.JSX.Element

A markdown editor with a formatting toolbar, filling its container. The toolbar is active while the container it shares with the editor has the user's attention.

MarkdownEditor
= () => {
const
const themeMode: ThemeMode
themeMode
=
import Hooks
Hooks
.
const useThemeMode: () => ThemeMode

The ThemeProvider's light or dark mode.

useThemeMode
();
// The text lives in state, so it survives the editor being rebuilt when the theme changes.
const [
const text: string
text
,
const setText: React.Dispatch<React.SetStateAction<string>>
setText
] =
useState<string>(initialState: string | (() => string)): [string, React.Dispatch<React.SetStateAction<string>>] (+1 overload)

Returns a stateful value, and a function to update it.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useState

useState
(
const NOTES: "# Notes\n\nSome **bold** text and a [link](https://dxos.org).\n"
NOTES
);
const
const extensions: Extension[]
extensions
=
useMemo<Extension[]>(factory: () => Extension[], deps: React.DependencyList): Extension[]

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
(
() => [
function createBasicExtensions(propsProp?: BasicExtensionsOptions): Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
({
lineWrapping?: boolean | undefined

If false then do not set a max-width or side margin on the editor.

lineWrapping
: true }),
// Padding inside the scroller keeps the text off the container's edges.
function createThemeExtensions({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions): Extension
createThemeExtensions
({
themeMode?: ThemeMode | undefined
themeMode
,
slots?: {
editor?: {
className?: string;
};
scroller?: {
className?: string;
};
content?: {
className?: string;
};
} | undefined
slots
: {
scroller?: {
className?: string;
} | undefined
scroller
: {
className?: string | undefined
className
: 'px-4 py-2' } },
}),
function createMarkdownExtensions(options?: MarkdownBundleOptions): Extension[]

Creates markdown extensions. To be used in conjunction with createBasicExtensions.

Refs: https://codemirror.net/docs/community https://codemirror.net/docs/ref/#codemirror.basicSetup

createMarkdownExtensions
(),
function decorateMarkdown(options?: DecorateOptions): import("@codemirror/state").Extension[]

WYSIWYG markdown decorator (headings, lists, tasks, quotes, code, links, horizontal rules); bundles image, table, adjustChanges, and formattingStyles.

decorateMarkdown
(),
],
[
const themeMode: ThemeMode
themeMode
],
);
return (
<
const RootAttentionProvider: ({ children, attention: propsAttention, onChange }: RootAttentionProviderProps) => React.JSX.Element
RootAttentionProvider
>
<
const AttendableContainer: React.ForwardRefExoticComponent<Omit<AttendableContainerProps, "ref"> & React.RefAttributes<HTMLDivElement>>

Note that DeckPlugin and StackPlugin both handle attention on their own, and when rendering content in those cases it is not necessary to also render an AttendableContainer. This component is primarily for Storybook stories and other testing scenarios, or the rare cases where an attendable entity is rendered outside of either of those plugins.

AttendableContainer
id: string
id
='notes'
classNames?: ClassNameValue
classNames
='h-full'>
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
extensions?: Extension[] | undefined
extensions
={
const extensions: Extension[]
extensions
}
viewMode?: "source" | "preview" | "readonly" | undefined
viewMode
='preview'>
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Content: {
({ classNames, children }: EditorContentProps): React.JSX.Element;
displayName: string;
}
Content
>
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Toolbar: {
(props: EditorToolbarProps): React.JSX.Element;
displayName: string;
}
Toolbar
attendableId?: string | undefined
attendableId
='notes' />
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type View: {
({ extensions: providedExtensions, ...props }: EditorViewProps): React.JSX.Element;
displayName: string;
}
View
value?: string | undefined
value
={
const text: string
text
}
onChange?: ((value: string) => void) | undefined
onChange
={
const setText: React.Dispatch<React.SetStateAction<string>>
setText
} />
</
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Content: {
({ classNames, children }: EditorContentProps): React.JSX.Element;
displayName: string;
}
Content
>
</
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
>
</
const AttendableContainer: React.ForwardRefExoticComponent<Omit<AttendableContainerProps, "ref"> & React.RefAttributes<HTMLDivElement>>

Note that DeckPlugin and StackPlugin both handle attention on their own, and when rendering content in those cases it is not necessary to also render an AttendableContainer. This component is primarily for Storybook stories and other testing scenarios, or the rare cases where an attendable entity is rendered outside of either of those plugins.

AttendableContainer
>
</
const RootAttentionProvider: ({ children, attention: propsAttention, onChange }: RootAttentionProviderProps) => React.JSX.Element
RootAttentionProvider
>
);
};

AttendableContainer and Editor.Toolbar share the id notes. RootAttentionProvider wraps the editor here so that the sample stands alone. In an app it goes at the root, with the RegistryProvider and the strings, which the sample leaves out.

An ECHO Text object from @dxos/schema holds a documentโ€™s text. The automerge extension binds the editor to one of the objectโ€™s properties:

  • Each edit goes into the object.
  • Each change to the object, from another window or device, shows in the editor.

With the binding, Editor.View needs no value.

Example (Binding the editor to a Text object)

import {
function useMemo<T>(factory: () => T, deps: React.DependencyList): T

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
} from 'react';
import {
import Doc
Doc
} from '@dxos/echo-doc';
import {
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
} from '@dxos/react-ui-editor';
import * as
import Hooks
Hooks
from '@dxos/react-ui/Hooks';
import {
import Text
Text
} from '@dxos/schema';
import {
const automerge: (accessor: Doc.Accessor) => Extension

CodeMirror extension that two-way syncs the editor with the string the

Doc.Accessor

points at, reconciling local edits and remote document mutations via Automerge.

automerge
,
const createBasicExtensions: (propsProp?: BasicExtensionsOptions) => Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
,
const createMarkdownExtensions: (options?: MarkdownBundleOptions) => Extension[]

Creates markdown extensions. To be used in conjunction with createBasicExtensions.

Refs: https://codemirror.net/docs/community https://codemirror.net/docs/ref/#codemirror.basicSetup

createMarkdownExtensions
,
const createThemeExtensions: ({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions) => Extension
createThemeExtensions
,
const decorateMarkdown: (options?: DecorateOptions) => import("@codemirror/state").Extension[]

WYSIWYG markdown decorator (headings, lists, tasks, quotes, code, links, horizontal rules); bundles image, table, adjustChanges, and formattingStyles.

decorateMarkdown
} from '@dxos/ui-editor';
/**
* An editor bound to an ECHO `Text` object: edits go into the object, and edits made to it elsewhere (another
* window, another device) appear in the editor.
*/
export const
const SharedEditor: ({ text }: {
text: Text.Text;
}) => React.JSX.Element

An editor bound to an ECHO Text object: edits go into the object, and edits made to it elsewhere (another window, another device) appear in the editor.

SharedEditor
= ({
text: Text.Text
text
}: {
text: Text.Text
text
:
import Text
Text
.
class Text
Text
}) => {
const
const themeMode: ThemeMode
themeMode
=
import Hooks
Hooks
.
const useThemeMode: () => ThemeMode

The ThemeProvider's light or dark mode.

useThemeMode
();
const
const extensions: Extension[]
extensions
=
useMemo<Extension[]>(factory: () => Extension[], deps: React.DependencyList): Extension[]

useMemo will only recompute the memoized value when one of the deps has changed.

@version โ€• 16.8.0

@see โ€• https://react.dev/reference/react/useMemo

useMemo
(
() => [
function createBasicExtensions(propsProp?: BasicExtensionsOptions): Extension

Assembles the base editor setup: keymaps, history, bracket matching/closing, drop cursor, read-only handling, line wrapping, and tab handling.

createBasicExtensions
({
lineWrapping?: boolean | undefined

If false then do not set a max-width or side margin on the editor.

lineWrapping
: true }),
function createThemeExtensions({ monospace, scrollbarThin, slots: slotsProp, syntaxHighlighting: syntaxHighlightingProp, themeMode, }?: ThemeExtensionsOptions): Extension
createThemeExtensions
({
themeMode?: ThemeMode | undefined
themeMode
}),
function createMarkdownExtensions(options?: MarkdownBundleOptions): Extension[]

Creates markdown extensions. To be used in conjunction with createBasicExtensions.

Refs: https://codemirror.net/docs/community https://codemirror.net/docs/ref/#codemirror.basicSetup

createMarkdownExtensions
(),
function decorateMarkdown(options?: DecorateOptions): import("@codemirror/state").Extension[]

WYSIWYG markdown decorator (headings, lists, tasks, quotes, code, links, horizontal rules); bundles image, table, adjustChanges, and formattingStyles.

decorateMarkdown
(),
function automerge(accessor: Doc.Accessor): Extension

CodeMirror extension that two-way syncs the editor with the string the

Doc.Accessor

points at, reconciling local edits and remote document mutations via Automerge.

automerge
(
import Doc
Doc
.
const createAccessor: <Text.Text>(obj: Text.Text, path: KeyPath | keyof Text.Text) => Doc.Accessor<Text.Text>

Resolves an

Accessor

for a value within an object, agnostic to whether the object is in-memory or attached to a database. An Obj.make object has no ObjectCore until db.add, so an in-memory core is materialized (via createObject) to let the accessor bind before persistence; the object's edits then survive a later db.add.

createAccessor
(
text: Text.Text
text
, ['content'])),
],
[
const themeMode: ThemeMode
themeMode
,
text: Text.Text
text
],
);
return (
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
extensions?: Extension[] | undefined
extensions
={
const extensions: Extension[]
extensions
}>
<
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type View: {
({ extensions: providedExtensions, ...props }: EditorViewProps): React.JSX.Element;
displayName: string;
}
View
/>
</
const Editor: {
Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>;
... 6 more ...;
Diagnostics: {
...;
};
}
Editor
.
type Root: React.ForwardRefExoticComponent<Pick<EditorContextValue, "extensions" | "widgets"> & Omit<UseEditorMenuProps, "viewRef"> & Pick<EditorMenuProviderProps, "numItems" | "searchPlaceholder"> & {
viewMode?: EditorToolbarState["viewMode"];
} & {
children?: React.ReactNode | undefined;
} & React.RefAttributes<EditorController | null>>
Root
>
);
};

Doc.createAccessor points the automerge extension at the content property of text.

This preview binds to a Text object that is not in a database, so its edits stay in this page: