import type { Editor, Extension } from '@prosekit/core' import { createMemo, type Accessor } from 'solid-js' import type { MaybeAccessor } from '../types.ts' import { toValue } from '../utils/to-value.ts' import { useEditor } from './use-editor.ts' export interface UseEditorDerivedOptions { /** * The editor to add the extension to. If not provided, it will use the * editor from the nearest `` component. */ editor?: MaybeAccessor> } /** * Runs a function to derive a value from the editor instance after editor state * changes. * * This is useful when you need to render something based on the editor state, * for example, whether the selected text is wrapped in an italic mark. * * It returns an accessor of the derived value that updates whenever the editor * state changes. */ export function useEditorDerivedValue( /** * A function that receives the editor instance and returns a derived value. * * It will be called whenever the editor's document state changes, or when it * mounts. */ derive: (editor: Editor) => Derived, options?: UseEditorDerivedOptions, ): Accessor { const initialEditor = options?.editor const editorAccessor: Accessor> = initialEditor ? () => toValue(initialEditor) : useEditor({ update: true }) return createMemo(() => derive(editorAccessor())) }